Am 02.03.2017 um 22:44 schrieb Andres Rodriguez: > Add an initial framework for changing the HW priorities of rings. The > framework allows requesting priority changes for the lifetime of an > amdgpu_job. After the job completes the priority will decay to the next > lowest priority for which a request is still valid. > > A new ring function set_priority() can now be populated to take care of > the HW specific programming sequence for priority changes. > > v2: set priority before emitting IB, and take a ref on amdgpu_job > v3: use AMD_SCHED_PRIORITY_* instead of AMDGPU_CTX_PRIORITY_* > v4: plug amdgpu_ring_restore_priority_cb into amdgpu_job_free_cb > > Signed-off-by: Andres Rodriguez <andresx7 at gmail.com> > --- > drivers/gpu/drm/amd/amdgpu/amdgpu.h | 3 +- > drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c | 2 +- > drivers/gpu/drm/amd/amdgpu/amdgpu_job.c | 14 +++++- > drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 73 ++++++++++++++++++++++++++++++++ > drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h | 12 ++++++ > 5 files changed, 100 insertions(+), 4 deletions(-) > > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h b/drivers/gpu/drm/amd/amdgpu/amdgpu.h > index bf15373..e7e92fd 100644 > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h > @@ -636,21 +636,21 @@ struct amdgpu_flip_work { > struct amdgpu_ib { > struct amdgpu_sa_bo *sa_bo; > uint32_t length_dw; > uint64_t gpu_addr; > uint32_t *ptr; > uint32_t flags; > }; > > extern const struct amd_sched_backend_ops amdgpu_sched_ops; > > -int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs, > +int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs, int priority, > struct amdgpu_job **job, struct amdgpu_vm *vm); > int amdgpu_job_alloc_with_ib(struct amdgpu_device *adev, unsigned size, > struct amdgpu_job **job); > > 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 amdgpu_ring *ring, > struct amd_sched_entity *entity, void *owner, > struct dma_fence **f); > > @@ -981,20 +981,21 @@ struct amdgpu_cs_parser { > #define AMDGPU_VM_DOMAIN (1 << 3) /* bit set means in virtual memory context */ > > struct amdgpu_job { > struct amd_sched_job base; > struct amdgpu_device *adev; > struct amdgpu_vm *vm; > struct amdgpu_ring *ring; > struct amdgpu_sync sync; > struct amdgpu_ib *ibs; > struct dma_fence *fence; /* the hw fence */ > + int priority; Actually that is unnecessary (and thinking more about it the priority in the context is unnecessary as well). (job->base.sched->sched_rq - job->base.s_entity->rq) should give you the scheduler priority of the job as well. We can add an inline helper in the scheduler for that. Additional to that please use enum amd_sched_priority instead of int for the priority all over the patch. > uint32_t preamble_status; > uint32_t num_ibs; > void *owner; > uint64_t fence_ctx; /* the fence_context this job uses */ > bool vm_needs_flush; > unsigned vm_id; > uint64_t vm_pd_addr; > uint32_t gds_base, gds_size; > uint32_t gws_base, gws_size; > uint32_t oa_base, oa_size; > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c > index 605d40e..19ce202 100644 > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c > @@ -179,21 +179,21 @@ int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data) > > case AMDGPU_CHUNK_ID_DEPENDENCIES: > break; > > default: > ret = -EINVAL; > goto free_partial_kdata; > } > } > > - ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm); > + ret = amdgpu_job_alloc(p->adev, num_ibs, p->ctx->priority, &p->job, vm); > if (ret) > goto free_all_kdata; > > if (p->uf_entry.robj) > p->job->uf_addr = uf_offset; > kfree(chunk_array); > return 0; > > free_all_kdata: > i = p->nchunks - 1; > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c > index 86a1242..28f8a50 100644 > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_job.c > @@ -32,50 +32,51 @@ static void amdgpu_job_timedout(struct amd_sched_job *s_job) > { > struct amdgpu_job *job = container_of(s_job, struct amdgpu_job, base); > > DRM_ERROR("ring %s timeout, last signaled seq=%u, last emitted seq=%u\n", > job->base.sched->name, > atomic_read(&job->ring->fence_drv.last_seq), > job->ring->fence_drv.sync_seq); > amdgpu_gpu_reset(job->adev); > } > > -int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs, > +int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs, int priority, > struct amdgpu_job **job, struct amdgpu_vm *vm) > { > size_t size = sizeof(struct amdgpu_job); > > if (num_ibs == 0) > return -EINVAL; > > size += sizeof(struct amdgpu_ib) * num_ibs; > > *job = kzalloc(size, GFP_KERNEL); > if (!*job) > return -ENOMEM; > > (*job)->adev = adev; > (*job)->vm = vm; > + (*job)->priority = priority; > (*job)->ibs = (void *)&(*job)[1]; > (*job)->num_ibs = num_ibs; > > amdgpu_sync_create(&(*job)->sync); > > return 0; > } > > int amdgpu_job_alloc_with_ib(struct amdgpu_device *adev, unsigned size, > struct amdgpu_job **job) > { > int r; > > - r = amdgpu_job_alloc(adev, 1, job, NULL); > + r = amdgpu_job_alloc(adev, 1, AMD_SCHED_PRIORITY_NORMAL, job, NULL); > if (r) > return r; > > r = amdgpu_ib_get(adev, NULL, size, &(*job)->ibs[0]); > if (r) > kfree(*job); > > return r; > } > > @@ -88,20 +89,24 @@ void amdgpu_job_free_resources(struct amdgpu_job *job) > f = job->base.s_fence ? &job->base.s_fence->finished : job->fence; > > for (i = 0; i < job->num_ibs; ++i) > amdgpu_ib_free(job->adev, &job->ibs[i], f); > } > > static void amdgpu_job_free_cb(struct amd_sched_job *s_job) > { > struct amdgpu_job *job = container_of(s_job, struct amdgpu_job, base); > > + /* no need to restore if we are already at the lowest priority */ > + if (job->priority < AMD_SCHED_PRIORITY_NORMAL) > + amdgpu_ring_restore_priority_cb(job); Please put that check into amdgpu_ring_restore_priority_cb(). And by the way remove the _cb postfix from amdgpu_ring_restore_priority_cb(). > + > dma_fence_put(job->fence); > amdgpu_sync_free(&job->sync); > kfree(job); > } > > void amdgpu_job_free(struct amdgpu_job *job) > { > amdgpu_job_free_resources(job); > > dma_fence_put(job->fence); > @@ -162,28 +167,33 @@ static struct dma_fence *amdgpu_job_run(struct amd_sched_job *sched_job) > int r; > > if (!sched_job) { > DRM_ERROR("job is null\n"); > return NULL; > } > job = to_amdgpu_job(sched_job); > > BUG_ON(amdgpu_sync_peek_fence(&job->sync, NULL)); > > + r = amdgpu_ring_elevate_priority(job->ring, job->priority, job); > + if (r) > + DRM_ERROR("Failed to set job priority (%d)\n", r); > + > trace_amdgpu_sched_run_job(job); > r = amdgpu_ib_schedule(job->ring, job->num_ibs, job->ibs, job, &fence); > if (r) > DRM_ERROR("Error scheduling IBs (%d)\n", r); > > /* if gpu reset, hw fence will be replaced here */ > dma_fence_put(job->fence); > job->fence = dma_fence_get(fence); > + > amdgpu_job_free_resources(job); > return fence; > } > > const struct amd_sched_backend_ops amdgpu_sched_ops = { > .dependency = amdgpu_job_dependency, > .run_job = amdgpu_job_run, > .timedout_job = amdgpu_job_timedout, > .free_job = amdgpu_job_free_cb > }; > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c > index 31c6274..fa6e72f2 100644 > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c > @@ -193,20 +193,91 @@ void amdgpu_ring_commit(struct amdgpu_ring *ring) > */ > void amdgpu_ring_undo(struct amdgpu_ring *ring) > { > ring->wptr = ring->wptr_old; > > if (ring->funcs->end_use) > ring->funcs->end_use(ring); > } > > /** > + * amdgpu_ring_restore_priority_cb - restore a ring's priority > + * > + * @job: job which completed execution > + * > + * If necessary, restore a ring's priority after a job finishes executing > + */ > +void amdgpu_ring_restore_priority_cb(struct amdgpu_job *job) > +{ > + int i; > + struct amdgpu_ring *ring = job->ring; > + > + spin_lock(&ring->priority_lock); > + > + /* remove ourselves from the list if necessary */ > + if (job == ring->last_job[job->priority]) > + ring->last_job[job->priority] = NULL; > + > + /* something higher prio is executing, no need to decay */ > + if (ring->priority < job->priority) > + goto out_unlock; > + > + /* decay priority to the next level with a job available */ > + for (i = job->priority; i < AMD_SCHED_MAX_PRIORITY; i++) { > + if (i == AMD_SCHED_PRIORITY_NORMAL || ring->last_job[i]) { > + ring->priority = i; > + if (ring->funcs->set_priority) > + ring->funcs->set_priority(ring, i); > + > + break; > + } > + } > + > +out_unlock: > + spin_unlock(&ring->priority_lock); > +} If you use an atomic to count the number of jobs for each priority running you would only need the spinlock for updating the priority. Additional to that I would abort early if ring->funcs->set_priority isn't available (e.g. before doing anything else). Regards, Christian. > + > +/** > + * amdgpu_ring_elevate_priority - change the ring's priority > + * > + * @ring: amdgpu_ring structure holding the information > + * @priority: target priority > + * @job: priority should remain elevated for the duration of this job > + * > + * Use HW specific mechanism's to elevate the ring's priority while @job > + * is executing. Once @job finishes executing, the ring will reset back > + * to normal priority. > + * Returns 0 on success, error otherwise > + */ > +int amdgpu_ring_elevate_priority(struct amdgpu_ring *ring, int priority, > + struct amdgpu_job *job) > +{ > + if (priority < 0 || priority >= AMD_SCHED_MAX_PRIORITY) > + return -EINVAL; > + > + spin_lock(&ring->priority_lock); > + ring->last_job[priority] = job; > + > + /* lower number means higher priority */ > + if (priority >= ring->priority) > + goto out_unlock; > + > + ring->priority = priority; > + if (ring->funcs->set_priority) > + ring->funcs->set_priority(ring, priority); > + > +out_unlock: > + spin_unlock(&ring->priority_lock); > + return 0; > +} > + > +/** > * amdgpu_ring_init - init driver ring struct. > * > * @adev: amdgpu_device pointer > * @ring: amdgpu_ring structure holding ring information > * @max_ndw: maximum number of dw for ring alloc > * @nop: nop packet for this ring > * > * Initialize the driver information for the selected ring (all asics). > * Returns 0 on success, error on failure. > */ > @@ -274,20 +345,22 @@ int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, > &ring->gpu_addr, > (void **)&ring->ring); > if (r) { > dev_err(adev->dev, "(%d) ring create failed\n", r); > return r; > } > memset((void *)ring->ring, 0, ring->ring_size); > } > ring->ptr_mask = (ring->ring_size / 4) - 1; > ring->max_dw = max_dw; > + ring->priority = AMD_SCHED_PRIORITY_NORMAL; > + spin_lock_init(&ring->priority_lock); > INIT_LIST_HEAD(&ring->lru_list); > amdgpu_ring_lru_touch(adev, ring); > > if (amdgpu_debugfs_ring_init(adev, ring)) { > DRM_ERROR("Failed to register debugfs file for rings !\n"); > } > return 0; > } > > /** > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h > index b51bdcd..ee6be0c 100644 > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h > @@ -17,20 +17,21 @@ > * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR > * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, > * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR > * OTHER DEALINGS IN THE SOFTWARE. > * > * Authors: Christian König > */ > #ifndef __AMDGPU_RING_H__ > #define __AMDGPU_RING_H__ > > +#include <drm/amdgpu_drm.h> > #include "gpu_scheduler.h" > > /* max number of rings */ > #define AMDGPU_MAX_RINGS 16 > #define AMDGPU_MAX_GFX_RINGS 1 > #define AMDGPU_MAX_COMPUTE_RINGS 8 > #define AMDGPU_MAX_VCE_RINGS 3 > > /* some special values for the owner field */ > #define AMDGPU_FENCE_OWNER_UNDEFINED ((void*)0ul) > @@ -130,20 +131,22 @@ struct amdgpu_ring_funcs { > void (*pad_ib)(struct amdgpu_ring *ring, struct amdgpu_ib *ib); > unsigned (*init_cond_exec)(struct amdgpu_ring *ring); > void (*patch_cond_exec)(struct amdgpu_ring *ring, unsigned offset); > /* note usage for clock and power gating */ > void (*begin_use)(struct amdgpu_ring *ring); > void (*end_use)(struct amdgpu_ring *ring); > void (*emit_switch_buffer) (struct amdgpu_ring *ring); > void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags); > void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg); > void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val); > + /* priority functions */ > + void (*set_priority) (struct amdgpu_ring *ring, int priority); > }; > > struct amdgpu_ring { > struct amdgpu_device *adev; > const struct amdgpu_ring_funcs *funcs; > struct amdgpu_fence_driver fence_drv; > struct amd_gpu_scheduler sched; > struct list_head lru_list; > > struct amdgpu_bo *ring_obj; > @@ -164,31 +167,40 @@ struct amdgpu_ring { > struct amdgpu_bo *mqd_obj; > u32 doorbell_index; > bool use_doorbell; > unsigned wptr_offs; > unsigned fence_offs; > uint64_t current_ctx; > char name[16]; > unsigned cond_exe_offs; > u64 cond_exe_gpu_addr; > volatile u32 *cond_exe_cpu_addr; > + > + spinlock_t priority_lock; > + /* protected by priority_lock */ > + struct amdgpu_job *last_job[AMD_SCHED_MAX_PRIORITY]; > + int priority; > + > #if defined(CONFIG_DEBUG_FS) > struct dentry *ent; > #endif > }; > > int amdgpu_ring_is_valid_index(struct amdgpu_device *adev, > int hw_ip, int ring); > int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw); > void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count); > void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib); > void amdgpu_ring_commit(struct amdgpu_ring *ring); > void amdgpu_ring_undo(struct amdgpu_ring *ring); > +int amdgpu_ring_elevate_priority(struct amdgpu_ring *ring, int priority, > + struct amdgpu_job *job); > +void amdgpu_ring_restore_priority_cb(struct amdgpu_job *job); > int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, > unsigned ring_size, struct amdgpu_irq_src *irq_src, > unsigned irq_type); > void amdgpu_ring_fini(struct amdgpu_ring *ring); > int amdgpu_ring_lru_get(struct amdgpu_device *adev, int hw_ip, > struct amdgpu_ring **ring); > void amdgpu_ring_lru_touch(struct amdgpu_device *adev, struct amdgpu_ring *ring); > > #endif