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 #define to_amdgpu_ctx_entity(e) \
31 container_of((e), struct amdgpu_ctx_entity, entity)
33 const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM] = {
34 [AMDGPU_HW_IP_GFX] = 1,
35 [AMDGPU_HW_IP_COMPUTE] = 4,
36 [AMDGPU_HW_IP_DMA] = 2,
37 [AMDGPU_HW_IP_UVD] = 1,
38 [AMDGPU_HW_IP_VCE] = 1,
39 [AMDGPU_HW_IP_UVD_ENC] = 1,
40 [AMDGPU_HW_IP_VCN_DEC] = 1,
41 [AMDGPU_HW_IP_VCN_ENC] = 1,
42 [AMDGPU_HW_IP_VCN_JPEG] = 1,
45 static int amdgput_ctx_total_num_entities(void)
47 unsigned i, num_entities = 0;
49 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
50 num_entities += amdgpu_ctx_num_entities[i];
55 static int amdgpu_ctx_priority_permit(struct drm_file *filp,
56 enum drm_sched_priority priority)
58 /* NORMAL and below are accessible by everyone */
59 if (priority <= DRM_SCHED_PRIORITY_NORMAL)
62 if (capable(CAP_SYS_NICE))
65 if (drm_is_current_master(filp))
71 static int amdgpu_ctx_init(struct amdgpu_device *adev,
72 enum drm_sched_priority priority,
73 struct drm_file *filp,
74 struct amdgpu_ctx *ctx)
76 unsigned num_entities = amdgput_ctx_total_num_entities();
80 if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
83 r = amdgpu_ctx_priority_permit(filp, priority);
87 memset(ctx, 0, sizeof(*ctx));
90 ctx->fences = kcalloc(amdgpu_sched_jobs * num_entities,
91 sizeof(struct dma_fence*), GFP_KERNEL);
95 ctx->entities[0] = kcalloc(num_entities,
96 sizeof(struct amdgpu_ctx_entity),
98 if (!ctx->entities[0]) {
100 goto error_free_fences;
103 for (i = 0; i < num_entities; ++i) {
104 struct amdgpu_ctx_entity *entity = &ctx->entities[0][i];
106 entity->sequence = 1;
107 entity->fences = &ctx->fences[amdgpu_sched_jobs * i];
109 for (i = 1; i < AMDGPU_HW_IP_NUM; ++i)
110 ctx->entities[i] = ctx->entities[i - 1] +
111 amdgpu_ctx_num_entities[i - 1];
113 kref_init(&ctx->refcount);
114 spin_lock_init(&ctx->ring_lock);
115 mutex_init(&ctx->lock);
117 ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
118 ctx->reset_counter_query = ctx->reset_counter;
119 ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
120 ctx->init_priority = priority;
121 ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
123 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
124 struct amdgpu_ring *rings[AMDGPU_MAX_RINGS];
125 struct drm_sched_rq *rqs[AMDGPU_MAX_RINGS];
129 case AMDGPU_HW_IP_GFX:
130 rings[0] = &adev->gfx.gfx_ring[0];
133 case AMDGPU_HW_IP_COMPUTE:
134 for (j = 0; j < adev->gfx.num_compute_rings; ++j)
135 rings[j] = &adev->gfx.compute_ring[j];
136 num_rings = adev->gfx.num_compute_rings;
138 case AMDGPU_HW_IP_DMA:
139 for (j = 0; j < adev->sdma.num_instances; ++j)
140 rings[j] = &adev->sdma.instance[j].ring;
141 num_rings = adev->sdma.num_instances;
143 case AMDGPU_HW_IP_UVD:
144 rings[0] = &adev->uvd.inst[0].ring;
147 case AMDGPU_HW_IP_VCE:
148 rings[0] = &adev->vce.ring[0];
151 case AMDGPU_HW_IP_UVD_ENC:
152 rings[0] = &adev->uvd.inst[0].ring_enc[0];
155 case AMDGPU_HW_IP_VCN_DEC:
156 rings[0] = &adev->vcn.ring_dec;
159 case AMDGPU_HW_IP_VCN_ENC:
160 rings[0] = &adev->vcn.ring_enc[0];
163 case AMDGPU_HW_IP_VCN_JPEG:
164 rings[0] = &adev->vcn.ring_jpeg;
169 for (j = 0; j < num_rings; ++j)
170 rqs[j] = &rings[j]->sched.sched_rq[priority];
172 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
173 r = drm_sched_entity_init(&ctx->entities[i][j].entity,
174 rqs, num_rings, &ctx->guilty);
176 goto error_cleanup_entities;
181 error_cleanup_entities:
182 for (i = 0; i < num_entities; ++i)
183 drm_sched_entity_destroy(&ctx->entities[0][i].entity);
184 kfree(ctx->entities[0]);
192 static void amdgpu_ctx_fini(struct kref *ref)
194 struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
195 unsigned num_entities = amdgput_ctx_total_num_entities();
196 struct amdgpu_device *adev = ctx->adev;
202 for (i = 0; i < num_entities; ++i)
203 for (j = 0; j < amdgpu_sched_jobs; ++j)
204 dma_fence_put(ctx->entities[0][i].fences[j]);
206 kfree(ctx->entities[0]);
208 mutex_destroy(&ctx->lock);
213 int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
214 u32 ring, struct drm_sched_entity **entity)
216 if (hw_ip >= AMDGPU_HW_IP_NUM) {
217 DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
221 /* Right now all IPs have only one instance - multiple rings. */
223 DRM_DEBUG("invalid ip instance: %d\n", instance);
227 if (ring >= amdgpu_ctx_num_entities[hw_ip]) {
228 DRM_DEBUG("invalid ring: %d %d\n", hw_ip, ring);
232 *entity = &ctx->entities[hw_ip][ring].entity;
236 static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
237 struct amdgpu_fpriv *fpriv,
238 struct drm_file *filp,
239 enum drm_sched_priority priority,
242 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
243 struct amdgpu_ctx *ctx;
246 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
250 mutex_lock(&mgr->lock);
251 r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
253 mutex_unlock(&mgr->lock);
259 r = amdgpu_ctx_init(adev, priority, filp, ctx);
261 idr_remove(&mgr->ctx_handles, *id);
265 mutex_unlock(&mgr->lock);
269 static void amdgpu_ctx_do_release(struct kref *ref)
271 struct amdgpu_ctx *ctx;
272 unsigned num_entities;
275 ctx = container_of(ref, struct amdgpu_ctx, refcount);
278 for (i = 0; i < AMDGPU_HW_IP_NUM; i++)
279 num_entities += amdgpu_ctx_num_entities[i];
281 for (i = 0; i < num_entities; i++)
282 drm_sched_entity_destroy(&ctx->entities[0][i].entity);
284 amdgpu_ctx_fini(ref);
287 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
289 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
290 struct amdgpu_ctx *ctx;
292 mutex_lock(&mgr->lock);
293 ctx = idr_remove(&mgr->ctx_handles, id);
295 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
296 mutex_unlock(&mgr->lock);
297 return ctx ? 0 : -EINVAL;
300 static int amdgpu_ctx_query(struct amdgpu_device *adev,
301 struct amdgpu_fpriv *fpriv, uint32_t id,
302 union drm_amdgpu_ctx_out *out)
304 struct amdgpu_ctx *ctx;
305 struct amdgpu_ctx_mgr *mgr;
306 unsigned reset_counter;
311 mgr = &fpriv->ctx_mgr;
312 mutex_lock(&mgr->lock);
313 ctx = idr_find(&mgr->ctx_handles, id);
315 mutex_unlock(&mgr->lock);
319 /* TODO: these two are always zero */
320 out->state.flags = 0x0;
321 out->state.hangs = 0x0;
323 /* determine if a GPU reset has occured since the last call */
324 reset_counter = atomic_read(&adev->gpu_reset_counter);
325 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
326 if (ctx->reset_counter_query == reset_counter)
327 out->state.reset_status = AMDGPU_CTX_NO_RESET;
329 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
330 ctx->reset_counter_query = reset_counter;
332 mutex_unlock(&mgr->lock);
336 static int amdgpu_ctx_query2(struct amdgpu_device *adev,
337 struct amdgpu_fpriv *fpriv, uint32_t id,
338 union drm_amdgpu_ctx_out *out)
340 struct amdgpu_ctx *ctx;
341 struct amdgpu_ctx_mgr *mgr;
346 mgr = &fpriv->ctx_mgr;
347 mutex_lock(&mgr->lock);
348 ctx = idr_find(&mgr->ctx_handles, id);
350 mutex_unlock(&mgr->lock);
354 out->state.flags = 0x0;
355 out->state.hangs = 0x0;
357 if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
358 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
360 if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
361 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
363 if (atomic_read(&ctx->guilty))
364 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
366 mutex_unlock(&mgr->lock);
370 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
371 struct drm_file *filp)
375 enum drm_sched_priority priority;
377 union drm_amdgpu_ctx *args = data;
378 struct amdgpu_device *adev = dev->dev_private;
379 struct amdgpu_fpriv *fpriv = filp->driver_priv;
382 id = args->in.ctx_id;
383 priority = amdgpu_to_sched_priority(args->in.priority);
385 /* For backwards compatibility reasons, we need to accept
386 * ioctls with garbage in the priority field */
387 if (priority == DRM_SCHED_PRIORITY_INVALID)
388 priority = DRM_SCHED_PRIORITY_NORMAL;
390 switch (args->in.op) {
391 case AMDGPU_CTX_OP_ALLOC_CTX:
392 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
393 args->out.alloc.ctx_id = id;
395 case AMDGPU_CTX_OP_FREE_CTX:
396 r = amdgpu_ctx_free(fpriv, id);
398 case AMDGPU_CTX_OP_QUERY_STATE:
399 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
401 case AMDGPU_CTX_OP_QUERY_STATE2:
402 r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
411 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
413 struct amdgpu_ctx *ctx;
414 struct amdgpu_ctx_mgr *mgr;
419 mgr = &fpriv->ctx_mgr;
421 mutex_lock(&mgr->lock);
422 ctx = idr_find(&mgr->ctx_handles, id);
424 kref_get(&ctx->refcount);
425 mutex_unlock(&mgr->lock);
429 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
434 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
438 void amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
439 struct drm_sched_entity *entity,
440 struct dma_fence *fence, uint64_t* handle)
442 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
443 uint64_t seq = centity->sequence;
444 struct dma_fence *other = NULL;
447 idx = seq & (amdgpu_sched_jobs - 1);
448 other = centity->fences[idx];
450 BUG_ON(!dma_fence_is_signaled(other));
452 dma_fence_get(fence);
454 spin_lock(&ctx->ring_lock);
455 centity->fences[idx] = fence;
457 spin_unlock(&ctx->ring_lock);
459 dma_fence_put(other);
464 struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
465 struct drm_sched_entity *entity,
468 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
469 struct dma_fence *fence;
471 spin_lock(&ctx->ring_lock);
474 seq = centity->sequence - 1;
476 if (seq >= centity->sequence) {
477 spin_unlock(&ctx->ring_lock);
478 return ERR_PTR(-EINVAL);
482 if (seq + amdgpu_sched_jobs < centity->sequence) {
483 spin_unlock(&ctx->ring_lock);
487 fence = dma_fence_get(centity->fences[seq & (amdgpu_sched_jobs - 1)]);
488 spin_unlock(&ctx->ring_lock);
493 void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
494 enum drm_sched_priority priority)
496 unsigned num_entities = amdgput_ctx_total_num_entities();
497 enum drm_sched_priority ctx_prio;
500 ctx->override_priority = priority;
502 ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
503 ctx->init_priority : ctx->override_priority;
505 for (i = 0; i < num_entities; i++) {
506 struct drm_sched_entity *entity = &ctx->entities[0][i].entity;
508 drm_sched_entity_set_priority(entity, ctx_prio);
512 int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
513 struct drm_sched_entity *entity)
515 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
516 unsigned idx = centity->sequence & (amdgpu_sched_jobs - 1);
517 struct dma_fence *other = centity->fences[idx];
521 r = dma_fence_wait(other, true);
523 if (r != -ERESTARTSYS)
524 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
533 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
535 mutex_init(&mgr->lock);
536 idr_init(&mgr->ctx_handles);
539 void amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr)
541 unsigned num_entities = amdgput_ctx_total_num_entities();
542 struct amdgpu_ctx *ctx;
545 long max_wait = MAX_WAIT_SCHED_ENTITY_Q_EMPTY;
547 idp = &mgr->ctx_handles;
549 mutex_lock(&mgr->lock);
550 idr_for_each_entry(idp, ctx, id) {
553 mutex_unlock(&mgr->lock);
557 for (i = 0; i < num_entities; i++) {
558 struct drm_sched_entity *entity;
560 entity = &ctx->entities[0][i].entity;
561 max_wait = drm_sched_entity_flush(entity, max_wait);
564 mutex_unlock(&mgr->lock);
567 void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
569 unsigned num_entities = amdgput_ctx_total_num_entities();
570 struct amdgpu_ctx *ctx;
574 idp = &mgr->ctx_handles;
576 idr_for_each_entry(idp, ctx, id) {
581 if (kref_read(&ctx->refcount) != 1) {
582 DRM_ERROR("ctx %p is still alive\n", ctx);
586 for (i = 0; i < num_entities; i++)
587 drm_sched_entity_fini(&ctx->entities[0][i].entity);
591 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
593 struct amdgpu_ctx *ctx;
597 amdgpu_ctx_mgr_entity_fini(mgr);
599 idp = &mgr->ctx_handles;
601 idr_for_each_entry(idp, ctx, id) {
602 if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
603 DRM_ERROR("ctx %p is still alive\n", ctx);
606 idr_destroy(&mgr->ctx_handles);
607 mutex_destroy(&mgr->lock);