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>
27 #include "amdgpu_sched.h"
28 #include "amdgpu_ras.h"
29 #include <linux/nospec.h>
31 #define to_amdgpu_ctx_entity(e) \
32 container_of((e), struct amdgpu_ctx_entity, entity)
34 const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM] = {
35 [AMDGPU_HW_IP_GFX] = 1,
36 [AMDGPU_HW_IP_COMPUTE] = 4,
37 [AMDGPU_HW_IP_DMA] = 2,
38 [AMDGPU_HW_IP_UVD] = 1,
39 [AMDGPU_HW_IP_VCE] = 1,
40 [AMDGPU_HW_IP_UVD_ENC] = 1,
41 [AMDGPU_HW_IP_VCN_DEC] = 1,
42 [AMDGPU_HW_IP_VCN_ENC] = 1,
43 [AMDGPU_HW_IP_VCN_JPEG] = 1,
46 bool amdgpu_ctx_priority_is_valid(int32_t ctx_prio)
49 case AMDGPU_CTX_PRIORITY_UNSET:
50 case AMDGPU_CTX_PRIORITY_VERY_LOW:
51 case AMDGPU_CTX_PRIORITY_LOW:
52 case AMDGPU_CTX_PRIORITY_NORMAL:
53 case AMDGPU_CTX_PRIORITY_HIGH:
54 case AMDGPU_CTX_PRIORITY_VERY_HIGH:
61 static enum drm_sched_priority
62 amdgpu_ctx_to_drm_sched_prio(int32_t ctx_prio)
65 case AMDGPU_CTX_PRIORITY_UNSET:
66 return DRM_SCHED_PRIORITY_UNSET;
68 case AMDGPU_CTX_PRIORITY_VERY_LOW:
69 return DRM_SCHED_PRIORITY_MIN;
71 case AMDGPU_CTX_PRIORITY_LOW:
72 return DRM_SCHED_PRIORITY_MIN;
74 case AMDGPU_CTX_PRIORITY_NORMAL:
75 return DRM_SCHED_PRIORITY_NORMAL;
77 case AMDGPU_CTX_PRIORITY_HIGH:
78 return DRM_SCHED_PRIORITY_HIGH;
80 case AMDGPU_CTX_PRIORITY_VERY_HIGH:
81 return DRM_SCHED_PRIORITY_HIGH;
83 /* This should not happen as we sanitized userspace provided priority
84 * already, WARN if this happens.
87 WARN(1, "Invalid context priority %d\n", ctx_prio);
88 return DRM_SCHED_PRIORITY_NORMAL;
93 static int amdgpu_ctx_priority_permit(struct drm_file *filp,
96 if (!amdgpu_ctx_priority_is_valid(priority))
99 /* NORMAL and below are accessible by everyone */
100 if (priority <= AMDGPU_CTX_PRIORITY_NORMAL)
103 if (capable(CAP_SYS_NICE))
106 if (drm_is_current_master(filp))
112 static enum amdgpu_gfx_pipe_priority amdgpu_ctx_prio_to_compute_prio(int32_t prio)
115 case AMDGPU_CTX_PRIORITY_HIGH:
116 case AMDGPU_CTX_PRIORITY_VERY_HIGH:
117 return AMDGPU_GFX_PIPE_PRIO_HIGH;
119 return AMDGPU_GFX_PIPE_PRIO_NORMAL;
123 static enum amdgpu_ring_priority_level amdgpu_ctx_sched_prio_to_ring_prio(int32_t prio)
126 case AMDGPU_CTX_PRIORITY_HIGH:
127 return AMDGPU_RING_PRIO_1;
128 case AMDGPU_CTX_PRIORITY_VERY_HIGH:
129 return AMDGPU_RING_PRIO_2;
131 return AMDGPU_RING_PRIO_0;
135 static unsigned int amdgpu_ctx_get_hw_prio(struct amdgpu_ctx *ctx, u32 hw_ip)
137 struct amdgpu_device *adev = ctx->adev;
139 unsigned int hw_prio;
141 ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
142 ctx->init_priority : ctx->override_priority;
145 case AMDGPU_HW_IP_COMPUTE:
146 hw_prio = amdgpu_ctx_prio_to_compute_prio(ctx_prio);
148 case AMDGPU_HW_IP_VCE:
149 case AMDGPU_HW_IP_VCN_ENC:
150 hw_prio = amdgpu_ctx_sched_prio_to_ring_prio(ctx_prio);
153 hw_prio = AMDGPU_RING_PRIO_DEFAULT;
157 hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
158 if (adev->gpu_sched[hw_ip][hw_prio].num_scheds == 0)
159 hw_prio = AMDGPU_RING_PRIO_DEFAULT;
165 static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,
168 struct amdgpu_device *adev = ctx->adev;
169 struct amdgpu_ctx_entity *entity;
170 struct drm_gpu_scheduler **scheds = NULL, *sched = NULL;
171 unsigned num_scheds = 0;
173 unsigned int hw_prio;
174 enum drm_sched_priority drm_prio;
177 entity = kzalloc(struct_size(entity, fences, amdgpu_sched_jobs),
182 ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
183 ctx->init_priority : ctx->override_priority;
184 entity->sequence = 1;
185 hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
186 drm_prio = amdgpu_ctx_to_drm_sched_prio(ctx_prio);
188 hw_ip = array_index_nospec(hw_ip, AMDGPU_HW_IP_NUM);
189 scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
190 num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
192 /* disable load balance if the hw engine retains context among dependent jobs */
193 if (hw_ip == AMDGPU_HW_IP_VCN_ENC ||
194 hw_ip == AMDGPU_HW_IP_VCN_DEC ||
195 hw_ip == AMDGPU_HW_IP_UVD_ENC ||
196 hw_ip == AMDGPU_HW_IP_UVD) {
197 sched = drm_sched_pick_best(scheds, num_scheds);
202 r = drm_sched_entity_init(&entity->entity, drm_prio, scheds, num_scheds,
205 goto error_free_entity;
207 ctx->entities[hw_ip][ring] = entity;
216 static int amdgpu_ctx_init(struct amdgpu_device *adev,
218 struct drm_file *filp,
219 struct amdgpu_ctx *ctx)
223 r = amdgpu_ctx_priority_permit(filp, priority);
227 memset(ctx, 0, sizeof(*ctx));
231 kref_init(&ctx->refcount);
232 spin_lock_init(&ctx->ring_lock);
233 mutex_init(&ctx->lock);
235 ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
236 ctx->reset_counter_query = ctx->reset_counter;
237 ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
238 ctx->init_priority = priority;
239 ctx->override_priority = AMDGPU_CTX_PRIORITY_UNSET;
244 static void amdgpu_ctx_fini_entity(struct amdgpu_ctx_entity *entity)
252 for (i = 0; i < amdgpu_sched_jobs; ++i)
253 dma_fence_put(entity->fences[i]);
258 static void amdgpu_ctx_fini(struct kref *ref)
260 struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
261 struct amdgpu_device *adev = ctx->adev;
267 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
268 for (j = 0; j < AMDGPU_MAX_ENTITY_NUM; ++j) {
269 amdgpu_ctx_fini_entity(ctx->entities[i][j]);
270 ctx->entities[i][j] = NULL;
274 mutex_destroy(&ctx->lock);
278 int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
279 u32 ring, struct drm_sched_entity **entity)
283 if (hw_ip >= AMDGPU_HW_IP_NUM) {
284 DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
288 /* Right now all IPs have only one instance - multiple rings. */
290 DRM_DEBUG("invalid ip instance: %d\n", instance);
294 if (ring >= amdgpu_ctx_num_entities[hw_ip]) {
295 DRM_DEBUG("invalid ring: %d %d\n", hw_ip, ring);
299 if (ctx->entities[hw_ip][ring] == NULL) {
300 r = amdgpu_ctx_init_entity(ctx, hw_ip, ring);
305 *entity = &ctx->entities[hw_ip][ring]->entity;
309 static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
310 struct amdgpu_fpriv *fpriv,
311 struct drm_file *filp,
315 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
316 struct amdgpu_ctx *ctx;
319 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
323 mutex_lock(&mgr->lock);
324 r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
326 mutex_unlock(&mgr->lock);
332 r = amdgpu_ctx_init(adev, priority, filp, ctx);
334 idr_remove(&mgr->ctx_handles, *id);
338 mutex_unlock(&mgr->lock);
342 static void amdgpu_ctx_do_release(struct kref *ref)
344 struct amdgpu_ctx *ctx;
347 ctx = container_of(ref, struct amdgpu_ctx, refcount);
348 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
349 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
350 if (!ctx->entities[i][j])
353 drm_sched_entity_destroy(&ctx->entities[i][j]->entity);
357 amdgpu_ctx_fini(ref);
360 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
362 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
363 struct amdgpu_ctx *ctx;
365 mutex_lock(&mgr->lock);
366 ctx = idr_remove(&mgr->ctx_handles, id);
368 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
369 mutex_unlock(&mgr->lock);
370 return ctx ? 0 : -EINVAL;
373 static int amdgpu_ctx_query(struct amdgpu_device *adev,
374 struct amdgpu_fpriv *fpriv, uint32_t id,
375 union drm_amdgpu_ctx_out *out)
377 struct amdgpu_ctx *ctx;
378 struct amdgpu_ctx_mgr *mgr;
379 unsigned reset_counter;
384 mgr = &fpriv->ctx_mgr;
385 mutex_lock(&mgr->lock);
386 ctx = idr_find(&mgr->ctx_handles, id);
388 mutex_unlock(&mgr->lock);
392 /* TODO: these two are always zero */
393 out->state.flags = 0x0;
394 out->state.hangs = 0x0;
396 /* determine if a GPU reset has occured since the last call */
397 reset_counter = atomic_read(&adev->gpu_reset_counter);
398 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
399 if (ctx->reset_counter_query == reset_counter)
400 out->state.reset_status = AMDGPU_CTX_NO_RESET;
402 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
403 ctx->reset_counter_query = reset_counter;
405 mutex_unlock(&mgr->lock);
409 #define AMDGPU_RAS_COUNTE_DELAY_MS 3000
411 static int amdgpu_ctx_query2(struct amdgpu_device *adev,
412 struct amdgpu_fpriv *fpriv, uint32_t id,
413 union drm_amdgpu_ctx_out *out)
415 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
416 struct amdgpu_ctx *ctx;
417 struct amdgpu_ctx_mgr *mgr;
422 mgr = &fpriv->ctx_mgr;
423 mutex_lock(&mgr->lock);
424 ctx = idr_find(&mgr->ctx_handles, id);
426 mutex_unlock(&mgr->lock);
430 out->state.flags = 0x0;
431 out->state.hangs = 0x0;
433 if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
434 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
436 if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
437 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
439 if (atomic_read(&ctx->guilty))
440 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
442 if (adev->ras_enabled && con) {
443 /* Return the cached values in O(1),
444 * and schedule delayed work to cache
447 int ce_count, ue_count;
449 ce_count = atomic_read(&con->ras_ce_count);
450 ue_count = atomic_read(&con->ras_ue_count);
452 if (ce_count != ctx->ras_counter_ce) {
453 ctx->ras_counter_ce = ce_count;
454 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_CE;
457 if (ue_count != ctx->ras_counter_ue) {
458 ctx->ras_counter_ue = ue_count;
459 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_UE;
462 schedule_delayed_work(&con->ras_counte_delay_work,
463 msecs_to_jiffies(AMDGPU_RAS_COUNTE_DELAY_MS));
466 mutex_unlock(&mgr->lock);
470 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
471 struct drm_file *filp)
477 union drm_amdgpu_ctx *args = data;
478 struct amdgpu_device *adev = drm_to_adev(dev);
479 struct amdgpu_fpriv *fpriv = filp->driver_priv;
481 id = args->in.ctx_id;
482 priority = args->in.priority;
484 /* For backwards compatibility reasons, we need to accept
485 * ioctls with garbage in the priority field */
486 if (!amdgpu_ctx_priority_is_valid(priority))
487 priority = AMDGPU_CTX_PRIORITY_NORMAL;
489 switch (args->in.op) {
490 case AMDGPU_CTX_OP_ALLOC_CTX:
491 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
492 args->out.alloc.ctx_id = id;
494 case AMDGPU_CTX_OP_FREE_CTX:
495 r = amdgpu_ctx_free(fpriv, id);
497 case AMDGPU_CTX_OP_QUERY_STATE:
498 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
500 case AMDGPU_CTX_OP_QUERY_STATE2:
501 r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
510 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
512 struct amdgpu_ctx *ctx;
513 struct amdgpu_ctx_mgr *mgr;
518 mgr = &fpriv->ctx_mgr;
520 mutex_lock(&mgr->lock);
521 ctx = idr_find(&mgr->ctx_handles, id);
523 kref_get(&ctx->refcount);
524 mutex_unlock(&mgr->lock);
528 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
533 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
537 void amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
538 struct drm_sched_entity *entity,
539 struct dma_fence *fence, uint64_t *handle)
541 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
542 uint64_t seq = centity->sequence;
543 struct dma_fence *other = NULL;
546 idx = seq & (amdgpu_sched_jobs - 1);
547 other = centity->fences[idx];
549 BUG_ON(!dma_fence_is_signaled(other));
551 dma_fence_get(fence);
553 spin_lock(&ctx->ring_lock);
554 centity->fences[idx] = fence;
556 spin_unlock(&ctx->ring_lock);
558 dma_fence_put(other);
563 struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
564 struct drm_sched_entity *entity,
567 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
568 struct dma_fence *fence;
570 spin_lock(&ctx->ring_lock);
573 seq = centity->sequence - 1;
575 if (seq >= centity->sequence) {
576 spin_unlock(&ctx->ring_lock);
577 return ERR_PTR(-EINVAL);
581 if (seq + amdgpu_sched_jobs < centity->sequence) {
582 spin_unlock(&ctx->ring_lock);
586 fence = dma_fence_get(centity->fences[seq & (amdgpu_sched_jobs - 1)]);
587 spin_unlock(&ctx->ring_lock);
592 static void amdgpu_ctx_set_entity_priority(struct amdgpu_ctx *ctx,
593 struct amdgpu_ctx_entity *aentity,
597 struct amdgpu_device *adev = ctx->adev;
598 unsigned int hw_prio;
599 struct drm_gpu_scheduler **scheds = NULL;
602 /* set sw priority */
603 drm_sched_entity_set_priority(&aentity->entity,
604 amdgpu_ctx_to_drm_sched_prio(priority));
606 /* set hw priority */
607 if (hw_ip == AMDGPU_HW_IP_COMPUTE) {
608 hw_prio = amdgpu_ctx_get_hw_prio(ctx, hw_ip);
609 hw_prio = array_index_nospec(hw_prio, AMDGPU_RING_PRIO_MAX);
610 scheds = adev->gpu_sched[hw_ip][hw_prio].sched;
611 num_scheds = adev->gpu_sched[hw_ip][hw_prio].num_scheds;
612 drm_sched_entity_modify_sched(&aentity->entity, scheds,
617 void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
623 ctx->override_priority = priority;
625 ctx_prio = (ctx->override_priority == AMDGPU_CTX_PRIORITY_UNSET) ?
626 ctx->init_priority : ctx->override_priority;
627 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
628 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
629 if (!ctx->entities[i][j])
632 amdgpu_ctx_set_entity_priority(ctx, ctx->entities[i][j],
638 int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
639 struct drm_sched_entity *entity)
641 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
642 struct dma_fence *other;
646 spin_lock(&ctx->ring_lock);
647 idx = centity->sequence & (amdgpu_sched_jobs - 1);
648 other = dma_fence_get(centity->fences[idx]);
649 spin_unlock(&ctx->ring_lock);
654 r = dma_fence_wait(other, true);
655 if (r < 0 && r != -ERESTARTSYS)
656 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
658 dma_fence_put(other);
662 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
664 mutex_init(&mgr->lock);
665 idr_init(&mgr->ctx_handles);
668 long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
670 struct amdgpu_ctx *ctx;
674 idp = &mgr->ctx_handles;
676 mutex_lock(&mgr->lock);
677 idr_for_each_entry(idp, ctx, id) {
678 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
679 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
680 struct drm_sched_entity *entity;
682 if (!ctx->entities[i][j])
685 entity = &ctx->entities[i][j]->entity;
686 timeout = drm_sched_entity_flush(entity, timeout);
690 mutex_unlock(&mgr->lock);
694 void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
696 struct amdgpu_ctx *ctx;
700 idp = &mgr->ctx_handles;
702 idr_for_each_entry(idp, ctx, id) {
703 if (kref_read(&ctx->refcount) != 1) {
704 DRM_ERROR("ctx %p is still alive\n", ctx);
708 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
709 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
710 struct drm_sched_entity *entity;
712 if (!ctx->entities[i][j])
715 entity = &ctx->entities[i][j]->entity;
716 drm_sched_entity_fini(entity);
722 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
724 struct amdgpu_ctx *ctx;
728 amdgpu_ctx_mgr_entity_fini(mgr);
730 idp = &mgr->ctx_handles;
732 idr_for_each_entry(idp, ctx, id) {
733 if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
734 DRM_ERROR("ctx %p is still alive\n", ctx);
737 idr_destroy(&mgr->ctx_handles);
738 mutex_destroy(&mgr->lock);
741 static void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx,
742 struct amdgpu_ctx_entity *centity, ktime_t *total, ktime_t *max)
750 for (i = 0; i < amdgpu_sched_jobs; i++) {
751 struct dma_fence *fence;
752 struct drm_sched_fence *s_fence;
754 spin_lock(&ctx->ring_lock);
755 fence = dma_fence_get(centity->fences[i]);
756 spin_unlock(&ctx->ring_lock);
759 s_fence = to_drm_sched_fence(fence);
760 if (!dma_fence_is_signaled(&s_fence->scheduled)) {
761 dma_fence_put(fence);
764 t1 = s_fence->scheduled.timestamp;
765 if (!ktime_before(t1, now)) {
766 dma_fence_put(fence);
769 if (dma_fence_is_signaled(&s_fence->finished) &&
770 s_fence->finished.timestamp < now)
771 *total += ktime_sub(s_fence->finished.timestamp, t1);
773 *total += ktime_sub(now, t1);
774 t1 = ktime_sub(now, t1);
775 dma_fence_put(fence);
776 *max = max(t1, *max);
780 ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
781 uint32_t idx, uint64_t *elapsed)
784 struct amdgpu_ctx *ctx;
786 struct amdgpu_ctx_entity *centity;
787 ktime_t total = 0, max = 0;
789 if (idx >= AMDGPU_MAX_ENTITY_NUM)
791 idp = &mgr->ctx_handles;
792 mutex_lock(&mgr->lock);
793 idr_for_each_entry(idp, ctx, id) {
794 ktime_t ttotal, tmax;
796 if (!ctx->entities[hwip][idx])
799 centity = ctx->entities[hwip][idx];
800 amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
802 /* Harmonic mean approximation diverges for very small
803 * values. If ratio < 0.01% ignore
805 if (AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(tmax, ttotal))
808 total = ktime_add(total, ttotal);
809 max = ktime_after(tmax, max) ? tmax : max;
812 mutex_unlock(&mgr->lock);