]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
drm/amdgpu: use amdgpu_bo_param for amdgpu_bo_create v2
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_ctx.c
1 /*
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 <[email protected]>
23  */
24
25 #include <drm/drmP.h>
26 #include <drm/drm_auth.h>
27 #include "amdgpu.h"
28 #include "amdgpu_sched.h"
29
30 static int amdgpu_ctx_priority_permit(struct drm_file *filp,
31                                       enum drm_sched_priority priority)
32 {
33         /* NORMAL and below are accessible by everyone */
34         if (priority <= DRM_SCHED_PRIORITY_NORMAL)
35                 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
46 static int amdgpu_ctx_init(struct amdgpu_device *adev,
47                            enum drm_sched_priority priority,
48                            struct drm_file *filp,
49                            struct amdgpu_ctx *ctx)
50 {
51         unsigned i, j;
52         int r;
53
54         if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
55                 return -EINVAL;
56
57         r = amdgpu_ctx_priority_permit(filp, priority);
58         if (r)
59                 return r;
60
61         memset(ctx, 0, sizeof(*ctx));
62         ctx->adev = adev;
63         kref_init(&ctx->refcount);
64         spin_lock_init(&ctx->ring_lock);
65         ctx->fences = kcalloc(amdgpu_sched_jobs * AMDGPU_MAX_RINGS,
66                               sizeof(struct dma_fence*), GFP_KERNEL);
67         if (!ctx->fences)
68                 return -ENOMEM;
69
70         mutex_init(&ctx->lock);
71
72         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
73                 ctx->rings[i].sequence = 1;
74                 ctx->rings[i].fences = &ctx->fences[amdgpu_sched_jobs * i];
75         }
76
77         ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
78         ctx->reset_counter_query = ctx->reset_counter;
79         ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
80         ctx->init_priority = priority;
81         ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
82
83         /* create context entity for each ring */
84         for (i = 0; i < adev->num_rings; i++) {
85                 struct amdgpu_ring *ring = adev->rings[i];
86                 struct drm_sched_rq *rq;
87
88                 rq = &ring->sched.sched_rq[priority];
89
90                 if (ring == &adev->gfx.kiq.ring)
91                         continue;
92
93                 r = drm_sched_entity_init(&ring->sched, &ctx->rings[i].entity,
94                                           rq, amdgpu_sched_jobs, &ctx->guilty);
95                 if (r)
96                         goto failed;
97         }
98
99         r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr);
100         if (r)
101                 goto failed;
102
103         return 0;
104
105 failed:
106         for (j = 0; j < i; j++)
107                 drm_sched_entity_fini(&adev->rings[j]->sched,
108                                       &ctx->rings[j].entity);
109         kfree(ctx->fences);
110         ctx->fences = NULL;
111         return r;
112 }
113
114 static void amdgpu_ctx_fini(struct kref *ref)
115 {
116         struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
117         struct amdgpu_device *adev = ctx->adev;
118         unsigned i, j;
119
120         if (!adev)
121                 return;
122
123         for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
124                 for (j = 0; j < amdgpu_sched_jobs; ++j)
125                         dma_fence_put(ctx->rings[i].fences[j]);
126         kfree(ctx->fences);
127         ctx->fences = NULL;
128
129         amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
130
131         mutex_destroy(&ctx->lock);
132
133         kfree(ctx);
134 }
135
136 static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
137                             struct amdgpu_fpriv *fpriv,
138                             struct drm_file *filp,
139                             enum drm_sched_priority priority,
140                             uint32_t *id)
141 {
142         struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
143         struct amdgpu_ctx *ctx;
144         int r;
145
146         ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
147         if (!ctx)
148                 return -ENOMEM;
149
150         mutex_lock(&mgr->lock);
151         r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
152         if (r < 0) {
153                 mutex_unlock(&mgr->lock);
154                 kfree(ctx);
155                 return r;
156         }
157
158         *id = (uint32_t)r;
159         r = amdgpu_ctx_init(adev, priority, filp, ctx);
160         if (r) {
161                 idr_remove(&mgr->ctx_handles, *id);
162                 *id = 0;
163                 kfree(ctx);
164         }
165         mutex_unlock(&mgr->lock);
166         return r;
167 }
168
169 static void amdgpu_ctx_do_release(struct kref *ref)
170 {
171         struct amdgpu_ctx *ctx;
172         u32 i;
173
174         ctx = container_of(ref, struct amdgpu_ctx, refcount);
175
176         for (i = 0; i < ctx->adev->num_rings; i++)
177                 drm_sched_entity_fini(&ctx->adev->rings[i]->sched,
178                         &ctx->rings[i].entity);
179
180         amdgpu_ctx_fini(ref);
181 }
182
183 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
184 {
185         struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
186         struct amdgpu_ctx *ctx;
187
188         mutex_lock(&mgr->lock);
189         ctx = idr_remove(&mgr->ctx_handles, id);
190         if (ctx)
191                 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
192         mutex_unlock(&mgr->lock);
193         return ctx ? 0 : -EINVAL;
194 }
195
196 static int amdgpu_ctx_query(struct amdgpu_device *adev,
197                             struct amdgpu_fpriv *fpriv, uint32_t id,
198                             union drm_amdgpu_ctx_out *out)
199 {
200         struct amdgpu_ctx *ctx;
201         struct amdgpu_ctx_mgr *mgr;
202         unsigned reset_counter;
203
204         if (!fpriv)
205                 return -EINVAL;
206
207         mgr = &fpriv->ctx_mgr;
208         mutex_lock(&mgr->lock);
209         ctx = idr_find(&mgr->ctx_handles, id);
210         if (!ctx) {
211                 mutex_unlock(&mgr->lock);
212                 return -EINVAL;
213         }
214
215         /* TODO: these two are always zero */
216         out->state.flags = 0x0;
217         out->state.hangs = 0x0;
218
219         /* determine if a GPU reset has occured since the last call */
220         reset_counter = atomic_read(&adev->gpu_reset_counter);
221         /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
222         if (ctx->reset_counter_query == reset_counter)
223                 out->state.reset_status = AMDGPU_CTX_NO_RESET;
224         else
225                 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
226         ctx->reset_counter_query = reset_counter;
227
228         mutex_unlock(&mgr->lock);
229         return 0;
230 }
231
232 static int amdgpu_ctx_query2(struct amdgpu_device *adev,
233         struct amdgpu_fpriv *fpriv, uint32_t id,
234         union drm_amdgpu_ctx_out *out)
235 {
236         struct amdgpu_ctx *ctx;
237         struct amdgpu_ctx_mgr *mgr;
238
239         if (!fpriv)
240                 return -EINVAL;
241
242         mgr = &fpriv->ctx_mgr;
243         mutex_lock(&mgr->lock);
244         ctx = idr_find(&mgr->ctx_handles, id);
245         if (!ctx) {
246                 mutex_unlock(&mgr->lock);
247                 return -EINVAL;
248         }
249
250         out->state.flags = 0x0;
251         out->state.hangs = 0x0;
252
253         if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
254                 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
255
256         if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
257                 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
258
259         if (atomic_read(&ctx->guilty))
260                 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
261
262         mutex_unlock(&mgr->lock);
263         return 0;
264 }
265
266 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
267                      struct drm_file *filp)
268 {
269         int r;
270         uint32_t id;
271         enum drm_sched_priority priority;
272
273         union drm_amdgpu_ctx *args = data;
274         struct amdgpu_device *adev = dev->dev_private;
275         struct amdgpu_fpriv *fpriv = filp->driver_priv;
276
277         r = 0;
278         id = args->in.ctx_id;
279         priority = amdgpu_to_sched_priority(args->in.priority);
280
281         /* For backwards compatibility reasons, we need to accept
282          * ioctls with garbage in the priority field */
283         if (priority == DRM_SCHED_PRIORITY_INVALID)
284                 priority = DRM_SCHED_PRIORITY_NORMAL;
285
286         switch (args->in.op) {
287         case AMDGPU_CTX_OP_ALLOC_CTX:
288                 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
289                 args->out.alloc.ctx_id = id;
290                 break;
291         case AMDGPU_CTX_OP_FREE_CTX:
292                 r = amdgpu_ctx_free(fpriv, id);
293                 break;
294         case AMDGPU_CTX_OP_QUERY_STATE:
295                 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
296                 break;
297         case AMDGPU_CTX_OP_QUERY_STATE2:
298                 r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
299                 break;
300         default:
301                 return -EINVAL;
302         }
303
304         return r;
305 }
306
307 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
308 {
309         struct amdgpu_ctx *ctx;
310         struct amdgpu_ctx_mgr *mgr;
311
312         if (!fpriv)
313                 return NULL;
314
315         mgr = &fpriv->ctx_mgr;
316
317         mutex_lock(&mgr->lock);
318         ctx = idr_find(&mgr->ctx_handles, id);
319         if (ctx)
320                 kref_get(&ctx->refcount);
321         mutex_unlock(&mgr->lock);
322         return ctx;
323 }
324
325 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
326 {
327         if (ctx == NULL)
328                 return -EINVAL;
329
330         kref_put(&ctx->refcount, amdgpu_ctx_do_release);
331         return 0;
332 }
333
334 int amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
335                               struct dma_fence *fence, uint64_t* handler)
336 {
337         struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
338         uint64_t seq = cring->sequence;
339         unsigned idx = 0;
340         struct dma_fence *other = NULL;
341
342         idx = seq & (amdgpu_sched_jobs - 1);
343         other = cring->fences[idx];
344         if (other)
345                 BUG_ON(!dma_fence_is_signaled(other));
346
347         dma_fence_get(fence);
348
349         spin_lock(&ctx->ring_lock);
350         cring->fences[idx] = fence;
351         cring->sequence++;
352         spin_unlock(&ctx->ring_lock);
353
354         dma_fence_put(other);
355         if (handler)
356                 *handler = seq;
357
358         return 0;
359 }
360
361 struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
362                                        struct amdgpu_ring *ring, uint64_t seq)
363 {
364         struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
365         struct dma_fence *fence;
366
367         spin_lock(&ctx->ring_lock);
368
369         if (seq == ~0ull)
370                 seq = ctx->rings[ring->idx].sequence - 1;
371
372         if (seq >= cring->sequence) {
373                 spin_unlock(&ctx->ring_lock);
374                 return ERR_PTR(-EINVAL);
375         }
376
377
378         if (seq + amdgpu_sched_jobs < cring->sequence) {
379                 spin_unlock(&ctx->ring_lock);
380                 return NULL;
381         }
382
383         fence = dma_fence_get(cring->fences[seq & (amdgpu_sched_jobs - 1)]);
384         spin_unlock(&ctx->ring_lock);
385
386         return fence;
387 }
388
389 void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
390                                   enum drm_sched_priority priority)
391 {
392         int i;
393         struct amdgpu_device *adev = ctx->adev;
394         struct drm_sched_rq *rq;
395         struct drm_sched_entity *entity;
396         struct amdgpu_ring *ring;
397         enum drm_sched_priority ctx_prio;
398
399         ctx->override_priority = priority;
400
401         ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
402                         ctx->init_priority : ctx->override_priority;
403
404         for (i = 0; i < adev->num_rings; i++) {
405                 ring = adev->rings[i];
406                 entity = &ctx->rings[i].entity;
407                 rq = &ring->sched.sched_rq[ctx_prio];
408
409                 if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
410                         continue;
411
412                 drm_sched_entity_set_rq(entity, rq);
413         }
414 }
415
416 int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx, unsigned ring_id)
417 {
418         struct amdgpu_ctx_ring *cring = &ctx->rings[ring_id];
419         unsigned idx = cring->sequence & (amdgpu_sched_jobs - 1);
420         struct dma_fence *other = cring->fences[idx];
421
422         if (other) {
423                 signed long r;
424                 r = dma_fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT);
425                 if (r < 0) {
426                         DRM_ERROR("Error (%ld) waiting for fence!\n", r);
427                         return r;
428                 }
429         }
430
431         return 0;
432 }
433
434 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
435 {
436         mutex_init(&mgr->lock);
437         idr_init(&mgr->ctx_handles);
438 }
439
440 void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
441 {
442         struct amdgpu_ctx *ctx;
443         struct idr *idp;
444         uint32_t id, i;
445
446         idp = &mgr->ctx_handles;
447
448         idr_for_each_entry(idp, ctx, id) {
449
450                 if (!ctx->adev)
451                         return;
452
453                 for (i = 0; i < ctx->adev->num_rings; i++)
454                         if (kref_read(&ctx->refcount) == 1)
455                                 drm_sched_entity_do_release(&ctx->adev->rings[i]->sched,
456                                                   &ctx->rings[i].entity);
457                         else
458                                 DRM_ERROR("ctx %p is still alive\n", ctx);
459         }
460 }
461
462 void amdgpu_ctx_mgr_entity_cleanup(struct amdgpu_ctx_mgr *mgr)
463 {
464         struct amdgpu_ctx *ctx;
465         struct idr *idp;
466         uint32_t id, i;
467
468         idp = &mgr->ctx_handles;
469
470         idr_for_each_entry(idp, ctx, id) {
471
472                 if (!ctx->adev)
473                         return;
474
475                 for (i = 0; i < ctx->adev->num_rings; i++)
476                         if (kref_read(&ctx->refcount) == 1)
477                                 drm_sched_entity_cleanup(&ctx->adev->rings[i]->sched,
478                                         &ctx->rings[i].entity);
479                         else
480                                 DRM_ERROR("ctx %p is still alive\n", ctx);
481         }
482 }
483
484 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
485 {
486         struct amdgpu_ctx *ctx;
487         struct idr *idp;
488         uint32_t id;
489
490         amdgpu_ctx_mgr_entity_cleanup(mgr);
491
492         idp = &mgr->ctx_handles;
493
494         idr_for_each_entry(idp, ctx, id) {
495                 if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
496                         DRM_ERROR("ctx %p is still alive\n", ctx);
497         }
498
499         idr_destroy(&mgr->ctx_handles);
500         mutex_destroy(&mgr->lock);
501 }
This page took 0.063085 seconds and 4 git commands to generate.