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"
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 amdgpu_ctx_priority_permit(struct drm_file *filp,
46 enum drm_sched_priority priority)
48 if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
51 /* NORMAL and below are accessible by everyone */
52 if (priority <= DRM_SCHED_PRIORITY_NORMAL)
55 if (capable(CAP_SYS_NICE))
58 if (drm_is_current_master(filp))
64 static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, const u32 hw_ip, const u32 ring)
66 struct amdgpu_device *adev = ctx->adev;
67 struct amdgpu_ctx_entity *entity;
68 struct drm_gpu_scheduler **scheds = NULL, *sched = NULL;
69 unsigned num_scheds = 0;
70 enum drm_sched_priority priority;
73 entity = kcalloc(1, offsetof(typeof(*entity), fences[amdgpu_sched_jobs]),
79 priority = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
80 ctx->init_priority : ctx->override_priority;
82 case AMDGPU_HW_IP_GFX:
83 sched = &adev->gfx.gfx_ring[0].sched;
87 case AMDGPU_HW_IP_COMPUTE:
88 scheds = adev->gfx.compute_sched;
89 num_scheds = adev->gfx.num_compute_sched;
91 case AMDGPU_HW_IP_DMA:
92 scheds = adev->sdma.sdma_sched;
93 num_scheds = adev->sdma.num_sdma_sched;
95 case AMDGPU_HW_IP_UVD:
96 sched = &adev->uvd.inst[0].ring.sched;
100 case AMDGPU_HW_IP_VCE:
101 sched = &adev->vce.ring[0].sched;
105 case AMDGPU_HW_IP_UVD_ENC:
106 sched = &adev->uvd.inst[0].ring_enc[0].sched;
110 case AMDGPU_HW_IP_VCN_DEC:
111 scheds = adev->vcn.vcn_dec_sched;
112 num_scheds = adev->vcn.num_vcn_dec_sched;
114 case AMDGPU_HW_IP_VCN_ENC:
115 scheds = adev->vcn.vcn_enc_sched;
116 num_scheds = adev->vcn.num_vcn_enc_sched;
118 case AMDGPU_HW_IP_VCN_JPEG:
119 scheds = adev->jpeg.jpeg_sched;
120 num_scheds = adev->jpeg.num_jpeg_sched;
124 r = drm_sched_entity_init(&entity->entity, priority, scheds, num_scheds,
127 goto error_free_entity;
129 ctx->entities[hw_ip][ring] = entity;
138 static int amdgpu_ctx_init(struct amdgpu_device *adev,
139 enum drm_sched_priority priority,
140 struct drm_file *filp,
141 struct amdgpu_ctx *ctx)
145 r = amdgpu_ctx_priority_permit(filp, priority);
149 memset(ctx, 0, sizeof(*ctx));
153 kref_init(&ctx->refcount);
154 spin_lock_init(&ctx->ring_lock);
155 mutex_init(&ctx->lock);
157 ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
158 ctx->reset_counter_query = ctx->reset_counter;
159 ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
160 ctx->init_priority = priority;
161 ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
167 static void amdgpu_ctx_fini_entity(struct amdgpu_ctx_entity *entity)
175 for (i = 0; i < amdgpu_sched_jobs; ++i)
176 dma_fence_put(entity->fences[i]);
181 static void amdgpu_ctx_fini(struct kref *ref)
183 struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
184 struct amdgpu_device *adev = ctx->adev;
190 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
191 for (j = 0; j < AMDGPU_MAX_ENTITY_NUM; ++j) {
192 amdgpu_ctx_fini_entity(ctx->entities[i][j]);
193 ctx->entities[i][j] = NULL;
197 mutex_destroy(&ctx->lock);
201 int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
202 u32 ring, struct drm_sched_entity **entity)
206 if (hw_ip >= AMDGPU_HW_IP_NUM) {
207 DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
211 /* Right now all IPs have only one instance - multiple rings. */
213 DRM_DEBUG("invalid ip instance: %d\n", instance);
217 if (ring >= amdgpu_ctx_num_entities[hw_ip]) {
218 DRM_DEBUG("invalid ring: %d %d\n", hw_ip, ring);
222 if (ctx->entities[hw_ip][ring] == NULL) {
223 r = amdgpu_ctx_init_entity(ctx, hw_ip, ring);
228 *entity = &ctx->entities[hw_ip][ring]->entity;
232 static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
233 struct amdgpu_fpriv *fpriv,
234 struct drm_file *filp,
235 enum drm_sched_priority priority,
238 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
239 struct amdgpu_ctx *ctx;
242 ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
246 mutex_lock(&mgr->lock);
247 r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
249 mutex_unlock(&mgr->lock);
255 r = amdgpu_ctx_init(adev, priority, filp, ctx);
257 idr_remove(&mgr->ctx_handles, *id);
261 mutex_unlock(&mgr->lock);
265 static void amdgpu_ctx_do_release(struct kref *ref)
267 struct amdgpu_ctx *ctx;
270 ctx = container_of(ref, struct amdgpu_ctx, refcount);
271 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
272 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
273 if (!ctx->entities[i][j])
276 drm_sched_entity_destroy(&ctx->entities[i][j]->entity);
280 amdgpu_ctx_fini(ref);
283 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
285 struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
286 struct amdgpu_ctx *ctx;
288 mutex_lock(&mgr->lock);
289 ctx = idr_remove(&mgr->ctx_handles, id);
291 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
292 mutex_unlock(&mgr->lock);
293 return ctx ? 0 : -EINVAL;
296 static int amdgpu_ctx_query(struct amdgpu_device *adev,
297 struct amdgpu_fpriv *fpriv, uint32_t id,
298 union drm_amdgpu_ctx_out *out)
300 struct amdgpu_ctx *ctx;
301 struct amdgpu_ctx_mgr *mgr;
302 unsigned reset_counter;
307 mgr = &fpriv->ctx_mgr;
308 mutex_lock(&mgr->lock);
309 ctx = idr_find(&mgr->ctx_handles, id);
311 mutex_unlock(&mgr->lock);
315 /* TODO: these two are always zero */
316 out->state.flags = 0x0;
317 out->state.hangs = 0x0;
319 /* determine if a GPU reset has occured since the last call */
320 reset_counter = atomic_read(&adev->gpu_reset_counter);
321 /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
322 if (ctx->reset_counter_query == reset_counter)
323 out->state.reset_status = AMDGPU_CTX_NO_RESET;
325 out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
326 ctx->reset_counter_query = reset_counter;
328 mutex_unlock(&mgr->lock);
332 static int amdgpu_ctx_query2(struct amdgpu_device *adev,
333 struct amdgpu_fpriv *fpriv, uint32_t id,
334 union drm_amdgpu_ctx_out *out)
336 struct amdgpu_ctx *ctx;
337 struct amdgpu_ctx_mgr *mgr;
338 unsigned long ras_counter;
343 mgr = &fpriv->ctx_mgr;
344 mutex_lock(&mgr->lock);
345 ctx = idr_find(&mgr->ctx_handles, id);
347 mutex_unlock(&mgr->lock);
351 out->state.flags = 0x0;
352 out->state.hangs = 0x0;
354 if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
355 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
357 if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
358 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
360 if (atomic_read(&ctx->guilty))
361 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
364 ras_counter = amdgpu_ras_query_error_count(adev, false);
365 /*ras counter is monotonic increasing*/
366 if (ras_counter != ctx->ras_counter_ue) {
367 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_UE;
368 ctx->ras_counter_ue = ras_counter;
372 ras_counter = amdgpu_ras_query_error_count(adev, true);
373 if (ras_counter != ctx->ras_counter_ce) {
374 out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RAS_CE;
375 ctx->ras_counter_ce = ras_counter;
378 mutex_unlock(&mgr->lock);
382 int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
383 struct drm_file *filp)
387 enum drm_sched_priority priority;
389 union drm_amdgpu_ctx *args = data;
390 struct amdgpu_device *adev = dev->dev_private;
391 struct amdgpu_fpriv *fpriv = filp->driver_priv;
394 id = args->in.ctx_id;
395 priority = amdgpu_to_sched_priority(args->in.priority);
397 /* For backwards compatibility reasons, we need to accept
398 * ioctls with garbage in the priority field */
399 if (priority == DRM_SCHED_PRIORITY_INVALID)
400 priority = DRM_SCHED_PRIORITY_NORMAL;
402 switch (args->in.op) {
403 case AMDGPU_CTX_OP_ALLOC_CTX:
404 r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
405 args->out.alloc.ctx_id = id;
407 case AMDGPU_CTX_OP_FREE_CTX:
408 r = amdgpu_ctx_free(fpriv, id);
410 case AMDGPU_CTX_OP_QUERY_STATE:
411 r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
413 case AMDGPU_CTX_OP_QUERY_STATE2:
414 r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
423 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
425 struct amdgpu_ctx *ctx;
426 struct amdgpu_ctx_mgr *mgr;
431 mgr = &fpriv->ctx_mgr;
433 mutex_lock(&mgr->lock);
434 ctx = idr_find(&mgr->ctx_handles, id);
436 kref_get(&ctx->refcount);
437 mutex_unlock(&mgr->lock);
441 int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
446 kref_put(&ctx->refcount, amdgpu_ctx_do_release);
450 void amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
451 struct drm_sched_entity *entity,
452 struct dma_fence *fence, uint64_t* handle)
454 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
455 uint64_t seq = centity->sequence;
456 struct dma_fence *other = NULL;
459 idx = seq & (amdgpu_sched_jobs - 1);
460 other = centity->fences[idx];
462 BUG_ON(!dma_fence_is_signaled(other));
464 dma_fence_get(fence);
466 spin_lock(&ctx->ring_lock);
467 centity->fences[idx] = fence;
469 spin_unlock(&ctx->ring_lock);
471 dma_fence_put(other);
476 struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
477 struct drm_sched_entity *entity,
480 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
481 struct dma_fence *fence;
483 spin_lock(&ctx->ring_lock);
486 seq = centity->sequence - 1;
488 if (seq >= centity->sequence) {
489 spin_unlock(&ctx->ring_lock);
490 return ERR_PTR(-EINVAL);
494 if (seq + amdgpu_sched_jobs < centity->sequence) {
495 spin_unlock(&ctx->ring_lock);
499 fence = dma_fence_get(centity->fences[seq & (amdgpu_sched_jobs - 1)]);
500 spin_unlock(&ctx->ring_lock);
505 void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
506 enum drm_sched_priority priority)
508 enum drm_sched_priority ctx_prio;
511 ctx->override_priority = priority;
513 ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
514 ctx->init_priority : ctx->override_priority;
515 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
516 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
517 struct drm_sched_entity *entity;
519 if (!ctx->entities[i][j])
522 entity = &ctx->entities[i][j]->entity;
523 drm_sched_entity_set_priority(entity, ctx_prio);
528 int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
529 struct drm_sched_entity *entity)
531 struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
532 struct dma_fence *other;
536 spin_lock(&ctx->ring_lock);
537 idx = centity->sequence & (amdgpu_sched_jobs - 1);
538 other = dma_fence_get(centity->fences[idx]);
539 spin_unlock(&ctx->ring_lock);
544 r = dma_fence_wait(other, true);
545 if (r < 0 && r != -ERESTARTSYS)
546 DRM_ERROR("Error (%ld) waiting for fence!\n", r);
548 dma_fence_put(other);
552 void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
554 mutex_init(&mgr->lock);
555 idr_init(&mgr->ctx_handles);
558 long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
560 struct amdgpu_ctx *ctx;
564 idp = &mgr->ctx_handles;
566 mutex_lock(&mgr->lock);
567 idr_for_each_entry(idp, ctx, id) {
568 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
569 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
570 struct drm_sched_entity *entity;
572 if (!ctx->entities[i][j])
575 entity = &ctx->entities[i][j]->entity;
576 timeout = drm_sched_entity_flush(entity, timeout);
580 mutex_unlock(&mgr->lock);
584 void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
586 struct amdgpu_ctx *ctx;
590 idp = &mgr->ctx_handles;
592 idr_for_each_entry(idp, ctx, id) {
593 if (kref_read(&ctx->refcount) != 1) {
594 DRM_ERROR("ctx %p is still alive\n", ctx);
598 for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
599 for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
600 struct drm_sched_entity *entity;
602 if (!ctx->entities[i][j])
605 entity = &ctx->entities[i][j]->entity;
606 drm_sched_entity_fini(entity);
612 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
614 struct amdgpu_ctx *ctx;
618 amdgpu_ctx_mgr_entity_fini(mgr);
620 idp = &mgr->ctx_handles;
622 idr_for_each_entry(idp, ctx, id) {
623 if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
624 DRM_ERROR("ctx %p is still alive\n", ctx);
627 idr_destroy(&mgr->ctx_handles);
628 mutex_destroy(&mgr->lock);
631 void amdgpu_ctx_init_sched(struct amdgpu_device *adev)
635 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
636 adev->gfx.gfx_sched[i] = &adev->gfx.gfx_ring[i].sched;
637 adev->gfx.num_gfx_sched++;
640 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
641 adev->gfx.compute_sched[i] = &adev->gfx.compute_ring[i].sched;
642 adev->gfx.num_compute_sched++;
645 for (i = 0; i < adev->sdma.num_instances; i++) {
646 adev->sdma.sdma_sched[i] = &adev->sdma.instance[i].ring.sched;
647 adev->sdma.num_sdma_sched++;
650 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
651 if (adev->vcn.harvest_config & (1 << i))
653 adev->vcn.vcn_dec_sched[adev->vcn.num_vcn_dec_sched++] =
654 &adev->vcn.inst[i].ring_dec.sched;
657 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
658 if (adev->vcn.harvest_config & (1 << i))
660 for (j = 0; j < adev->vcn.num_enc_rings; ++j)
661 adev->vcn.vcn_enc_sched[adev->vcn.num_vcn_enc_sched++] =
662 &adev->vcn.inst[i].ring_enc[j].sched;
665 for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) {
666 if (adev->jpeg.harvest_config & (1 << i))
668 adev->jpeg.jpeg_sched[adev->jpeg.num_jpeg_sched++] =
669 &adev->jpeg.inst[i].ring_dec.sched;