2 * Copyright 2015 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/kthread.h>
25 #include <linux/wait.h>
26 #include <linux/sched.h>
28 #include "gpu_scheduler.h"
30 static void amd_sched_wakeup(struct amd_gpu_scheduler *sched);
32 /* Initialize a given run queue struct */
33 static void amd_sched_rq_init(struct amd_sched_rq *rq)
35 spin_lock_init(&rq->lock);
36 INIT_LIST_HEAD(&rq->entities);
37 rq->current_entity = NULL;
40 static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
41 struct amd_sched_entity *entity)
44 list_add_tail(&entity->list, &rq->entities);
45 spin_unlock(&rq->lock);
48 static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
49 struct amd_sched_entity *entity)
52 list_del_init(&entity->list);
53 if (rq->current_entity == entity)
54 rq->current_entity = NULL;
55 spin_unlock(&rq->lock);
59 * Select next entity from a specified run queue with round robin policy.
60 * It could return the same entity as current one if current is the only
61 * available one in the queue. Return NULL if nothing available.
63 static struct amd_sched_entity *
64 amd_sched_rq_select_entity(struct amd_sched_rq *rq)
66 struct amd_sched_entity *entity;
70 entity = rq->current_entity;
72 list_for_each_entry_continue(entity, &rq->entities, list) {
73 if (!kfifo_is_empty(&entity->job_queue)) {
74 rq->current_entity = entity;
75 spin_unlock(&rq->lock);
76 return rq->current_entity;
81 list_for_each_entry(entity, &rq->entities, list) {
83 if (!kfifo_is_empty(&entity->job_queue)) {
84 rq->current_entity = entity;
85 spin_unlock(&rq->lock);
86 return rq->current_entity;
89 if (entity == rq->current_entity)
93 spin_unlock(&rq->lock);
99 * Init a context entity used by scheduler when submit to HW ring.
101 * @sched The pointer to the scheduler
102 * @entity The pointer to a valid amd_sched_entity
103 * @rq The run queue this entity belongs
104 * @kernel If this is an entity for the kernel
105 * @jobs The max number of jobs in the job queue
107 * return 0 if succeed. negative error code on failure
109 int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
110 struct amd_sched_entity *entity,
111 struct amd_sched_rq *rq,
114 if (!(sched && entity && rq))
117 memset(entity, 0, sizeof(struct amd_sched_entity));
118 entity->belongto_rq = rq;
119 entity->scheduler = sched;
120 init_waitqueue_head(&entity->wait_queue);
121 entity->fence_context = fence_context_alloc(1);
122 if(kfifo_alloc(&entity->job_queue,
123 jobs * sizeof(void *),
127 spin_lock_init(&entity->queue_lock);
128 atomic_set(&entity->fence_seq, 0);
130 /* Add the entity to the run queue */
131 amd_sched_rq_add_entity(rq, entity);
136 * Query if entity is initialized
138 * @sched Pointer to scheduler instance
139 * @entity The pointer to a valid scheduler entity
141 * return true if entity is initialized, false otherwise
143 static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched,
144 struct amd_sched_entity *entity)
146 return entity->scheduler == sched &&
147 entity->belongto_rq != NULL;
151 * Check if entity is idle
153 * @entity The pointer to a valid scheduler entity
155 * Return true if entity don't has any unscheduled jobs.
157 static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
160 if (kfifo_is_empty(&entity->job_queue))
167 * Destroy a context entity
169 * @sched Pointer to scheduler instance
170 * @entity The pointer to a valid scheduler entity
172 * Cleanup and free the allocated resources.
174 void amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
175 struct amd_sched_entity *entity)
177 struct amd_sched_rq *rq = entity->belongto_rq;
179 if (!amd_sched_entity_is_initialized(sched, entity))
183 * The client will not queue more IBs during this fini, consume existing
186 wait_event(entity->wait_queue, amd_sched_entity_is_idle(entity));
188 amd_sched_rq_remove_entity(rq, entity);
189 kfifo_free(&entity->job_queue);
193 * Helper to submit a job to the job queue
195 * @job The pointer to job required to submit
197 * Returns true if we could submit the job.
199 static bool amd_sched_entity_in(struct amd_sched_job *job)
201 struct amd_sched_entity *entity = job->s_entity;
202 bool added, first = false;
204 spin_lock(&entity->queue_lock);
205 added = kfifo_in(&entity->job_queue, &job, sizeof(job)) == sizeof(job);
207 if (added && kfifo_len(&entity->job_queue) == sizeof(job))
210 spin_unlock(&entity->queue_lock);
212 /* first job wakes up scheduler */
214 amd_sched_wakeup(job->sched);
220 * Submit a job to the job queue
222 * @job The pointer to job required to submit
224 * Returns 0 for success, negative error code otherwise.
226 int amd_sched_entity_push_job(struct amd_sched_job *sched_job)
228 struct amd_sched_entity *entity = sched_job->s_entity;
229 struct amd_sched_fence *fence = amd_sched_fence_create(
230 entity, sched_job->owner);
236 fence_get(&fence->base);
237 sched_job->s_fence = fence;
239 r = wait_event_interruptible(entity->wait_queue,
240 amd_sched_entity_in(sched_job));
246 * Return ture if we can push more jobs to the hw.
248 static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
250 return atomic_read(&sched->hw_rq_count) <
251 sched->hw_submission_limit;
255 * Wake up the scheduler when it is ready
257 static void amd_sched_wakeup(struct amd_gpu_scheduler *sched)
259 if (amd_sched_ready(sched))
260 wake_up_interruptible(&sched->wait_queue);
264 * Select next entity containing real IB submissions
266 static struct amd_sched_entity *
267 amd_sched_select_context(struct amd_gpu_scheduler *sched)
269 struct amd_sched_entity *tmp;
271 if (!amd_sched_ready(sched))
274 /* Kernel run queue has higher priority than normal run queue*/
275 tmp = amd_sched_rq_select_entity(&sched->kernel_rq);
277 tmp = amd_sched_rq_select_entity(&sched->sched_rq);
282 static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
284 struct amd_sched_job *sched_job =
285 container_of(cb, struct amd_sched_job, cb);
286 struct amd_gpu_scheduler *sched;
288 sched = sched_job->sched;
289 amd_sched_fence_signal(sched_job->s_fence);
290 atomic_dec(&sched->hw_rq_count);
291 fence_put(&sched_job->s_fence->base);
292 sched->ops->process_job(sched, sched_job);
293 wake_up_interruptible(&sched->wait_queue);
296 static int amd_sched_main(void *param)
298 struct sched_param sparam = {.sched_priority = 1};
299 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
302 sched_setscheduler(current, SCHED_FIFO, &sparam);
304 while (!kthread_should_stop()) {
305 struct amd_sched_entity *c_entity = NULL;
306 struct amd_sched_job *job;
309 wait_event_interruptible(sched->wait_queue,
310 kthread_should_stop() ||
311 (c_entity = amd_sched_select_context(sched)));
316 r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *));
317 if (r != sizeof(void *))
319 atomic_inc(&sched->hw_rq_count);
321 fence = sched->ops->run_job(sched, c_entity, job);
323 r = fence_add_callback(fence, &job->cb,
324 amd_sched_process_job);
326 amd_sched_process_job(fence, &job->cb);
328 DRM_ERROR("fence add callback failed (%d)\n", r);
332 wake_up(&c_entity->wait_queue);
338 * Create a gpu scheduler
340 * @ops The backend operations for this scheduler.
341 * @ring The the ring id for the scheduler.
342 * @hw_submissions Number of hw submissions to do.
344 * Return the pointer to scheduler for success, otherwise return NULL
346 struct amd_gpu_scheduler *amd_sched_create(struct amd_sched_backend_ops *ops,
347 unsigned ring, unsigned hw_submission,
350 struct amd_gpu_scheduler *sched;
352 sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL);
357 sched->ring_id = ring;
358 sched->hw_submission_limit = hw_submission;
360 snprintf(sched->name, sizeof(sched->name), "amdgpu[%d]", ring);
361 amd_sched_rq_init(&sched->sched_rq);
362 amd_sched_rq_init(&sched->kernel_rq);
364 init_waitqueue_head(&sched->wait_queue);
365 atomic_set(&sched->hw_rq_count, 0);
366 /* Each scheduler will run on a seperate kernel thread */
367 sched->thread = kthread_run(amd_sched_main, sched, sched->name);
368 if (IS_ERR(sched->thread)) {
369 DRM_ERROR("Failed to create scheduler for id %d.\n", ring);
378 * Destroy a gpu scheduler
380 * @sched The pointer to the scheduler
382 * return 0 if succeed. -1 if failed.
384 int amd_sched_destroy(struct amd_gpu_scheduler *sched)
386 kthread_stop(sched->thread);