2 * Copyright 2015 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include <drm/drm_auth.h>
28 #include "amdgpu_sched.h"
30 static int amdgpu_ctx_priority_permit(struct drm_file *filp,
31 enum drm_sched_priority priority)
33 /* NORMAL and below are accessible by everyone */
34 if (priority <= DRM_SCHED_PRIORITY_NORMAL)
37 if (capable(CAP_SYS_NICE))
40 if (drm_is_current_master(filp))
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)
54 if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
57 r = amdgpu_ctx_priority_permit(filp, priority);
61 memset(ctx, 0, sizeof(*ctx));
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);
70 mutex_init(&ctx->lock);
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];
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;
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;
88 rq = &ring->sched.sched_rq[priority];
90 if (ring == &adev->gfx.kiq.ring)
93 r = drm_sched_entity_init(&ring->sched, &ctx->rings[i].entity,
99 r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr);
106 for (j = 0; j < i; j++)
107 drm_sched_entity_fini(&adev->rings[j]->sched,
108 &ctx->rings[j].entity);
114 static void amdgpu_ctx_fini(struct kref *ref)
116 struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
117 struct amdgpu_device *adev = ctx->adev;
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]);
129 amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
131 mutex_destroy(&ctx->lock);
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,
142 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
143 struct amdgpu_ctx *ctx;
146 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
150 mutex_lock(&mgr->lock);
151 r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
153 mutex_unlock(&mgr->lock);
159 r = amdgpu_ctx_init(adev, priority, filp, ctx);
161 idr_remove(&mgr->ctx_handles, *id);
165 mutex_unlock(&mgr->lock);
169 static void amdgpu_ctx_do_release(struct kref *ref)
171 struct amdgpu_ctx *ctx;
174 ctx = container_of(ref, struct amdgpu_ctx, refcount);
176 for (i = 0; i < ctx->adev->num_rings; i++) {
178 if (ctx->adev->rings[i] == &ctx->adev->gfx.kiq.ring)
181 drm_sched_entity_fini(&ctx->adev->rings[i]->sched,
182 &ctx->rings[i].entity);
185 amdgpu_ctx_fini(ref);
188 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
190 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
191 struct amdgpu_ctx *ctx;
193 mutex_lock(&mgr->lock);
194 ctx = idr_remove(&mgr->ctx_handles, id);
196 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
197 mutex_unlock(&mgr->lock);
198 return ctx ? 0 : -EINVAL;
201 static int amdgpu_ctx_query(struct amdgpu_device *adev,
202 struct amdgpu_fpriv *fpriv, uint32_t id,
203 union drm_amdgpu_ctx_out *out)
205 struct amdgpu_ctx *ctx;
206 struct amdgpu_ctx_mgr *mgr;
207 unsigned reset_counter;
212 mgr = &fpriv->ctx_mgr;
213 mutex_lock(&mgr->lock);
214 ctx = idr_find(&mgr->ctx_handles, id);
216 mutex_unlock(&mgr->lock);
220 /* TODO: these two are always zero */
221 out->state.flags = 0x0;
222 out->state.hangs = 0x0;
224 /* determine if a GPU reset has occured since the last call */
225 reset_counter = atomic_read(&adev->gpu_reset_counter);
226 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
227 if (ctx->reset_counter_query == reset_counter)
228 out->state.reset_status = AMDGPU_CTX_NO_RESET;
230 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
231 ctx->reset_counter_query = reset_counter;
233 mutex_unlock(&mgr->lock);
237 static int amdgpu_ctx_query2(struct amdgpu_device *adev,
238 struct amdgpu_fpriv *fpriv, uint32_t id,
239 union drm_amdgpu_ctx_out *out)
241 struct amdgpu_ctx *ctx;
242 struct amdgpu_ctx_mgr *mgr;
247 mgr = &fpriv->ctx_mgr;
248 mutex_lock(&mgr->lock);
249 ctx = idr_find(&mgr->ctx_handles, id);
251 mutex_unlock(&mgr->lock);
255 out->state.flags = 0x0;
256 out->state.hangs = 0x0;
258 if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
259 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
261 if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
262 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
264 if (atomic_read(&ctx->guilty))
265 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
267 mutex_unlock(&mgr->lock);
271 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
272 struct drm_file *filp)
276 enum drm_sched_priority priority;
278 union drm_amdgpu_ctx *args = data;
279 struct amdgpu_device *adev = dev->dev_private;
280 struct amdgpu_fpriv *fpriv = filp->driver_priv;
283 id = args->in.ctx_id;
284 priority = amdgpu_to_sched_priority(args->in.priority);
286 /* For backwards compatibility reasons, we need to accept
287 * ioctls with garbage in the priority field */
288 if (priority == DRM_SCHED_PRIORITY_INVALID)
289 priority = DRM_SCHED_PRIORITY_NORMAL;
291 switch (args->in.op) {
292 case AMDGPU_CTX_OP_ALLOC_CTX:
293 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
294 args->out.alloc.ctx_id = id;
296 case AMDGPU_CTX_OP_FREE_CTX:
297 r = amdgpu_ctx_free(fpriv, id);
299 case AMDGPU_CTX_OP_QUERY_STATE:
300 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
302 case AMDGPU_CTX_OP_QUERY_STATE2:
303 r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
312 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
314 struct amdgpu_ctx *ctx;
315 struct amdgpu_ctx_mgr *mgr;
320 mgr = &fpriv->ctx_mgr;
322 mutex_lock(&mgr->lock);
323 ctx = idr_find(&mgr->ctx_handles, id);
325 kref_get(&ctx->refcount);
326 mutex_unlock(&mgr->lock);
330 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
335 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
339 int amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
340 struct dma_fence *fence, uint64_t* handler)
342 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
343 uint64_t seq = cring->sequence;
345 struct dma_fence *other = NULL;
347 idx = seq & (amdgpu_sched_jobs - 1);
348 other = cring->fences[idx];
350 BUG_ON(!dma_fence_is_signaled(other));
352 dma_fence_get(fence);
354 spin_lock(&ctx->ring_lock);
355 cring->fences[idx] = fence;
357 spin_unlock(&ctx->ring_lock);
359 dma_fence_put(other);
366 struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
367 struct amdgpu_ring *ring, uint64_t seq)
369 struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
370 struct dma_fence *fence;
372 spin_lock(&ctx->ring_lock);
375 seq = ctx->rings[ring->idx].sequence - 1;
377 if (seq >= cring->sequence) {
378 spin_unlock(&ctx->ring_lock);
379 return ERR_PTR(-EINVAL);
383 if (seq + amdgpu_sched_jobs < cring->sequence) {
384 spin_unlock(&ctx->ring_lock);
388 fence = dma_fence_get(cring->fences[seq & (amdgpu_sched_jobs - 1)]);
389 spin_unlock(&ctx->ring_lock);
394 void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
395 enum drm_sched_priority priority)
398 struct amdgpu_device *adev = ctx->adev;
399 struct drm_sched_rq *rq;
400 struct drm_sched_entity *entity;
401 struct amdgpu_ring *ring;
402 enum drm_sched_priority ctx_prio;
404 ctx->override_priority = priority;
406 ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
407 ctx->init_priority : ctx->override_priority;
409 for (i = 0; i < adev->num_rings; i++) {
410 ring = adev->rings[i];
411 entity = &ctx->rings[i].entity;
412 rq = &ring->sched.sched_rq[ctx_prio];
414 if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
417 drm_sched_entity_set_rq(entity, rq);
421 int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx, unsigned ring_id)
423 struct amdgpu_ctx_ring *cring = &ctx->rings[ring_id];
424 unsigned idx = cring->sequence & (amdgpu_sched_jobs - 1);
425 struct dma_fence *other = cring->fences[idx];
429 r = dma_fence_wait(other, true);
431 if (r != -ERESTARTSYS)
432 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
441 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
443 mutex_init(&mgr->lock);
444 idr_init(&mgr->ctx_handles);
447 void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
449 struct amdgpu_ctx *ctx;
453 idp = &mgr->ctx_handles;
455 idr_for_each_entry(idp, ctx, id) {
460 for (i = 0; i < ctx->adev->num_rings; i++) {
462 if (ctx->adev->rings[i] == &ctx->adev->gfx.kiq.ring)
465 if (kref_read(&ctx->refcount) == 1)
466 drm_sched_entity_do_release(&ctx->adev->rings[i]->sched,
467 &ctx->rings[i].entity);
469 DRM_ERROR("ctx %p is still alive\n", ctx);
474 void amdgpu_ctx_mgr_entity_cleanup(struct amdgpu_ctx_mgr *mgr)
476 struct amdgpu_ctx *ctx;
480 idp = &mgr->ctx_handles;
482 idr_for_each_entry(idp, ctx, id) {
487 for (i = 0; i < ctx->adev->num_rings; i++) {
489 if (ctx->adev->rings[i] == &ctx->adev->gfx.kiq.ring)
492 if (kref_read(&ctx->refcount) == 1)
493 drm_sched_entity_cleanup(&ctx->adev->rings[i]->sched,
494 &ctx->rings[i].entity);
496 DRM_ERROR("ctx %p is still alive\n", ctx);
501 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
503 struct amdgpu_ctx *ctx;
507 amdgpu_ctx_mgr_entity_cleanup(mgr);
509 idp = &mgr->ctx_handles;
511 idr_for_each_entry(idp, ctx, id) {
512 if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
513 DRM_ERROR("ctx %p is still alive\n", ctx);
516 idr_destroy(&mgr->ctx_handles);
517 mutex_destroy(&mgr->lock);