Commit 432a4ff8 authored by Christian König's avatar Christian König Committed by Alex Deucher

drm/amdgpu: cleanup sheduler rq handling v2

Rework run queue implementation, especially remove the odd list handling.

v2: cleanup the code only, no algorithem change.
Signed-off-by: default avatarChristian König <christian.koenig@amd.com>
Reviewed-by: default avatarChunming Zhou <david1.zhou@amd.com>
parent c3b95d4f
...@@ -41,7 +41,7 @@ int amdgpu_ctx_init(struct amdgpu_device *adev, bool kernel, ...@@ -41,7 +41,7 @@ int amdgpu_ctx_init(struct amdgpu_device *adev, bool kernel,
if (amdgpu_enable_scheduler) { if (amdgpu_enable_scheduler) {
/* create context entity for each ring */ /* create context entity for each ring */
for (i = 0; i < adev->num_rings; i++) { for (i = 0; i < adev->num_rings; i++) {
struct amd_run_queue *rq; struct amd_sched_rq *rq;
if (kernel) if (kernel)
rq = &adev->rings[i]->scheduler->kernel_rq; rq = &adev->rings[i]->scheduler->kernel_rq;
else else
......
...@@ -28,31 +28,29 @@ ...@@ -28,31 +28,29 @@
#include "gpu_scheduler.h" #include "gpu_scheduler.h"
/* Initialize a given run queue struct */ /* Initialize a given run queue struct */
static void init_rq(struct amd_run_queue *rq) static void amd_sched_rq_init(struct amd_sched_rq *rq)
{ {
INIT_LIST_HEAD(&rq->head.list); INIT_LIST_HEAD(&rq->entities);
rq->head.belongto_rq = rq;
mutex_init(&rq->lock); mutex_init(&rq->lock);
atomic_set(&rq->nr_entity, 0); rq->current_entity = NULL;
rq->current_entity = &rq->head;
} }
/* Note: caller must hold the lock or in a atomic context */ static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
static void rq_remove_entity(struct amd_run_queue *rq,
struct amd_sched_entity *entity) struct amd_sched_entity *entity)
{ {
if (rq->current_entity == entity) mutex_lock(&rq->lock);
rq->current_entity = list_entry(entity->list.prev, list_add_tail(&entity->list, &rq->entities);
typeof(*entity), list); mutex_unlock(&rq->lock);
list_del_init(&entity->list);
atomic_dec(&rq->nr_entity);
} }
static void rq_add_entity(struct amd_run_queue *rq, static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
struct amd_sched_entity *entity) struct amd_sched_entity *entity)
{ {
list_add_tail(&entity->list, &rq->head.list); mutex_lock(&rq->lock);
atomic_inc(&rq->nr_entity); list_del_init(&entity->list);
if (rq->current_entity == entity)
rq->current_entity = NULL;
mutex_unlock(&rq->lock);
} }
/** /**
...@@ -60,38 +58,32 @@ static void rq_add_entity(struct amd_run_queue *rq, ...@@ -60,38 +58,32 @@ static void rq_add_entity(struct amd_run_queue *rq,
* It could return the same entity as current one if current is the only * It could return the same entity as current one if current is the only
* available one in the queue. Return NULL if nothing available. * available one in the queue. Return NULL if nothing available.
*/ */
static struct amd_sched_entity *rq_select_entity(struct amd_run_queue *rq) static struct amd_sched_entity *
amd_sched_rq_select_entity(struct amd_sched_rq *rq)
{ {
struct amd_sched_entity *p = rq->current_entity; struct amd_sched_entity *entity = rq->current_entity;
int i = atomic_read(&rq->nr_entity) + 1; /*real count + dummy head*/
while (i) { if (entity) {
p = list_entry(p->list.next, typeof(*p), list); list_for_each_entry_continue(entity, &rq->entities, list) {
if (!rq->check_entity_status(p)) { if (!kfifo_is_empty(&entity->job_queue)) {
rq->current_entity = p; rq->current_entity = entity;
break; return rq->current_entity;
}
} }
i--;
} }
return i ? p : NULL;
}
static bool context_entity_is_waiting(struct amd_sched_entity *entity) list_for_each_entry(entity, &rq->entities, list) {
{
/* TODO: sync obj for multi-ring synchronization */
return false;
}
static int gpu_entity_check_status(struct amd_sched_entity *entity) if (!kfifo_is_empty(&entity->job_queue)) {
{ rq->current_entity = entity;
if (entity == &entity->belongto_rq->head) return rq->current_entity;
return -1; }
if (kfifo_is_empty(&entity->job_queue) || if (entity == rq->current_entity)
context_entity_is_waiting(entity)) break;
return -1; }
return 0; return NULL;
} }
/** /**
...@@ -124,10 +116,10 @@ static struct amd_sched_entity * ...@@ -124,10 +116,10 @@ static struct amd_sched_entity *
kernel_rq_select_context(struct amd_gpu_scheduler *sched) kernel_rq_select_context(struct amd_gpu_scheduler *sched)
{ {
struct amd_sched_entity *sched_entity; struct amd_sched_entity *sched_entity;
struct amd_run_queue *rq = &sched->kernel_rq; struct amd_sched_rq *rq = &sched->kernel_rq;
mutex_lock(&rq->lock); mutex_lock(&rq->lock);
sched_entity = rq_select_entity(rq); sched_entity = amd_sched_rq_select_entity(rq);
mutex_unlock(&rq->lock); mutex_unlock(&rq->lock);
return sched_entity; return sched_entity;
} }
...@@ -140,7 +132,7 @@ select_context(struct amd_gpu_scheduler *sched) ...@@ -140,7 +132,7 @@ select_context(struct amd_gpu_scheduler *sched)
{ {
struct amd_sched_entity *wake_entity = NULL; struct amd_sched_entity *wake_entity = NULL;
struct amd_sched_entity *tmp; struct amd_sched_entity *tmp;
struct amd_run_queue *rq; struct amd_sched_rq *rq;
if (!is_scheduler_ready(sched)) if (!is_scheduler_ready(sched))
return NULL; return NULL;
...@@ -152,7 +144,7 @@ select_context(struct amd_gpu_scheduler *sched) ...@@ -152,7 +144,7 @@ select_context(struct amd_gpu_scheduler *sched)
rq = &sched->sched_rq; rq = &sched->sched_rq;
mutex_lock(&rq->lock); mutex_lock(&rq->lock);
tmp = rq_select_entity(rq); tmp = amd_sched_rq_select_entity(rq);
mutex_unlock(&rq->lock); mutex_unlock(&rq->lock);
exit: exit:
if (sched->current_entity && (sched->current_entity != tmp)) if (sched->current_entity && (sched->current_entity != tmp))
...@@ -176,7 +168,7 @@ select_context(struct amd_gpu_scheduler *sched) ...@@ -176,7 +168,7 @@ select_context(struct amd_gpu_scheduler *sched)
*/ */
int amd_sched_entity_init(struct amd_gpu_scheduler *sched, int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
struct amd_sched_entity *entity, struct amd_sched_entity *entity,
struct amd_run_queue *rq, struct amd_sched_rq *rq,
uint32_t jobs) uint32_t jobs)
{ {
uint64_t seq_ring = 0; uint64_t seq_ring = 0;
...@@ -206,9 +198,7 @@ int amd_sched_entity_init(struct amd_gpu_scheduler *sched, ...@@ -206,9 +198,7 @@ int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
atomic64_set(&entity->last_signaled_v_seq, seq_ring); atomic64_set(&entity->last_signaled_v_seq, seq_ring);
/* Add the entity to the run queue */ /* Add the entity to the run queue */
mutex_lock(&rq->lock); amd_sched_rq_add_entity(rq, entity);
rq_add_entity(rq, entity);
mutex_unlock(&rq->lock);
return 0; return 0;
} }
...@@ -254,7 +244,7 @@ int amd_sched_entity_fini(struct amd_gpu_scheduler *sched, ...@@ -254,7 +244,7 @@ int amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
struct amd_sched_entity *entity) struct amd_sched_entity *entity)
{ {
int r = 0; int r = 0;
struct amd_run_queue *rq = entity->belongto_rq; struct amd_sched_rq *rq = entity->belongto_rq;
if (!is_context_entity_initialized(sched, entity)) if (!is_context_entity_initialized(sched, entity))
return 0; return 0;
...@@ -276,9 +266,7 @@ int amd_sched_entity_fini(struct amd_gpu_scheduler *sched, ...@@ -276,9 +266,7 @@ int amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
entity); entity);
} }
mutex_lock(&rq->lock); amd_sched_rq_remove_entity(rq, entity);
rq_remove_entity(rq, entity);
mutex_unlock(&rq->lock);
kfifo_free(&entity->job_queue); kfifo_free(&entity->job_queue);
return r; return r;
} }
...@@ -429,11 +417,8 @@ struct amd_gpu_scheduler *amd_sched_create(void *device, ...@@ -429,11 +417,8 @@ struct amd_gpu_scheduler *amd_sched_create(void *device,
snprintf(name, sizeof(name), "gpu_sched[%d]", ring); snprintf(name, sizeof(name), "gpu_sched[%d]", ring);
mutex_init(&sched->sched_lock); mutex_init(&sched->sched_lock);
spin_lock_init(&sched->queue_lock); spin_lock_init(&sched->queue_lock);
init_rq(&sched->sched_rq); amd_sched_rq_init(&sched->sched_rq);
sched->sched_rq.check_entity_status = gpu_entity_check_status; amd_sched_rq_init(&sched->kernel_rq);
init_rq(&sched->kernel_rq);
sched->kernel_rq.check_entity_status = gpu_entity_check_status;
init_waitqueue_head(&sched->wait_queue); init_waitqueue_head(&sched->wait_queue);
INIT_LIST_HEAD(&sched->active_hw_rq); INIT_LIST_HEAD(&sched->active_hw_rq);
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
#define AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS 3000 #define AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS 3000
struct amd_gpu_scheduler; struct amd_gpu_scheduler;
struct amd_run_queue; struct amd_sched_rq;
/** /**
* A scheduler entity is a wrapper around a job queue or a group * A scheduler entity is a wrapper around a job queue or a group
...@@ -40,7 +40,7 @@ struct amd_run_queue; ...@@ -40,7 +40,7 @@ struct amd_run_queue;
*/ */
struct amd_sched_entity { struct amd_sched_entity {
struct list_head list; struct list_head list;
struct amd_run_queue *belongto_rq; struct amd_sched_rq *belongto_rq;
spinlock_t lock; spinlock_t lock;
/* the virtual_seq is unique per context per ring */ /* the virtual_seq is unique per context per ring */
atomic64_t last_queued_v_seq; atomic64_t last_queued_v_seq;
...@@ -62,17 +62,10 @@ struct amd_sched_entity { ...@@ -62,17 +62,10 @@ struct amd_sched_entity {
* one specific ring. It implements the scheduling policy that selects * one specific ring. It implements the scheduling policy that selects
* the next entity to emit commands from. * the next entity to emit commands from.
*/ */
struct amd_run_queue { struct amd_sched_rq {
struct mutex lock; struct mutex lock;
atomic_t nr_entity; struct list_head entities;
struct amd_sched_entity head;
struct amd_sched_entity *current_entity; struct amd_sched_entity *current_entity;
/**
* Return 0 means this entity can be scheduled
* Return -1 means this entity cannot be scheduled for reasons,
* i.e, it is the head, or these is no job, etc
*/
int (*check_entity_status)(struct amd_sched_entity *entity);
}; };
struct amd_sched_fence { struct amd_sched_fence {
...@@ -124,8 +117,8 @@ struct amd_sched_backend_ops { ...@@ -124,8 +117,8 @@ struct amd_sched_backend_ops {
struct amd_gpu_scheduler { struct amd_gpu_scheduler {
void *device; void *device;
struct task_struct *thread; struct task_struct *thread;
struct amd_run_queue sched_rq; struct amd_sched_rq sched_rq;
struct amd_run_queue kernel_rq; struct amd_sched_rq kernel_rq;
struct list_head active_hw_rq; struct list_head active_hw_rq;
atomic64_t hw_rq_count; atomic64_t hw_rq_count;
struct amd_sched_backend_ops *ops; struct amd_sched_backend_ops *ops;
...@@ -154,7 +147,7 @@ int amd_sched_push_job(struct amd_gpu_scheduler *sched, ...@@ -154,7 +147,7 @@ int amd_sched_push_job(struct amd_gpu_scheduler *sched,
int amd_sched_entity_init(struct amd_gpu_scheduler *sched, int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
struct amd_sched_entity *entity, struct amd_sched_entity *entity,
struct amd_run_queue *rq, struct amd_sched_rq *rq,
uint32_t jobs); uint32_t jobs);
int amd_sched_entity_fini(struct amd_gpu_scheduler *sched, int amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
struct amd_sched_entity *entity); struct amd_sched_entity *entity);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment