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.
25 #include <drm/drm_auth.h>
26 #include <drm/drm_drv.h>
28 #include "amdgpu_sched.h"
29 #include "amdgpu_ras.h"
30 #include <linux/nospec.h>
32 #define to_amdgpu_ctx_entity(e) \
33 container_of((e), struct amdgpu_ctx_entity, entity)
35 const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM] = {
36 [AMDGPU_HW_IP_GFX] = 1,
37 [AMDGPU_HW_IP_COMPUTE] = 4,
38 [AMDGPU_HW_IP_DMA] = 2,
39 [AMDGPU_HW_IP_UVD] = 1,
40 [AMDGPU_HW_IP_VCE] = 1,
41 [AMDGPU_HW_IP_UVD_ENC] = 1,
42 [AMDGPU_HW_IP_VCN_DEC] = 1,
43 [AMDGPU_HW_IP_VCN_ENC] = 1,
44 [AMDGPU_HW_IP_VCN_JPEG] = 1,
47 bool amdgpu_ctx_priority_is_valid(int32_t ctx_prio)
50 case AMDGPU_CTX_PRIORITY_UNSET:
51 case AMDGPU_CTX_PRIORITY_VERY_LOW:
52 case AMDGPU_CTX_PRIORITY_LOW:
53 case AMDGPU_CTX_PRIORITY_NORMAL:
54 case AMDGPU_CTX_PRIORITY_HIGH:
55 case AMDGPU_CTX_PRIORITY_VERY_HIGH:
62 static enum drm_sched_priority
63 amdgpu_ctx_to_drm_sched_prio(int32_t ctx_prio)
66 case AMDGPU_CTX_PRIORITY_UNSET:
67 return DRM_SCHED_PRIORITY_UNSET;
69 case AMDGPU_CTX_PRIORITY_VERY_LOW:
70 return DRM_SCHED_PRIORITY_MIN;
72 case AMDGPU_CTX_PRIORITY_LOW:
73 return DRM_SCHED_PRIORITY_MIN;
75 case AMDGPU_CTX_PRIORITY_NORMAL:
76 return DRM_SCHED_PRIORITY_NORMAL;
78 case AMDGPU_CTX_PRIORITY_HIGH:
79 return DRM_SCHED_PRIORITY_HIGH;
81 case AMDGPU_CTX_PRIORITY_VERY_HIGH:
82 return DRM_SCHED_PRIORITY_HIGH;
84 /* This should not happen as we sanitized userspace provided priority
85 * already, WARN if this happens.
88 WARN(1, "Invalid context priority %d\n", ctx_prio);
89 return DRM_SCHED_PRIORITY_NORMAL;
94 static int amdgpu_ctx_priority_permit(struct drm_file *filp,
97 if (!amdgpu_ctx_priority_is_valid(priority))
100 /* NORMAL and below are accessible by everyone */
101 if (priority <= AMDGPU_CTX_PRIORITY_NORMAL)
104 if (capable(CAP_SYS_NICE))
107 if (drm_is_current_master(filp))
113 static enum amdgpu_gfx_pipe_priority amdgpu_ctx_prio_to_compute_prio(int32_t prio)
116 case AMDGPU_CTX_PRIORITY_HIGH:
117 case AMDGPU_CTX_PRIORITY_VERY_HIGH:
118 return AMDGPU_GFX_PIPE_PRIO_HIGH;
120 return AMDGPU_GFX_PIPE_PRIO_NORMAL;
124 static enum amdgpu_ring_priority_level amdgpu_ctx_sched_prio_to_ring_prio(int32_t prio)
127 case AMDGPU_CTX_PRIORITY_HIGH:
128 return AMDGPU_RING_PRIO_1;
129 case AMDGPU_CTX_PRIORITY_VERY_HIGH:
130 return AMDGPU_RING_PRIO_2;
132 return AMDGPU_RING_PRIO_0;
136 static unsigned int amdgpu_ctx_get_hw_prio(struct amdgpu_ctx *ctx, u32 hw_ip)
138 struct amdgpu_device *adev = ctx->mgr->adev;
139 unsigned int hw_prio;
142 ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
143 ctx->init_priority : ctx->override_priority;
146 case AMDGPU_HW_IP_COMPUTE:
147 hw_prio = amdgpu_ctx_prio_to_compute_prio(ctx_prio);
149 case AMDGPU_HW_IP_VCE:
150 case AMDGPU_HW_IP_VCN_ENC:
151 hw_prio = amdgpu_ctx_sched_prio_to_ring_prio(ctx_prio);
154 hw_prio = AMDGPU_RING_PRIO_DEFAULT;
158 hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
159 if (adev->gpu_sched[hw_ip][hw_prio].num_scheds == 0)
160 hw_prio = AMDGPU_RING_PRIO_DEFAULT;
165 /* Calculate the time spend on the hw */
166 static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence)
168 struct drm_sched_fence *s_fence;
171 return ns_to_ktime(0);
173 /* When the fence is not even scheduled it can't have spend time */
174 s_fence = to_drm_sched_fence(fence);
175 if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->scheduled.flags))
176 return ns_to_ktime(0);
178 /* When it is still running account how much already spend */
179 if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->finished.flags))
180 return ktime_sub(ktime_get(), s_fence->scheduled.timestamp);
182 return ktime_sub(s_fence->finished.timestamp,
183 s_fence->scheduled.timestamp);
186 static ktime_t amdgpu_ctx_entity_time(struct amdgpu_ctx *ctx,
187 struct amdgpu_ctx_entity *centity)
189 ktime_t res = ns_to_ktime(0);
192 spin_lock(&ctx->ring_lock);
193 for (i = 0; i < amdgpu_sched_jobs; i++) {
194 res = ktime_add(res, amdgpu_ctx_fence_time(centity->fences[i]));
196 spin_unlock(&ctx->ring_lock);
200 static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
203 struct drm_gpu_scheduler **scheds = NULL, *sched = NULL;
204 struct amdgpu_device *adev = ctx->mgr->adev;
205 struct amdgpu_ctx_entity *entity;
206 enum drm_sched_priority drm_prio;
207 unsigned int hw_prio, num_scheds;
211 entity = kzalloc(struct_size(entity, fences, amdgpu_sched_jobs),
216 ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
217 ctx->init_priority : ctx->override_priority;
218 entity->hw_ip = hw_ip;
219 entity->sequence = 1;
220 hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
221 drm_prio = amdgpu_ctx_to_drm_sched_prio(ctx_prio);
223 hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
224 scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
225 num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
227 /* disable load balance if the hw engine retains context among dependent jobs */
228 if (hw_ip == AMDGPU_HW_IP_VCN_ENC ||
229 hw_ip == AMDGPU_HW_IP_VCN_DEC ||
230 hw_ip == AMDGPU_HW_IP_UVD_ENC ||
231 hw_ip == AMDGPU_HW_IP_UVD) {
232 sched = drm_sched_pick_best(scheds, num_scheds);
237 r = drm_sched_entity_init(&entity->entity, drm_prio, scheds, num_scheds,
240 goto error_free_entity;
242 /* It's not an error if we fail to install the new entity */
243 if (cmpxchg(&ctx->entities[hw_ip][ring], NULL, entity))
249 drm_sched_entity_fini(&entity->entity);
257 static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_ctx_entity *entity)
259 ktime_t res = ns_to_ktime(0);
265 for (i = 0; i < amdgpu_sched_jobs; ++i) {
266 res = ktime_add(res, amdgpu_ctx_fence_time(entity->fences[i]));
267 dma_fence_put(entity->fences[i]);
274 static int amdgpu_ctx_init(struct amdgpu_ctx_mgr *mgr, int32_t priority,
275 struct drm_file *filp, struct amdgpu_ctx *ctx)
279 r = amdgpu_ctx_priority_permit(filp, priority);
283 memset(ctx, 0, sizeof(*ctx));
285 kref_init(&ctx->refcount);
287 spin_lock_init(&ctx->ring_lock);
288 mutex_init(&ctx->lock);
290 ctx->reset_counter = atomic_read(&mgr->adev->gpu_reset_counter);
291 ctx->reset_counter_query = ctx->reset_counter;
292 ctx->vram_lost_counter = atomic_read(&mgr->adev->vram_lost_counter);
293 ctx->init_priority = priority;
294 ctx->override_priority = AMDGPU_CTX_PRIORITY_UNSET;
295 ctx->stable_pstate = AMDGPU_CTX_STABLE_PSTATE_NONE;
300 static int amdgpu_ctx_get_stable_pstate(struct amdgpu_ctx *ctx,
303 struct amdgpu_device *adev = ctx->mgr->adev;
304 enum amd_dpm_forced_level current_level;
306 current_level = amdgpu_dpm_get_performance_level(adev);
308 switch (current_level) {
309 case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
310 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_STANDARD;
312 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
313 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_MIN_SCLK;
315 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
316 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_MIN_MCLK;
318 case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
319 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_PEAK;
322 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_NONE;
328 static int amdgpu_ctx_set_stable_pstate(struct amdgpu_ctx *ctx,
331 struct amdgpu_device *adev = ctx->mgr->adev;
332 enum amd_dpm_forced_level level;
333 u32 current_stable_pstate;
336 mutex_lock(&adev->pm.stable_pstate_ctx_lock);
337 if (adev->pm.stable_pstate_ctx && adev->pm.stable_pstate_ctx != ctx) {
342 r = amdgpu_ctx_get_stable_pstate(ctx, ¤t_stable_pstate);
343 if (r || (stable_pstate == current_stable_pstate))
346 switch (stable_pstate) {
347 case AMDGPU_CTX_STABLE_PSTATE_NONE:
348 level = AMD_DPM_FORCED_LEVEL_AUTO;
350 case AMDGPU_CTX_STABLE_PSTATE_STANDARD:
351 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
353 case AMDGPU_CTX_STABLE_PSTATE_MIN_SCLK:
354 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
356 case AMDGPU_CTX_STABLE_PSTATE_MIN_MCLK:
357 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
359 case AMDGPU_CTX_STABLE_PSTATE_PEAK:
360 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
367 r = amdgpu_dpm_force_performance_level(adev, level);
369 if (level == AMD_DPM_FORCED_LEVEL_AUTO)
370 adev->pm.stable_pstate_ctx = NULL;
372 adev->pm.stable_pstate_ctx = ctx;
374 mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
379 static void amdgpu_ctx_fini(struct kref *ref)
381 struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
382 struct amdgpu_ctx_mgr *mgr = ctx->mgr;
383 struct amdgpu_device *adev = mgr->adev;
389 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
390 for (j = 0; j < AMDGPU_MAX_ENTITY_NUM; ++j) {
393 spend = amdgpu_ctx_fini_entity(ctx->entities[i][j]);
394 atomic64_add(ktime_to_ns(spend), &mgr->time_spend[i]);
398 if (drm_dev_enter(&adev->ddev, &idx)) {
399 amdgpu_ctx_set_stable_pstate(ctx, AMDGPU_CTX_STABLE_PSTATE_NONE);
403 mutex_destroy(&ctx->lock);
407 int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
408 u32 ring, struct drm_sched_entity **entity)
412 if (hw_ip >= AMDGPU_HW_IP_NUM) {
413 DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
417 /* Right now all IPs have only one instance - multiple rings. */
419 DRM_DEBUG("invalid ip instance: %d\n", instance);
423 if (ring >= amdgpu_ctx_num_entities[hw_ip]) {
424 DRM_DEBUG("invalid ring: %d %d\n", hw_ip, ring);
428 if (ctx->entities[hw_ip][ring] == NULL) {
429 r = amdgpu_ctx_init_entity(ctx, hw_ip, ring);
434 *entity = &ctx->entities[hw_ip][ring]->entity;
438 static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
439 struct amdgpu_fpriv *fpriv,
440 struct drm_file *filp,
444 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
445 struct amdgpu_ctx *ctx;
448 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
452 mutex_lock(&mgr->lock);
453 r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
455 mutex_unlock(&mgr->lock);
461 r = amdgpu_ctx_init(mgr, priority, filp, ctx);
463 idr_remove(&mgr->ctx_handles, *id);
467 mutex_unlock(&mgr->lock);
471 static void amdgpu_ctx_do_release(struct kref *ref)
473 struct amdgpu_ctx *ctx;
476 ctx = container_of(ref, struct amdgpu_ctx, refcount);
477 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
478 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
479 if (!ctx->entities[i][j])
482 drm_sched_entity_destroy(&ctx->entities[i][j]->entity);
486 amdgpu_ctx_fini(ref);
489 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
491 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
492 struct amdgpu_ctx *ctx;
494 mutex_lock(&mgr->lock);
495 ctx = idr_remove(&mgr->ctx_handles, id);
497 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
498 mutex_unlock(&mgr->lock);
499 return ctx ? 0 : -EINVAL;
502 static int amdgpu_ctx_query(struct amdgpu_device *adev,
503 struct amdgpu_fpriv *fpriv, uint32_t id,
504 union drm_amdgpu_ctx_out *out)
506 struct amdgpu_ctx *ctx;
507 struct amdgpu_ctx_mgr *mgr;
508 unsigned reset_counter;
513 mgr = &fpriv->ctx_mgr;
514 mutex_lock(&mgr->lock);
515 ctx = idr_find(&mgr->ctx_handles, id);
517 mutex_unlock(&mgr->lock);
521 /* TODO: these two are always zero */
522 out->state.flags = 0x0;
523 out->state.hangs = 0x0;
525 /* determine if a GPU reset has occured since the last call */
526 reset_counter = atomic_read(&adev->gpu_reset_counter);
527 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
528 if (ctx->reset_counter_query == reset_counter)
529 out->state.reset_status = AMDGPU_CTX_NO_RESET;
531 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
532 ctx->reset_counter_query = reset_counter;
534 mutex_unlock(&mgr->lock);
538 #define AMDGPU_RAS_COUNTE_DELAY_MS 3000
540 static int amdgpu_ctx_query2(struct amdgpu_device *adev,
541 struct amdgpu_fpriv *fpriv, uint32_t id,
542 union drm_amdgpu_ctx_out *out)
544 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
545 struct amdgpu_ctx *ctx;
546 struct amdgpu_ctx_mgr *mgr;
551 mgr = &fpriv->ctx_mgr;
552 mutex_lock(&mgr->lock);
553 ctx = idr_find(&mgr->ctx_handles, id);
555 mutex_unlock(&mgr->lock);
559 out->state.flags = 0x0;
560 out->state.hangs = 0x0;
562 if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
563 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
565 if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
566 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
568 if (atomic_read(&ctx->guilty))
569 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
571 if (adev->ras_enabled && con) {
572 /* Return the cached values in O(1),
573 * and schedule delayed work to cache
576 int ce_count, ue_count;
578 ce_count = atomic_read(&con->ras_ce_count);
579 ue_count = atomic_read(&con->ras_ue_count);
581 if (ce_count != ctx->ras_counter_ce) {
582 ctx->ras_counter_ce = ce_count;
583 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_CE;
586 if (ue_count != ctx->ras_counter_ue) {
587 ctx->ras_counter_ue = ue_count;
588 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_UE;
591 schedule_delayed_work(&con->ras_counte_delay_work,
592 msecs_to_jiffies(AMDGPU_RAS_COUNTE_DELAY_MS));
595 mutex_unlock(&mgr->lock);
601 static int amdgpu_ctx_stable_pstate(struct amdgpu_device *adev,
602 struct amdgpu_fpriv *fpriv, uint32_t id,
603 bool set, u32 *stable_pstate)
605 struct amdgpu_ctx *ctx;
606 struct amdgpu_ctx_mgr *mgr;
612 mgr = &fpriv->ctx_mgr;
613 mutex_lock(&mgr->lock);
614 ctx = idr_find(&mgr->ctx_handles, id);
616 mutex_unlock(&mgr->lock);
621 r = amdgpu_ctx_set_stable_pstate(ctx, *stable_pstate);
623 r = amdgpu_ctx_get_stable_pstate(ctx, stable_pstate);
625 mutex_unlock(&mgr->lock);
629 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
630 struct drm_file *filp)
633 uint32_t id, stable_pstate;
636 union drm_amdgpu_ctx *args = data;
637 struct amdgpu_device *adev = drm_to_adev(dev);
638 struct amdgpu_fpriv *fpriv = filp->driver_priv;
640 id = args->in.ctx_id;
641 priority = args->in.priority;
643 /* For backwards compatibility reasons, we need to accept
644 * ioctls with garbage in the priority field */
645 if (!amdgpu_ctx_priority_is_valid(priority))
646 priority = AMDGPU_CTX_PRIORITY_NORMAL;
648 switch (args->in.op) {
649 case AMDGPU_CTX_OP_ALLOC_CTX:
650 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
651 args->out.alloc.ctx_id = id;
653 case AMDGPU_CTX_OP_FREE_CTX:
654 r = amdgpu_ctx_free(fpriv, id);
656 case AMDGPU_CTX_OP_QUERY_STATE:
657 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
659 case AMDGPU_CTX_OP_QUERY_STATE2:
660 r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
662 case AMDGPU_CTX_OP_GET_STABLE_PSTATE:
665 r = amdgpu_ctx_stable_pstate(adev, fpriv, id, false, &stable_pstate);
667 args->out.pstate.flags = stable_pstate;
669 case AMDGPU_CTX_OP_SET_STABLE_PSTATE:
670 if (args->in.flags & ~AMDGPU_CTX_STABLE_PSTATE_FLAGS_MASK)
672 stable_pstate = args->in.flags & AMDGPU_CTX_STABLE_PSTATE_FLAGS_MASK;
673 if (stable_pstate > AMDGPU_CTX_STABLE_PSTATE_PEAK)
675 r = amdgpu_ctx_stable_pstate(adev, fpriv, id, true, &stable_pstate);
684 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
686 struct amdgpu_ctx *ctx;
687 struct amdgpu_ctx_mgr *mgr;
692 mgr = &fpriv->ctx_mgr;
694 mutex_lock(&mgr->lock);
695 ctx = idr_find(&mgr->ctx_handles, id);
697 kref_get(&ctx->refcount);
698 mutex_unlock(&mgr->lock);
702 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
707 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
711 uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
712 struct drm_sched_entity *entity,
713 struct dma_fence *fence)
715 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
716 uint64_t seq = centity->sequence;
717 struct dma_fence *other = NULL;
720 idx = seq & (amdgpu_sched_jobs - 1);
721 other = centity->fences[idx];
722 WARN_ON(other && !dma_fence_is_signaled(other));
724 dma_fence_get(fence);
726 spin_lock(&ctx->ring_lock);
727 centity->fences[idx] = fence;
729 spin_unlock(&ctx->ring_lock);
731 atomic64_add(ktime_to_ns(amdgpu_ctx_fence_time(other)),
732 &ctx->mgr->time_spend[centity->hw_ip]);
734 dma_fence_put(other);
738 struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
739 struct drm_sched_entity *entity,
742 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
743 struct dma_fence *fence;
745 spin_lock(&ctx->ring_lock);
748 seq = centity->sequence - 1;
750 if (seq >= centity->sequence) {
751 spin_unlock(&ctx->ring_lock);
752 return ERR_PTR(-EINVAL);
756 if (seq + amdgpu_sched_jobs < centity->sequence) {
757 spin_unlock(&ctx->ring_lock);
761 fence = dma_fence_get(centity->fences[seq & (amdgpu_sched_jobs - 1)]);
762 spin_unlock(&ctx->ring_lock);
767 static void amdgpu_ctx_set_entity_priority(struct amdgpu_ctx *ctx,
768 struct amdgpu_ctx_entity *aentity,
772 struct amdgpu_device *adev = ctx->mgr->adev;
773 unsigned int hw_prio;
774 struct drm_gpu_scheduler **scheds = NULL;
777 /* set sw priority */
778 drm_sched_entity_set_priority(&aentity->entity,
779 amdgpu_ctx_to_drm_sched_prio(priority));
781 /* set hw priority */
782 if (hw_ip == AMDGPU_HW_IP_COMPUTE) {
783 hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
784 hw_prio = array_index_nospec(hw_prio, AMDGPU_RING_PRIO_MAX);
785 scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
786 num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
787 drm_sched_entity_modify_sched(&aentity->entity, scheds,
792 void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
798 ctx->override_priority = priority;
800 ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
801 ctx->init_priority : ctx->override_priority;
802 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
803 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
804 if (!ctx->entities[i][j])
807 amdgpu_ctx_set_entity_priority(ctx, ctx->entities[i][j],
813 int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
814 struct drm_sched_entity *entity)
816 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
817 struct dma_fence *other;
821 spin_lock(&ctx->ring_lock);
822 idx = centity->sequence & (amdgpu_sched_jobs - 1);
823 other = dma_fence_get(centity->fences[idx]);
824 spin_unlock(&ctx->ring_lock);
829 r = dma_fence_wait(other, true);
830 if (r < 0 && r != -ERESTARTSYS)
831 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
833 dma_fence_put(other);
837 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
838 struct amdgpu_device *adev)
843 mutex_init(&mgr->lock);
844 idr_init(&mgr->ctx_handles);
846 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
847 atomic64_set(&mgr->time_spend[i], 0);
850 long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
852 struct amdgpu_ctx *ctx;
856 idp = &mgr->ctx_handles;
858 mutex_lock(&mgr->lock);
859 idr_for_each_entry(idp, ctx, id) {
860 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
861 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
862 struct drm_sched_entity *entity;
864 if (!ctx->entities[i][j])
867 entity = &ctx->entities[i][j]->entity;
868 timeout = drm_sched_entity_flush(entity, timeout);
872 mutex_unlock(&mgr->lock);
876 void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
878 struct amdgpu_ctx *ctx;
882 idp = &mgr->ctx_handles;
884 idr_for_each_entry(idp, ctx, id) {
885 if (kref_read(&ctx->refcount) != 1) {
886 DRM_ERROR("ctx %p is still alive\n", ctx);
890 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
891 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
892 struct drm_sched_entity *entity;
894 if (!ctx->entities[i][j])
897 entity = &ctx->entities[i][j]->entity;
898 drm_sched_entity_fini(entity);
904 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
906 struct amdgpu_ctx *ctx;
910 amdgpu_ctx_mgr_entity_fini(mgr);
912 idp = &mgr->ctx_handles;
914 idr_for_each_entry(idp, ctx, id) {
915 if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
916 DRM_ERROR("ctx %p is still alive\n", ctx);
919 idr_destroy(&mgr->ctx_handles);
920 mutex_destroy(&mgr->lock);
923 void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
924 ktime_t usage[AMDGPU_HW_IP_NUM])
926 struct amdgpu_ctx *ctx;
927 unsigned int hw_ip, i;
931 * This is a little bit racy because it can be that a ctx or a fence are
932 * destroyed just in the moment we try to account them. But that is ok
933 * since exactly that case is explicitely allowed by the interface.
935 mutex_lock(&mgr->lock);
936 for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
937 uint64_t ns = atomic64_read(&mgr->time_spend[hw_ip]);
939 usage[hw_ip] = ns_to_ktime(ns);
942 idr_for_each_entry(&mgr->ctx_handles, ctx, id) {
943 for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
944 for (i = 0; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {
945 struct amdgpu_ctx_entity *centity;
948 centity = ctx->entities[hw_ip][i];
951 spend = amdgpu_ctx_entity_time(ctx, centity);
952 usage[hw_ip] = ktime_add(usage[hw_ip], spend);
956 mutex_unlock(&mgr->lock);