blob: 3fabf9f97022e9c74c2fe0f59e9207b408a6546f [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2015 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 * Authors: monk liu <monk.liu@amd.com>
23 */
24
25#include <drm/drmP.h>
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050026#include <drm/drm_auth.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040027#include "amdgpu.h"
Andres Rodriguez52c6a622017-06-26 16:17:13 -040028#include "amdgpu_sched.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050030static int amdgpu_ctx_priority_permit(struct drm_file *filp,
Lucas Stach1b1f42d2017-12-06 17:49:39 +010031 enum drm_sched_priority priority)
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050032{
33 /* NORMAL and below are accessible by everyone */
Lucas Stach1b1f42d2017-12-06 17:49:39 +010034 if (priority <= DRM_SCHED_PRIORITY_NORMAL)
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050035 return 0;
36
37 if (capable(CAP_SYS_NICE))
38 return 0;
39
40 if (drm_is_current_master(filp))
41 return 0;
42
43 return -EACCES;
44}
45
46static int amdgpu_ctx_init(struct amdgpu_device *adev,
Lucas Stach1b1f42d2017-12-06 17:49:39 +010047 enum drm_sched_priority priority,
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050048 struct drm_file *filp,
49 struct amdgpu_ctx *ctx)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040050{
Christian König21c16bf2015-07-07 17:24:49 +020051 unsigned i, j;
Christian König47f38502015-08-04 17:51:05 +020052 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053
Lucas Stach1b1f42d2017-12-06 17:49:39 +010054 if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050055 return -EINVAL;
56
57 r = amdgpu_ctx_priority_permit(filp, priority);
58 if (r)
59 return r;
60
Alex Deucherd38ceaf2015-04-20 16:55:21 -040061 memset(ctx, 0, sizeof(*ctx));
Chunming Zhou9cb7e5a2015-07-21 13:17:19 +080062 ctx->adev = adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040063 kref_init(&ctx->refcount);
Christian König21c16bf2015-07-07 17:24:49 +020064 spin_lock_init(&ctx->ring_lock);
Christian Königa750b472016-02-11 10:20:53 +010065 ctx->fences = kcalloc(amdgpu_sched_jobs * AMDGPU_MAX_RINGS,
Chris Wilsonf54d1862016-10-25 13:00:45 +010066 sizeof(struct dma_fence*), GFP_KERNEL);
Chunming Zhou37cd0ca2015-12-10 15:45:11 +080067 if (!ctx->fences)
68 return -ENOMEM;
Chunming Zhou23ca0e42015-07-06 13:42:58 +080069
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -040070 mutex_init(&ctx->lock);
71
Chunming Zhou37cd0ca2015-12-10 15:45:11 +080072 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
73 ctx->rings[i].sequence = 1;
Christian Königa750b472016-02-11 10:20:53 +010074 ctx->rings[i].fences = &ctx->fences[amdgpu_sched_jobs * i];
Chunming Zhou37cd0ca2015-12-10 15:45:11 +080075 }
Nicolai Hähnlece199ad2016-10-04 09:43:30 +020076
77 ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
Monk Liu668ca1b2017-10-17 14:39:23 +080078 ctx->reset_counter_query = ctx->reset_counter;
Christian Könige55f2b62017-10-09 15:18:43 +020079 ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
Andres Rodriguezc23be4a2017-06-06 20:20:38 -040080 ctx->init_priority = priority;
Lucas Stach1b1f42d2017-12-06 17:49:39 +010081 ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
Nicolai Hähnlece199ad2016-10-04 09:43:30 +020082
Chunming Zhoucadf97b2016-01-15 11:25:00 +080083 /* create context entity for each ring */
84 for (i = 0; i < adev->num_rings; i++) {
Christian König20874172016-02-11 09:56:44 +010085 struct amdgpu_ring *ring = adev->rings[i];
Lucas Stach1b1f42d2017-12-06 17:49:39 +010086 struct drm_sched_rq *rq;
Christian König20874172016-02-11 09:56:44 +010087
Andres Rodriguezc2636dc2016-12-22 17:06:50 -050088 rq = &ring->sched.sched_rq[priority];
Monk Liu75fbed22017-05-11 13:36:33 +080089
90 if (ring == &adev->gfx.kiq.ring)
91 continue;
92
Lucas Stach1b1f42d2017-12-06 17:49:39 +010093 r = drm_sched_entity_init(&ring->sched, &ctx->rings[i].entity,
Monk Liu11029002017-10-23 12:25:24 +080094 rq, amdgpu_sched_jobs, &ctx->guilty);
Chunming Zhoucadf97b2016-01-15 11:25:00 +080095 if (r)
Huang Rui8ed81472016-10-26 17:07:03 +080096 goto failed;
Chunming Zhoucadf97b2016-01-15 11:25:00 +080097 }
98
Andres Rodriguezeffd9242017-02-16 00:47:32 -050099 r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr);
100 if (r)
101 goto failed;
102
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103 return 0;
Huang Rui8ed81472016-10-26 17:07:03 +0800104
105failed:
106 for (j = 0; j < i; j++)
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100107 drm_sched_entity_fini(&adev->rings[j]->sched,
Huang Rui8ed81472016-10-26 17:07:03 +0800108 &ctx->rings[j].entity);
109 kfree(ctx->fences);
110 ctx->fences = NULL;
111 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112}
113
Christian König20874172016-02-11 09:56:44 +0100114static void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
Christian König47f38502015-08-04 17:51:05 +0200115{
116 struct amdgpu_device *adev = ctx->adev;
117 unsigned i, j;
118
Dave Airliefe295b22015-11-03 11:07:11 -0500119 if (!adev)
120 return;
121
Christian König47f38502015-08-04 17:51:05 +0200122 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
Chunming Zhou37cd0ca2015-12-10 15:45:11 +0800123 for (j = 0; j < amdgpu_sched_jobs; ++j)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100124 dma_fence_put(ctx->rings[i].fences[j]);
Chunming Zhou37cd0ca2015-12-10 15:45:11 +0800125 kfree(ctx->fences);
Grazvydas Ignotas54ddf3a2016-09-25 23:34:46 +0300126 ctx->fences = NULL;
Christian König47f38502015-08-04 17:51:05 +0200127
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800128 for (i = 0; i < adev->num_rings; i++)
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100129 drm_sched_entity_fini(&adev->rings[i]->sched,
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800130 &ctx->rings[i].entity);
Andres Rodriguezeffd9242017-02-16 00:47:32 -0500131
132 amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400133
134 mutex_destroy(&ctx->lock);
Christian König47f38502015-08-04 17:51:05 +0200135}
136
137static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
138 struct amdgpu_fpriv *fpriv,
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500139 struct drm_file *filp,
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100140 enum drm_sched_priority priority,
Christian König47f38502015-08-04 17:51:05 +0200141 uint32_t *id)
142{
143 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
144 struct amdgpu_ctx *ctx;
145 int r;
146
147 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
148 if (!ctx)
149 return -ENOMEM;
150
151 mutex_lock(&mgr->lock);
152 r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
153 if (r < 0) {
154 mutex_unlock(&mgr->lock);
155 kfree(ctx);
156 return r;
157 }
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500158
Christian König47f38502015-08-04 17:51:05 +0200159 *id = (uint32_t)r;
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500160 r = amdgpu_ctx_init(adev, priority, filp, ctx);
Chunming Zhouc648ed72015-12-10 15:50:02 +0800161 if (r) {
162 idr_remove(&mgr->ctx_handles, *id);
163 *id = 0;
164 kfree(ctx);
165 }
Christian König47f38502015-08-04 17:51:05 +0200166 mutex_unlock(&mgr->lock);
Christian König47f38502015-08-04 17:51:05 +0200167 return r;
168}
169
170static void amdgpu_ctx_do_release(struct kref *ref)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172 struct amdgpu_ctx *ctx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400173
Christian König47f38502015-08-04 17:51:05 +0200174 ctx = container_of(ref, struct amdgpu_ctx, refcount);
175
176 amdgpu_ctx_fini(ctx);
177
178 kfree(ctx);
179}
180
181static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
182{
183 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
184 struct amdgpu_ctx *ctx;
185
186 mutex_lock(&mgr->lock);
Matthew Wilcoxd3e709e2016-12-22 13:30:22 -0500187 ctx = idr_remove(&mgr->ctx_handles, id);
188 if (ctx)
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800189 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Christian König47f38502015-08-04 17:51:05 +0200190 mutex_unlock(&mgr->lock);
Matthew Wilcoxd3e709e2016-12-22 13:30:22 -0500191 return ctx ? 0 : -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400192}
193
Marek Olšákd94aed52015-05-05 21:13:49 +0200194static int amdgpu_ctx_query(struct amdgpu_device *adev,
195 struct amdgpu_fpriv *fpriv, uint32_t id,
196 union drm_amdgpu_ctx_out *out)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400197{
198 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800199 struct amdgpu_ctx_mgr *mgr;
Marek Olšákd94aed52015-05-05 21:13:49 +0200200 unsigned reset_counter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400201
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800202 if (!fpriv)
203 return -EINVAL;
204
205 mgr = &fpriv->ctx_mgr;
Marek Olšák0147ee02015-05-05 20:52:00 +0200206 mutex_lock(&mgr->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400207 ctx = idr_find(&mgr->ctx_handles, id);
Marek Olšákd94aed52015-05-05 21:13:49 +0200208 if (!ctx) {
Marek Olšák0147ee02015-05-05 20:52:00 +0200209 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200210 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400211 }
Marek Olšákd94aed52015-05-05 21:13:49 +0200212
213 /* TODO: these two are always zero */
Alex Deucher0b492a42015-08-16 22:48:26 -0400214 out->state.flags = 0x0;
215 out->state.hangs = 0x0;
Marek Olšákd94aed52015-05-05 21:13:49 +0200216
217 /* determine if a GPU reset has occured since the last call */
218 reset_counter = atomic_read(&adev->gpu_reset_counter);
219 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
Monk Liu668ca1b2017-10-17 14:39:23 +0800220 if (ctx->reset_counter_query == reset_counter)
Marek Olšákd94aed52015-05-05 21:13:49 +0200221 out->state.reset_status = AMDGPU_CTX_NO_RESET;
222 else
223 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
Monk Liu668ca1b2017-10-17 14:39:23 +0800224 ctx->reset_counter_query = reset_counter;
Marek Olšákd94aed52015-05-05 21:13:49 +0200225
Marek Olšák0147ee02015-05-05 20:52:00 +0200226 mutex_unlock(&mgr->lock);
Marek Olšákd94aed52015-05-05 21:13:49 +0200227 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400228}
229
Monk Liubc1b1bf2017-10-17 14:58:01 +0800230static int amdgpu_ctx_query2(struct amdgpu_device *adev,
231 struct amdgpu_fpriv *fpriv, uint32_t id,
232 union drm_amdgpu_ctx_out *out)
233{
234 struct amdgpu_ctx *ctx;
235 struct amdgpu_ctx_mgr *mgr;
236
237 if (!fpriv)
238 return -EINVAL;
239
240 mgr = &fpriv->ctx_mgr;
241 mutex_lock(&mgr->lock);
242 ctx = idr_find(&mgr->ctx_handles, id);
243 if (!ctx) {
244 mutex_unlock(&mgr->lock);
245 return -EINVAL;
246 }
247
248 out->state.flags = 0x0;
249 out->state.hangs = 0x0;
250
251 if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
252 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
253
254 if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
255 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
256
257 if (atomic_read(&ctx->guilty))
258 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
259
260 mutex_unlock(&mgr->lock);
261 return 0;
262}
263
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400264int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
Marek Olšákd94aed52015-05-05 21:13:49 +0200265 struct drm_file *filp)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266{
267 int r;
268 uint32_t id;
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100269 enum drm_sched_priority priority;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400270
271 union drm_amdgpu_ctx *args = data;
272 struct amdgpu_device *adev = dev->dev_private;
273 struct amdgpu_fpriv *fpriv = filp->driver_priv;
274
275 r = 0;
276 id = args->in.ctx_id;
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500277 priority = amdgpu_to_sched_priority(args->in.priority);
278
Andres Rodriguezb6d8a432017-05-24 17:00:10 -0400279 /* For backwards compatibility reasons, we need to accept
280 * ioctls with garbage in the priority field */
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100281 if (priority == DRM_SCHED_PRIORITY_INVALID)
282 priority = DRM_SCHED_PRIORITY_NORMAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400283
284 switch (args->in.op) {
Christian Königa750b472016-02-11 10:20:53 +0100285 case AMDGPU_CTX_OP_ALLOC_CTX:
Andres Rodriguezc2636dc2016-12-22 17:06:50 -0500286 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
Christian Königa750b472016-02-11 10:20:53 +0100287 args->out.alloc.ctx_id = id;
288 break;
289 case AMDGPU_CTX_OP_FREE_CTX:
290 r = amdgpu_ctx_free(fpriv, id);
291 break;
292 case AMDGPU_CTX_OP_QUERY_STATE:
293 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
294 break;
Monk Liubc1b1bf2017-10-17 14:58:01 +0800295 case AMDGPU_CTX_OP_QUERY_STATE2:
296 r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
297 break;
Christian Königa750b472016-02-11 10:20:53 +0100298 default:
299 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400300 }
301
302 return r;
303}
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800304
305struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
306{
307 struct amdgpu_ctx *ctx;
Chunming Zhou23ca0e42015-07-06 13:42:58 +0800308 struct amdgpu_ctx_mgr *mgr;
309
310 if (!fpriv)
311 return NULL;
312
313 mgr = &fpriv->ctx_mgr;
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800314
315 mutex_lock(&mgr->lock);
316 ctx = idr_find(&mgr->ctx_handles, id);
317 if (ctx)
318 kref_get(&ctx->refcount);
319 mutex_unlock(&mgr->lock);
320 return ctx;
321}
322
323int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
324{
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800325 if (ctx == NULL)
326 return -EINVAL;
327
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800328 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
Jammy Zhou66b3cf22015-05-08 17:29:40 +0800329 return 0;
330}
Christian König21c16bf2015-07-07 17:24:49 +0200331
Monk Liueb01abc2017-09-15 13:40:31 +0800332int amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
333 struct dma_fence *fence, uint64_t* handler)
Christian König21c16bf2015-07-07 17:24:49 +0200334{
335 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
Christian Königce882e62015-08-19 15:00:55 +0200336 uint64_t seq = cring->sequence;
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800337 unsigned idx = 0;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100338 struct dma_fence *other = NULL;
Christian König21c16bf2015-07-07 17:24:49 +0200339
Chunming Zhou5b011232015-12-10 17:34:33 +0800340 idx = seq & (amdgpu_sched_jobs - 1);
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800341 other = cring->fences[idx];
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400342 if (other)
343 BUG_ON(!dma_fence_is_signaled(other));
Christian König21c16bf2015-07-07 17:24:49 +0200344
Chris Wilsonf54d1862016-10-25 13:00:45 +0100345 dma_fence_get(fence);
Christian König21c16bf2015-07-07 17:24:49 +0200346
347 spin_lock(&ctx->ring_lock);
348 cring->fences[idx] = fence;
Christian Königce882e62015-08-19 15:00:55 +0200349 cring->sequence++;
Christian König21c16bf2015-07-07 17:24:49 +0200350 spin_unlock(&ctx->ring_lock);
351
Chris Wilsonf54d1862016-10-25 13:00:45 +0100352 dma_fence_put(other);
Monk Liueb01abc2017-09-15 13:40:31 +0800353 if (handler)
354 *handler = seq;
Christian König21c16bf2015-07-07 17:24:49 +0200355
Monk Liueb01abc2017-09-15 13:40:31 +0800356 return 0;
Christian König21c16bf2015-07-07 17:24:49 +0200357}
358
Chris Wilsonf54d1862016-10-25 13:00:45 +0100359struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
360 struct amdgpu_ring *ring, uint64_t seq)
Christian König21c16bf2015-07-07 17:24:49 +0200361{
362 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
Chris Wilsonf54d1862016-10-25 13:00:45 +0100363 struct dma_fence *fence;
Christian König21c16bf2015-07-07 17:24:49 +0200364
365 spin_lock(&ctx->ring_lock);
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800366
Monk Liud7b1eeb2017-04-07 18:39:07 +0800367 if (seq == ~0ull)
368 seq = ctx->rings[ring->idx].sequence - 1;
369
Christian Königce882e62015-08-19 15:00:55 +0200370 if (seq >= cring->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200371 spin_unlock(&ctx->ring_lock);
372 return ERR_PTR(-EINVAL);
373 }
374
Chunming Zhoub43a9a72015-07-21 15:13:53 +0800375
Chunming Zhou37cd0ca2015-12-10 15:45:11 +0800376 if (seq + amdgpu_sched_jobs < cring->sequence) {
Christian König21c16bf2015-07-07 17:24:49 +0200377 spin_unlock(&ctx->ring_lock);
378 return NULL;
379 }
380
Chris Wilsonf54d1862016-10-25 13:00:45 +0100381 fence = dma_fence_get(cring->fences[seq & (amdgpu_sched_jobs - 1)]);
Christian König21c16bf2015-07-07 17:24:49 +0200382 spin_unlock(&ctx->ring_lock);
383
384 return fence;
385}
Christian Königefd4ccb2015-08-04 16:20:31 +0200386
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400387void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100388 enum drm_sched_priority priority)
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400389{
390 int i;
391 struct amdgpu_device *adev = ctx->adev;
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100392 struct drm_sched_rq *rq;
393 struct drm_sched_entity *entity;
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400394 struct amdgpu_ring *ring;
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100395 enum drm_sched_priority ctx_prio;
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400396
397 ctx->override_priority = priority;
398
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100399 ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400400 ctx->init_priority : ctx->override_priority;
401
402 for (i = 0; i < adev->num_rings; i++) {
403 ring = adev->rings[i];
404 entity = &ctx->rings[i].entity;
405 rq = &ring->sched.sched_rq[ctx_prio];
406
407 if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
408 continue;
409
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100410 drm_sched_entity_set_rq(entity, rq);
Andres Rodriguezc23be4a2017-06-06 20:20:38 -0400411 }
412}
413
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400414int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx, unsigned ring_id)
415{
416 struct amdgpu_ctx_ring *cring = &ctx->rings[ring_id];
417 unsigned idx = cring->sequence & (amdgpu_sched_jobs - 1);
418 struct dma_fence *other = cring->fences[idx];
419
420 if (other) {
421 signed long r;
Andrey Grodzovskye6a5b9f2018-04-30 10:04:42 -0400422 r = dma_fence_wait(other, true);
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400423 if (r < 0) {
Andrey Grodzovskye6a5b9f2018-04-30 10:04:42 -0400424 if (r != -ERESTARTSYS)
425 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
426
Andrey Grodzovsky0ae94442017-10-10 16:50:17 -0400427 return r;
428 }
429 }
430
431 return 0;
432}
433
Christian Königefd4ccb2015-08-04 16:20:31 +0200434void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
435{
436 mutex_init(&mgr->lock);
437 idr_init(&mgr->ctx_handles);
438}
439
440void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
441{
442 struct amdgpu_ctx *ctx;
443 struct idr *idp;
444 uint32_t id;
445
446 idp = &mgr->ctx_handles;
447
448 idr_for_each_entry(idp, ctx, id) {
449 if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
450 DRM_ERROR("ctx %p is still alive\n", ctx);
451 }
452
453 idr_destroy(&mgr->ctx_handles);
454 mutex_destroy(&mgr->lock);
455}