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_gfx_pipe_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_GFX:
147 case AMDGPU_HW_IP_COMPUTE:
148 hw_prio = amdgpu_ctx_prio_to_gfx_pipe_prio(ctx_prio);
150 case AMDGPU_HW_IP_VCE:
151 case AMDGPU_HW_IP_VCN_ENC:
152 hw_prio = amdgpu_ctx_sched_prio_to_ring_prio(ctx_prio);
155 hw_prio = AMDGPU_RING_PRIO_DEFAULT;
159 hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
160 if (adev->gpu_sched[hw_ip][hw_prio].num_scheds == 0)
161 hw_prio = AMDGPU_RING_PRIO_DEFAULT;
166 /* Calculate the time spend on the hw */
167 static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence)
169 struct drm_sched_fence *s_fence;
172 return ns_to_ktime(0);
174 /* When the fence is not even scheduled it can't have spend time */
175 s_fence = to_drm_sched_fence(fence);
176 if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->scheduled.flags))
177 return ns_to_ktime(0);
179 /* When it is still running account how much already spend */
180 if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->finished.flags))
181 return ktime_sub(ktime_get(), s_fence->scheduled.timestamp);
183 return ktime_sub(s_fence->finished.timestamp,
184 s_fence->scheduled.timestamp);
187 static ktime_t amdgpu_ctx_entity_time(struct amdgpu_ctx *ctx,
188 struct amdgpu_ctx_entity *centity)
190 ktime_t res = ns_to_ktime(0);
193 spin_lock(&ctx->ring_lock);
194 for (i = 0; i < amdgpu_sched_jobs; i++) {
195 res = ktime_add(res, amdgpu_ctx_fence_time(centity->fences[i]));
197 spin_unlock(&ctx->ring_lock);
201 static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
204 struct drm_gpu_scheduler **scheds = NULL, *sched = NULL;
205 struct amdgpu_device *adev = ctx->mgr->adev;
206 struct amdgpu_ctx_entity *entity;
207 enum drm_sched_priority drm_prio;
208 unsigned int hw_prio, num_scheds;
212 entity = kzalloc(struct_size(entity, fences, amdgpu_sched_jobs),
217 ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
218 ctx->init_priority : ctx->override_priority;
219 entity->hw_ip = hw_ip;
220 entity->sequence = 1;
221 hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
222 drm_prio = amdgpu_ctx_to_drm_sched_prio(ctx_prio);
224 hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
226 if (!(adev)->xcp_mgr) {
227 scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
228 num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
230 struct amdgpu_fpriv *fpriv;
232 fpriv = container_of(ctx->ctx_mgr, struct amdgpu_fpriv, ctx_mgr);
233 r = amdgpu_xcp_select_scheds(adev, hw_ip, hw_prio, fpriv,
234 &num_scheds, &scheds);
239 /* disable load balance if the hw engine retains context among dependent jobs */
240 if (hw_ip == AMDGPU_HW_IP_VCN_ENC ||
241 hw_ip == AMDGPU_HW_IP_VCN_DEC ||
242 hw_ip == AMDGPU_HW_IP_UVD_ENC ||
243 hw_ip == AMDGPU_HW_IP_UVD) {
244 sched = drm_sched_pick_best(scheds, num_scheds);
249 r = drm_sched_entity_init(&entity->entity, drm_prio, scheds, num_scheds,
252 goto error_free_entity;
254 /* It's not an error if we fail to install the new entity */
255 if (cmpxchg(&ctx->entities[hw_ip][ring], NULL, entity))
261 drm_sched_entity_fini(&entity->entity);
269 static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_device *adev,
270 struct amdgpu_ctx_entity *entity)
272 ktime_t res = ns_to_ktime(0);
278 for (i = 0; i < amdgpu_sched_jobs; ++i) {
279 res = ktime_add(res, amdgpu_ctx_fence_time(entity->fences[i]));
280 dma_fence_put(entity->fences[i]);
283 amdgpu_xcp_release_sched(adev, entity);
289 static int amdgpu_ctx_get_stable_pstate(struct amdgpu_ctx *ctx,
292 struct amdgpu_device *adev = ctx->mgr->adev;
293 enum amd_dpm_forced_level current_level;
295 current_level = amdgpu_dpm_get_performance_level(adev);
297 switch (current_level) {
298 case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
299 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_STANDARD;
301 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
302 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_MIN_SCLK;
304 case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
305 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_MIN_MCLK;
307 case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
308 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_PEAK;
311 *stable_pstate = AMDGPU_CTX_STABLE_PSTATE_NONE;
317 static int amdgpu_ctx_init(struct amdgpu_ctx_mgr *mgr, int32_t priority,
318 struct drm_file *filp, struct amdgpu_ctx *ctx)
320 struct amdgpu_fpriv *fpriv = filp->driver_priv;
321 u32 current_stable_pstate;
324 r = amdgpu_ctx_priority_permit(filp, priority);
328 memset(ctx, 0, sizeof(*ctx));
330 kref_init(&ctx->refcount);
332 spin_lock_init(&ctx->ring_lock);
334 ctx->reset_counter = atomic_read(&mgr->adev->gpu_reset_counter);
335 ctx->reset_counter_query = ctx->reset_counter;
336 ctx->generation = amdgpu_vm_generation(mgr->adev, &fpriv->vm);
337 ctx->init_priority = priority;
338 ctx->override_priority = AMDGPU_CTX_PRIORITY_UNSET;
340 r = amdgpu_ctx_get_stable_pstate(ctx, ¤t_stable_pstate);
344 if (mgr->adev->pm.stable_pstate_ctx)
345 ctx->stable_pstate = mgr->adev->pm.stable_pstate_ctx->stable_pstate;
347 ctx->stable_pstate = current_stable_pstate;
349 ctx->ctx_mgr = &(fpriv->ctx_mgr);
353 static int amdgpu_ctx_set_stable_pstate(struct amdgpu_ctx *ctx,
356 struct amdgpu_device *adev = ctx->mgr->adev;
357 enum amd_dpm_forced_level level;
358 u32 current_stable_pstate;
361 mutex_lock(&adev->pm.stable_pstate_ctx_lock);
362 if (adev->pm.stable_pstate_ctx && adev->pm.stable_pstate_ctx != ctx) {
367 r = amdgpu_ctx_get_stable_pstate(ctx, ¤t_stable_pstate);
368 if (r || (stable_pstate == current_stable_pstate))
371 switch (stable_pstate) {
372 case AMDGPU_CTX_STABLE_PSTATE_NONE:
373 level = AMD_DPM_FORCED_LEVEL_AUTO;
375 case AMDGPU_CTX_STABLE_PSTATE_STANDARD:
376 level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
378 case AMDGPU_CTX_STABLE_PSTATE_MIN_SCLK:
379 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
381 case AMDGPU_CTX_STABLE_PSTATE_MIN_MCLK:
382 level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
384 case AMDGPU_CTX_STABLE_PSTATE_PEAK:
385 level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
392 r = amdgpu_dpm_force_performance_level(adev, level);
394 if (level == AMD_DPM_FORCED_LEVEL_AUTO)
395 adev->pm.stable_pstate_ctx = NULL;
397 adev->pm.stable_pstate_ctx = ctx;
399 mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
404 static void amdgpu_ctx_fini(struct kref *ref)
406 struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
407 struct amdgpu_ctx_mgr *mgr = ctx->mgr;
408 struct amdgpu_device *adev = mgr->adev;
414 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
415 for (j = 0; j < AMDGPU_MAX_ENTITY_NUM; ++j) {
418 spend = amdgpu_ctx_fini_entity(adev, ctx->entities[i][j]);
419 atomic64_add(ktime_to_ns(spend), &mgr->time_spend[i]);
423 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
424 amdgpu_ctx_set_stable_pstate(ctx, ctx->stable_pstate);
431 int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
432 u32 ring, struct drm_sched_entity **entity)
435 struct drm_sched_entity *ctx_entity;
437 if (hw_ip >= AMDGPU_HW_IP_NUM) {
438 DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
442 /* Right now all IPs have only one instance - multiple rings. */
444 DRM_DEBUG("invalid ip instance: %d\n", instance);
448 if (ring >= amdgpu_ctx_num_entities[hw_ip]) {
449 DRM_DEBUG("invalid ring: %d %d\n", hw_ip, ring);
453 if (ctx->entities[hw_ip][ring] == NULL) {
454 r = amdgpu_ctx_init_entity(ctx, hw_ip, ring);
459 ctx_entity = &ctx->entities[hw_ip][ring]->entity;
460 r = drm_sched_entity_error(ctx_entity);
462 DRM_DEBUG("error entity %p\n", ctx_entity);
466 *entity = ctx_entity;
470 static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
471 struct amdgpu_fpriv *fpriv,
472 struct drm_file *filp,
476 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
477 struct amdgpu_ctx *ctx;
480 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
484 mutex_lock(&mgr->lock);
485 r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
487 mutex_unlock(&mgr->lock);
493 r = amdgpu_ctx_init(mgr, priority, filp, ctx);
495 idr_remove(&mgr->ctx_handles, *id);
499 mutex_unlock(&mgr->lock);
503 static void amdgpu_ctx_do_release(struct kref *ref)
505 struct amdgpu_ctx *ctx;
508 ctx = container_of(ref, struct amdgpu_ctx, refcount);
509 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
510 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
511 if (!ctx->entities[i][j])
514 drm_sched_entity_destroy(&ctx->entities[i][j]->entity);
518 amdgpu_ctx_fini(ref);
521 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
523 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
524 struct amdgpu_ctx *ctx;
526 mutex_lock(&mgr->lock);
527 ctx = idr_remove(&mgr->ctx_handles, id);
529 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
530 mutex_unlock(&mgr->lock);
531 return ctx ? 0 : -EINVAL;
534 static int amdgpu_ctx_query(struct amdgpu_device *adev,
535 struct amdgpu_fpriv *fpriv, uint32_t id,
536 union drm_amdgpu_ctx_out *out)
538 struct amdgpu_ctx *ctx;
539 struct amdgpu_ctx_mgr *mgr;
540 unsigned reset_counter;
545 mgr = &fpriv->ctx_mgr;
546 mutex_lock(&mgr->lock);
547 ctx = idr_find(&mgr->ctx_handles, id);
549 mutex_unlock(&mgr->lock);
553 /* TODO: these two are always zero */
554 out->state.flags = 0x0;
555 out->state.hangs = 0x0;
557 /* determine if a GPU reset has occured since the last call */
558 reset_counter = atomic_read(&adev->gpu_reset_counter);
559 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
560 if (ctx->reset_counter_query == reset_counter)
561 out->state.reset_status = AMDGPU_CTX_NO_RESET;
563 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
564 ctx->reset_counter_query = reset_counter;
566 mutex_unlock(&mgr->lock);
570 #define AMDGPU_RAS_COUNTE_DELAY_MS 3000
572 static int amdgpu_ctx_query2(struct amdgpu_device *adev,
573 struct amdgpu_fpriv *fpriv, uint32_t id,
574 union drm_amdgpu_ctx_out *out)
576 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
577 struct amdgpu_ctx *ctx;
578 struct amdgpu_ctx_mgr *mgr;
583 mgr = &fpriv->ctx_mgr;
584 mutex_lock(&mgr->lock);
585 ctx = idr_find(&mgr->ctx_handles, id);
587 mutex_unlock(&mgr->lock);
591 out->state.flags = 0x0;
592 out->state.hangs = 0x0;
594 if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
595 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
597 if (ctx->generation != amdgpu_vm_generation(adev, &fpriv->vm))
598 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
600 if (atomic_read(&ctx->guilty))
601 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
603 if (amdgpu_in_reset(adev))
604 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET_IN_PROGRESS;
606 if (adev->ras_enabled && con) {
607 /* Return the cached values in O(1),
608 * and schedule delayed work to cache
611 int ce_count, ue_count;
613 ce_count = atomic_read(&con->ras_ce_count);
614 ue_count = atomic_read(&con->ras_ue_count);
616 if (ce_count != ctx->ras_counter_ce) {
617 ctx->ras_counter_ce = ce_count;
618 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_CE;
621 if (ue_count != ctx->ras_counter_ue) {
622 ctx->ras_counter_ue = ue_count;
623 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_UE;
626 schedule_delayed_work(&con->ras_counte_delay_work,
627 msecs_to_jiffies(AMDGPU_RAS_COUNTE_DELAY_MS));
630 mutex_unlock(&mgr->lock);
636 static int amdgpu_ctx_stable_pstate(struct amdgpu_device *adev,
637 struct amdgpu_fpriv *fpriv, uint32_t id,
638 bool set, u32 *stable_pstate)
640 struct amdgpu_ctx *ctx;
641 struct amdgpu_ctx_mgr *mgr;
647 mgr = &fpriv->ctx_mgr;
648 mutex_lock(&mgr->lock);
649 ctx = idr_find(&mgr->ctx_handles, id);
651 mutex_unlock(&mgr->lock);
656 r = amdgpu_ctx_set_stable_pstate(ctx, *stable_pstate);
658 r = amdgpu_ctx_get_stable_pstate(ctx, stable_pstate);
660 mutex_unlock(&mgr->lock);
664 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
665 struct drm_file *filp)
668 uint32_t id, stable_pstate;
671 union drm_amdgpu_ctx *args = data;
672 struct amdgpu_device *adev = drm_to_adev(dev);
673 struct amdgpu_fpriv *fpriv = filp->driver_priv;
675 id = args->in.ctx_id;
676 priority = args->in.priority;
678 /* For backwards compatibility reasons, we need to accept
679 * ioctls with garbage in the priority field */
680 if (!amdgpu_ctx_priority_is_valid(priority))
681 priority = AMDGPU_CTX_PRIORITY_NORMAL;
683 switch (args->in.op) {
684 case AMDGPU_CTX_OP_ALLOC_CTX:
685 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
686 args->out.alloc.ctx_id = id;
688 case AMDGPU_CTX_OP_FREE_CTX:
689 r = amdgpu_ctx_free(fpriv, id);
691 case AMDGPU_CTX_OP_QUERY_STATE:
692 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
694 case AMDGPU_CTX_OP_QUERY_STATE2:
695 r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
697 case AMDGPU_CTX_OP_GET_STABLE_PSTATE:
700 r = amdgpu_ctx_stable_pstate(adev, fpriv, id, false, &stable_pstate);
702 args->out.pstate.flags = stable_pstate;
704 case AMDGPU_CTX_OP_SET_STABLE_PSTATE:
705 if (args->in.flags & ~AMDGPU_CTX_STABLE_PSTATE_FLAGS_MASK)
707 stable_pstate = args->in.flags & AMDGPU_CTX_STABLE_PSTATE_FLAGS_MASK;
708 if (stable_pstate > AMDGPU_CTX_STABLE_PSTATE_PEAK)
710 r = amdgpu_ctx_stable_pstate(adev, fpriv, id, true, &stable_pstate);
719 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
721 struct amdgpu_ctx *ctx;
722 struct amdgpu_ctx_mgr *mgr;
727 mgr = &fpriv->ctx_mgr;
729 mutex_lock(&mgr->lock);
730 ctx = idr_find(&mgr->ctx_handles, id);
732 kref_get(&ctx->refcount);
733 mutex_unlock(&mgr->lock);
737 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
742 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
746 uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
747 struct drm_sched_entity *entity,
748 struct dma_fence *fence)
750 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
751 uint64_t seq = centity->sequence;
752 struct dma_fence *other = NULL;
755 idx = seq & (amdgpu_sched_jobs - 1);
756 other = centity->fences[idx];
757 WARN_ON(other && !dma_fence_is_signaled(other));
759 dma_fence_get(fence);
761 spin_lock(&ctx->ring_lock);
762 centity->fences[idx] = fence;
764 spin_unlock(&ctx->ring_lock);
766 atomic64_add(ktime_to_ns(amdgpu_ctx_fence_time(other)),
767 &ctx->mgr->time_spend[centity->hw_ip]);
769 dma_fence_put(other);
773 struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
774 struct drm_sched_entity *entity,
777 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
778 struct dma_fence *fence;
780 spin_lock(&ctx->ring_lock);
783 seq = centity->sequence - 1;
785 if (seq >= centity->sequence) {
786 spin_unlock(&ctx->ring_lock);
787 return ERR_PTR(-EINVAL);
791 if (seq + amdgpu_sched_jobs < centity->sequence) {
792 spin_unlock(&ctx->ring_lock);
796 fence = dma_fence_get(centity->fences[seq & (amdgpu_sched_jobs - 1)]);
797 spin_unlock(&ctx->ring_lock);
802 static void amdgpu_ctx_set_entity_priority(struct amdgpu_ctx *ctx,
803 struct amdgpu_ctx_entity *aentity,
807 struct amdgpu_device *adev = ctx->mgr->adev;
808 unsigned int hw_prio;
809 struct drm_gpu_scheduler **scheds = NULL;
812 /* set sw priority */
813 drm_sched_entity_set_priority(&aentity->entity,
814 amdgpu_ctx_to_drm_sched_prio(priority));
816 /* set hw priority */
817 if (hw_ip == AMDGPU_HW_IP_COMPUTE || hw_ip == AMDGPU_HW_IP_GFX) {
818 hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
819 hw_prio = array_index_nospec(hw_prio, AMDGPU_RING_PRIO_MAX);
820 scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
821 num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
822 drm_sched_entity_modify_sched(&aentity->entity, scheds,
827 void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
833 ctx->override_priority = priority;
835 ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
836 ctx->init_priority : ctx->override_priority;
837 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
838 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
839 if (!ctx->entities[i][j])
842 amdgpu_ctx_set_entity_priority(ctx, ctx->entities[i][j],
848 int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
849 struct drm_sched_entity *entity)
851 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
852 struct dma_fence *other;
856 spin_lock(&ctx->ring_lock);
857 idx = centity->sequence & (amdgpu_sched_jobs - 1);
858 other = dma_fence_get(centity->fences[idx]);
859 spin_unlock(&ctx->ring_lock);
864 r = dma_fence_wait(other, true);
865 if (r < 0 && r != -ERESTARTSYS)
866 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
868 dma_fence_put(other);
872 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
873 struct amdgpu_device *adev)
878 mutex_init(&mgr->lock);
879 idr_init_base(&mgr->ctx_handles, 1);
881 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
882 atomic64_set(&mgr->time_spend[i], 0);
885 long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
887 struct amdgpu_ctx *ctx;
891 idp = &mgr->ctx_handles;
893 mutex_lock(&mgr->lock);
894 idr_for_each_entry(idp, ctx, id) {
895 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
896 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
897 struct drm_sched_entity *entity;
899 if (!ctx->entities[i][j])
902 entity = &ctx->entities[i][j]->entity;
903 timeout = drm_sched_entity_flush(entity, timeout);
907 mutex_unlock(&mgr->lock);
911 void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
913 struct amdgpu_ctx *ctx;
917 idp = &mgr->ctx_handles;
919 idr_for_each_entry(idp, ctx, id) {
920 if (kref_read(&ctx->refcount) != 1) {
921 DRM_ERROR("ctx %p is still alive\n", ctx);
925 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
926 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
927 struct drm_sched_entity *entity;
929 if (!ctx->entities[i][j])
932 entity = &ctx->entities[i][j]->entity;
933 drm_sched_entity_fini(entity);
939 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
941 struct amdgpu_ctx *ctx;
945 amdgpu_ctx_mgr_entity_fini(mgr);
947 idp = &mgr->ctx_handles;
949 idr_for_each_entry(idp, ctx, id) {
950 if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
951 DRM_ERROR("ctx %p is still alive\n", ctx);
954 idr_destroy(&mgr->ctx_handles);
955 mutex_destroy(&mgr->lock);
958 void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
959 ktime_t usage[AMDGPU_HW_IP_NUM])
961 struct amdgpu_ctx *ctx;
962 unsigned int hw_ip, i;
966 * This is a little bit racy because it can be that a ctx or a fence are
967 * destroyed just in the moment we try to account them. But that is ok
968 * since exactly that case is explicitely allowed by the interface.
970 mutex_lock(&mgr->lock);
971 for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
972 uint64_t ns = atomic64_read(&mgr->time_spend[hw_ip]);
974 usage[hw_ip] = ns_to_ktime(ns);
977 idr_for_each_entry(&mgr->ctx_handles, ctx, id) {
978 for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
979 for (i = 0; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {
980 struct amdgpu_ctx_entity *centity;
983 centity = ctx->entities[hw_ip][i];
986 spend = amdgpu_ctx_entity_time(ctx, centity);
987 usage[hw_ip] = ktime_add(usage[hw_ip], spend);
991 mutex_unlock(&mgr->lock);