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.
27 * The GPU scheduler provides entities which allow userspace to push jobs
28 * into software queues which are then scheduled on a hardware run queue.
29 * The software queues have a priority among them. The scheduler selects the entities
30 * from the run queue using a FIFO. The scheduler provides dependency handling
31 * features among jobs. The driver is supposed to provide callback functions for
32 * backend operations to the scheduler like submitting a job to hardware run queue,
33 * returning the dependencies of a job etc.
35 * The organisation of the scheduler is the following:
37 * 1. Each hw run queue has one scheduler
38 * 2. Each scheduler has multiple run queues with different priorities
39 * (e.g., HIGH_HW,HIGH_SW, KERNEL, NORMAL)
40 * 3. Each scheduler run queue has a queue of entities to schedule
41 * 4. Entities themselves maintain a queue of jobs that will be scheduled on
44 * The jobs in a entity are always scheduled in the order that they were pushed.
47 #include <linux/kthread.h>
48 #include <linux/wait.h>
49 #include <linux/sched.h>
50 #include <uapi/linux/sched/types.h>
52 #include <drm/gpu_scheduler.h>
53 #include <drm/spsc_queue.h>
55 #define CREATE_TRACE_POINTS
56 #include "gpu_scheduler_trace.h"
58 #define to_drm_sched_job(sched_job) \
59 container_of((sched_job), struct drm_sched_job, queue_node)
61 static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb);
64 * drm_sched_rq_init - initialize a given run queue struct
66 * @rq: scheduler run queue
68 * Initializes a scheduler runqueue.
70 static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
71 struct drm_sched_rq *rq)
73 spin_lock_init(&rq->lock);
74 INIT_LIST_HEAD(&rq->entities);
75 rq->current_entity = NULL;
80 * drm_sched_rq_add_entity - add an entity
82 * @rq: scheduler run queue
83 * @entity: scheduler entity
85 * Adds a scheduler entity to the run queue.
87 void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
88 struct drm_sched_entity *entity)
90 if (!list_empty(&entity->list))
93 list_add_tail(&entity->list, &rq->entities);
94 spin_unlock(&rq->lock);
98 * drm_sched_rq_remove_entity - remove an entity
100 * @rq: scheduler run queue
101 * @entity: scheduler entity
103 * Removes a scheduler entity from the run queue.
105 void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
106 struct drm_sched_entity *entity)
108 if (list_empty(&entity->list))
110 spin_lock(&rq->lock);
111 list_del_init(&entity->list);
112 if (rq->current_entity == entity)
113 rq->current_entity = NULL;
114 spin_unlock(&rq->lock);
118 * drm_sched_rq_select_entity - Select an entity which could provide a job to run
120 * @rq: scheduler run queue to check.
122 * Try to find a ready entity, returns NULL if none found.
124 static struct drm_sched_entity *
125 drm_sched_rq_select_entity(struct drm_sched_rq *rq)
127 struct drm_sched_entity *entity;
129 spin_lock(&rq->lock);
131 entity = rq->current_entity;
133 list_for_each_entry_continue(entity, &rq->entities, list) {
134 if (drm_sched_entity_is_ready(entity)) {
135 rq->current_entity = entity;
136 spin_unlock(&rq->lock);
142 list_for_each_entry(entity, &rq->entities, list) {
144 if (drm_sched_entity_is_ready(entity)) {
145 rq->current_entity = entity;
146 spin_unlock(&rq->lock);
150 if (entity == rq->current_entity)
154 spin_unlock(&rq->lock);
160 * drm_sched_dependency_optimized
162 * @fence: the dependency fence
163 * @entity: the entity which depends on the above fence
165 * Returns true if the dependency can be optimized and false otherwise
167 bool drm_sched_dependency_optimized(struct dma_fence* fence,
168 struct drm_sched_entity *entity)
170 struct drm_gpu_scheduler *sched = entity->rq->sched;
171 struct drm_sched_fence *s_fence;
173 if (!fence || dma_fence_is_signaled(fence))
175 if (fence->context == entity->fence_context)
177 s_fence = to_drm_sched_fence(fence);
178 if (s_fence && s_fence->sched == sched)
183 EXPORT_SYMBOL(drm_sched_dependency_optimized);
186 * drm_sched_start_timeout - start timeout for reset worker
188 * @sched: scheduler instance to start the worker for
190 * Start the timeout for the given scheduler.
192 static void drm_sched_start_timeout(struct drm_gpu_scheduler *sched)
194 if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
195 !list_empty(&sched->ring_mirror_list))
196 schedule_delayed_work(&sched->work_tdr, sched->timeout);
199 /* job_finish is called after hw fence signaled
201 static void drm_sched_job_finish(struct work_struct *work)
203 struct drm_sched_job *s_job = container_of(work, struct drm_sched_job,
205 struct drm_gpu_scheduler *sched = s_job->sched;
208 * Canceling the timeout without removing our job from the ring mirror
209 * list is safe, as we will only end up in this worker if our jobs
210 * finished fence has been signaled. So even if some another worker
211 * manages to find this job as the next job in the list, the fence
212 * signaled check below will prevent the timeout to be restarted.
214 cancel_delayed_work_sync(&sched->work_tdr);
216 spin_lock(&sched->job_list_lock);
217 /* remove job from ring_mirror_list */
218 list_del(&s_job->node);
219 /* queue TDR for next job */
220 drm_sched_start_timeout(sched);
221 spin_unlock(&sched->job_list_lock);
223 dma_fence_put(&s_job->s_fence->finished);
224 sched->ops->free_job(s_job);
227 static void drm_sched_job_finish_cb(struct dma_fence *f,
228 struct dma_fence_cb *cb)
230 struct drm_sched_job *job = container_of(cb, struct drm_sched_job,
232 schedule_work(&job->finish_work);
235 static void drm_sched_job_begin(struct drm_sched_job *s_job)
237 struct drm_gpu_scheduler *sched = s_job->sched;
239 dma_fence_add_callback(&s_job->s_fence->finished, &s_job->finish_cb,
240 drm_sched_job_finish_cb);
242 spin_lock(&sched->job_list_lock);
243 list_add_tail(&s_job->node, &sched->ring_mirror_list);
244 drm_sched_start_timeout(sched);
245 spin_unlock(&sched->job_list_lock);
248 static void drm_sched_job_timedout(struct work_struct *work)
250 struct drm_gpu_scheduler *sched;
251 struct drm_sched_job *job;
254 sched = container_of(work, struct drm_gpu_scheduler, work_tdr.work);
256 spin_lock(&sched->job_list_lock);
257 list_for_each_entry_reverse(job, &sched->ring_mirror_list, node) {
258 struct drm_sched_fence *fence = job->s_fence;
260 if (!dma_fence_remove_callback(fence->parent, &fence->cb))
261 goto already_signaled;
264 job = list_first_entry_or_null(&sched->ring_mirror_list,
265 struct drm_sched_job, node);
266 spin_unlock(&sched->job_list_lock);
269 sched->ops->timedout_job(job);
271 spin_lock(&sched->job_list_lock);
272 list_for_each_entry(job, &sched->ring_mirror_list, node) {
273 struct drm_sched_fence *fence = job->s_fence;
275 if (!fence->parent || !list_empty(&fence->cb.node))
278 r = dma_fence_add_callback(fence->parent, &fence->cb,
279 drm_sched_process_job);
281 drm_sched_process_job(fence->parent, &fence->cb);
286 spin_unlock(&sched->job_list_lock);
290 * drm_sched_hw_job_reset - stop the scheduler if it contains the bad job
292 * @sched: scheduler instance
293 * @bad: bad scheduler job
296 void drm_sched_hw_job_reset(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
298 struct drm_sched_job *s_job;
299 struct drm_sched_entity *entity, *tmp;
302 spin_lock(&sched->job_list_lock);
303 list_for_each_entry_reverse(s_job, &sched->ring_mirror_list, node) {
304 if (s_job->s_fence->parent &&
305 dma_fence_remove_callback(s_job->s_fence->parent,
306 &s_job->s_fence->cb)) {
307 dma_fence_put(s_job->s_fence->parent);
308 s_job->s_fence->parent = NULL;
309 atomic_dec(&sched->hw_rq_count);
312 spin_unlock(&sched->job_list_lock);
314 if (bad && bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) {
315 atomic_inc(&bad->karma);
316 /* don't increase @bad's karma if it's from KERNEL RQ,
317 * becuase sometimes GPU hang would cause kernel jobs (like VM updating jobs)
318 * corrupt but keep in mind that kernel jobs always considered good.
320 for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_KERNEL; i++ ) {
321 struct drm_sched_rq *rq = &sched->sched_rq[i];
323 spin_lock(&rq->lock);
324 list_for_each_entry_safe(entity, tmp, &rq->entities, list) {
325 if (bad->s_fence->scheduled.context == entity->fence_context) {
326 if (atomic_read(&bad->karma) > bad->sched->hang_limit)
328 atomic_set(entity->guilty, 1);
332 spin_unlock(&rq->lock);
333 if (&entity->list != &rq->entities)
338 EXPORT_SYMBOL(drm_sched_hw_job_reset);
341 * drm_sched_job_recovery - recover jobs after a reset
343 * @sched: scheduler instance
346 void drm_sched_job_recovery(struct drm_gpu_scheduler *sched)
348 struct drm_sched_job *s_job, *tmp;
349 bool found_guilty = false;
352 spin_lock(&sched->job_list_lock);
353 list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
354 struct drm_sched_fence *s_fence = s_job->s_fence;
355 struct dma_fence *fence;
356 uint64_t guilty_context;
358 if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) {
360 guilty_context = s_job->s_fence->scheduled.context;
363 if (found_guilty && s_job->s_fence->scheduled.context == guilty_context)
364 dma_fence_set_error(&s_fence->finished, -ECANCELED);
366 spin_unlock(&sched->job_list_lock);
367 fence = sched->ops->run_job(s_job);
368 atomic_inc(&sched->hw_rq_count);
371 s_fence->parent = dma_fence_get(fence);
372 r = dma_fence_add_callback(fence, &s_fence->cb,
373 drm_sched_process_job);
375 drm_sched_process_job(fence, &s_fence->cb);
377 DRM_ERROR("fence add callback failed (%d)\n",
379 dma_fence_put(fence);
381 drm_sched_process_job(NULL, &s_fence->cb);
383 spin_lock(&sched->job_list_lock);
385 drm_sched_start_timeout(sched);
386 spin_unlock(&sched->job_list_lock);
388 EXPORT_SYMBOL(drm_sched_job_recovery);
391 * drm_sched_job_init - init a scheduler job
393 * @job: scheduler job to init
394 * @entity: scheduler entity to use
395 * @owner: job owner for debugging
397 * Refer to drm_sched_entity_push_job() documentation
398 * for locking considerations.
400 * Returns 0 for success, negative error code otherwise.
402 int drm_sched_job_init(struct drm_sched_job *job,
403 struct drm_sched_entity *entity,
406 struct drm_gpu_scheduler *sched;
408 drm_sched_entity_select_rq(entity);
409 sched = entity->rq->sched;
412 job->entity = entity;
413 job->s_priority = entity->rq - sched->sched_rq;
414 job->s_fence = drm_sched_fence_create(entity, owner);
417 job->id = atomic64_inc_return(&sched->job_id_count);
419 INIT_WORK(&job->finish_work, drm_sched_job_finish);
420 INIT_LIST_HEAD(&job->node);
424 EXPORT_SYMBOL(drm_sched_job_init);
427 * drm_sched_ready - is the scheduler ready
429 * @sched: scheduler instance
431 * Return true if we can push more jobs to the hw, otherwise false.
433 static bool drm_sched_ready(struct drm_gpu_scheduler *sched)
435 return atomic_read(&sched->hw_rq_count) <
436 sched->hw_submission_limit;
440 * drm_sched_wakeup - Wake up the scheduler when it is ready
442 * @sched: scheduler instance
445 void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
447 if (drm_sched_ready(sched))
448 wake_up_interruptible(&sched->wake_up_worker);
452 * drm_sched_select_entity - Select next entity to process
454 * @sched: scheduler instance
456 * Returns the entity to process or NULL if none are found.
458 static struct drm_sched_entity *
459 drm_sched_select_entity(struct drm_gpu_scheduler *sched)
461 struct drm_sched_entity *entity;
464 if (!drm_sched_ready(sched))
467 /* Kernel run queue has higher priority than normal run queue*/
468 for (i = DRM_SCHED_PRIORITY_MAX - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
469 entity = drm_sched_rq_select_entity(&sched->sched_rq[i]);
478 * drm_sched_process_job - process a job
481 * @cb: fence callbacks
483 * Called after job has finished execution.
485 static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
487 struct drm_sched_fence *s_fence =
488 container_of(cb, struct drm_sched_fence, cb);
489 struct drm_gpu_scheduler *sched = s_fence->sched;
491 dma_fence_get(&s_fence->finished);
492 atomic_dec(&sched->hw_rq_count);
493 atomic_dec(&sched->num_jobs);
494 drm_sched_fence_finished(s_fence);
496 trace_drm_sched_process_job(s_fence);
497 dma_fence_put(&s_fence->finished);
498 wake_up_interruptible(&sched->wake_up_worker);
502 * drm_sched_blocked - check if the scheduler is blocked
504 * @sched: scheduler instance
506 * Returns true if blocked, otherwise false.
508 static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
510 if (kthread_should_park()) {
519 * drm_sched_main - main scheduler thread
521 * @param: scheduler instance
525 static int drm_sched_main(void *param)
527 struct sched_param sparam = {.sched_priority = 1};
528 struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
531 sched_setscheduler(current, SCHED_FIFO, &sparam);
533 while (!kthread_should_stop()) {
534 struct drm_sched_entity *entity = NULL;
535 struct drm_sched_fence *s_fence;
536 struct drm_sched_job *sched_job;
537 struct dma_fence *fence;
539 wait_event_interruptible(sched->wake_up_worker,
540 (!drm_sched_blocked(sched) &&
541 (entity = drm_sched_select_entity(sched))) ||
542 kthread_should_stop());
547 sched_job = drm_sched_entity_pop_job(entity);
551 s_fence = sched_job->s_fence;
553 atomic_inc(&sched->hw_rq_count);
554 drm_sched_job_begin(sched_job);
556 fence = sched->ops->run_job(sched_job);
557 drm_sched_fence_scheduled(s_fence);
560 s_fence->parent = dma_fence_get(fence);
561 r = dma_fence_add_callback(fence, &s_fence->cb,
562 drm_sched_process_job);
564 drm_sched_process_job(fence, &s_fence->cb);
566 DRM_ERROR("fence add callback failed (%d)\n",
568 dma_fence_put(fence);
570 drm_sched_process_job(NULL, &s_fence->cb);
573 wake_up(&sched->job_scheduled);
579 * drm_sched_init - Init a gpu scheduler instance
581 * @sched: scheduler instance
582 * @ops: backend operations for this scheduler
583 * @hw_submission: number of hw submissions that can be in flight
584 * @hang_limit: number of times to allow a job to hang before dropping it
585 * @timeout: timeout value in jiffies for the scheduler
586 * @name: name used for debugging
588 * Return 0 on success, otherwise error code.
590 int drm_sched_init(struct drm_gpu_scheduler *sched,
591 const struct drm_sched_backend_ops *ops,
592 unsigned hw_submission,
599 sched->hw_submission_limit = hw_submission;
601 sched->timeout = timeout;
602 sched->hang_limit = hang_limit;
603 for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_MAX; i++)
604 drm_sched_rq_init(sched, &sched->sched_rq[i]);
606 init_waitqueue_head(&sched->wake_up_worker);
607 init_waitqueue_head(&sched->job_scheduled);
608 INIT_LIST_HEAD(&sched->ring_mirror_list);
609 spin_lock_init(&sched->job_list_lock);
610 atomic_set(&sched->hw_rq_count, 0);
611 INIT_DELAYED_WORK(&sched->work_tdr, drm_sched_job_timedout);
612 atomic_set(&sched->num_jobs, 0);
613 atomic64_set(&sched->job_id_count, 0);
615 /* Each scheduler will run on a seperate kernel thread */
616 sched->thread = kthread_run(drm_sched_main, sched, sched->name);
617 if (IS_ERR(sched->thread)) {
618 DRM_ERROR("Failed to create scheduler for %s.\n", name);
619 return PTR_ERR(sched->thread);
624 EXPORT_SYMBOL(drm_sched_init);
627 * drm_sched_fini - Destroy a gpu scheduler
629 * @sched: scheduler instance
631 * Tears down and cleans up the scheduler.
633 void drm_sched_fini(struct drm_gpu_scheduler *sched)
636 kthread_stop(sched->thread);
638 EXPORT_SYMBOL(drm_sched_fini);