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,
45 [AMDGPU_HW_IP_VPE] = 1,
48 bool amdgpu_ctx_priority_is_valid(int32_t ctx_prio)
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:
58 case AMDGPU_CTX_PRIORITY_UNSET:
59 /* UNSET priority is not valid and we don't carry that
60 * around, but set it to NORMAL in the only place this
61 * function is called, amdgpu_ctx_ioctl().
67 static enum drm_sched_priority
68 amdgpu_ctx_to_drm_sched_prio(int32_t ctx_prio)
71 case AMDGPU_CTX_PRIORITY_UNSET:
72 pr_warn_once("AMD-->DRM context priority value UNSET-->NORMAL");
73 return DRM_SCHED_PRIORITY_NORMAL;
75 case AMDGPU_CTX_PRIORITY_VERY_LOW:
76 return DRM_SCHED_PRIORITY_LOW;
78 case AMDGPU_CTX_PRIORITY_LOW:
79 return DRM_SCHED_PRIORITY_LOW;
81 case AMDGPU_CTX_PRIORITY_NORMAL:
82 return DRM_SCHED_PRIORITY_NORMAL;
84 case AMDGPU_CTX_PRIORITY_HIGH:
85 return DRM_SCHED_PRIORITY_HIGH;
87 case AMDGPU_CTX_PRIORITY_VERY_HIGH:
88 return DRM_SCHED_PRIORITY_HIGH;
90 /* This should not happen as we sanitized userspace provided priority
91 * already, WARN if this happens.
94 WARN(1, "Invalid context priority %d\n", ctx_prio);
95 return DRM_SCHED_PRIORITY_NORMAL;
100 static int amdgpu_ctx_priority_permit(struct drm_file *filp,
103 /* NORMAL and below are accessible by everyone */
104 if (priority <= AMDGPU_CTX_PRIORITY_NORMAL)
107 if (capable(CAP_SYS_NICE))
110 if (drm_is_current_master(filp))
116 static enum amdgpu_gfx_pipe_priority amdgpu_ctx_prio_to_gfx_pipe_prio(int32_t prio)
119 case AMDGPU_CTX_PRIORITY_HIGH:
120 case AMDGPU_CTX_PRIORITY_VERY_HIGH:
121 return AMDGPU_GFX_PIPE_PRIO_HIGH;
123 return AMDGPU_GFX_PIPE_PRIO_NORMAL;
127 static enum amdgpu_ring_priority_level amdgpu_ctx_sched_prio_to_ring_prio(int32_t prio)
130 case AMDGPU_CTX_PRIORITY_HIGH:
131 return AMDGPU_RING_PRIO_1;
132 case AMDGPU_CTX_PRIORITY_VERY_HIGH:
133 return AMDGPU_RING_PRIO_2;
135 return AMDGPU_RING_PRIO_0;
139 static unsigned int amdgpu_ctx_get_hw_prio(struct amdgpu_ctx *ctx, u32 hw_ip)
141 struct amdgpu_device *adev = ctx->mgr->adev;
142 unsigned int hw_prio;
145 ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
146 ctx->init_priority : ctx->override_priority;
149 case AMDGPU_HW_IP_GFX:
150 case AMDGPU_HW_IP_COMPUTE:
151 hw_prio = amdgpu_ctx_prio_to_gfx_pipe_prio(ctx_prio);
153 case AMDGPU_HW_IP_VCE:
154 case AMDGPU_HW_IP_VCN_ENC:
155 hw_prio = amdgpu_ctx_sched_prio_to_ring_prio(ctx_prio);
158 hw_prio = AMDGPU_RING_PRIO_DEFAULT;
162 hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
163 if (adev->gpu_sched[hw_ip][hw_prio].num_scheds == 0)
164 hw_prio = AMDGPU_RING_PRIO_DEFAULT;
169 /* Calculate the time spend on the hw */
170 static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence)
172 struct drm_sched_fence *s_fence;
175 return ns_to_ktime(0);
177 /* When the fence is not even scheduled it can't have spend time */
178 s_fence = to_drm_sched_fence(fence);
179 if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->scheduled.flags))
180 return ns_to_ktime(0);
182 /* When it is still running account how much already spend */
183 if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->finished.flags))
184 return ktime_sub(ktime_get(), s_fence->scheduled.timestamp);
186 return ktime_sub(s_fence->finished.timestamp,
187 s_fence->scheduled.timestamp);
190 static ktime_t amdgpu_ctx_entity_time(struct amdgpu_ctx *ctx,
191 struct amdgpu_ctx_entity *centity)
193 ktime_t res = ns_to_ktime(0);
196 spin_lock(&ctx->ring_lock);
197 for (i = 0; i < amdgpu_sched_jobs; i++) {
198 res = ktime_add(res, amdgpu_ctx_fence_time(centity->fences[i]));
200 spin_unlock(&ctx->ring_lock);
204 static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
207 struct drm_gpu_scheduler **scheds = NULL, *sched = NULL;
208 struct amdgpu_device *adev = ctx->mgr->adev;
209 struct amdgpu_ctx_entity *entity;
210 enum drm_sched_priority drm_prio;
211 unsigned int hw_prio, num_scheds;
215 entity = kzalloc(struct_size(entity, fences, amdgpu_sched_jobs),
220 ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
221 ctx->init_priority : ctx->override_priority;
222 entity->hw_ip = hw_ip;
223 entity->sequence = 1;
224 hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
225 drm_prio = amdgpu_ctx_to_drm_sched_prio(ctx_prio);
227 hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
229 if (!(adev)->xcp_mgr) {
230 scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
231 num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
233 struct amdgpu_fpriv *fpriv;
235 fpriv = container_of(ctx->ctx_mgr, struct amdgpu_fpriv, ctx_mgr);
236 r = amdgpu_xcp_select_scheds(adev, hw_ip, hw_prio, fpriv,
237 &num_scheds, &scheds);
242 /* disable load balance if the hw engine retains context among dependent jobs */
243 if (hw_ip == AMDGPU_HW_IP_VCN_ENC ||
244 hw_ip == AMDGPU_HW_IP_VCN_DEC ||
245 hw_ip == AMDGPU_HW_IP_UVD_ENC ||
246 hw_ip == AMDGPU_HW_IP_UVD) {
247 sched = drm_sched_pick_best(scheds, num_scheds);
252 r = drm_sched_entity_init(&entity->entity, drm_prio, scheds, num_scheds,
255 goto error_free_entity;
257 /* It's not an error if we fail to install the new entity */
258 if (cmpxchg(&ctx->entities[hw_ip][ring], NULL, entity))
264 drm_sched_entity_fini(&entity->entity);
272 static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_device *adev,
273 struct amdgpu_ctx_entity *entity)
275 ktime_t res = ns_to_ktime(0);
281 for (i = 0; i < amdgpu_sched_jobs; ++i) {
282 res = ktime_add(res, amdgpu_ctx_fence_time(entity->fences[i]));
283 dma_fence_put(entity->fences[i]);
286 amdgpu_xcp_release_sched(adev, entity);
292 static int amdgpu_ctx_get_stable_pstate(struct amdgpu_ctx *ctx,
295 struct amdgpu_device *adev = ctx->mgr->adev;
296 enum amd_dpm_forced_level current_level;
298 current_level = amdgpu_dpm_get_performance_level(adev);
300 switch (current_level) {
301 case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
302 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_STANDARD;
304 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
305 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_MIN_SCLK;
307 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
308 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_MIN_MCLK;
310 case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
311 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_PEAK;
314 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_NONE;
320 static int amdgpu_ctx_init(struct amdgpu_ctx_mgr *mgr, int32_t priority,
321 struct drm_file *filp, struct amdgpu_ctx *ctx)
323 struct amdgpu_fpriv *fpriv = filp->driver_priv;
324 u32 current_stable_pstate;
327 r = amdgpu_ctx_priority_permit(filp, priority);
331 memset(ctx, 0, sizeof(*ctx));
333 kref_init(&ctx->refcount);
335 spin_lock_init(&ctx->ring_lock);
337 ctx->reset_counter = atomic_read(&mgr->adev->gpu_reset_counter);
338 ctx->reset_counter_query = ctx->reset_counter;
339 ctx->generation = amdgpu_vm_generation(mgr->adev, &fpriv->vm);
340 ctx->init_priority = priority;
341 ctx->override_priority = AMDGPU_CTX_PRIORITY_UNSET;
343 r = amdgpu_ctx_get_stable_pstate(ctx, ¤t_stable_pstate);
347 if (mgr->adev->pm.stable_pstate_ctx)
348 ctx->stable_pstate = mgr->adev->pm.stable_pstate_ctx->stable_pstate;
350 ctx->stable_pstate = current_stable_pstate;
352 ctx->ctx_mgr = &(fpriv->ctx_mgr);
356 static int amdgpu_ctx_set_stable_pstate(struct amdgpu_ctx *ctx,
359 struct amdgpu_device *adev = ctx->mgr->adev;
360 enum amd_dpm_forced_level level;
361 u32 current_stable_pstate;
364 mutex_lock(&adev->pm.stable_pstate_ctx_lock);
365 if (adev->pm.stable_pstate_ctx && adev->pm.stable_pstate_ctx != ctx) {
370 r = amdgpu_ctx_get_stable_pstate(ctx, ¤t_stable_pstate);
371 if (r || (stable_pstate == current_stable_pstate))
374 switch (stable_pstate) {
375 case AMDGPU_CTX_STABLE_PSTATE_NONE:
376 level = AMD_DPM_FORCED_LEVEL_AUTO;
378 case AMDGPU_CTX_STABLE_PSTATE_STANDARD:
379 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
381 case AMDGPU_CTX_STABLE_PSTATE_MIN_SCLK:
382 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
384 case AMDGPU_CTX_STABLE_PSTATE_MIN_MCLK:
385 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
387 case AMDGPU_CTX_STABLE_PSTATE_PEAK:
388 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
395 r = amdgpu_dpm_force_performance_level(adev, level);
397 if (level == AMD_DPM_FORCED_LEVEL_AUTO)
398 adev->pm.stable_pstate_ctx = NULL;
400 adev->pm.stable_pstate_ctx = ctx;
402 mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
407 static void amdgpu_ctx_fini(struct kref *ref)
409 struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
410 struct amdgpu_ctx_mgr *mgr = ctx->mgr;
411 struct amdgpu_device *adev = mgr->adev;
417 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
418 for (j = 0; j < AMDGPU_MAX_ENTITY_NUM; ++j) {
421 spend = amdgpu_ctx_fini_entity(adev, ctx->entities[i][j]);
422 atomic64_add(ktime_to_ns(spend), &mgr->time_spend[i]);
426 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
427 amdgpu_ctx_set_stable_pstate(ctx, ctx->stable_pstate);
434 int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
435 u32 ring, struct drm_sched_entity **entity)
438 struct drm_sched_entity *ctx_entity;
440 if (hw_ip >= AMDGPU_HW_IP_NUM) {
441 DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
445 /* Right now all IPs have only one instance - multiple rings. */
447 DRM_DEBUG("invalid ip instance: %d\n", instance);
451 if (ring >= amdgpu_ctx_num_entities[hw_ip]) {
452 DRM_DEBUG("invalid ring: %d %d\n", hw_ip, ring);
456 if (ctx->entities[hw_ip][ring] == NULL) {
457 r = amdgpu_ctx_init_entity(ctx, hw_ip, ring);
462 ctx_entity = &ctx->entities[hw_ip][ring]->entity;
463 r = drm_sched_entity_error(ctx_entity);
465 DRM_DEBUG("error entity %p\n", ctx_entity);
469 *entity = ctx_entity;
473 static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
474 struct amdgpu_fpriv *fpriv,
475 struct drm_file *filp,
479 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
480 struct amdgpu_ctx *ctx;
483 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
487 mutex_lock(&mgr->lock);
488 r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
490 mutex_unlock(&mgr->lock);
496 r = amdgpu_ctx_init(mgr, priority, filp, ctx);
498 idr_remove(&mgr->ctx_handles, *id);
502 mutex_unlock(&mgr->lock);
506 static void amdgpu_ctx_do_release(struct kref *ref)
508 struct amdgpu_ctx *ctx;
511 ctx = container_of(ref, struct amdgpu_ctx, refcount);
512 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
513 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
514 if (!ctx->entities[i][j])
517 drm_sched_entity_destroy(&ctx->entities[i][j]->entity);
521 amdgpu_ctx_fini(ref);
524 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
526 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
527 struct amdgpu_ctx *ctx;
529 mutex_lock(&mgr->lock);
530 ctx = idr_remove(&mgr->ctx_handles, id);
532 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
533 mutex_unlock(&mgr->lock);
534 return ctx ? 0 : -EINVAL;
537 static int amdgpu_ctx_query(struct amdgpu_device *adev,
538 struct amdgpu_fpriv *fpriv, uint32_t id,
539 union drm_amdgpu_ctx_out *out)
541 struct amdgpu_ctx *ctx;
542 struct amdgpu_ctx_mgr *mgr;
543 unsigned reset_counter;
548 mgr = &fpriv->ctx_mgr;
549 mutex_lock(&mgr->lock);
550 ctx = idr_find(&mgr->ctx_handles, id);
552 mutex_unlock(&mgr->lock);
556 /* TODO: these two are always zero */
557 out->state.flags = 0x0;
558 out->state.hangs = 0x0;
560 /* determine if a GPU reset has occured since the last call */
561 reset_counter = atomic_read(&adev->gpu_reset_counter);
562 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
563 if (ctx->reset_counter_query == reset_counter)
564 out->state.reset_status = AMDGPU_CTX_NO_RESET;
566 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
567 ctx->reset_counter_query = reset_counter;
569 mutex_unlock(&mgr->lock);
573 #define AMDGPU_RAS_COUNTE_DELAY_MS 3000
575 static int amdgpu_ctx_query2(struct amdgpu_device *adev,
576 struct amdgpu_fpriv *fpriv, uint32_t id,
577 union drm_amdgpu_ctx_out *out)
579 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
580 struct amdgpu_ctx *ctx;
581 struct amdgpu_ctx_mgr *mgr;
586 mgr = &fpriv->ctx_mgr;
587 mutex_lock(&mgr->lock);
588 ctx = idr_find(&mgr->ctx_handles, id);
590 mutex_unlock(&mgr->lock);
594 out->state.flags = 0x0;
595 out->state.hangs = 0x0;
597 if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
598 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
600 if (ctx->generation != amdgpu_vm_generation(adev, &fpriv->vm))
601 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
603 if (atomic_read(&ctx->guilty))
604 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
606 if (amdgpu_in_reset(adev))
607 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET_IN_PROGRESS;
609 if (adev->ras_enabled && con) {
610 /* Return the cached values in O(1),
611 * and schedule delayed work to cache
614 int ce_count, ue_count;
616 ce_count = atomic_read(&con->ras_ce_count);
617 ue_count = atomic_read(&con->ras_ue_count);
619 if (ce_count != ctx->ras_counter_ce) {
620 ctx->ras_counter_ce = ce_count;
621 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_CE;
624 if (ue_count != ctx->ras_counter_ue) {
625 ctx->ras_counter_ue = ue_count;
626 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_UE;
629 schedule_delayed_work(&con->ras_counte_delay_work,
630 msecs_to_jiffies(AMDGPU_RAS_COUNTE_DELAY_MS));
633 mutex_unlock(&mgr->lock);
637 static int amdgpu_ctx_stable_pstate(struct amdgpu_device *adev,
638 struct amdgpu_fpriv *fpriv, uint32_t id,
639 bool set, u32 *stable_pstate)
641 struct amdgpu_ctx *ctx;
642 struct amdgpu_ctx_mgr *mgr;
648 mgr = &fpriv->ctx_mgr;
649 mutex_lock(&mgr->lock);
650 ctx = idr_find(&mgr->ctx_handles, id);
652 mutex_unlock(&mgr->lock);
657 r = amdgpu_ctx_set_stable_pstate(ctx, *stable_pstate);
659 r = amdgpu_ctx_get_stable_pstate(ctx, stable_pstate);
661 mutex_unlock(&mgr->lock);
665 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
666 struct drm_file *filp)
669 uint32_t id, stable_pstate;
672 union drm_amdgpu_ctx *args = data;
673 struct amdgpu_device *adev = drm_to_adev(dev);
674 struct amdgpu_fpriv *fpriv = filp->driver_priv;
676 id = args->in.ctx_id;
677 priority = args->in.priority;
679 /* For backwards compatibility, we need to accept ioctls with garbage
680 * in the priority field. Garbage values in the priority field, result
681 * in the priority being set to NORMAL.
683 if (!amdgpu_ctx_priority_is_valid(priority))
684 priority = AMDGPU_CTX_PRIORITY_NORMAL;
686 switch (args->in.op) {
687 case AMDGPU_CTX_OP_ALLOC_CTX:
690 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
691 args->out.alloc.ctx_id = id;
693 case AMDGPU_CTX_OP_FREE_CTX:
696 r = amdgpu_ctx_free(fpriv, id);
698 case AMDGPU_CTX_OP_QUERY_STATE:
701 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
703 case AMDGPU_CTX_OP_QUERY_STATE2:
706 r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
708 case AMDGPU_CTX_OP_GET_STABLE_PSTATE:
711 r = amdgpu_ctx_stable_pstate(adev, fpriv, id, false, &stable_pstate);
713 args->out.pstate.flags = stable_pstate;
715 case AMDGPU_CTX_OP_SET_STABLE_PSTATE:
716 if (args->in.flags & ~AMDGPU_CTX_STABLE_PSTATE_FLAGS_MASK)
718 stable_pstate = args->in.flags & AMDGPU_CTX_STABLE_PSTATE_FLAGS_MASK;
719 if (stable_pstate > AMDGPU_CTX_STABLE_PSTATE_PEAK)
721 r = amdgpu_ctx_stable_pstate(adev, fpriv, id, true, &stable_pstate);
730 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
732 struct amdgpu_ctx *ctx;
733 struct amdgpu_ctx_mgr *mgr;
738 mgr = &fpriv->ctx_mgr;
740 mutex_lock(&mgr->lock);
741 ctx = idr_find(&mgr->ctx_handles, id);
743 kref_get(&ctx->refcount);
744 mutex_unlock(&mgr->lock);
748 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
753 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
757 uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
758 struct drm_sched_entity *entity,
759 struct dma_fence *fence)
761 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
762 uint64_t seq = centity->sequence;
763 struct dma_fence *other = NULL;
766 idx = seq & (amdgpu_sched_jobs - 1);
767 other = centity->fences[idx];
768 WARN_ON(other && !dma_fence_is_signaled(other));
770 dma_fence_get(fence);
772 spin_lock(&ctx->ring_lock);
773 centity->fences[idx] = fence;
775 spin_unlock(&ctx->ring_lock);
777 atomic64_add(ktime_to_ns(amdgpu_ctx_fence_time(other)),
778 &ctx->mgr->time_spend[centity->hw_ip]);
780 dma_fence_put(other);
784 struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
785 struct drm_sched_entity *entity,
788 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
789 struct dma_fence *fence;
791 spin_lock(&ctx->ring_lock);
794 seq = centity->sequence - 1;
796 if (seq >= centity->sequence) {
797 spin_unlock(&ctx->ring_lock);
798 return ERR_PTR(-EINVAL);
802 if (seq + amdgpu_sched_jobs < centity->sequence) {
803 spin_unlock(&ctx->ring_lock);
807 fence = dma_fence_get(centity->fences[seq & (amdgpu_sched_jobs - 1)]);
808 spin_unlock(&ctx->ring_lock);
813 static void amdgpu_ctx_set_entity_priority(struct amdgpu_ctx *ctx,
814 struct amdgpu_ctx_entity *aentity,
818 struct amdgpu_device *adev = ctx->mgr->adev;
819 unsigned int hw_prio;
820 struct drm_gpu_scheduler **scheds = NULL;
823 /* set sw priority */
824 drm_sched_entity_set_priority(&aentity->entity,
825 amdgpu_ctx_to_drm_sched_prio(priority));
827 /* set hw priority */
828 if (hw_ip == AMDGPU_HW_IP_COMPUTE || hw_ip == AMDGPU_HW_IP_GFX) {
829 hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
830 hw_prio = array_index_nospec(hw_prio, AMDGPU_RING_PRIO_MAX);
831 scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
832 num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
833 drm_sched_entity_modify_sched(&aentity->entity, scheds,
838 void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
844 ctx->override_priority = priority;
846 ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
847 ctx->init_priority : ctx->override_priority;
848 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
849 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
850 if (!ctx->entities[i][j])
853 amdgpu_ctx_set_entity_priority(ctx, ctx->entities[i][j],
859 int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
860 struct drm_sched_entity *entity)
862 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
863 struct dma_fence *other;
867 spin_lock(&ctx->ring_lock);
868 idx = centity->sequence & (amdgpu_sched_jobs - 1);
869 other = dma_fence_get(centity->fences[idx]);
870 spin_unlock(&ctx->ring_lock);
875 r = dma_fence_wait(other, true);
876 if (r < 0 && r != -ERESTARTSYS)
877 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
879 dma_fence_put(other);
883 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
884 struct amdgpu_device *adev)
889 mutex_init(&mgr->lock);
890 idr_init_base(&mgr->ctx_handles, 1);
892 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
893 atomic64_set(&mgr->time_spend[i], 0);
896 long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
898 struct amdgpu_ctx *ctx;
902 idp = &mgr->ctx_handles;
904 mutex_lock(&mgr->lock);
905 idr_for_each_entry(idp, ctx, id) {
906 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
907 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
908 struct drm_sched_entity *entity;
910 if (!ctx->entities[i][j])
913 entity = &ctx->entities[i][j]->entity;
914 timeout = drm_sched_entity_flush(entity, timeout);
918 mutex_unlock(&mgr->lock);
922 void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
924 struct amdgpu_ctx *ctx;
928 idp = &mgr->ctx_handles;
930 idr_for_each_entry(idp, ctx, id) {
931 if (kref_read(&ctx->refcount) != 1) {
932 DRM_ERROR("ctx %p is still alive\n", ctx);
936 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
937 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
938 struct drm_sched_entity *entity;
940 if (!ctx->entities[i][j])
943 entity = &ctx->entities[i][j]->entity;
944 drm_sched_entity_fini(entity);
950 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
952 struct amdgpu_ctx *ctx;
956 amdgpu_ctx_mgr_entity_fini(mgr);
958 idp = &mgr->ctx_handles;
960 idr_for_each_entry(idp, ctx, id) {
961 if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
962 DRM_ERROR("ctx %p is still alive\n", ctx);
965 idr_destroy(&mgr->ctx_handles);
966 mutex_destroy(&mgr->lock);
969 void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
970 ktime_t usage[AMDGPU_HW_IP_NUM])
972 struct amdgpu_ctx *ctx;
973 unsigned int hw_ip, i;
977 * This is a little bit racy because it can be that a ctx or a fence are
978 * destroyed just in the moment we try to account them. But that is ok
979 * since exactly that case is explicitely allowed by the interface.
981 mutex_lock(&mgr->lock);
982 for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
983 uint64_t ns = atomic64_read(&mgr->time_spend[hw_ip]);
985 usage[hw_ip] = ns_to_ktime(ns);
988 idr_for_each_entry(&mgr->ctx_handles, ctx, id) {
989 for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
990 for (i = 0; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {
991 struct amdgpu_ctx_entity *centity;
994 centity = ctx->entities[hw_ip][i];
997 spend = amdgpu_ctx_entity_time(ctx, centity);
998 usage[hw_ip] = ktime_add(usage[hw_ip], spend);
1002 mutex_unlock(&mgr->lock);