drm/sched: move calling drm_sched_entity_select_rq
Subsystems (5)
Christian König
reviewerPATCH 1/12 drm/sched: move calling drm_sched_entity_select_rq
We already discussed that the call to drm_sched_entity_select_rq() needs to move to drm_sched_job_arm() to be able to set a new scheduler list between _init() and _arm(). This was just not applied for some reason.
--- drivers/gpu/drm/scheduler/sched_main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +1 -2
@@ -592,7 +592,6 @@ int drm_sched_job_init(struct drm_sched_job *job, struct drm_sched_entity *entity, void *owner) { -drm_sched_entity_select_rq (entity );if (!entity->rq)return -ENOENT ;
@@ -628,7 +627,7 @@ void drm_sched_job_arm(struct drm_sched_job *job)struct drm_sched_entity *entity = job->entity;BUG_ON (!entity); - +drm_sched_entity_select_rq (entity); sched = entity->rq->sched; job->sched = sched;
Signature · 1 line
2.25.1
Christian König
reviewerPATCH 2/12 drm/amdgpu: remove SRIOV and MCBP dependencies from the CS
We should not have any different CS constrains based on the execution environment.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +6 -8
@@ -814,7 +814,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)if (r)return r; -if (amdgpu_mcbp ||amdgpu_sriov_vf (adev)) { +if (fpriv->csa_va) { bo_va = fpriv->csa_va;BUG_ON (!bo_va); r =amdgpu_vm_bo_update (adev, bo_va, false);
@@ -898,13 +898,11 @@ static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,continue ;if (chunk_ib->ip_type ==AMDGPU_HW_IP_GFX && - (amdgpu_mcbp ||amdgpu_sriov_vf (adev))) { -if (chunk_ib->flags &AMDGPU_IB_FLAG_PREEMPT ) { -if (chunk_ib->flags &AMDGPU_IB_FLAG_CE ) - ce_preempt++; -else - de_preempt++; - } + chunk_ib->flags &AMDGPU_IB_FLAG_PREEMPT ) { +if (chunk_ib->flags &AMDGPU_IB_FLAG_CE ) + ce_preempt++; +else + de_preempt++;/* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */ if (ce_preempt >1 || de_preempt >1 )
Signature · 1 line
2.25.1
Christian König
reviewerPATCH 3/12 drm/amdgpu: move setting the job resources
In reply to Christian KönigMove setting the job resources into amdgpu_job.c
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 21 ++------------------- drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 17 +++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_job.h | 2 ++ 3 files changed, 21 insertions(+), 19 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +2 -19
@@ -495,9 +495,6 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,struct amdgpu_vm *vm = &fpriv->vm;struct amdgpu_bo_list_entry *e;struct list_head duplicates; -struct amdgpu_bo *gds; -struct amdgpu_bo *gws; -struct amdgpu_bo *oa;int r;INIT_LIST_HEAD (&p->validated);
@@ -614,22 +611,8 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,amdgpu_cs_report_moved_bytes (p->adev, p->bytes_moved, p->bytes_moved_vis); - gds = p->bo_list->gds_obj; - gws = p->bo_list->gws_obj; - oa = p->bo_list->oa_obj; - -if (gds) { - p->job->gds_base =amdgpu_bo_gpu_offset (gds) >>PAGE_SHIFT ; - p->job->gds_size =amdgpu_bo_size (gds) >>PAGE_SHIFT ; - } -if (gws) { - p->job->gws_base =amdgpu_bo_gpu_offset (gws) >>PAGE_SHIFT ; - p->job->gws_size =amdgpu_bo_size (gws) >>PAGE_SHIFT ; - } -if (oa) { - p->job->oa_base =amdgpu_bo_gpu_offset (oa) >>PAGE_SHIFT ; - p->job->oa_size =amdgpu_bo_size (oa) >>PAGE_SHIFT ; - } +amdgpu_job_set_resources (p->job, p->bo_list->gds_obj, + p->bo_list->gws_obj, p->bo_list->oa_obj);if (!r && p->uf_entry.tv.bo) {struct amdgpu_bo *uf =ttm_to_amdgpu_bo (p->uf_entry.tv.bo);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +17 -0
@@ -132,6 +132,23 @@ int amdgpu_job_alloc_with_ib(struct amdgpu_device *adev, unsigned size,return r; } +void amdgpu_job_set_resources (struct amdgpu_job *job,struct amdgpu_bo *gds, +struct amdgpu_bo *gws,struct amdgpu_bo *oa) +{ +if (gds) { + job->gds_base =amdgpu_bo_gpu_offset (gds) >>PAGE_SHIFT ; + job->gds_size =amdgpu_bo_size (gds) >>PAGE_SHIFT ; + } +if (gws) { + job->gws_base =amdgpu_bo_gpu_offset (gws) >>PAGE_SHIFT ; + job->gws_size =amdgpu_bo_size (gws) >>PAGE_SHIFT ; + } +if (oa) { + job->oa_base =amdgpu_bo_gpu_offset (oa) >>PAGE_SHIFT ; + job->oa_size =amdgpu_bo_size (oa) >>PAGE_SHIFT ; + } +} +void amdgpu_job_free_resources (struct amdgpu_job *job) {struct amdgpu_ring *ring =to_amdgpu_ring (job->base.sched);
drivers/gpu/drm/amd/amdgpu/amdgpu_job.h +2 -0
@@ -76,6 +76,8 @@ int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs, struct amdgpu_job **job, struct amdgpu_vm *vm);int amdgpu_job_alloc_with_ib (struct amdgpu_device *adev,unsigned size,enum amdgpu_ib_pool_type pool,struct amdgpu_job **job); +void amdgpu_job_set_resources (struct amdgpu_job *job,struct amdgpu_bo *gds, +struct amdgpu_bo *gws,struct amdgpu_bo *oa);void amdgpu_job_free_resources (struct amdgpu_job *job);void amdgpu_job_free (struct amdgpu_job *job); int amdgpu_job_submit(struct amdgpu_job *job, struct drm_sched_entity *entity,
Signature · 1 line
2.25.1
Christian König
reviewerPATCH 4/12 drm/amdgpu: revert "partial revert "remove ctx->lock" v2"
In reply to Christian KönigThis reverts commit 94f4c4965e5513ba624488f4b601d6b385635aec.
We found that the bo_list is missing a protection for its list entries. Since that is fixed now this workaround can be removed again.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 21 ++++++--------------- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 2 -- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 1 - 3 files changed, 6 insertions(+), 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +6 -15
@@ -128,8 +128,6 @@ static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, union drm_amdgpu_cs goto free_chunk; } -mutex_lock (&p->ctx->lock); -/* skip guilty context job */ if (atomic_read (&p->ctx->guilty) ==1 ) { ret = -ECANCELED ;
@@ -691,7 +689,6 @@ static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error,dma_fence_put (parser->fence);if (parser->ctx) { -mutex_unlock (&parser->ctx->lock);amdgpu_ctx_put (parser->ctx); }if (parser->bo_list)
@@ -1138,9 +1135,6 @@ static int amdgpu_cs_dependencies(struct amdgpu_device *adev, {int i, r; -/* TODO: Investigate why we still need the context lock */ -mutex_unlock (&p->ctx->lock); -for (i =0 ; i < p->nchunks; ++i) {struct amdgpu_cs_chunk *chunk;
@@ -1151,34 +1145,32 @@ static int amdgpu_cs_dependencies(struct amdgpu_device *adev,case AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES : r =amdgpu_cs_process_fence_dep (p, chunk);if (r) - goto out; +return r;break ;case AMDGPU_CHUNK_ID_SYNCOBJ_IN : r =amdgpu_cs_process_syncobj_in_dep (p, chunk);if (r) - goto out; +return r;break ;case AMDGPU_CHUNK_ID_SYNCOBJ_OUT : r =amdgpu_cs_process_syncobj_out_dep (p, chunk);if (r) - goto out; +return r;break ;case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_WAIT : r =amdgpu_cs_process_syncobj_timeline_in_dep (p, chunk);if (r) - goto out; +return r;break ;case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_SIGNAL : r =amdgpu_cs_process_syncobj_timeline_out_dep (p, chunk);if (r) - goto out; +return r;break ; } } -out: -mutex_lock (&p->ctx->lock); -return r; +return 0 ; }static void amdgpu_cs_post_dependencies (struct amdgpu_cs_parser *p)
@@ -1340,7 +1332,6 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) goto out; r =amdgpu_cs_submit (&parser, cs); - out:amdgpu_cs_parser_fini (&parser, r, reserved_buffers);
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -2
@@ -315,7 +315,6 @@ static int amdgpu_ctx_init(struct amdgpu_ctx_mgr *mgr, int32_t priority,kref_init (&ctx->refcount); ctx->mgr = mgr;spin_lock_init (&ctx->ring_lock); -mutex_init (&ctx->lock); ctx->reset_counter =atomic_read (&mgr->adev->gpu_reset_counter); ctx->reset_counter_query = ctx->reset_counter;
@@ -407,7 +406,6 @@ static void amdgpu_ctx_fini(struct kref *ref)drm_dev_exit (idx); } -mutex_destroy (&ctx->lock);kfree (ctx); }
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0 -1
@@ -53,7 +53,6 @@ struct amdgpu_ctx {bool preamble_presented;int32_t init_priority;int32_t override_priority; -struct mutex lock;atomic_t guilty;unsigned long ras_counter_ce;unsigned long ras_counter_ue;
Signature · 1 line
2.25.1
Christian König
reviewerPATCH 5/12 drm/amdgpu: use DMA_RESV_USAGE_BOOKKEEP v2
In reply to Christian KönigUse DMA_RESV_USAGE_BOOKKEEP for VM page table updates and KFD preemption fence.
v2: actually update all usages for KFD
--- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 26 ++++++++++++------- drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c | 3 ++- 2 files changed, 18 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +16 -10
@@ -297,7 +297,7 @@ static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo,*/ replacement =dma_fence_get_stub ();dma_resv_replace_fences (bo->tbo.base.resv, ef->base.context, - replacement,DMA_RESV_USAGE_READ ); + replacement,DMA_RESV_USAGE_BOOKKEEP );dma_fence_put (replacement);return 0 ; }
@@ -1390,8 +1390,9 @@ static int init_kfd_vm(struct amdgpu_vm *vm, void **process_info, ret =dma_resv_reserve_fences (vm->root.bo->tbo.base.resv,1 );if (ret) goto reserve_shared_fail; -amdgpu_bo_fence (vm->root.bo, - &vm->process_info->eviction_fence->base, true); +dma_resv_add_fence (vm->root.bo->tbo.base.resv, + &vm->process_info->eviction_fence->base, +DMA_RESV_USAGE_BOOKKEEP );amdgpu_bo_unreserve (vm->root.bo);/* Update process info */
@@ -1987,9 +1988,9 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( }if (!amdgpu_ttm_tt_get_usermm (bo->tbo.ttm) && !bo->tbo.pin_count) -amdgpu_bo_fence (bo, - &avm->process_info->eviction_fence->base, - true); +dma_resv_add_fence (bo->tbo.base.resv, + &avm->process_info->eviction_fence->base, +DMA_RESV_USAGE_BOOKKEEP ); ret =unreserve_bo_and_vms (&ctx, false, false); goto out;
@@ -2758,15 +2759,18 @@ int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence **ef)if (mem->bo->tbo.pin_count)continue ; -amdgpu_bo_fence (mem->bo, - &process_info->eviction_fence->base, true); +dma_resv_add_fence (mem->bo->tbo.base.resv, + &process_info->eviction_fence->base, +DMA_RESV_USAGE_BOOKKEEP ); }/* Attach eviction fence to PD / PT BOs */ list_for_each_entry (peer_vm, &process_info->vm_list_head, vm_list_node) {struct amdgpu_bo *bo = peer_vm->root.bo; -amdgpu_bo_fence (bo, &process_info->eviction_fence->base, true); +dma_resv_add_fence (bo->tbo.base.resv, + &process_info->eviction_fence->base, +DMA_RESV_USAGE_BOOKKEEP ); } validate_map_fail:
@@ -2820,7 +2824,9 @@ int amdgpu_amdkfd_add_gws_to_process(void *info, void *gws, struct kgd_mem **mem ret =dma_resv_reserve_fences (gws_bo->tbo.base.resv,1 );if (ret) goto reserve_shared_fail; -amdgpu_bo_fence (gws_bo, &process_info->eviction_fence->base, true); +dma_resv_add_fence (gws_bo->tbo.base.resv, + &process_info->eviction_fence->base, +DMA_RESV_USAGE_BOOKKEEP );amdgpu_bo_unreserve (gws_bo);mutex_unlock (&(*mem)->process_info->lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_sdma.c +2 -1
@@ -112,7 +112,8 @@ static int amdgpu_vm_sdma_commit(struct amdgpu_vm_update_params *p,swap (p->vm->last_unlocked, tmp);dma_fence_put (tmp); }else { -amdgpu_bo_fence (p->vm->root.bo, f, true); +dma_resv_add_fence (p->vm->root.bo->tbo.base.resv, f, +DMA_RESV_USAGE_BOOKKEEP ); }if (fence && !p->immediate)
Signature · 1 line
2.25.1
Christian König
reviewerPATCH 6/12 drm/amdgpu: cleanup and reorder amdgpu_cs.c v3
In reply to Christian KönigSort the functions in the order they are called and cleanup the coding style and function names to represent the data they process.
Check the size of the IB chunk.
v2: fix job initialisation order and use correct scheduler instance
v3: try to move all functional changes into a separate patch.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 1344 ++++++++++++------------ drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h | 2 +- 2 files changed, 671 insertions(+), 675 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +670 -674
@@ -39,9 +39,42 @@#include "amdgpu_gem.h" #include "amdgpu_ras.h" -static int amdgpu_cs_user_fence_chunk (struct amdgpu_cs_parser *p, -struct drm_amdgpu_cs_chunk_fence *data, -uint32_t *offset) +static int amdgpu_cs_parser_init (struct amdgpu_cs_parser *p, +struct amdgpu_device *adev, +struct drm_file *filp, +union drm_amdgpu_cs *cs) +{ +struct amdgpu_fpriv *fpriv = filp->driver_priv; + +if (cs->in.num_chunks ==0 ) +return -EINVAL ; + +memset (p,0 ,sizeof (*p)); + p->adev = adev; + p->filp = filp; + + p->ctx =amdgpu_ctx_get (fpriv, cs->in.ctx_id); +if (!p->ctx) +return -EINVAL ; + +if (atomic_read (&p->ctx->guilty)) { +amdgpu_ctx_put (p->ctx); +return -ECANCELED ; + } +return 0 ; +} + +static int amdgpu_cs_p1_ib (struct amdgpu_cs_parser *p, +struct drm_amdgpu_cs_chunk_ib *chunk_ib, +unsigned int *num_ibs) +{ + ++(*num_ibs); +return 0 ; +} + +static int amdgpu_cs_p1_user_fence (struct amdgpu_cs_parser *p, +struct drm_amdgpu_cs_chunk_fence *data, +uint32_t *offset) {struct drm_gem_object *gobj;struct amdgpu_bo *bo;
@@ -80,11 +113,11 @@ static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,return r; } -static int amdgpu_cs_bo_handles_chunk (struct amdgpu_cs_parser *p, -struct drm_amdgpu_bo_list_in *data) +static int amdgpu_cs_p1_bo_handles (struct amdgpu_cs_parser *p, +struct drm_amdgpu_bo_list_in *data) { +struct drm_amdgpu_bo_list_entry *info;int r; -struct drm_amdgpu_bo_list_entry *info =NULL ; r =amdgpu_bo_create_list_entry_array (data, &info);if (r)
@@ -104,7 +137,9 @@ static int amdgpu_cs_bo_handles_chunk(struct amdgpu_cs_parser *p,return r; } -static int amdgpu_cs_parser_init (struct amdgpu_cs_parser *p,union drm_amdgpu_cs *cs) +/* Copy the data from userspace and go over it the first time */ +static int amdgpu_cs_pass1 (struct amdgpu_cs_parser *p, +union drm_amdgpu_cs *cs) {struct amdgpu_fpriv *fpriv = p->filp->driver_priv;struct amdgpu_vm *vm = &fpriv->vm;
@@ -112,28 +147,14 @@ static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, union drm_amdgpu_csuint64_t *chunk_array;unsigned size, num_ibs =0 ;uint32_t uf_offset =0 ; -int i;int ret; +int i; -if (cs->in.num_chunks ==0 ) -return -EINVAL ; - - chunk_array =kvmalloc_array (cs->in.num_chunks,sizeof (uint64_t ),GFP_KERNEL ); + chunk_array =kvmalloc_array (cs->in.num_chunks,sizeof (uint64_t ), +GFP_KERNEL );if (!chunk_array)return -ENOMEM ; - p->ctx =amdgpu_ctx_get (fpriv, cs->in.ctx_id); -if (!p->ctx) { - ret = -EINVAL ; - goto free_chunk; - } - -/* skip guilty context job */ -if (atomic_read (&p->ctx->guilty) ==1 ) { - ret = -ECANCELED ; - goto free_chunk; - } -/* get chunks */ chunk_array_user =u64_to_user_ptr (cs->in.chunks); if (copy_from_user(chunk_array, chunk_array_user,
@@ -168,7 +189,8 @@ static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, union drm_amdgpu_cs size = p->chunks[i].length_dw; cdata =u64_to_user_ptr (user_chunk.chunk_data); - p->chunks[i].kdata =kvmalloc_array (size,sizeof (uint32_t ),GFP_KERNEL ); + p->chunks[i].kdata =kvmalloc_array (size,sizeof (uint32_t ), +GFP_KERNEL );if (p->chunks[i].kdata ==NULL ) { ret = -ENOMEM ; i--;
@@ -180,36 +202,35 @@ static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, union drm_amdgpu_cs goto free_partial_kdata; } +/* Assume the worst on the following checks */ + ret = -EINVAL ;switch (p->chunks[i].chunk_id) {case AMDGPU_CHUNK_ID_IB : - ++num_ibs; +if (size <sizeof (struct drm_amdgpu_cs_chunk_ib )) + goto free_partial_kdata; + + ret =amdgpu_cs_p1_ib (p, p->chunks[i].kdata, &num_ibs); +if (ret) + goto free_partial_kdata;break ;case AMDGPU_CHUNK_ID_FENCE : - size =sizeof (struct drm_amdgpu_cs_chunk_fence ); -if (p->chunks[i].length_dw *sizeof (uint32_t ) < size) { - ret = -EINVAL ; +if (size <sizeof (struct drm_amdgpu_cs_chunk_fence )) goto free_partial_kdata; - } - ret =amdgpu_cs_user_fence_chunk (p, p->chunks[i].kdata, - &uf_offset); + ret =amdgpu_cs_p1_user_fence (p, p->chunks[i].kdata, + &uf_offset);if (ret) goto free_partial_kdata; -break ;case AMDGPU_CHUNK_ID_BO_HANDLES : - size =sizeof (struct drm_amdgpu_bo_list_in ); -if (p->chunks[i].length_dw *sizeof (uint32_t ) < size) { - ret = -EINVAL ; +if (size <sizeof (struct drm_amdgpu_bo_list_in )) goto free_partial_kdata; - } - ret =amdgpu_cs_bo_handles_chunk (p, p->chunks[i].kdata); + ret =amdgpu_cs_p1_bo_handles (p, p->chunks[i].kdata);if (ret) goto free_partial_kdata; -break ;case AMDGPU_CHUNK_ID_DEPENDENCIES :
@@ -221,7 +242,6 @@ static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, union drm_amdgpu_csbreak ;default : - ret = -EINVAL ; goto free_partial_kdata; } }
@@ -258,160 +278,469 @@ static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, union drm_amdgpu_csreturn ret; } -/* Convert microseconds to bytes. */ -static u64 us_to_bytes (struct amdgpu_device *adev,s64 us) +static int amdgpu_cs_p2_ib (struct amdgpu_cs_parser *p, +struct amdgpu_cs_chunk *chunk, +unsigned int *num_ibs, +unsigned int *ce_preempt, +unsigned int *de_preempt) { -if (us <=0 || !adev->mm_stats.log2_max_MBps) -return 0 ; +struct drm_amdgpu_cs_chunk_ib *chunk_ib = chunk->kdata; +struct amdgpu_fpriv *fpriv = p->filp->driver_priv; +struct amdgpu_ib *ib = &p->job->ibs[*num_ibs]; +struct amdgpu_vm *vm = &fpriv->vm; +struct drm_sched_entity *entity; +struct amdgpu_ring *ring; +int r; -/* Since accum_us is incremented by a million per second, just -* multiply it by the number of MB/s to get the number of bytes. + r =amdgpu_ctx_get_entity (p->ctx, chunk_ib->ip_type, + chunk_ib->ip_instance, + chunk_ib->ring, &entity); +if (r) +return r; + +/* +* Abort if there is no run queue associated with this entity. +* Possibly because of disabled HW IP. */ -return us << adev->mm_stats.log2_max_MBps; -} +if (entity->rq ==NULL ) +return -EINVAL ; -static s64 bytes_to_us (struct amdgpu_device *adev,u64 bytes) -{ -if (!adev->mm_stats.log2_max_MBps) -return 0 ; +/* Currently we don't support submitting to multiple entities */ +if (p->entity && p->entity != entity) +return -EINVAL ; -return bytes >> adev->mm_stats.log2_max_MBps; -} + p->entity = entity; -/* Returns how many bytes TTM can move right now. If no bytes can be moved, -* it returns 0. If it returns non-zero, it's OK to move at least one buffer, -* which means it can go over the threshold once. If that happens, the driver -* will be in debt and no other buffer migrations can be done until that debt -* is repaid. -* -* This approach allows moving a buffer of any size (it's important to allow -* that). -* -* The currency is simply time in microseconds and it increases as the clock -* ticks. The accumulated microseconds (us) are converted to bytes and -* returned. -*/ -static void amdgpu_cs_get_threshold_for_moves (struct amdgpu_device *adev, -u64 *max_bytes, -u64 *max_vis_bytes) -{ -s64 time_us, increment_us; -u64 free_vram, total_vram, used_vram; -/* Allow a maximum of 200 accumulated ms. This is basically per-IB -* throttling. -* -* It means that in order to get full max MBps, at least 5 IBs per -* second must be submitted and not more than 200ms apart from each -* other. -*/ -const s64 us_upper_bound =200000 ; + ring =to_amdgpu_ring (entity->rq->sched); +/* MM engine doesn't support user fences */ +if (p->job->uf_addr && ring->funcs->no_user_fence) +return -EINVAL ; -if (!adev->mm_stats.log2_max_MBps) { - *max_bytes =0 ; - *max_vis_bytes =0 ; -return ; +if (chunk_ib->ip_type ==AMDGPU_HW_IP_GFX && + chunk_ib->flags &AMDGPU_IB_FLAG_PREEMPT ) { +if (chunk_ib->flags &AMDGPU_IB_FLAG_CE ) + (*ce_preempt)++; +else + (*de_preempt)++; + +/* Each GFX command submit allows only 1 IB max +* preemptible for CE & DE */ +if (*ce_preempt >1 || *de_preempt >1 ) +return -EINVAL ; } - total_vram = adev->gmc.real_vram_size -atomic64_read (&adev->vram_pin_size); - used_vram =ttm_resource_manager_usage (&adev->mman.vram_mgr.manager); - free_vram = used_vram >= total_vram ?0 : total_vram - used_vram; +if (chunk_ib->flags &AMDGPU_IB_FLAG_PREAMBLE ) + p->job->preamble_status |=AMDGPU_PREAMBLE_IB_PRESENT ; -spin_lock (&adev->mm_stats.lock); + r =amdgpu_ib_get (p->adev, vm, ring->funcs->parse_cs ? + chunk_ib->ib_bytes :0 , +AMDGPU_IB_POOL_DELAYED , ib); +if (r) { +DRM_ERROR ("Failed to get ib !\n" ); +return r; + } -/* Increase the amount of accumulated us. */ - time_us =ktime_to_us (ktime_get ()); - increment_us = time_us - adev->mm_stats.last_update_us; - adev->mm_stats.last_update_us = time_us; - adev->mm_stats.accum_us =min (adev->mm_stats.accum_us + increment_us, - us_upper_bound); + ib->gpu_addr = chunk_ib->va_start; + ib->length_dw = chunk_ib->ib_bytes /4 ; + ib->flags = chunk_ib->flags; -/* This prevents the short period of low performance when the VRAM -* usage is low and the driver is in debt or doesn't have enough -* accumulated us to fill VRAM quickly. -* -* The situation can occur in these cases: -* - a lot of VRAM is freed by userspace -* - the presence of a big buffer causes a lot of evictions -* (solution: split buffers into smaller ones) -* -* If 128 MB or 1/8th of VRAM is free, start filling it now by setting -* accum_us to a positive number. -*/ -if (free_vram >=128 *1024 *1024 || free_vram >= total_vram /8 ) { -s64 min_us; + (*num_ibs)++; +return 0 ; +} -/* Be more aggressive on dGPUs. Try to fill a portion of free -* VRAM now. -*/ -if (!(adev->flags &AMD_IS_APU )) - min_us =bytes_to_us (adev, free_vram /4 ); -else - min_us =0 ;/* Reset accum_us on APUs. */ +static int amdgpu_cs_p2_dependencies (struct amdgpu_cs_parser *p, +struct amdgpu_cs_chunk *chunk) +{ +struct drm_amdgpu_cs_chunk_dep *deps = chunk->kdata; +struct amdgpu_fpriv *fpriv = p->filp->driver_priv; +unsigned num_deps; +int i, r; - adev->mm_stats.accum_us =max (min_us, adev->mm_stats.accum_us); - } + num_deps = chunk->length_dw *4 / +sizeof (struct drm_amdgpu_cs_chunk_dep ); -/* This is set to 0 if the driver is in debt to disallow (optional) -* buffer moves. -*/ - *max_bytes =us_to_bytes (adev, adev->mm_stats.accum_us); +for (i =0 ; i < num_deps; ++i) { +struct amdgpu_ctx *ctx; +struct drm_sched_entity *entity; +struct dma_fence *fence; -/* Do the same for visible VRAM if half of it is free */ -if (!amdgpu_gmc_vram_full_visible (&adev->gmc)) { -u64 total_vis_vram = adev->gmc.visible_vram_size; -u64 used_vis_vram = -amdgpu_vram_mgr_vis_usage (&adev->mman.vram_mgr); + ctx =amdgpu_ctx_get (fpriv, deps[i].ctx_id); +if (ctx ==NULL ) +return -EINVAL ; -if (used_vis_vram < total_vis_vram) { -u64 free_vis_vram = total_vis_vram - used_vis_vram; - adev->mm_stats.accum_us_vis =min (adev->mm_stats.accum_us_vis + - increment_us, us_upper_bound); + r =amdgpu_ctx_get_entity (ctx, deps[i].ip_type, + deps[i].ip_instance, + deps[i].ring, &entity); +if (r) { +amdgpu_ctx_put (ctx); +return r; + } -if (free_vis_vram >= total_vis_vram /2 ) - adev->mm_stats.accum_us_vis = -max (bytes_to_us (adev, free_vis_vram /2 ), - adev->mm_stats.accum_us_vis); + fence =amdgpu_ctx_get_fence (ctx, entity, deps[i].handle); +amdgpu_ctx_put (ctx); + +if (IS_ERR (fence)) +return PTR_ERR (fence); +else if (!fence) +continue ; + +if (chunk->chunk_id ==AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES ) { +struct drm_sched_fence *s_fence; +struct dma_fence *old = fence; + + s_fence =to_drm_sched_fence (fence); + fence =dma_fence_get (&s_fence->scheduled); +dma_fence_put (old); } - *max_vis_bytes =us_to_bytes (adev, adev->mm_stats.accum_us_vis); - }else { - *max_vis_bytes =0 ; + r =amdgpu_sync_fence (&p->job->sync, fence); +dma_fence_put (fence); +if (r) +return r; } +return 0 ; +} -spin_unlock (&adev->mm_stats.lock); +static int amdgpu_syncobj_lookup_and_add (struct amdgpu_cs_parser *p, +uint32_t handle,u64 point, +u64 flags) +{ +struct dma_fence *fence; +int r; + + r =drm_syncobj_find_fence (p->filp, handle, point, flags, &fence); +if (r) { +DRM_ERROR ("syncobj %u failed to find fence @ %llu (%d)!\n" , + handle, point, r); +return r; + } + + r =amdgpu_sync_fence (&p->job->sync, fence); +dma_fence_put (fence); + +return r; } -/* Report how many bytes have really been moved for the last command -* submission. This can result in a debt that can stop buffer migrations -* temporarily. -*/ -void amdgpu_cs_report_moved_bytes (struct amdgpu_device *adev,u64 num_bytes, -u64 num_vis_bytes) +static int amdgpu_cs_p2_syncobj_in (struct amdgpu_cs_parser *p, +struct amdgpu_cs_chunk *chunk) { -spin_lock (&adev->mm_stats.lock); - adev->mm_stats.accum_us -=bytes_to_us (adev, num_bytes); - adev->mm_stats.accum_us_vis -=bytes_to_us (adev, num_vis_bytes); -spin_unlock (&adev->mm_stats.lock); +struct drm_amdgpu_cs_chunk_sem *deps = chunk->kdata; +unsigned num_deps; +int i, r; + + num_deps = chunk->length_dw *4 / +sizeof (struct drm_amdgpu_cs_chunk_sem ); +for (i =0 ; i < num_deps; ++i) { + r =amdgpu_syncobj_lookup_and_add (p, deps[i].handle,0 ,0 ); +if (r) +return r; + } + +return 0 ; } -static int amdgpu_cs_bo_validate (void *param,struct amdgpu_bo *bo) +static int amdgpu_cs_p2_syncobj_timeline_wait (struct amdgpu_cs_parser *p, +struct amdgpu_cs_chunk *chunk) { -struct amdgpu_device *adev =amdgpu_ttm_adev (bo->tbo.bdev); -struct amdgpu_cs_parser *p = param; -struct ttm_operation_ctx ctx = { - .interruptible = true, - .no_wait_gpu = false, - .resv = bo->tbo.base.resv - }; -uint32_t domain; -int r; +struct drm_amdgpu_cs_chunk_syncobj *syncobj_deps = chunk->kdata; +unsigned num_deps; +int i, r; -if (bo->tbo.pin_count) -return 0 ; + num_deps = chunk->length_dw *4 / +sizeof (struct drm_amdgpu_cs_chunk_syncobj ); +for (i =0 ; i < num_deps; ++i) { + r =amdgpu_syncobj_lookup_and_add (p, syncobj_deps[i].handle, + syncobj_deps[i].point, + syncobj_deps[i].flags); +if (r) +return r; + } -/* Don't move this buffer if we have depleted our allowance -* to move it. Don't move anything if the threshold is zero. -*/ +return 0 ; +} + +static int amdgpu_cs_p2_syncobj_out (struct amdgpu_cs_parser *p, +struct amdgpu_cs_chunk *chunk) +{ +struct drm_amdgpu_cs_chunk_sem *deps = chunk->kdata; +unsigned num_deps; +int i; + + num_deps = chunk->length_dw *4 / +sizeof (struct drm_amdgpu_cs_chunk_sem ); + +if (p->post_deps) +return -EINVAL ; + + p->post_deps =kmalloc_array (num_deps,sizeof (*p->post_deps), +GFP_KERNEL ); + p->num_post_deps =0 ; + +if (!p->post_deps) +return -ENOMEM ; + + +for (i =0 ; i < num_deps; ++i) { + p->post_deps[i].syncobj = +drm_syncobj_find (p->filp, deps[i].handle); +if (!p->post_deps[i].syncobj) +return -EINVAL ; + p->post_deps[i].chain =NULL ; + p->post_deps[i].point =0 ; + p->num_post_deps++; + } + +return 0 ; +} + +static int amdgpu_cs_p2_syncobj_timeline_signal (struct amdgpu_cs_parser *p, +struct amdgpu_cs_chunk *chunk) +{ +struct drm_amdgpu_cs_chunk_syncobj *syncobj_deps = chunk->kdata; +unsigned num_deps; +int i; + + num_deps = chunk->length_dw *4 / +sizeof (struct drm_amdgpu_cs_chunk_syncobj ); + +if (p->post_deps) +return -EINVAL ; + + p->post_deps =kmalloc_array (num_deps,sizeof (*p->post_deps), +GFP_KERNEL ); + p->num_post_deps =0 ; + +if (!p->post_deps) +return -ENOMEM ; + +for (i =0 ; i < num_deps; ++i) { +struct amdgpu_cs_post_dep *dep = &p->post_deps[i]; + + dep->chain =NULL ; +if (syncobj_deps[i].point) { + dep->chain =dma_fence_chain_alloc (); +if (!dep->chain) +return -ENOMEM ; + } + + dep->syncobj =drm_syncobj_find (p->filp, + syncobj_deps[i].handle); +if (!dep->syncobj) { +dma_fence_chain_free (dep->chain); +return -EINVAL ; + } + dep->point = syncobj_deps[i].point; + p->num_post_deps++; + } + +return 0 ; +} + +static int amdgpu_cs_pass2 (struct amdgpu_cs_parser *p) +{ +unsigned int num_ibs =0 , ce_preempt =0 , de_preempt =0 ; +int i, r; + +for (i =0 ; i < p->nchunks; ++i) { +struct amdgpu_cs_chunk *chunk; + + chunk = &p->chunks[i]; + +switch (chunk->chunk_id) { +case AMDGPU_CHUNK_ID_IB : + r =amdgpu_cs_p2_ib (p, chunk, &num_ibs, + &ce_preempt, &de_preempt); +if (r) +return r; +break ; +case AMDGPU_CHUNK_ID_DEPENDENCIES : +case AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES : + r =amdgpu_cs_p2_dependencies (p, chunk); +if (r) +return r; +break ; +case AMDGPU_CHUNK_ID_SYNCOBJ_IN : + r =amdgpu_cs_p2_syncobj_in (p, chunk); +if (r) +return r; +break ; +case AMDGPU_CHUNK_ID_SYNCOBJ_OUT : + r =amdgpu_cs_p2_syncobj_out (p, chunk); +if (r) +return r; +break ; +case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_WAIT : + r =amdgpu_cs_p2_syncobj_timeline_wait (p, chunk); +if (r) +return r; +break ; +case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_SIGNAL : + r =amdgpu_cs_p2_syncobj_timeline_signal (p, chunk); +if (r) +return r; +break ; + } + } + +return 0 ; +} + +/* Convert microseconds to bytes. */ +static u64 us_to_bytes (struct amdgpu_device *adev,s64 us) +{ +if (us <=0 || !adev->mm_stats.log2_max_MBps) +return 0 ; + +/* Since accum_us is incremented by a million per second, just +* multiply it by the number of MB/s to get the number of bytes. +*/ +return us << adev->mm_stats.log2_max_MBps; +} + +static s64 bytes_to_us (struct amdgpu_device *adev,u64 bytes) +{ +if (!adev->mm_stats.log2_max_MBps) +return 0 ; + +return bytes >> adev->mm_stats.log2_max_MBps; +} + +/* Returns how many bytes TTM can move right now. If no bytes can be moved, +* it returns 0. If it returns non-zero, it's OK to move at least one buffer, +* which means it can go over the threshold once. If that happens, the driver +* will be in debt and no other buffer migrations can be done until that debt +* is repaid. +* +* This approach allows moving a buffer of any size (it's important to allow +* that). +* +* The currency is simply time in microseconds and it increases as the clock +* ticks. The accumulated microseconds (us) are converted to bytes and +* returned. +*/ +static void amdgpu_cs_get_threshold_for_moves (struct amdgpu_device *adev, +u64 *max_bytes, +u64 *max_vis_bytes) +{ +s64 time_us, increment_us; +u64 free_vram, total_vram, used_vram; +/* Allow a maximum of 200 accumulated ms. This is basically per-IB +* throttling. +* +* It means that in order to get full max MBps, at least 5 IBs per +* second must be submitted and not more than 200ms apart from each +* other. +*/ +const s64 us_upper_bound =200000 ; + +if (!adev->mm_stats.log2_max_MBps) { + *max_bytes =0 ; + *max_vis_bytes =0 ; +return ; + } + + total_vram = adev->gmc.real_vram_size - +atomic64_read (&adev->vram_pin_size); + used_vram =ttm_resource_manager_usage (&adev->mman.vram_mgr.manager); + free_vram = used_vram >= total_vram ?0 : total_vram - used_vram; + +spin_lock (&adev->mm_stats.lock); + +/* Increase the amount of accumulated us. */ + time_us =ktime_to_us (ktime_get ()); + increment_us = time_us - adev->mm_stats.last_update_us; + adev->mm_stats.last_update_us = time_us; + adev->mm_stats.accum_us =min (adev->mm_stats.accum_us + increment_us, + us_upper_bound); + +/* This prevents the short period of low performance when the VRAM +* usage is low and the driver is in debt or doesn't have enough +* accumulated us to fill VRAM quickly. +* +* The situation can occur in these cases: +* - a lot of VRAM is freed by userspace +* - the presence of a big buffer causes a lot of evictions +* (solution: split buffers into smaller ones) +* +* If 128 MB or 1/8th of VRAM is free, start filling it now by setting +* accum_us to a positive number. +*/ +if (free_vram >=128 *1024 *1024 || free_vram >= total_vram /8 ) { +s64 min_us; + +/* Be more aggresive on dGPUs. Try to fill a portion of free +* VRAM now. +*/ +if (!(adev->flags &AMD_IS_APU )) + min_us =bytes_to_us (adev, free_vram /4 ); +else + min_us =0 ;/* Reset accum_us on APUs. */ + + adev->mm_stats.accum_us =max (min_us, adev->mm_stats.accum_us); + } + +/* This is set to 0 if the driver is in debt to disallow (optional) +* buffer moves. +*/ + *max_bytes =us_to_bytes (adev, adev->mm_stats.accum_us); + +/* Do the same for visible VRAM if half of it is free */ +if (!amdgpu_gmc_vram_full_visible (&adev->gmc)) { +u64 total_vis_vram = adev->gmc.visible_vram_size; +u64 used_vis_vram = +amdgpu_vram_mgr_vis_usage (&adev->mman.vram_mgr); + +if (used_vis_vram < total_vis_vram) { +u64 free_vis_vram = total_vis_vram - used_vis_vram; + adev->mm_stats.accum_us_vis = +min (adev->mm_stats.accum_us_vis + + increment_us, us_upper_bound); + +if (free_vis_vram >= total_vis_vram /2 ) + adev->mm_stats.accum_us_vis = +max (bytes_to_us (adev, free_vis_vram /2 ), + adev->mm_stats.accum_us_vis); + } + + *max_vis_bytes =us_to_bytes (adev, adev->mm_stats.accum_us_vis); + }else { + *max_vis_bytes =0 ; + } + +spin_unlock (&adev->mm_stats.lock); +} + +/* Report how many bytes have really been moved for the last command +* submission. This can result in a debt that can stop buffer migrations +* temporarily. +*/ +void amdgpu_cs_report_moved_bytes (struct amdgpu_device *adev,u64 num_bytes, +u64 num_vis_bytes) +{ +spin_lock (&adev->mm_stats.lock); + adev->mm_stats.accum_us -=bytes_to_us (adev, num_bytes); + adev->mm_stats.accum_us_vis -=bytes_to_us (adev, num_vis_bytes); +spin_unlock (&adev->mm_stats.lock); +} + +static int amdgpu_cs_bo_validate (void *param,struct amdgpu_bo *bo) +{ +struct amdgpu_device *adev =amdgpu_ttm_adev (bo->tbo.bdev); +struct amdgpu_cs_parser *p = param; +struct ttm_operation_ctx ctx = { + .interruptible = true, + .no_wait_gpu = false, + .resv = bo->tbo.base.resv + }; +uint32_t domain; +int r; + +if (bo->tbo.pin_count) +return 0 ; + +/* Don't move this buffer if we have depleted our allowance +* to move it. Don't move anything if the threshold is zero. +*/ if (p->bytes_moved < p->bytes_moved_threshold && (!bo->tbo.base.dma_buf ||list_empty (&bo->tbo.base.dma_buf->attachments))) {
@@ -612,564 +941,203 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,amdgpu_job_set_resources (p->job, p->bo_list->gds_obj, p->bo_list->gws_obj, p->bo_list->oa_obj); -if (!r && p->uf_entry.tv.bo) { +if (p->uf_entry.tv.bo) {struct amdgpu_bo *uf =ttm_to_amdgpu_bo (p->uf_entry.tv.bo); r =amdgpu_ttm_alloc_gart (&uf->tbo); +if (r) + goto error_validate; + p->job->uf_addr +=amdgpu_bo_gpu_offset (uf); } +return 0 ; error_validate: -if (r) -ttm_eu_backoff_reservation (&p->ticket, &p->validated); +ttm_eu_backoff_reservation (&p->ticket, &p->validated); out_free_user_pages: -if (r) { -amdgpu_bo_list_for_each_userptr_entry (e, p->bo_list) { -struct amdgpu_bo *bo =ttm_to_amdgpu_bo (e->tv.bo); - -if (!e->user_pages) -continue ; -amdgpu_ttm_tt_get_user_pages_done (bo->tbo.ttm); -kvfree (e->user_pages); - e->user_pages =NULL ; - } -mutex_unlock (&p->bo_list->bo_list_mutex); - } -return r; -} - -static int amdgpu_cs_sync_rings (struct amdgpu_cs_parser *p) -{ -struct amdgpu_fpriv *fpriv = p->filp->driver_priv; -struct amdgpu_bo_list_entry *e; -int r; - -list_for_each_entry (e, &p->validated, tv.head) { +amdgpu_bo_list_for_each_userptr_entry (e, p->bo_list) {struct amdgpu_bo *bo =ttm_to_amdgpu_bo (e->tv.bo); -struct dma_resv *resv = bo->tbo.base.resv; -enum amdgpu_sync_mode sync_mode; - sync_mode =amdgpu_bo_explicit_sync (bo) ? -AMDGPU_SYNC_EXPLICIT :AMDGPU_SYNC_NE_OWNER ; - r =amdgpu_sync_resv (p->adev, &p->job->sync, resv, sync_mode, - &fpriv->vm); -if (r) -return r; +if (!e->user_pages) +continue ; +amdgpu_ttm_tt_get_user_pages_done (bo->tbo.ttm); +kvfree (e->user_pages); + e->user_pages =NULL ; } -return 0 ; +mutex_unlock (&p->bo_list->bo_list_mutex); +return r; } -/** -* amdgpu_cs_parser_fini() - clean parser states -* @parser: parser structure holding parsing context. -* @error: error number -* @backoff: indicator to backoff the reservation -* -* If error is set then unvalidate buffer, otherwise just free memory -* used by parsing context. -**/ -static void amdgpu_cs_parser_fini (struct amdgpu_cs_parser *parser,int error, -bool backoff) +static void trace_amdgpu_cs_ibs (struct amdgpu_cs_parser *parser) { -unsigned i; - -if (error && backoff) { -ttm_eu_backoff_reservation (&parser->ticket, - &parser->validated); -mutex_unlock (&parser->bo_list->bo_list_mutex); - } - -for (i =0 ; i < parser->num_post_deps; i++) { -drm_syncobj_put (parser->post_deps[i].syncobj); -kfree (parser->post_deps[i].chain); - } -kfree (parser->post_deps); - -dma_fence_put (parser->fence); - -if (parser->ctx) { -amdgpu_ctx_put (parser->ctx); - } -if (parser->bo_list) -amdgpu_bo_list_put (parser->bo_list); +int i; -for (i =0 ; i < parser->nchunks; i++) -kvfree (parser->chunks[i].kdata); -kvfree (parser->chunks); -if (parser->job) -amdgpu_job_free (parser->job); -if (parser->uf_entry.tv.bo) { -struct amdgpu_bo *uf =ttm_to_amdgpu_bo (parser->uf_entry.tv.bo); +if (!trace_amdgpu_cs_enabled ()) +return ; -amdgpu_bo_unref (&uf); - } +for (i =0 ; i < parser->job->num_ibs; i++) +trace_amdgpu_cs (parser, i); } -static int amdgpu_cs_vm_handling (struct amdgpu_cs_parser *p) +static int amdgpu_cs_patch_ibs (struct amdgpu_cs_parser *p) {struct amdgpu_ring *ring =to_amdgpu_ring (p->entity->rq->sched); -struct amdgpu_fpriv *fpriv = p->filp->driver_priv; -struct amdgpu_device *adev = p->adev; -struct amdgpu_vm *vm = &fpriv->vm; -struct amdgpu_bo_list_entry *e; -struct amdgpu_bo_va *bo_va; -struct amdgpu_bo *bo; +struct amdgpu_job *job = p->job; +unsigned int i;int r;/* Only for UVD/VCE VM emulation */ -if (ring->funcs->parse_cs || ring->funcs->patch_cs_in_place) { -unsigned i, j; - -for (i =0 , j =0 ; i < p->nchunks && j < p->job->num_ibs; i++) { -struct drm_amdgpu_cs_chunk_ib *chunk_ib; -struct amdgpu_bo_va_mapping *m; -struct amdgpu_bo *aobj =NULL ; -struct amdgpu_cs_chunk *chunk; -uint64_t offset, va_start; -struct amdgpu_ib *ib; -uint8_t *kptr; - - chunk = &p->chunks[i]; - ib = &p->job->ibs[j]; - chunk_ib = chunk->kdata; - -if (chunk->chunk_id !=AMDGPU_CHUNK_ID_IB ) -continue ; - - va_start = chunk_ib->va_start &AMDGPU_GMC_HOLE_MASK ; - r =amdgpu_cs_find_mapping (p, va_start, &aobj, &m); -if (r) { -DRM_ERROR ("IB va_start is invalid\n" ); -return r; - } - -if ((va_start + chunk_ib->ib_bytes) > - (m->last +1 ) *AMDGPU_GPU_PAGE_SIZE ) { -DRM_ERROR ("IB va_start+ib_bytes is invalid\n" ); -return -EINVAL ; - } - -/* the IB should be reserved at this point */ - r =amdgpu_bo_kmap (aobj, (void **)&kptr); -if (r) { -return r; - } - - offset = m->start *AMDGPU_GPU_PAGE_SIZE ; - kptr += va_start - offset; - -if (ring->funcs->parse_cs) { -memcpy (ib->ptr, kptr, chunk_ib->ib_bytes); -amdgpu_bo_kunmap (aobj); - - r =amdgpu_ring_parse_cs (ring, p, p->job, ib); -if (r) -return r; - }else { - ib->ptr = (uint32_t *)kptr; - r =amdgpu_ring_patch_cs_in_place (ring, p, p->job, ib); -amdgpu_bo_kunmap (aobj); -if (r) -return r; - } - - j++; - } - } - -if (!p->job->vm) -return amdgpu_cs_sync_rings (p); - - - r =amdgpu_vm_clear_freed (adev, vm,NULL ); -if (r) -return r; - - r =amdgpu_vm_bo_update (adev, fpriv->prt_va, false); -if (r) -return r; - - r =amdgpu_sync_fence (&p->job->sync, fpriv->prt_va->last_pt_update); -if (r) -return r; - -if (fpriv->csa_va) { - bo_va = fpriv->csa_va; -BUG_ON (!bo_va); - r =amdgpu_vm_bo_update (adev, bo_va, false); -if (r) -return r; - - r =amdgpu_sync_fence (&p->job->sync, bo_va->last_pt_update); -if (r) -return r; - } - -amdgpu_bo_list_for_each_entry (e, p->bo_list) { -/* ignore duplicates */ - bo =ttm_to_amdgpu_bo (e->tv.bo); -if (!bo) -continue ; - - bo_va = e->bo_va; -if (bo_va ==NULL ) -continue ; - - r =amdgpu_vm_bo_update (adev, bo_va, false); -if (r) -return r; - - r =amdgpu_sync_fence (&p->job->sync, bo_va->last_pt_update); -if (r) -return r; - } - - r =amdgpu_vm_handle_moved (adev, vm); -if (r) -return r; - - r =amdgpu_vm_update_pdes (adev, vm, false); -if (r) -return r; - - r =amdgpu_sync_fence (&p->job->sync, vm->last_update); -if (r) -return r; - - p->job->vm_pd_addr =amdgpu_gmc_pd_addr (vm->root.bo); - -if (amdgpu_vm_debug) { -/* Invalidate all BOs to test for userspace bugs */ -amdgpu_bo_list_for_each_entry (e, p->bo_list) { -struct amdgpu_bo *bo =ttm_to_amdgpu_bo (e->tv.bo); - -/* ignore duplicates */ -if (!bo) -continue ; - -amdgpu_vm_bo_invalidate (adev, bo, false); - } - } - -return amdgpu_cs_sync_rings (p); -} - -static int amdgpu_cs_ib_fill (struct amdgpu_device *adev, -struct amdgpu_cs_parser *parser) -{ -struct amdgpu_fpriv *fpriv = parser->filp->driver_priv; -struct amdgpu_vm *vm = &fpriv->vm; -int r, ce_preempt =0 , de_preempt =0 ; -struct amdgpu_ring *ring; -int i, j; - -for (i =0 , j =0 ; i < parser->nchunks && j < parser->job->num_ibs; i++) { -struct amdgpu_cs_chunk *chunk; -struct amdgpu_ib *ib; -struct drm_amdgpu_cs_chunk_ib *chunk_ib; -struct drm_sched_entity *entity; - - chunk = &parser->chunks[i]; - ib = &parser->job->ibs[j]; - chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata; - -if (chunk->chunk_id !=AMDGPU_CHUNK_ID_IB ) -continue ; - -if (chunk_ib->ip_type ==AMDGPU_HW_IP_GFX && - chunk_ib->flags &AMDGPU_IB_FLAG_PREEMPT ) { -if (chunk_ib->flags &AMDGPU_IB_FLAG_CE ) - ce_preempt++; -else - de_preempt++; - -/* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */ -if (ce_preempt >1 || de_preempt >1 ) -return -EINVAL ; - } - - r =amdgpu_ctx_get_entity (parser->ctx, chunk_ib->ip_type, - chunk_ib->ip_instance, chunk_ib->ring, - &entity); -if (r) -return r; - -if (chunk_ib->flags &AMDGPU_IB_FLAG_PREAMBLE ) - parser->job->preamble_status |= -AMDGPU_PREAMBLE_IB_PRESENT ; - -if (parser->entity && parser->entity != entity) -return -EINVAL ; - -/* Return if there is no run queue associated with this entity. -* Possibly because of disabled HW IP*/ -if (entity->rq ==NULL ) -return -EINVAL ; +if (!ring->funcs->parse_cs && !ring->funcs->patch_cs_in_place) +return 0 ; - parser->entity = entity; +for (i =0 ; i < job->num_ibs; ++i) { +struct amdgpu_ib *ib = &job->ibs[i]; +struct amdgpu_bo_va_mapping *m; +struct amdgpu_bo *aobj; +uint64_t va_start; +uint8_t *kptr; - ring =to_amdgpu_ring (entity->rq->sched); - r =amdgpu_ib_get (adev, vm, ring->funcs->parse_cs ? - chunk_ib->ib_bytes :0 , -AMDGPU_IB_POOL_DELAYED , ib); + va_start = ib->gpu_addr; + r =amdgpu_cs_find_mapping (p, va_start, &aobj, &m);if (r) { -DRM_ERROR ("Failed to get ib !\n" ); +DRM_ERROR ("IB va_start is invalid\n" );return r; } - - ib->gpu_addr = chunk_ib->va_start; - ib->length_dw = chunk_ib->ib_bytes /4 ; - ib->flags = chunk_ib->flags; - - j++; - } - -/* MM engine doesn't support user fences */ - ring =to_amdgpu_ring (parser->entity->rq->sched); -if (parser->job->uf_addr && ring->funcs->no_user_fence) -return -EINVAL ; - -return 0 ; -} - -static int amdgpu_cs_process_fence_dep (struct amdgpu_cs_parser *p, -struct amdgpu_cs_chunk *chunk) -{ -struct amdgpu_fpriv *fpriv = p->filp->driver_priv; -unsigned num_deps; -int i, r; -struct drm_amdgpu_cs_chunk_dep *deps; - - deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata; - num_deps = chunk->length_dw *4 / -sizeof (struct drm_amdgpu_cs_chunk_dep ); - -for (i =0 ; i < num_deps; ++i) { -struct amdgpu_ctx *ctx; -struct drm_sched_entity *entity; -struct dma_fence *fence; - - ctx =amdgpu_ctx_get (fpriv, deps[i].ctx_id); -if (ctx ==NULL ) + +if ((va_start + ib->length_dw *4 ) > + (m->last +1 ) *AMDGPU_GPU_PAGE_SIZE ) { +DRM_ERROR ("IB va_start+ib_bytes is invalid\n" );return -EINVAL ; + } - r =amdgpu_ctx_get_entity (ctx, deps[i].ip_type, - deps[i].ip_instance, - deps[i].ring, &entity); +/* the IB should be reserved at this point */ + r =amdgpu_bo_kmap (aobj, (void **)&kptr);if (r) { -amdgpu_ctx_put (ctx);return r; } - fence =amdgpu_ctx_get_fence (ctx, entity, deps[i].handle); -amdgpu_ctx_put (ctx); - -if (IS_ERR (fence)) -return PTR_ERR (fence); -else if (!fence) -continue ; + kptr += va_start - (m->start *AMDGPU_GPU_PAGE_SIZE ); -if (chunk->chunk_id ==AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES ) { -struct drm_sched_fence *s_fence; -struct dma_fence *old = fence; +if (ring->funcs->parse_cs) { +memcpy (ib->ptr, kptr, ib->length_dw *4 ); +amdgpu_bo_kunmap (aobj); - s_fence =to_drm_sched_fence (fence); - fence =dma_fence_get (&s_fence->scheduled); -dma_fence_put (old); + r =amdgpu_ring_parse_cs (ring, p, p->job, ib); +if (r) +return r; + }else { + ib->ptr = (uint32_t *)kptr; + r =amdgpu_ring_patch_cs_in_place (ring, p, p->job, ib); +amdgpu_bo_kunmap (aobj); +if (r) +return r; } - - r =amdgpu_sync_fence (&p->job->sync, fence); -dma_fence_put (fence); -if (r) -return r; } +return 0 ; } -static int amdgpu_syncobj_lookup_and_add_to_sync (struct amdgpu_cs_parser *p, -uint32_t handle,u64 point, -u64 flags) +static int amdgpu_cs_vm_handling (struct amdgpu_cs_parser *p) { -struct dma_fence *fence; +struct amdgpu_fpriv *fpriv = p->filp->driver_priv; +struct amdgpu_device *adev = p->adev; +struct amdgpu_vm *vm = &fpriv->vm; +struct amdgpu_bo_list_entry *e; +struct amdgpu_bo_va *bo_va; +struct amdgpu_bo *bo;int r; - r =drm_syncobj_find_fence (p->filp, handle, point, flags, &fence); -if (r) { -DRM_ERROR ("syncobj %u failed to find fence @ %llu (%d)!\n" , - handle, point, r); + r =amdgpu_vm_clear_freed (adev, vm,NULL ); +if (r)return r; - } - - r =amdgpu_sync_fence (&p->job->sync, fence); -dma_fence_put (fence); -return r; -} + r =amdgpu_vm_bo_update (adev, fpriv->prt_va, false); +if (r) +return r; -static int amdgpu_cs_process_syncobj_in_dep (struct amdgpu_cs_parser *p, -struct amdgpu_cs_chunk *chunk) -{ -struct drm_amdgpu_cs_chunk_sem *deps; -unsigned num_deps; -int i, r; + r =amdgpu_sync_fence (&p->job->sync, fpriv->prt_va->last_pt_update); +if (r) +return r; - deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata; - num_deps = chunk->length_dw *4 / -sizeof (struct drm_amdgpu_cs_chunk_sem ); -for (i =0 ; i < num_deps; ++i) { - r =amdgpu_syncobj_lookup_and_add_to_sync (p, deps[i].handle, -0 ,0 ); +if (fpriv->csa_va) { + bo_va = fpriv->csa_va; + r =amdgpu_vm_bo_update (adev, bo_va, false);if (r)return r; - } - -return 0 ; -} - - -static int amdgpu_cs_process_syncobj_timeline_in_dep (struct amdgpu_cs_parser *p, -struct amdgpu_cs_chunk *chunk) -{ -struct drm_amdgpu_cs_chunk_syncobj *syncobj_deps; -unsigned num_deps; -int i, r; - syncobj_deps = (struct drm_amdgpu_cs_chunk_syncobj *)chunk->kdata; - num_deps = chunk->length_dw *4 / -sizeof (struct drm_amdgpu_cs_chunk_syncobj ); -for (i =0 ; i < num_deps; ++i) { - r =amdgpu_syncobj_lookup_and_add_to_sync (p, - syncobj_deps[i].handle, - syncobj_deps[i].point, - syncobj_deps[i].flags); + r =amdgpu_sync_fence (&p->job->sync, bo_va->last_pt_update);if (r)return r; } -return 0 ; -} - -static int amdgpu_cs_process_syncobj_out_dep (struct amdgpu_cs_parser *p, -struct amdgpu_cs_chunk *chunk) -{ -struct drm_amdgpu_cs_chunk_sem *deps; -unsigned num_deps; -int i; - - deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata; - num_deps = chunk->length_dw *4 / -sizeof (struct drm_amdgpu_cs_chunk_sem ); - -if (p->post_deps) -return -EINVAL ; - - p->post_deps =kmalloc_array (num_deps,sizeof (*p->post_deps), -GFP_KERNEL ); - p->num_post_deps =0 ; +amdgpu_bo_list_for_each_entry (e, p->bo_list) { +/* ignore duplicates */ + bo =ttm_to_amdgpu_bo (e->tv.bo); +if (!bo) +continue ; -if (!p->post_deps) -return -ENOMEM ; + bo_va = e->bo_va; +if (bo_va ==NULL ) +continue ; + r =amdgpu_vm_bo_update (adev, bo_va, false); +if (r) +return r; -for (i =0 ; i < num_deps; ++i) { - p->post_deps[i].syncobj = -drm_syncobj_find (p->filp, deps[i].handle); -if (!p->post_deps[i].syncobj) -return -EINVAL ; - p->post_deps[i].chain =NULL ; - p->post_deps[i].point =0 ; - p->num_post_deps++; + r =amdgpu_sync_fence (&p->job->sync, bo_va->last_pt_update); +if (r) +return r; } -return 0 ; -} - - -static int amdgpu_cs_process_syncobj_timeline_out_dep (struct amdgpu_cs_parser *p, -struct amdgpu_cs_chunk *chunk) -{ -struct drm_amdgpu_cs_chunk_syncobj *syncobj_deps; -unsigned num_deps; -int i; - - syncobj_deps = (struct drm_amdgpu_cs_chunk_syncobj *)chunk->kdata; - num_deps = chunk->length_dw *4 / -sizeof (struct drm_amdgpu_cs_chunk_syncobj ); + r =amdgpu_vm_handle_moved (adev, vm); +if (r) +return r; -if (p->post_deps) -return -EINVAL ; + r =amdgpu_vm_update_pdes (adev, vm, false); +if (r) +return r; - p->post_deps =kmalloc_array (num_deps,sizeof (*p->post_deps), -GFP_KERNEL ); - p->num_post_deps =0 ; + r =amdgpu_sync_fence (&p->job->sync, vm->last_update); +if (r) +return r; -if (!p->post_deps) -return -ENOMEM ; + p->job->vm_pd_addr =amdgpu_gmc_pd_addr (vm->root.bo); -for (i =0 ; i < num_deps; ++i) { -struct amdgpu_cs_post_dep *dep = &p->post_deps[i]; +if (amdgpu_vm_debug) { +/* Invalidate all BOs to test for userspace bugs */ +amdgpu_bo_list_for_each_entry (e, p->bo_list) { +struct amdgpu_bo *bo =ttm_to_amdgpu_bo (e->tv.bo); - dep->chain =NULL ; -if (syncobj_deps[i].point) { - dep->chain =dma_fence_chain_alloc (); -if (!dep->chain) -return -ENOMEM ; - } +/* ignore duplicates */ +if (!bo) +continue ; - dep->syncobj =drm_syncobj_find (p->filp, - syncobj_deps[i].handle); -if (!dep->syncobj) { -dma_fence_chain_free (dep->chain); -return -EINVAL ; +amdgpu_vm_bo_invalidate (adev, bo, false); } - dep->point = syncobj_deps[i].point; - p->num_post_deps++; }return 0 ; } -static int amdgpu_cs_dependencies (struct amdgpu_device *adev, -struct amdgpu_cs_parser *p) +static int amdgpu_cs_sync_rings (struct amdgpu_cs_parser *p) { -int i, r; - -for (i =0 ; i < p->nchunks; ++i) { -struct amdgpu_cs_chunk *chunk; +struct amdgpu_fpriv *fpriv = p->filp->driver_priv; +struct amdgpu_bo_list_entry *e; +int r; - chunk = &p->chunks[i]; +list_for_each_entry (e, &p->validated, tv.head) { +struct amdgpu_bo *bo =ttm_to_amdgpu_bo (e->tv.bo); +struct dma_resv *resv = bo->tbo.base.resv; +enum amdgpu_sync_mode sync_mode; -switch (chunk->chunk_id) { -case AMDGPU_CHUNK_ID_DEPENDENCIES : -case AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES : - r =amdgpu_cs_process_fence_dep (p, chunk); -if (r) -return r; -break ; -case AMDGPU_CHUNK_ID_SYNCOBJ_IN : - r =amdgpu_cs_process_syncobj_in_dep (p, chunk); -if (r) -return r; -break ; -case AMDGPU_CHUNK_ID_SYNCOBJ_OUT : - r =amdgpu_cs_process_syncobj_out_dep (p, chunk); -if (r) -return r; -break ; -case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_WAIT : - r =amdgpu_cs_process_syncobj_timeline_in_dep (p, chunk); -if (r) -return r; -break ; -case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_SIGNAL : - r =amdgpu_cs_process_syncobj_timeline_out_dep (p, chunk); -if (r) -return r; -break ; - } + sync_mode =amdgpu_bo_explicit_sync (bo) ? +AMDGPU_SYNC_EXPLICIT :AMDGPU_SYNC_NE_OWNER ; + r =amdgpu_sync_resv (p->adev, &p->job->sync, resv, sync_mode, + &fpriv->vm); +if (r) +return r; } -return 0 ; }
@@ -1203,7 +1171,7 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, job = p->job; p->job =NULL ; - r =drm_sched_job_init (&job->base, entity, &fpriv->vm); + r =drm_sched_job_init (&job->base, p->entity, &fpriv->vm);if (r) goto error_unlock;
@@ -1269,23 +1237,41 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,return r; } -static void trace_amdgpu_cs_ibs (struct amdgpu_cs_parser *parser) +/* Cleanup the parser structure */ +static void amdgpu_cs_parser_fini (struct amdgpu_cs_parser *parser) { -int i; +unsigned i; -if (!trace_amdgpu_cs_enabled ()) -return ; +for (i =0 ; i < parser->num_post_deps; i++) { +drm_syncobj_put (parser->post_deps[i].syncobj); +kfree (parser->post_deps[i].chain); + } +kfree (parser->post_deps); -for (i =0 ; i < parser->job->num_ibs; i++) -trace_amdgpu_cs (parser, i); +dma_fence_put (parser->fence); + +if (parser->ctx) { +amdgpu_ctx_put (parser->ctx); + } +if (parser->bo_list) +amdgpu_bo_list_put (parser->bo_list); + +for (i =0 ; i < parser->nchunks; i++) +kvfree (parser->chunks[i].kdata); +kvfree (parser->chunks); +if (parser->job) +amdgpu_job_free (parser->job); +if (parser->uf_entry.tv.bo) { +struct amdgpu_bo *uf =ttm_to_amdgpu_bo (parser->uf_entry.tv.bo); + +amdgpu_bo_unref (&uf); + } }int amdgpu_cs_ioctl (struct drm_device *dev,void *data,struct drm_file *filp) {struct amdgpu_device *adev =drm_to_adev (dev); -union drm_amdgpu_cs *cs = data; -struct amdgpu_cs_parser parser = {}; -bool reserved_buffers = false; +struct amdgpu_cs_parser parser;int r;if (amdgpu_ras_intr_triggered ())
@@ -1294,25 +1280,20 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)if (!adev->accel_working)return -EBUSY ; - parser.adev = adev; - parser.filp = filp; - - r =amdgpu_cs_parser_init (&parser, data); + r =amdgpu_cs_parser_init (&parser, adev, filp, data);if (r) {if (printk_ratelimit ())DRM_ERROR ("Failed to initialize parser %d!\n" , r); - goto out; +return r; } - r =amdgpu_cs_ib_fill (adev, &parser); + r =amdgpu_cs_pass1 (&parser, data);if (r) - goto out; + goto error_fini; - r =amdgpu_cs_dependencies (adev, &parser); -if (r) { -DRM_ERROR ("Failed in the dependencies handling %d!\n" , r); - goto out; - } + r =amdgpu_cs_pass2 (&parser); +if (r) + goto error_fini; r =amdgpu_cs_parser_bos (&parser, data);if (r) {
@@ -1320,21 +1301,36 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)DRM_ERROR ("Not enough memory for command submission!\n" ); else if (r != -ERESTARTSYS && r != -EAGAIN)DRM_ERROR ("Failed to process the buffer list %d!\n" , r); - goto out; + goto error_fini; } - reserved_buffers = true; + r =amdgpu_cs_patch_ibs (&parser); +if (r) + goto error_backoff; + + r =amdgpu_cs_vm_handling (&parser); +if (r) + goto error_backoff; + + r =amdgpu_cs_sync_rings (&parser); +if (r) + goto error_backoff;trace_amdgpu_cs_ibs (&parser); - r =amdgpu_cs_vm_handling (&parser); + r =amdgpu_cs_submit (&parser, data);if (r) - goto out; + goto error_backoff; - r =amdgpu_cs_submit (&parser, cs); -out: -amdgpu_cs_parser_fini (&parser, r, reserved_buffers); +amdgpu_cs_parser_fini (&parser); +return 0 ; + +error_backoff: +ttm_eu_backoff_reservation (&parser.ticket, &parser.validated); +mutex_unlock (&parser.bo_list->bo_list_mutex); +error_fini: +amdgpu_cs_parser_fini (&parser);return r; }
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h +1 -1
@@ -51,8 +51,8 @@ struct amdgpu_cs_parser {struct amdgpu_cs_chunk *chunks;/* scheduler job object */ -struct amdgpu_job *job;struct drm_sched_entity *entity; +struct amdgpu_job *job;/* buffer objects */ struct ww_acquire_ctx ticket;
Signature · 1 line
2.25.1
Christian König
reviewerPATCH 7/12 drm/amdgpu: move entity selection and job init earlier during CS
In reply to Christian KönigInitialize the entity for the CS and scheduler job much earlier.
v2: fix job initialisation order and use correct scheduler instance
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 54 ++++++++++++------------- drivers/gpu/drm/amd/amdgpu/amdgpu_job.h | 5 +++ 2 files changed, 30 insertions(+), 29 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +25 -29
@@ -68,6 +68,25 @@ static int amdgpu_cs_p1_ib(struct amdgpu_cs_parser *p,struct drm_amdgpu_cs_chunk_ib *chunk_ib, unsigned int *num_ibs) { + struct drm_sched_entity *entity; +int r; + + r =amdgpu_ctx_get_entity (p->ctx, chunk_ib->ip_type, + chunk_ib->ip_instance, + chunk_ib->ring, &entity); +if (r) +return r; + +/* Abort if there is no run queue associated with this entity. +* Possibly because of disabled HW IP*/ +if (entity->rq ==NULL ) +return -EINVAL ; + +/* Currently we don't support submitting to multiple entities */ +if (p->entity && p->entity != entity) +return -EINVAL ; + + p->entity = entity; ++(*num_ibs);return 0 ; }
@@ -250,6 +269,10 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,if (ret) goto free_all_kdata; + ret =drm_sched_job_init (&p->job->base, p->entity, &fpriv->vm); +if (ret) + goto free_all_kdata; +if (p->ctx->vram_lost_counter != p->job->vram_lost_counter) { ret = -ECANCELED ; goto free_all_kdata;
@@ -286,32 +309,11 @@ static int amdgpu_cs_p2_ib(struct amdgpu_cs_parser *p, {struct drm_amdgpu_cs_chunk_ib *chunk_ib = chunk->kdata;struct amdgpu_fpriv *fpriv = p->filp->driver_priv; +struct amdgpu_ring *ring =amdgpu_job_ring (p->job);struct amdgpu_ib *ib = &p->job->ibs[*num_ibs];struct amdgpu_vm *vm = &fpriv->vm; -struct drm_sched_entity *entity; -struct amdgpu_ring *ring;int r; - r =amdgpu_ctx_get_entity (p->ctx, chunk_ib->ip_type, - chunk_ib->ip_instance, - chunk_ib->ring, &entity); -if (r) -return r; - -/* -* Abort if there is no run queue associated with this entity. -* Possibly because of disabled HW IP. -*/ -if (entity->rq ==NULL ) -return -EINVAL ; - -/* Currently we don't support submitting to multiple entities */ -if (p->entity && p->entity != entity) -return -EINVAL ; - - p->entity = entity; - - ring =to_amdgpu_ring (entity->rq->sched);/* MM engine doesn't support user fences */ if (p->job->uf_addr && ring->funcs->no_user_fence)return -EINVAL ;
@@ -982,8 +984,8 @@ static void trace_amdgpu_cs_ibs(struct amdgpu_cs_parser *parser)static int amdgpu_cs_patch_ibs (struct amdgpu_cs_parser *p) { -struct amdgpu_ring *ring =to_amdgpu_ring (p->entity->rq->sched);struct amdgpu_job *job = p->job; +struct amdgpu_ring *ring =amdgpu_job_ring (job);unsigned int i;int r;
@@ -1171,10 +1173,6 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, job = p->job; p->job =NULL ; - r =drm_sched_job_init (&job->base, p->entity, &fpriv->vm); -if (r) - goto error_unlock; -drm_sched_job_arm (&job->base);/* No memory allocation is allowed while holding the notifier lock.
@@ -1231,8 +1229,6 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, error_abort:drm_sched_job_cleanup (&job->base);mutex_unlock (&p->adev->notifier_lock); - -error_unlock:amdgpu_job_free (job);return r; }
drivers/gpu/drm/amd/amdgpu/amdgpu_job.h +5 -0
@@ -72,6 +72,11 @@ struct amdgpu_job {struct amdgpu_ib ibs[]; }; +static inline struct amdgpu_ring *amdgpu_job_ring (struct amdgpu_job *job) +{ +return to_amdgpu_ring (job->base.entity->rq->sched); +} +int amdgpu_job_alloc (struct amdgpu_device *adev,unsigned num_ibs,struct amdgpu_job **job,struct amdgpu_vm *vm); int amdgpu_job_alloc_with_ib(struct amdgpu_device *adev, unsigned size,
Signature · 1 line
2.25.1
Christian König
reviewerPATCH 8/12 drm/amdgpu: revert "fix limiting AV1 to the first instance on VCN3"
In reply to Christian KönigThis reverts commit 250195ff744f260c169f5427422b6f39c58cb883.
The job should now be initialized when we reach the parser functions.
--- drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c +10 -7
@@ -1761,21 +1761,23 @@ static const struct amdgpu_ring_funcs vcn_v3_0_dec_sw_ring_vm_funcs = { .emit_reg_write_reg_wait = amdgpu_ring_emit_reg_write_reg_wait_helper, }; -static int vcn_v3_0_limit_sched (struct amdgpu_cs_parser *p) +static int vcn_v3_0_limit_sched (struct amdgpu_cs_parser *p, +struct amdgpu_job *job) {struct drm_gpu_scheduler **scheds;/* The create msg must be in the first IB submitted */ -if (atomic_read (&p->entity->fence_seq)) +if (atomic_read (&job->base.entity->fence_seq))return -EINVAL ; scheds = p->adev->gpu_sched[AMDGPU_HW_IP_VCN_DEC ] [AMDGPU_RING_PRIO_DEFAULT ].sched; -drm_sched_entity_modify_sched (p->entity, scheds,1 ); +drm_sched_entity_modify_sched (job->base.entity, scheds,1 );return 0 ; } -static int vcn_v3_0_dec_msg (struct amdgpu_cs_parser *p,uint64_t addr) +static int vcn_v3_0_dec_msg (struct amdgpu_cs_parser *p,struct amdgpu_job *job, +uint64_t addr) {struct ttm_operation_ctx ctx = { false, false };struct amdgpu_bo_va_mapping *map;
@@ -1846,7 +1848,7 @@ static int vcn_v3_0_dec_msg(struct amdgpu_cs_parser *p, uint64_t addr)if (create[0 ] ==0x7 || create[0 ] ==0x10 || create[0 ] ==0x11 )continue ; - r =vcn_v3_0_limit_sched (p); + r =vcn_v3_0_limit_sched (p, job);if (r) goto out; }
@@ -1860,7 +1862,7 @@ static int vcn_v3_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,struct amdgpu_job *job, struct amdgpu_ib *ib) { - struct amdgpu_ring *ring = to_amdgpu_ring(p->entity->rq->sched); + struct amdgpu_ring *ring = to_amdgpu_ring(job->base.sched);uint32_t msg_lo =0 , msg_hi =0 ;unsigned i;int r;
@@ -1879,7 +1881,8 @@ static int vcn_v3_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p, msg_hi = val; }else if (reg ==PACKET0 (p->adev->vcn.internal.cmd,0 ) && val ==0 ) { - r =vcn_v3_0_dec_msg (p, ((u64 )msg_hi) <<32 | msg_lo); + r =vcn_v3_0_dec_msg (p, job, + ((u64 )msg_hi) <<32 | msg_lo);if (r)return r; }
Signature · 1 line
2.25.1
Christian König
reviewerPATCH 9/12 drm/amdgpu: cleanup instance limit on VCN4
In reply to Christian KönigSimilar to what we did for VCN3 use the job instead of the parser entity. Cleanup the coding style quite a bit as well.
--- drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c | 46 +++++++++++++++------------ 1 file changed, 25 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c +25 -21
@@ -1327,21 +1327,23 @@ static void vcn_v4_0_unified_ring_set_wptr(struct amdgpu_ring *ring) } } -static int vcn_v4_0_limit_sched (struct amdgpu_cs_parser *p) +static int vcn_v4_0_limit_sched (struct amdgpu_cs_parser *p, +struct amdgpu_job *job) {struct drm_gpu_scheduler **scheds;/* The create msg must be in the first IB submitted */ -if (atomic_read (&p->entity->fence_seq)) +if (atomic_read (&job->base.entity->fence_seq))return -EINVAL ; - scheds = p->adev->gpu_sched[AMDGPU_HW_IP_VCN_ENC ] - [AMDGPU_RING_PRIO_0 ].sched; -drm_sched_entity_modify_sched (p->entity, scheds,1 ); + scheds = p->adev->gpu_sched[AMDGPU_HW_IP_VCN_DEC ] + [AMDGPU_RING_PRIO_DEFAULT ].sched; +drm_sched_entity_modify_sched (job->base.entity, scheds,1 );return 0 ; } -static int vcn_v4_0_dec_msg (struct amdgpu_cs_parser *p,uint64_t addr) +static int vcn_v4_0_dec_msg (struct amdgpu_cs_parser *p,struct amdgpu_job *job, +uint64_t addr) {struct ttm_operation_ctx ctx = { false, false };struct amdgpu_bo_va_mapping *map;
@@ -1412,7 +1414,7 @@ static int vcn_v4_0_dec_msg(struct amdgpu_cs_parser *p, uint64_t addr)if (create[0 ] ==0x7 || create[0 ] ==0x10 || create[0 ] ==0x11 )continue ; - r =vcn_v4_0_limit_sched (p); + r =vcn_v4_0_limit_sched (p, job);if (r) goto out; }
@@ -1425,32 +1427,34 @@ static int vcn_v4_0_dec_msg(struct amdgpu_cs_parser *p, uint64_t addr)#define RADEON_VCN_ENGINE_TYPE_DECODE (0x00000003)static int vcn_v4_0_ring_patch_cs_in_place (struct amdgpu_cs_parser *p, -struct amdgpu_job *job, -struct amdgpu_ib *ib) +struct amdgpu_job *job, +struct amdgpu_ib *ib) { -struct amdgpu_ring *ring =to_amdgpu_ring (p->entity->rq->sched); -struct amdgpu_vcn_decode_buffer *decode_buffer =NULL ; +struct amdgpu_ring *ring =to_amdgpu_ring (job->base.entity->rq->sched); +struct amdgpu_vcn_decode_buffer *decode_buffer; +uint64_t addr;uint32_t val; -int r =0 ;/* The first instance can decode anything */ if (!ring->me) -return r; +return 0 ;/* unified queue ib header has 8 double words. */ if (ib->length_dw <8 ) -return r; +return 0 ; val =amdgpu_ib_get_value (ib,6 );//RADEON_VCN_ENGINE_TYPE +if (val !=RADEON_VCN_ENGINE_TYPE_DECODE ) +return 0 ; -if (val ==RADEON_VCN_ENGINE_TYPE_DECODE ) { - decode_buffer = (struct amdgpu_vcn_decode_buffer *)&ib->ptr[10 ]; + decode_buffer = (struct amdgpu_vcn_decode_buffer *)&ib->ptr[10 ]; -if (decode_buffer->valid_buf_flag &0x1 ) - r =vcn_v4_0_dec_msg (p, ((u64 )decode_buffer->msg_buffer_address_hi) <<32 | - decode_buffer->msg_buffer_address_lo); - } -return r; +if (!(decode_buffer->valid_buf_flag &0x1 )) +return 0 ; + + addr = ((u64 )decode_buffer->msg_buffer_address_hi) <<32 | + decode_buffer->msg_buffer_address_lo; +return vcn_v4_0_dec_msg (p, job, addr); }static const struct amdgpu_ring_funcs vcn_v4_0_unified_ring_vm_funcs = {
Signature · 1 line
2.25.1
Christian König
reviewerPATCH 10/12 drm/amdgpu: add gang submit backend v2
In reply to Christian KönigAllows submitting jobs as gang which needs to run on multiple engines at the same time.
Basic idea is that we have a global gang submit fence representing when the gang leader is finally pushed to run on the hardware last.
Jobs submitted as gang are never re-submitted in case of a GPU reset since this won't work and will just deadlock the hardware immediately again.
v2: fix logic inversion, improve documentation, fix rcu
--- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 3 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 35 ++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 28 +++++++++++++++-- drivers/gpu/drm/amd/amdgpu/amdgpu_job.h | 3 ++ 4 files changed, 67 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +3 -0
@@ -885,6 +885,7 @@ struct amdgpu_device {u64 fence_context;unsigned num_rings;struct amdgpu_ring *rings[AMDGPU_MAX_RINGS ]; +struct dma_fence __rcu *gang_submit;bool ib_pool_ready;struct amdgpu_sa_manager ib_pools[AMDGPU_IB_POOL_MAX ];struct amdgpu_sched gpu_sched[AMDGPU_HW_IP_NUM ][AMDGPU_RING_PRIO_MAX ];
@@ -1294,6 +1295,8 @@ u32 amdgpu_device_pcie_port_rreg(struct amdgpu_device *adev, u32 reg);void amdgpu_device_pcie_port_wreg (struct amdgpu_device *adev,u32 reg,u32 v); +struct dma_fence *amdgpu_device_switch_gang (struct amdgpu_device *adev, +struct dma_fence *gang);/* atpx handler */ #if defined(CONFIG_VGA_SWITCHEROO )
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +35 -0
@@ -3501,6 +3501,7 @@ int amdgpu_device_init(struct amdgpu_device *adev, adev->gmc.gart_size =512 *1024 *1024 ; adev->accel_working = false; adev->num_rings =0 ; +RCU_INIT_POINTER (adev->gang_submit,dma_fence_get_stub ()); adev->mman.buffer_funcs =NULL ; adev->mman.buffer_funcs_ring =NULL ; adev->vm_manager.vm_pte_funcs =NULL ;
@@ -3983,6 +3984,7 @@ void amdgpu_device_fini_sw(struct amdgpu_device *adev)release_firmware (adev->firmware.gpu_info_fw); adev->firmware.gpu_info_fw =NULL ; adev->accel_working = false; +dma_fence_put (rcu_dereference_protected (adev->gang_submit, true));amdgpu_reset_fini (adev);
@@ -5916,3 +5918,36 @@ void amdgpu_device_pcie_port_wreg(struct amdgpu_device *adev, (void )RREG32 (data);spin_unlock_irqrestore (&adev->pcie_idx_lock, flags); } + +/** +* amdgpu_device_switch_gang - switch to a new gang +* @adev: amdgpu_device pointer +* @gang: the gang to switch to +* +* Try to switch to a new gang. +* Returns: NULL if we switched to the new gang or a reference to the current +* gang leader. +*/ +struct dma_fence *amdgpu_device_switch_gang (struct amdgpu_device *adev, +struct dma_fence *gang) +{ +struct dma_fence *old =NULL ; + +do { +dma_fence_put (old); +rcu_read_lock (); + old =dma_fence_get_rcu_safe (&adev->gang_submit); +rcu_read_unlock (); + +if (old == gang) +break ; + +if (!dma_fence_is_signaled (old)) +return old; + + }while (cmpxchg ((struct dma_fence __force **)&adev->gang_submit, + old, gang) != old); + +dma_fence_put (old); +return NULL ; +}
drivers/gpu/drm/amd/amdgpu/amdgpu_job.c +26 -2
@@ -173,11 +173,29 @@ static void amdgpu_job_free_cb(struct drm_sched_job *s_job)dma_fence_put (&job->hw_fence); } +void amdgpu_job_set_gang_leader (struct amdgpu_job *job, +struct amdgpu_job *leader) +{ +struct dma_fence *fence = &leader->base.s_fence->scheduled; + +WARN_ON (job->gang_submit); + +/* +* Don't add a reference when we are the gang leader to avoid circle +* dependency. +*/ +if (job != leader) +dma_fence_get (fence); + job->gang_submit = fence; +} +void amdgpu_job_free (struct amdgpu_job *job) {amdgpu_job_free_resources (job);amdgpu_sync_free (&job->sync);amdgpu_sync_free (&job->sched_sync); +if (job->gang_submit != &job->base.s_fence->scheduled) +dma_fence_put (job->gang_submit);dma_fence_put (&job->hw_fence); }
@@ -244,12 +262,16 @@ static struct dma_fence *amdgpu_job_dependency(struct drm_sched_job *sched_job, fence =amdgpu_sync_get_fence (&job->sync); } +if (!fence && job->gang_submit) + fence =amdgpu_device_switch_gang (ring->adev, job->gang_submit); +return fence; }static struct dma_fence *amdgpu_job_run (struct drm_sched_job *sched_job) {struct amdgpu_ring *ring =to_amdgpu_ring (sched_job->sched); +struct amdgpu_device *adev = ring->adev;struct dma_fence *fence =NULL , *finished;struct amdgpu_job *job;int r =0 ;
@@ -261,8 +283,10 @@ static struct dma_fence *amdgpu_job_run(struct drm_sched_job *sched_job)trace_amdgpu_sched_run_job (job); -if (job->vram_lost_counter !=atomic_read (&ring->adev->vram_lost_counter)) -dma_fence_set_error (finished, -ECANCELED );/* skip IB as well if VRAM lost */ +/* Skip job if VRAM is lost and never resubmit gangs */ +if (job->vram_lost_counter !=atomic_read (&adev->vram_lost_counter) || + (job->job_run_counter && job->gang_submit)) +dma_fence_set_error (finished, -ECANCELED );if (finished->error <0 ) {DRM_INFO ("Skip scheduling IBs!\n" );
drivers/gpu/drm/amd/amdgpu/amdgpu_job.h +3 -0
@@ -50,6 +50,7 @@ struct amdgpu_job {struct amdgpu_sync sync;struct amdgpu_sync sched_sync;struct dma_fence hw_fence; +struct dma_fence *gang_submit;uint32_t preamble_status;uint32_t preemption_status;bool vm_needs_flush;
@@ -84,6 +85,8 @@ int amdgpu_job_alloc_with_ib(struct amdgpu_device *adev, unsigned size,void amdgpu_job_set_resources (struct amdgpu_job *job,struct amdgpu_bo *gds,struct amdgpu_bo *gws,struct amdgpu_bo *oa);void amdgpu_job_free_resources (struct amdgpu_job *job); +void amdgpu_job_set_gang_leader (struct amdgpu_job *job, +struct amdgpu_job *leader);void amdgpu_job_free (struct amdgpu_job *job);int amdgpu_job_submit (struct amdgpu_job *job,struct drm_sched_entity *entity,void *owner,struct dma_fence **f);
Signature · 1 line
2.25.1
Christian König
reviewerPATCH 11/12 drm/amdgpu: add gang submit frontend v4
In reply to Christian KönigAllows submitting jobs as gang which needs to run on multiple engines at the same time.
All members of the gang get the same implicit, explicit and VM dependencies. So no gang member will start running until everything else is ready.
The last job is considered the gang leader (usually a submission to the GFX ring) and used for signaling output dependencies.
Each job is remembered individually as user of a buffer object, so there is no joining of work at the end.
v2: rebase and fix review comments from Andrey and Yogesh
v3: use READ instead of BOOKKEEP for now because of VM unmaps, set gang
leader only when necessary
v4: fix order of pushing jobs and adding fences found by Trigger.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 258 ++++++++++++++-------- drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h | 10 +- drivers/gpu/drm/amd/amdgpu/amdgpu_trace.h | 12 +- 3 files changed, 184 insertions(+), 96 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +170 -88
@@ -69,6 +69,7 @@ static int amdgpu_cs_p1_ib(struct amdgpu_cs_parser *p, unsigned int *num_ibs) {struct drm_sched_entity *entity; +unsigned int i;int r; r = amdgpu_ctx_get_entity(p->ctx, chunk_ib->ip_type,
@@ -77,17 +78,28 @@ static int amdgpu_cs_p1_ib(struct amdgpu_cs_parser *p,if (r)return r; -/* Abort if there is no run queue associated with this entity. -* Possibly because of disabled HW IP*/ +/* +* Abort if there is no run queue associated with this entity. +* Possibly because of disabled HW IP. +*/ if (entity->rq ==NULL )return -EINVAL ; -/* Currently we don't support submitting to multiple entities */ -if (p->entity && p->entity != entity) +/* Check if we can add this IB to some existing job */ +for (i =0 ; i < p->gang_size; ++i) { +if (p->entities[i] == entity) + goto found; + } + +/* If not increase the gang size if possible */ +if (i ==AMDGPU_CS_GANG_SIZE )return -EINVAL ; - p->entity = entity; - ++(*num_ibs); + p->entities[i] = entity; + p->gang_size = i +1 ; + +found: + ++(num_ibs[i]);return 0 ; }
@@ -161,11 +173,12 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p, union drm_amdgpu_cs *cs) {struct amdgpu_fpriv *fpriv = p->filp->driver_priv; +unsigned int num_ibs[AMDGPU_CS_GANG_SIZE ] = { };struct amdgpu_vm *vm = &fpriv->vm;uint64_t *chunk_array_user;uint64_t *chunk_array; -unsigned size, num_ibs =0 ;uint32_t uf_offset =0 ; +unsigned int size;int ret;int i;
@@ -228,7 +241,7 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,if (size <sizeof (struct drm_amdgpu_cs_chunk_ib )) goto free_partial_kdata; - ret =amdgpu_cs_p1_ib (p, p->chunks[i].kdata, &num_ibs); + ret =amdgpu_cs_p1_ib (p, p->chunks[i].kdata, num_ibs);if (ret) goto free_partial_kdata;break ;
@@ -265,21 +278,28 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p, } } - ret =amdgpu_job_alloc (p->adev, num_ibs, &p->job, vm); -if (ret) - goto free_all_kdata; +if (!p->gang_size) +return -EINVAL ; - ret =drm_sched_job_init (&p->job->base, p->entity, &fpriv->vm); -if (ret) - goto free_all_kdata; +for (i =0 ; i < p->gang_size; ++i) { + ret =amdgpu_job_alloc (p->adev, num_ibs[i], &p->jobs[i], vm); +if (ret) + goto free_all_kdata; + + ret =drm_sched_job_init (&p->jobs[i]->base, p->entities[i], + &fpriv->vm); +if (ret) + goto free_all_kdata; + } + p->gang_leader = p->jobs[p->gang_size -1 ]; -if (p->ctx->vram_lost_counter != p->job->vram_lost_counter) { +if (p->ctx->vram_lost_counter != p->gang_leader->vram_lost_counter) { ret = -ECANCELED ; goto free_all_kdata; }if (p->uf_entry.tv.bo) - p->job->uf_addr = uf_offset; + p->gang_leader->uf_addr = uf_offset;kvfree (chunk_array);/* Use this opportunity to fill in task info for the vm */
@@ -301,21 +321,18 @@ static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,return ret; } -static int amdgpu_cs_p2_ib (struct amdgpu_cs_parser *p, -struct amdgpu_cs_chunk *chunk, -unsigned int *num_ibs, -unsigned int *ce_preempt, -unsigned int *de_preempt) +static int amdgpu_cs_p2_ib (struct amdgpu_cs_parser *p,struct amdgpu_job *job, +struct amdgpu_ib *ib,struct amdgpu_cs_chunk *chunk, +unsigned int *ce_preempt,unsigned int *de_preempt) {struct drm_amdgpu_cs_chunk_ib *chunk_ib = chunk->kdata;struct amdgpu_fpriv *fpriv = p->filp->driver_priv; -struct amdgpu_ring *ring =amdgpu_job_ring (p->job); -struct amdgpu_ib *ib = &p->job->ibs[*num_ibs]; +struct amdgpu_ring *ring =amdgpu_job_ring (job);struct amdgpu_vm *vm = &fpriv->vm;int r;/* MM engine doesn't support user fences */ -if (p->job->uf_addr && ring->funcs->no_user_fence) +if (job->uf_addr && ring->funcs->no_user_fence)return -EINVAL ; if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX &&
@@ -332,7 +349,7 @@ static int amdgpu_cs_p2_ib(struct amdgpu_cs_parser *p, }if (chunk_ib->flags &AMDGPU_IB_FLAG_PREAMBLE ) - p->job->preamble_status |=AMDGPU_PREAMBLE_IB_PRESENT ; + job->preamble_status |=AMDGPU_PREAMBLE_IB_PRESENT ; r = amdgpu_ib_get(p->adev, vm, ring->funcs->parse_cs ? chunk_ib->ib_bytes : 0,
@@ -345,8 +362,6 @@ static int amdgpu_cs_p2_ib(struct amdgpu_cs_parser *p, ib->gpu_addr = chunk_ib->va_start; ib->length_dw = chunk_ib->ib_bytes /4 ; ib->flags = chunk_ib->flags; - - (*num_ibs)++;return 0 ; }
@@ -395,7 +410,7 @@ static int amdgpu_cs_p2_dependencies(struct amdgpu_cs_parser *p,dma_fence_put (old); } - r =amdgpu_sync_fence (&p->job->sync, fence); + r =amdgpu_sync_fence (&p->gang_leader->sync, fence);dma_fence_put (fence);if (r)return r;
@@ -417,7 +432,7 @@ static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p,return r; } - r =amdgpu_sync_fence (&p->job->sync, fence); + r =amdgpu_sync_fence (&p->gang_leader->sync, fence);dma_fence_put (fence);return r;
@@ -540,20 +555,30 @@ static int amdgpu_cs_p2_syncobj_timeline_signal(struct amdgpu_cs_parser *p,static int amdgpu_cs_pass2 (struct amdgpu_cs_parser *p) { -unsigned int num_ibs =0 , ce_preempt =0 , de_preempt =0 ; +unsigned int ce_preempt =0 , de_preempt =0 ; +unsigned int job_idx =0 , ib_idx =0 ;int i, r;for (i =0 ; i < p->nchunks; ++i) {struct amdgpu_cs_chunk *chunk; +struct amdgpu_job *job; chunk = &p->chunks[i];switch (chunk->chunk_id) {case AMDGPU_CHUNK_ID_IB : - r =amdgpu_cs_p2_ib (p, chunk, &num_ibs, + job = p->jobs[job_idx]; + r =amdgpu_cs_p2_ib (p, job, &job->ibs[ib_idx], chunk, &ce_preempt, &de_preempt);if (r)return r; + +if (++ib_idx == job->num_ibs) { + ++job_idx; + ib_idx =0 ; + ce_preempt =0 ; + de_preempt =0 ; + }break ;case AMDGPU_CHUNK_ID_DEPENDENCIES :case AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES :
@@ -824,6 +849,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,struct amdgpu_vm *vm = &fpriv->vm;struct amdgpu_bo_list_entry *e;struct list_head duplicates; +unsigned int i;int r;INIT_LIST_HEAD (&p->validated);
@@ -907,16 +933,6 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, e->bo_va =amdgpu_vm_bo_find (vm, bo); } -/* Move fence waiting after getting reservation lock of -* PD root. Then there is no need on a ctx mutex lock. -*/ - r =amdgpu_ctx_wait_prev_fence (p->ctx, p->entity); -if (unlikely (r !=0 )) { -if (r != -ERESTARTSYS ) -DRM_ERROR ("amdgpu_ctx_wait_prev_fence failed.\n" ); - goto error_validate; - } -amdgpu_cs_get_threshold_for_moves (p->adev, &p->bytes_moved_threshold, &p->bytes_moved_vis_threshold); p->bytes_moved =0 ;
@@ -940,8 +956,10 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,amdgpu_cs_report_moved_bytes (p->adev, p->bytes_moved, p->bytes_moved_vis); -amdgpu_job_set_resources (p->job, p->bo_list->gds_obj, - p->bo_list->gws_obj, p->bo_list->oa_obj); +for (i =0 ; i < p->gang_size; ++i) +amdgpu_job_set_resources (p->jobs[i], p->bo_list->gds_obj, + p->bo_list->gws_obj, + p->bo_list->oa_obj);if (p->uf_entry.tv.bo) {struct amdgpu_bo *uf =ttm_to_amdgpu_bo (p->uf_entry.tv.bo);
@@ -950,7 +968,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,if (r) goto error_validate; - p->job->uf_addr +=amdgpu_bo_gpu_offset (uf); + p->gang_leader->uf_addr +=amdgpu_bo_gpu_offset (uf); }return 0 ;
@@ -971,20 +989,24 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,return r; } -static void trace_amdgpu_cs_ibs (struct amdgpu_cs_parser *parser) +static void trace_amdgpu_cs_ibs (struct amdgpu_cs_parser *p) { -int i; +int i, j;if (!trace_amdgpu_cs_enabled ())return ; -for (i =0 ; i < parser->job->num_ibs; i++) -trace_amdgpu_cs (parser, i); +for (i =0 ; i < p->gang_size; ++i) { +struct amdgpu_job *job = p->jobs[i]; + +for (j =0 ; j < job->num_ibs; ++j) +trace_amdgpu_cs (p, job, &job->ibs[j]); + } } -static int amdgpu_cs_patch_ibs (struct amdgpu_cs_parser *p) +static int amdgpu_cs_patch_ibs (struct amdgpu_cs_parser *p, +struct amdgpu_job *job) { -struct amdgpu_job *job = p->job;struct amdgpu_ring *ring =amdgpu_job_ring (job);unsigned int i;int r;
@@ -1025,12 +1047,12 @@ static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p)memcpy (ib->ptr, kptr, ib->length_dw *4 );amdgpu_bo_kunmap (aobj); - r =amdgpu_ring_parse_cs (ring, p, p->job, ib); + r =amdgpu_ring_parse_cs (ring, p, job, ib);if (r)return r; }else { ib->ptr = (uint32_t *)kptr; - r =amdgpu_ring_patch_cs_in_place (ring, p, p->job, ib); + r =amdgpu_ring_patch_cs_in_place (ring, p, job, ib);amdgpu_bo_kunmap (aobj);if (r)return r;
@@ -1040,14 +1062,29 @@ static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p)return 0 ; } +static int amdgpu_cs_patch_jobs (struct amdgpu_cs_parser *p) +{ +unsigned int i; +int r; + +for (i =0 ; i < p->gang_size; ++i) { + r =amdgpu_cs_patch_ibs (p, p->jobs[i]); +if (r) +return r; + } +return 0 ; +} +static int amdgpu_cs_vm_handling (struct amdgpu_cs_parser *p) {struct amdgpu_fpriv *fpriv = p->filp->driver_priv; +struct amdgpu_job *job = p->gang_leader;struct amdgpu_device *adev = p->adev;struct amdgpu_vm *vm = &fpriv->vm;struct amdgpu_bo_list_entry *e;struct amdgpu_bo_va *bo_va;struct amdgpu_bo *bo; +unsigned int i;int r; r =amdgpu_vm_clear_freed (adev, vm,NULL );
@@ -1058,7 +1095,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)if (r)return r; - r =amdgpu_sync_fence (&p->job->sync, fpriv->prt_va->last_pt_update); + r =amdgpu_sync_fence (&job->sync, fpriv->prt_va->last_pt_update);if (r)return r;
@@ -1068,7 +1105,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)if (r)return r; - r =amdgpu_sync_fence (&p->job->sync, bo_va->last_pt_update); + r =amdgpu_sync_fence (&job->sync, bo_va->last_pt_update);if (r)return r; }
@@ -1087,7 +1124,7 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)if (r)return r; - r =amdgpu_sync_fence (&p->job->sync, bo_va->last_pt_update); + r =amdgpu_sync_fence (&job->sync, bo_va->last_pt_update);if (r)return r; }
@@ -1100,11 +1137,18 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)if (r)return r; - r =amdgpu_sync_fence (&p->job->sync, vm->last_update); + r =amdgpu_sync_fence (&job->sync, vm->last_update);if (r)return r; - p->job->vm_pd_addr =amdgpu_gmc_pd_addr (vm->root.bo); +for (i =0 ; i < p->gang_size; ++i) { + job = p->jobs[i]; + +if (!job->vm) +continue ; + + job->vm_pd_addr =amdgpu_gmc_pd_addr (vm->root.bo); + }if (amdgpu_vm_debug) {/* Invalidate all BOs to test for userspace bugs */
@@ -1125,7 +1169,9 @@ static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)static int amdgpu_cs_sync_rings (struct amdgpu_cs_parser *p) {struct amdgpu_fpriv *fpriv = p->filp->driver_priv; +struct amdgpu_job *leader = p->gang_leader;struct amdgpu_bo_list_entry *e; +unsigned int i;int r;list_for_each_entry (e, &p->validated, tv.head) {
@@ -1135,12 +1181,23 @@ static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p) sync_mode =amdgpu_bo_explicit_sync (bo) ?AMDGPU_SYNC_EXPLICIT :AMDGPU_SYNC_NE_OWNER ; - r =amdgpu_sync_resv (p->adev, &p->job->sync, resv, sync_mode, + r =amdgpu_sync_resv (p->adev, &leader->sync, resv, sync_mode, &fpriv->vm);if (r)return r; } -return 0 ; + +for (i =0 ; i < p->gang_size -1 ; ++i) { + r =amdgpu_sync_clone (&leader->sync, &p->jobs[i]->sync); +if (r) +return r; + } + + r =amdgpu_ctx_wait_prev_fence (p->ctx, p->entities[p->gang_size -1 ]); +if (r && r != -ERESTARTSYS ) +DRM_ERROR ("amdgpu_ctx_wait_prev_fence failed.\n" ); + +return r; }static void amdgpu_cs_post_dependencies (struct amdgpu_cs_parser *p)
@@ -1164,16 +1221,28 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, union drm_amdgpu_cs *cs) {struct amdgpu_fpriv *fpriv = p->filp->driver_priv; -struct drm_sched_entity *entity = p->entity; +struct amdgpu_job *leader = p->gang_leader;struct amdgpu_bo_list_entry *e; -struct amdgpu_job *job; +unsigned int i;uint64_t seq;int r; - job = p->job; - p->job =NULL ; +for (i =0 ; i < p->gang_size; ++i) +drm_sched_job_arm (&p->jobs[i]->base); + +for (i =0 ; i < (p->gang_size -1 ); ++i) { +struct dma_fence *fence; + + fence = &p->jobs[i]->base.s_fence->scheduled; + r =amdgpu_sync_fence (&leader->sync, fence); +if (r) + goto error_cleanup; + } -drm_sched_job_arm (&job->base); +if (p->gang_size >1 ) { +for (i =0 ; i < p->gang_size; ++i) +amdgpu_job_set_gang_leader (p->jobs[i], leader); + }/* No memory allocation is allowed while holding the notifier lock. * The lock is held until amdgpu_cs_submit is finished and fence is
@@ -1191,45 +1260,57 @@ static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, }if (r) { r = -EAGAIN ; - goto error_abort; + goto error_unlock; } - p->fence =dma_fence_get (&job->base.s_fence->finished); + p->fence =dma_fence_get (&leader->base.s_fence->finished); +list_for_each_entry (e, &p->validated, tv.head) { - seq =amdgpu_ctx_add_fence (p->ctx, entity, p->fence); +/* Everybody except for the gang leader uses READ */ +for (i =0 ; i < (p->gang_size -1 ); ++i) { +dma_resv_add_fence (e->tv.bo->base.resv, + &p->jobs[i]->base.s_fence->finished, +DMA_RESV_USAGE_READ ); + } + +/* The gang leader as remembered as writer */ + e->tv.num_shared =0 ; + } + + seq =amdgpu_ctx_add_fence (p->ctx, p->entities[p->gang_size -1 ], + p->fence);amdgpu_cs_post_dependencies (p); -if ((job->preamble_status &AMDGPU_PREAMBLE_IB_PRESENT ) && +if ((leader->preamble_status &AMDGPU_PREAMBLE_IB_PRESENT ) && !p->ctx->preamble_presented) { - job->preamble_status |=AMDGPU_PREAMBLE_IB_PRESENT_FIRST ; + leader->preamble_status |=AMDGPU_PREAMBLE_IB_PRESENT_FIRST ; p->ctx->preamble_presented = true; } cs->out.handle = seq; - job->uf_sequence = seq; + leader->uf_sequence = seq; -amdgpu_job_free_resources (job); - -trace_amdgpu_cs_ioctl (job);amdgpu_vm_bo_trace_cs (&fpriv->vm, &p->ticket); -drm_sched_entity_push_job (&job->base); +for (i =0 ; i < p->gang_size; ++i) { +amdgpu_job_free_resources (p->jobs[i]); +trace_amdgpu_cs_ioctl (p->jobs[i]); +drm_sched_entity_push_job (&p->jobs[i]->base); + p->jobs[i] =NULL ; + }amdgpu_vm_move_to_lru_tail (p->adev, &fpriv->vm); - -/* Make sure all BOs are remembered as writers */ -amdgpu_bo_list_for_each_entry (e, p->bo_list) - e->tv.num_shared =0 ; -ttm_eu_fence_buffer_objects (&p->ticket, &p->validated, p->fence); +mutex_unlock (&p->adev->notifier_lock);mutex_unlock (&p->bo_list->bo_list_mutex); -return 0 ; -error_abort: -drm_sched_job_cleanup (&job->base); +error_unlock:mutex_unlock (&p->adev->notifier_lock); -amdgpu_job_free (job); + +error_cleanup: +for (i =0 ; i < p->gang_size; ++i) +drm_sched_job_cleanup (&p->jobs[i]->base);return r; }
@@ -1246,17 +1327,18 @@ static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser)dma_fence_put (parser->fence); -if (parser->ctx) { +if (parser->ctx)amdgpu_ctx_put (parser->ctx); - }if (parser->bo_list)amdgpu_bo_list_put (parser->bo_list);for (i =0 ; i < parser->nchunks; i++)kvfree (parser->chunks[i].kdata);kvfree (parser->chunks); -if (parser->job) -amdgpu_job_free (parser->job); +for (i =0 ; i < parser->gang_size; ++i) { +if (parser->jobs[i]) +amdgpu_job_free (parser->jobs[i]); + }if (parser->uf_entry.tv.bo) {struct amdgpu_bo *uf =ttm_to_amdgpu_bo (parser->uf_entry.tv.bo);
@@ -1300,7 +1382,7 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) goto error_fini; } - r =amdgpu_cs_patch_ibs (&parser); + r =amdgpu_cs_patch_jobs (&parser);if (r) goto error_backoff;
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.h +7 -3
@@ -27,6 +27,8 @@#include "amdgpu_bo_list.h" #include "amdgpu_ring.h" +#define AMDGPU_CS_GANG_SIZE 4 +struct amdgpu_bo_va_mapping ;struct amdgpu_cs_chunk {
@@ -50,9 +52,11 @@ struct amdgpu_cs_parser {unsigned nchunks;struct amdgpu_cs_chunk *chunks; -/* scheduler job object */ -struct drm_sched_entity *entity; -struct amdgpu_job *job; +/* scheduler job objects */ +unsigned int gang_size; +struct drm_sched_entity *entities[AMDGPU_CS_GANG_SIZE ]; +struct amdgpu_job *jobs[AMDGPU_CS_GANG_SIZE ]; +struct amdgpu_job *gang_leader;/* buffer objects */ struct ww_acquire_ctx ticket;
drivers/gpu/drm/amd/amdgpu/amdgpu_trace.h +7 -5
@@ -140,8 +140,10 @@ TRACE_EVENT(amdgpu_bo_create, ); TRACE_EVENT(amdgpu_cs, - TP_PROTO(struct amdgpu_cs_parser *p, int i), - TP_ARGS(p, i), + TP_PROTO(struct amdgpu_cs_parser *p, + struct amdgpu_job *job, + struct amdgpu_ib *ib), + TP_ARGS(p, job, ib), TP_STRUCT__entry( __field(struct amdgpu_bo_list *, bo_list) __field(u32, ring)
@@ -151,10 +153,10 @@ TRACE_EVENT(amdgpu_cs,TP_fast_assign ( __entry->bo_list = p->bo_list; - __entry->ring =to_amdgpu_ring (p->entity->rq->sched)->idx; - __entry->dw = p->job->ibs[i].length_dw; + __entry->ring =to_amdgpu_ring (job->base.sched)->idx; + __entry->dw = ib->length_dw; __entry->fences =amdgpu_fence_count_emitted ( -to_amdgpu_ring (p->entity->rq->sched)); +to_amdgpu_ring (job->base.sched)); ), TP_printk("bo_list=%p, ring=%u, dw=%u, fences=%u", __entry->bo_list, __entry->ring, __entry->dw,
Signature · 1 line
2.25.1
Christian König
reviewerPATCH 12/12 drm/amdgpu: cleanup VCN3 and VCN4 instance limiting v2
In reply to Christian KönigCheck if the entity is already limited, not if it's assigned to the first instance.
v2: only a cleanup, not a fix
--- drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c | 5 ++--- drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c +2 -3
@@ -1862,13 +1862,12 @@ static int vcn_v3_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,struct amdgpu_job *job, struct amdgpu_ib *ib) { - struct amdgpu_ring *ring = to_amdgpu_ring(job->base.sched); uint32_t msg_lo = 0, msg_hi = 0;unsigned i;int r; -/* The first instance can decode anything */ -if (!ring->me) +/* Abort if it's already limited */ +if (job->base.entity->num_sched_list <=1 )return 0 ;for (i =0 ; i < ib->length_dw; i +=2 ) {
drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c +2 -3
@@ -1430,13 +1430,12 @@ static int vcn_v4_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,struct amdgpu_job *job, struct amdgpu_ib *ib) { - struct amdgpu_ring *ring = to_amdgpu_ring(job->base.entity->rq->sched); struct amdgpu_vcn_decode_buffer *decode_buffer;uint64_t addr;uint32_t val; -/* The first instance can decode anything */ -if (!ring->me) +/* Abort if it's already limited */ +if (job->base.entity->num_sched_list <=1 )return 0 ;/* unified queue ib header has 8 double words. */
Signature · 1 line
2.25.1