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 <linux/completion.h>
51 #include <linux/dma-resv.h>
52 #include <uapi/linux/sched/types.h>
54 #include <drm/drm_print.h>
55 #include <drm/drm_gem.h>
56 #include <drm/drm_syncobj.h>
57 #include <drm/gpu_scheduler.h>
58 #include <drm/spsc_queue.h>
60 #define CREATE_TRACE_POINTS
61 #include "gpu_scheduler_trace.h"
63 #define to_drm_sched_job(sched_job) \
64 container_of((sched_job), struct drm_sched_job, queue_node)
66 int drm_sched_policy = DRM_SCHED_POLICY_FIFO;
69 * DOC: sched_policy (int)
70 * Used to override default entities scheduling policy in a run queue.
72 MODULE_PARM_DESC(sched_policy, "Specify the scheduling policy for entities on a run-queue, " __stringify(DRM_SCHED_POLICY_RR) " = Round Robin, " __stringify(DRM_SCHED_POLICY_FIFO) " = FIFO (default).");
73 module_param_named(sched_policy, drm_sched_policy, int, 0444);
75 static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a,
76 const struct rb_node *b)
78 struct drm_sched_entity *ent_a = rb_entry((a), struct drm_sched_entity, rb_tree_node);
79 struct drm_sched_entity *ent_b = rb_entry((b), struct drm_sched_entity, rb_tree_node);
81 return ktime_before(ent_a->oldest_job_waiting, ent_b->oldest_job_waiting);
84 static inline void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *entity)
86 struct drm_sched_rq *rq = entity->rq;
88 if (!RB_EMPTY_NODE(&entity->rb_tree_node)) {
89 rb_erase_cached(&entity->rb_tree_node, &rq->rb_tree_root);
90 RB_CLEAR_NODE(&entity->rb_tree_node);
94 void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
97 * Both locks need to be grabbed, one to protect from entity->rq change
98 * for entity from within concurrent drm_sched_entity_select_rq and the
99 * other to update the rb tree structure.
101 spin_lock(&entity->rq_lock);
102 spin_lock(&entity->rq->lock);
104 drm_sched_rq_remove_fifo_locked(entity);
106 entity->oldest_job_waiting = ts;
108 rb_add_cached(&entity->rb_tree_node, &entity->rq->rb_tree_root,
109 drm_sched_entity_compare_before);
111 spin_unlock(&entity->rq->lock);
112 spin_unlock(&entity->rq_lock);
116 * drm_sched_rq_init - initialize a given run queue struct
118 * @sched: scheduler instance to associate with this run queue
119 * @rq: scheduler run queue
121 * Initializes a scheduler runqueue.
123 static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
124 struct drm_sched_rq *rq)
126 spin_lock_init(&rq->lock);
127 INIT_LIST_HEAD(&rq->entities);
128 rq->rb_tree_root = RB_ROOT_CACHED;
129 rq->current_entity = NULL;
134 * drm_sched_rq_add_entity - add an entity
136 * @rq: scheduler run queue
137 * @entity: scheduler entity
139 * Adds a scheduler entity to the run queue.
141 void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
142 struct drm_sched_entity *entity)
144 if (!list_empty(&entity->list))
147 spin_lock(&rq->lock);
149 atomic_inc(rq->sched->score);
150 list_add_tail(&entity->list, &rq->entities);
152 spin_unlock(&rq->lock);
156 * drm_sched_rq_remove_entity - remove an entity
158 * @rq: scheduler run queue
159 * @entity: scheduler entity
161 * Removes a scheduler entity from the run queue.
163 void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
164 struct drm_sched_entity *entity)
166 if (list_empty(&entity->list))
169 spin_lock(&rq->lock);
171 atomic_dec(rq->sched->score);
172 list_del_init(&entity->list);
174 if (rq->current_entity == entity)
175 rq->current_entity = NULL;
177 if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
178 drm_sched_rq_remove_fifo_locked(entity);
180 spin_unlock(&rq->lock);
184 * drm_sched_rq_select_entity_rr - Select an entity which could provide a job to run
186 * @rq: scheduler run queue to check.
188 * Try to find a ready entity, returns NULL if none found.
190 static struct drm_sched_entity *
191 drm_sched_rq_select_entity_rr(struct drm_sched_rq *rq)
193 struct drm_sched_entity *entity;
195 spin_lock(&rq->lock);
197 entity = rq->current_entity;
199 list_for_each_entry_continue(entity, &rq->entities, list) {
200 if (drm_sched_entity_is_ready(entity)) {
201 rq->current_entity = entity;
202 reinit_completion(&entity->entity_idle);
203 spin_unlock(&rq->lock);
209 list_for_each_entry(entity, &rq->entities, list) {
211 if (drm_sched_entity_is_ready(entity)) {
212 rq->current_entity = entity;
213 reinit_completion(&entity->entity_idle);
214 spin_unlock(&rq->lock);
218 if (entity == rq->current_entity)
222 spin_unlock(&rq->lock);
228 * drm_sched_rq_select_entity_fifo - Select an entity which provides a job to run
230 * @rq: scheduler run queue to check.
232 * Find oldest waiting ready entity, returns NULL if none found.
234 static struct drm_sched_entity *
235 drm_sched_rq_select_entity_fifo(struct drm_sched_rq *rq)
239 spin_lock(&rq->lock);
240 for (rb = rb_first_cached(&rq->rb_tree_root); rb; rb = rb_next(rb)) {
241 struct drm_sched_entity *entity;
243 entity = rb_entry(rb, struct drm_sched_entity, rb_tree_node);
244 if (drm_sched_entity_is_ready(entity)) {
245 rq->current_entity = entity;
246 reinit_completion(&entity->entity_idle);
250 spin_unlock(&rq->lock);
252 return rb ? rb_entry(rb, struct drm_sched_entity, rb_tree_node) : NULL;
256 * drm_sched_job_done - complete a job
257 * @s_job: pointer to the job which is done
259 * Finish the job's fence and wake up the worker thread.
261 static void drm_sched_job_done(struct drm_sched_job *s_job)
263 struct drm_sched_fence *s_fence = s_job->s_fence;
264 struct drm_gpu_scheduler *sched = s_fence->sched;
266 atomic_dec(&sched->hw_rq_count);
267 atomic_dec(sched->score);
269 trace_drm_sched_process_job(s_fence);
271 dma_fence_get(&s_fence->finished);
272 drm_sched_fence_finished(s_fence);
273 dma_fence_put(&s_fence->finished);
274 wake_up_interruptible(&sched->wake_up_worker);
278 * drm_sched_job_done_cb - the callback for a done job
280 * @cb: fence callbacks
282 static void drm_sched_job_done_cb(struct dma_fence *f, struct dma_fence_cb *cb)
284 struct drm_sched_job *s_job = container_of(cb, struct drm_sched_job, cb);
286 drm_sched_job_done(s_job);
290 * drm_sched_start_timeout - start timeout for reset worker
292 * @sched: scheduler instance to start the worker for
294 * Start the timeout for the given scheduler.
296 static void drm_sched_start_timeout(struct drm_gpu_scheduler *sched)
298 if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
299 !list_empty(&sched->pending_list))
300 queue_delayed_work(sched->timeout_wq, &sched->work_tdr, sched->timeout);
304 * drm_sched_fault - immediately start timeout handler
306 * @sched: scheduler where the timeout handling should be started.
308 * Start timeout handling immediately when the driver detects a hardware fault.
310 void drm_sched_fault(struct drm_gpu_scheduler *sched)
312 if (sched->timeout_wq)
313 mod_delayed_work(sched->timeout_wq, &sched->work_tdr, 0);
315 EXPORT_SYMBOL(drm_sched_fault);
318 * drm_sched_suspend_timeout - Suspend scheduler job timeout
320 * @sched: scheduler instance for which to suspend the timeout
322 * Suspend the delayed work timeout for the scheduler. This is done by
323 * modifying the delayed work timeout to an arbitrary large value,
324 * MAX_SCHEDULE_TIMEOUT in this case.
326 * Returns the timeout remaining
329 unsigned long drm_sched_suspend_timeout(struct drm_gpu_scheduler *sched)
331 unsigned long sched_timeout, now = jiffies;
333 sched_timeout = sched->work_tdr.timer.expires;
336 * Modify the timeout to an arbitrarily large value. This also prevents
337 * the timeout to be restarted when new submissions arrive
339 if (mod_delayed_work(sched->timeout_wq, &sched->work_tdr, MAX_SCHEDULE_TIMEOUT)
340 && time_after(sched_timeout, now))
341 return sched_timeout - now;
343 return sched->timeout;
345 EXPORT_SYMBOL(drm_sched_suspend_timeout);
348 * drm_sched_resume_timeout - Resume scheduler job timeout
350 * @sched: scheduler instance for which to resume the timeout
351 * @remaining: remaining timeout
353 * Resume the delayed work timeout for the scheduler.
355 void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched,
356 unsigned long remaining)
358 spin_lock(&sched->job_list_lock);
360 if (list_empty(&sched->pending_list))
361 cancel_delayed_work(&sched->work_tdr);
363 mod_delayed_work(sched->timeout_wq, &sched->work_tdr, remaining);
365 spin_unlock(&sched->job_list_lock);
367 EXPORT_SYMBOL(drm_sched_resume_timeout);
369 static void drm_sched_job_begin(struct drm_sched_job *s_job)
371 struct drm_gpu_scheduler *sched = s_job->sched;
373 spin_lock(&sched->job_list_lock);
374 list_add_tail(&s_job->list, &sched->pending_list);
375 drm_sched_start_timeout(sched);
376 spin_unlock(&sched->job_list_lock);
379 static void drm_sched_job_timedout(struct work_struct *work)
381 struct drm_gpu_scheduler *sched;
382 struct drm_sched_job *job;
383 enum drm_gpu_sched_stat status = DRM_GPU_SCHED_STAT_NOMINAL;
385 sched = container_of(work, struct drm_gpu_scheduler, work_tdr.work);
387 /* Protects against concurrent deletion in drm_sched_get_cleanup_job */
388 spin_lock(&sched->job_list_lock);
389 job = list_first_entry_or_null(&sched->pending_list,
390 struct drm_sched_job, list);
394 * Remove the bad job so it cannot be freed by concurrent
395 * drm_sched_cleanup_jobs. It will be reinserted back after sched->thread
396 * is parked at which point it's safe.
398 list_del_init(&job->list);
399 spin_unlock(&sched->job_list_lock);
401 status = job->sched->ops->timedout_job(job);
404 * Guilty job did complete and hence needs to be manually removed
405 * See drm_sched_stop doc.
407 if (sched->free_guilty) {
408 job->sched->ops->free_job(job);
409 sched->free_guilty = false;
412 spin_unlock(&sched->job_list_lock);
415 if (status != DRM_GPU_SCHED_STAT_ENODEV) {
416 spin_lock(&sched->job_list_lock);
417 drm_sched_start_timeout(sched);
418 spin_unlock(&sched->job_list_lock);
423 * drm_sched_stop - stop the scheduler
425 * @sched: scheduler instance
426 * @bad: job which caused the time out
428 * Stop the scheduler and also removes and frees all completed jobs.
429 * Note: bad job will not be freed as it might be used later and so it's
430 * callers responsibility to release it manually if it's not part of the
431 * pending list any more.
434 void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
436 struct drm_sched_job *s_job, *tmp;
438 kthread_park(sched->thread);
441 * Reinsert back the bad job here - now it's safe as
442 * drm_sched_get_cleanup_job cannot race against us and release the
443 * bad job at this point - we parked (waited for) any in progress
444 * (earlier) cleanups and drm_sched_get_cleanup_job will not be called
445 * now until the scheduler thread is unparked.
447 if (bad && bad->sched == sched)
449 * Add at the head of the queue to reflect it was the earliest
452 list_add(&bad->list, &sched->pending_list);
455 * Iterate the job list from later to earlier one and either deactive
456 * their HW callbacks or remove them from pending list if they already
458 * This iteration is thread safe as sched thread is stopped.
460 list_for_each_entry_safe_reverse(s_job, tmp, &sched->pending_list,
462 if (s_job->s_fence->parent &&
463 dma_fence_remove_callback(s_job->s_fence->parent,
465 dma_fence_put(s_job->s_fence->parent);
466 s_job->s_fence->parent = NULL;
467 atomic_dec(&sched->hw_rq_count);
470 * remove job from pending_list.
471 * Locking here is for concurrent resume timeout
473 spin_lock(&sched->job_list_lock);
474 list_del_init(&s_job->list);
475 spin_unlock(&sched->job_list_lock);
478 * Wait for job's HW fence callback to finish using s_job
479 * before releasing it.
481 * Job is still alive so fence refcount at least 1
483 dma_fence_wait(&s_job->s_fence->finished, false);
486 * We must keep bad job alive for later use during
487 * recovery by some of the drivers but leave a hint
488 * that the guilty job must be released.
491 sched->ops->free_job(s_job);
493 sched->free_guilty = true;
498 * Stop pending timer in flight as we rearm it in drm_sched_start. This
499 * avoids the pending timeout work in progress to fire right away after
500 * this TDR finished and before the newly restarted jobs had a
501 * chance to complete.
503 cancel_delayed_work(&sched->work_tdr);
506 EXPORT_SYMBOL(drm_sched_stop);
509 * drm_sched_start - recover jobs after a reset
511 * @sched: scheduler instance
512 * @full_recovery: proceed with complete sched restart
515 void drm_sched_start(struct drm_gpu_scheduler *sched, bool full_recovery)
517 struct drm_sched_job *s_job, *tmp;
521 * Locking the list is not required here as the sched thread is parked
522 * so no new jobs are being inserted or removed. Also concurrent
523 * GPU recovers can't run in parallel.
525 list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
526 struct dma_fence *fence = s_job->s_fence->parent;
528 atomic_inc(&sched->hw_rq_count);
534 r = dma_fence_add_callback(fence, &s_job->cb,
535 drm_sched_job_done_cb);
537 drm_sched_job_done(s_job);
539 DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n",
542 drm_sched_job_done(s_job);
546 spin_lock(&sched->job_list_lock);
547 drm_sched_start_timeout(sched);
548 spin_unlock(&sched->job_list_lock);
551 kthread_unpark(sched->thread);
553 EXPORT_SYMBOL(drm_sched_start);
556 * drm_sched_resubmit_jobs - Deprecated, don't use in new code!
558 * @sched: scheduler instance
560 * Re-submitting jobs was a concept AMD came up as cheap way to implement
561 * recovery after a job timeout.
563 * This turned out to be not working very well. First of all there are many
564 * problem with the dma_fence implementation and requirements. Either the
565 * implementation is risking deadlocks with core memory management or violating
566 * documented implementation details of the dma_fence object.
568 * Drivers can still save and restore their state for recovery operations, but
569 * we shouldn't make this a general scheduler feature around the dma_fence
572 void drm_sched_resubmit_jobs(struct drm_gpu_scheduler *sched)
574 struct drm_sched_job *s_job, *tmp;
575 uint64_t guilty_context;
576 bool found_guilty = false;
577 struct dma_fence *fence;
579 list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
580 struct drm_sched_fence *s_fence = s_job->s_fence;
582 if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) {
584 guilty_context = s_job->s_fence->scheduled.context;
587 if (found_guilty && s_job->s_fence->scheduled.context == guilty_context)
588 dma_fence_set_error(&s_fence->finished, -ECANCELED);
590 fence = sched->ops->run_job(s_job);
592 if (IS_ERR_OR_NULL(fence)) {
594 dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
596 s_job->s_fence->parent = NULL;
599 s_job->s_fence->parent = dma_fence_get(fence);
601 /* Drop for orignal kref_init */
602 dma_fence_put(fence);
606 EXPORT_SYMBOL(drm_sched_resubmit_jobs);
609 * drm_sched_job_init - init a scheduler job
610 * @job: scheduler job to init
611 * @entity: scheduler entity to use
612 * @owner: job owner for debugging
614 * Refer to drm_sched_entity_push_job() documentation
615 * for locking considerations.
617 * Drivers must make sure drm_sched_job_cleanup() if this function returns
618 * successfully, even when @job is aborted before drm_sched_job_arm() is called.
620 * WARNING: amdgpu abuses &drm_sched.ready to signal when the hardware
621 * has died, which can mean that there's no valid runqueue for a @entity.
622 * This function returns -ENOENT in this case (which probably should be -EIO as
623 * a more meanigful return value).
625 * Returns 0 for success, negative error code otherwise.
627 int drm_sched_job_init(struct drm_sched_job *job,
628 struct drm_sched_entity *entity,
634 job->entity = entity;
635 job->s_fence = drm_sched_fence_alloc(entity, owner);
639 INIT_LIST_HEAD(&job->list);
641 xa_init_flags(&job->dependencies, XA_FLAGS_ALLOC);
645 EXPORT_SYMBOL(drm_sched_job_init);
648 * drm_sched_job_arm - arm a scheduler job for execution
649 * @job: scheduler job to arm
651 * This arms a scheduler job for execution. Specifically it initializes the
652 * &drm_sched_job.s_fence of @job, so that it can be attached to struct dma_resv
653 * or other places that need to track the completion of this job.
655 * Refer to drm_sched_entity_push_job() documentation for locking
658 * This can only be called if drm_sched_job_init() succeeded.
660 void drm_sched_job_arm(struct drm_sched_job *job)
662 struct drm_gpu_scheduler *sched;
663 struct drm_sched_entity *entity = job->entity;
666 drm_sched_entity_select_rq(entity);
667 sched = entity->rq->sched;
670 job->s_priority = entity->rq - sched->sched_rq;
671 job->id = atomic64_inc_return(&sched->job_id_count);
673 drm_sched_fence_init(job->s_fence, job->entity);
675 EXPORT_SYMBOL(drm_sched_job_arm);
678 * drm_sched_job_add_dependency - adds the fence as a job dependency
679 * @job: scheduler job to add the dependencies to
680 * @fence: the dma_fence to add to the list of dependencies.
682 * Note that @fence is consumed in both the success and error cases.
685 * 0 on success, or an error on failing to expand the array.
687 int drm_sched_job_add_dependency(struct drm_sched_job *job,
688 struct dma_fence *fence)
690 struct dma_fence *entry;
698 /* Deduplicate if we already depend on a fence from the same context.
699 * This lets the size of the array of deps scale with the number of
700 * engines involved, rather than the number of BOs.
702 xa_for_each(&job->dependencies, index, entry) {
703 if (entry->context != fence->context)
706 if (dma_fence_is_later(fence, entry)) {
707 dma_fence_put(entry);
708 xa_store(&job->dependencies, index, fence, GFP_KERNEL);
710 dma_fence_put(fence);
715 ret = xa_alloc(&job->dependencies, &id, fence, xa_limit_32b, GFP_KERNEL);
717 dma_fence_put(fence);
721 EXPORT_SYMBOL(drm_sched_job_add_dependency);
724 * drm_sched_job_add_syncobj_dependency - adds a syncobj's fence as a job dependency
725 * @job: scheduler job to add the dependencies to
726 * @file: drm file private pointer
727 * @handle: syncobj handle to lookup
728 * @point: timeline point
730 * This adds the fence matching the given syncobj to @job.
733 * 0 on success, or an error on failing to expand the array.
735 int drm_sched_job_add_syncobj_dependency(struct drm_sched_job *job,
736 struct drm_file *file,
740 struct dma_fence *fence;
743 ret = drm_syncobj_find_fence(file, handle, point, 0, &fence);
747 return drm_sched_job_add_dependency(job, fence);
749 EXPORT_SYMBOL(drm_sched_job_add_syncobj_dependency);
752 * drm_sched_job_add_resv_dependencies - add all fences from the resv to the job
753 * @job: scheduler job to add the dependencies to
754 * @resv: the dma_resv object to get the fences from
755 * @usage: the dma_resv_usage to use to filter the fences
757 * This adds all fences matching the given usage from @resv to @job.
758 * Must be called with the @resv lock held.
761 * 0 on success, or an error on failing to expand the array.
763 int drm_sched_job_add_resv_dependencies(struct drm_sched_job *job,
764 struct dma_resv *resv,
765 enum dma_resv_usage usage)
767 struct dma_resv_iter cursor;
768 struct dma_fence *fence;
771 dma_resv_assert_held(resv);
773 dma_resv_for_each_fence(&cursor, resv, usage, fence) {
774 /* Make sure to grab an additional ref on the added fence */
775 dma_fence_get(fence);
776 ret = drm_sched_job_add_dependency(job, fence);
778 dma_fence_put(fence);
784 EXPORT_SYMBOL(drm_sched_job_add_resv_dependencies);
787 * drm_sched_job_add_implicit_dependencies - adds implicit dependencies as job
789 * @job: scheduler job to add the dependencies to
790 * @obj: the gem object to add new dependencies from.
791 * @write: whether the job might write the object (so we need to depend on
792 * shared fences in the reservation object).
794 * This should be called after drm_gem_lock_reservations() on your array of
795 * GEM objects used in the job but before updating the reservations with your
799 * 0 on success, or an error on failing to expand the array.
801 int drm_sched_job_add_implicit_dependencies(struct drm_sched_job *job,
802 struct drm_gem_object *obj,
805 return drm_sched_job_add_resv_dependencies(job, obj->resv,
806 dma_resv_usage_rw(write));
808 EXPORT_SYMBOL(drm_sched_job_add_implicit_dependencies);
811 * drm_sched_job_cleanup - clean up scheduler job resources
812 * @job: scheduler job to clean up
814 * Cleans up the resources allocated with drm_sched_job_init().
816 * Drivers should call this from their error unwind code if @job is aborted
817 * before drm_sched_job_arm() is called.
819 * After that point of no return @job is committed to be executed by the
820 * scheduler, and this function should be called from the
821 * &drm_sched_backend_ops.free_job callback.
823 void drm_sched_job_cleanup(struct drm_sched_job *job)
825 struct dma_fence *fence;
828 if (kref_read(&job->s_fence->finished.refcount)) {
829 /* drm_sched_job_arm() has been called */
830 dma_fence_put(&job->s_fence->finished);
832 /* aborted job before committing to run it */
833 drm_sched_fence_free(job->s_fence);
838 xa_for_each(&job->dependencies, index, fence) {
839 dma_fence_put(fence);
841 xa_destroy(&job->dependencies);
844 EXPORT_SYMBOL(drm_sched_job_cleanup);
847 * drm_sched_ready - is the scheduler ready
849 * @sched: scheduler instance
851 * Return true if we can push more jobs to the hw, otherwise false.
853 static bool drm_sched_ready(struct drm_gpu_scheduler *sched)
855 return atomic_read(&sched->hw_rq_count) <
856 sched->hw_submission_limit;
860 * drm_sched_wakeup - Wake up the scheduler when it is ready
862 * @sched: scheduler instance
865 void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
867 if (drm_sched_ready(sched))
868 wake_up_interruptible(&sched->wake_up_worker);
872 * drm_sched_select_entity - Select next entity to process
874 * @sched: scheduler instance
876 * Returns the entity to process or NULL if none are found.
878 static struct drm_sched_entity *
879 drm_sched_select_entity(struct drm_gpu_scheduler *sched)
881 struct drm_sched_entity *entity;
884 if (!drm_sched_ready(sched))
887 /* Kernel run queue has higher priority than normal run queue*/
888 for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
889 entity = drm_sched_policy == DRM_SCHED_POLICY_FIFO ?
890 drm_sched_rq_select_entity_fifo(&sched->sched_rq[i]) :
891 drm_sched_rq_select_entity_rr(&sched->sched_rq[i]);
900 * drm_sched_get_cleanup_job - fetch the next finished job to be destroyed
902 * @sched: scheduler instance
904 * Returns the next finished job from the pending list (if there is one)
905 * ready for it to be destroyed.
907 static struct drm_sched_job *
908 drm_sched_get_cleanup_job(struct drm_gpu_scheduler *sched)
910 struct drm_sched_job *job, *next;
912 spin_lock(&sched->job_list_lock);
914 job = list_first_entry_or_null(&sched->pending_list,
915 struct drm_sched_job, list);
917 if (job && dma_fence_is_signaled(&job->s_fence->finished)) {
918 /* remove job from pending_list */
919 list_del_init(&job->list);
921 /* cancel this job's TO timer */
922 cancel_delayed_work(&sched->work_tdr);
923 /* make the scheduled timestamp more accurate */
924 next = list_first_entry_or_null(&sched->pending_list,
925 typeof(*next), list);
928 next->s_fence->scheduled.timestamp =
929 job->s_fence->finished.timestamp;
930 /* start TO timer for next job */
931 drm_sched_start_timeout(sched);
937 spin_unlock(&sched->job_list_lock);
943 * drm_sched_pick_best - Get a drm sched from a sched_list with the least load
944 * @sched_list: list of drm_gpu_schedulers
945 * @num_sched_list: number of drm_gpu_schedulers in the sched_list
947 * Returns pointer of the sched with the least load or NULL if none of the
948 * drm_gpu_schedulers are ready
950 struct drm_gpu_scheduler *
951 drm_sched_pick_best(struct drm_gpu_scheduler **sched_list,
952 unsigned int num_sched_list)
954 struct drm_gpu_scheduler *sched, *picked_sched = NULL;
956 unsigned int min_score = UINT_MAX, num_score;
958 for (i = 0; i < num_sched_list; ++i) {
959 sched = sched_list[i];
962 DRM_WARN("scheduler %s is not ready, skipping",
967 num_score = atomic_read(sched->score);
968 if (num_score < min_score) {
969 min_score = num_score;
970 picked_sched = sched;
976 EXPORT_SYMBOL(drm_sched_pick_best);
979 * drm_sched_blocked - check if the scheduler is blocked
981 * @sched: scheduler instance
983 * Returns true if blocked, otherwise false.
985 static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
987 if (kthread_should_park()) {
996 * drm_sched_main - main scheduler thread
998 * @param: scheduler instance
1002 static int drm_sched_main(void *param)
1004 struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
1007 sched_set_fifo_low(current);
1009 while (!kthread_should_stop()) {
1010 struct drm_sched_entity *entity = NULL;
1011 struct drm_sched_fence *s_fence;
1012 struct drm_sched_job *sched_job;
1013 struct dma_fence *fence;
1014 struct drm_sched_job *cleanup_job = NULL;
1016 wait_event_interruptible(sched->wake_up_worker,
1017 (cleanup_job = drm_sched_get_cleanup_job(sched)) ||
1018 (!drm_sched_blocked(sched) &&
1019 (entity = drm_sched_select_entity(sched))) ||
1020 kthread_should_stop());
1023 sched->ops->free_job(cleanup_job);
1028 sched_job = drm_sched_entity_pop_job(entity);
1031 complete_all(&entity->entity_idle);
1035 s_fence = sched_job->s_fence;
1037 atomic_inc(&sched->hw_rq_count);
1038 drm_sched_job_begin(sched_job);
1040 trace_drm_run_job(sched_job, entity);
1041 fence = sched->ops->run_job(sched_job);
1042 complete_all(&entity->entity_idle);
1043 drm_sched_fence_scheduled(s_fence);
1045 if (!IS_ERR_OR_NULL(fence)) {
1046 drm_sched_fence_set_parent(s_fence, fence);
1047 /* Drop for original kref_init of the fence */
1048 dma_fence_put(fence);
1050 r = dma_fence_add_callback(fence, &sched_job->cb,
1051 drm_sched_job_done_cb);
1053 drm_sched_job_done(sched_job);
1055 DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n",
1059 dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
1061 drm_sched_job_done(sched_job);
1064 wake_up(&sched->job_scheduled);
1070 * drm_sched_init - Init a gpu scheduler instance
1072 * @sched: scheduler instance
1073 * @ops: backend operations for this scheduler
1074 * @hw_submission: number of hw submissions that can be in flight
1075 * @hang_limit: number of times to allow a job to hang before dropping it
1076 * @timeout: timeout value in jiffies for the scheduler
1077 * @timeout_wq: workqueue to use for timeout work. If NULL, the system_wq is
1079 * @score: optional score atomic shared with other schedulers
1080 * @name: name used for debugging
1081 * @dev: target &struct device
1083 * Return 0 on success, otherwise error code.
1085 int drm_sched_init(struct drm_gpu_scheduler *sched,
1086 const struct drm_sched_backend_ops *ops,
1087 unsigned hw_submission, unsigned hang_limit,
1088 long timeout, struct workqueue_struct *timeout_wq,
1089 atomic_t *score, const char *name, struct device *dev)
1093 sched->hw_submission_limit = hw_submission;
1095 sched->timeout = timeout;
1096 sched->timeout_wq = timeout_wq ? : system_wq;
1097 sched->hang_limit = hang_limit;
1098 sched->score = score ? score : &sched->_score;
1100 for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_COUNT; i++)
1101 drm_sched_rq_init(sched, &sched->sched_rq[i]);
1103 init_waitqueue_head(&sched->wake_up_worker);
1104 init_waitqueue_head(&sched->job_scheduled);
1105 INIT_LIST_HEAD(&sched->pending_list);
1106 spin_lock_init(&sched->job_list_lock);
1107 atomic_set(&sched->hw_rq_count, 0);
1108 INIT_DELAYED_WORK(&sched->work_tdr, drm_sched_job_timedout);
1109 atomic_set(&sched->_score, 0);
1110 atomic64_set(&sched->job_id_count, 0);
1112 /* Each scheduler will run on a seperate kernel thread */
1113 sched->thread = kthread_run(drm_sched_main, sched, sched->name);
1114 if (IS_ERR(sched->thread)) {
1115 ret = PTR_ERR(sched->thread);
1116 sched->thread = NULL;
1117 DRM_DEV_ERROR(sched->dev, "Failed to create scheduler for %s.\n", name);
1121 sched->ready = true;
1124 EXPORT_SYMBOL(drm_sched_init);
1127 * drm_sched_fini - Destroy a gpu scheduler
1129 * @sched: scheduler instance
1131 * Tears down and cleans up the scheduler.
1133 void drm_sched_fini(struct drm_gpu_scheduler *sched)
1135 struct drm_sched_entity *s_entity;
1139 kthread_stop(sched->thread);
1141 for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
1142 struct drm_sched_rq *rq = &sched->sched_rq[i];
1147 spin_lock(&rq->lock);
1148 list_for_each_entry(s_entity, &rq->entities, list)
1150 * Prevents reinsertion and marks job_queue as idle,
1151 * it will removed from rq in drm_sched_entity_fini
1154 s_entity->stopped = true;
1155 spin_unlock(&rq->lock);
1159 /* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */
1160 wake_up_all(&sched->job_scheduled);
1162 /* Confirm no work left behind accessing device structures */
1163 cancel_delayed_work_sync(&sched->work_tdr);
1165 sched->ready = false;
1167 EXPORT_SYMBOL(drm_sched_fini);
1170 * drm_sched_increase_karma - Update sched_entity guilty flag
1172 * @bad: The job guilty of time out
1174 * Increment on every hang caused by the 'bad' job. If this exceeds the hang
1175 * limit of the scheduler then the respective sched entity is marked guilty and
1176 * jobs from it will not be scheduled further
1178 void drm_sched_increase_karma(struct drm_sched_job *bad)
1181 struct drm_sched_entity *tmp;
1182 struct drm_sched_entity *entity;
1183 struct drm_gpu_scheduler *sched = bad->sched;
1185 /* don't change @bad's karma if it's from KERNEL RQ,
1186 * because sometimes GPU hang would cause kernel jobs (like VM updating jobs)
1187 * corrupt but keep in mind that kernel jobs always considered good.
1189 if (bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) {
1190 atomic_inc(&bad->karma);
1192 for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_KERNEL;
1194 struct drm_sched_rq *rq = &sched->sched_rq[i];
1196 spin_lock(&rq->lock);
1197 list_for_each_entry_safe(entity, tmp, &rq->entities, list) {
1198 if (bad->s_fence->scheduled.context ==
1199 entity->fence_context) {
1201 atomic_set(entity->guilty, 1);
1205 spin_unlock(&rq->lock);
1206 if (&entity->list != &rq->entities)
1211 EXPORT_SYMBOL(drm_sched_increase_karma);