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 <uapi/linux/sched/types.h>
53 #include <drm/drm_print.h>
54 #include <drm/gpu_scheduler.h>
55 #include <drm/spsc_queue.h>
57 #define CREATE_TRACE_POINTS
58 #include "gpu_scheduler_trace.h"
60 #define to_drm_sched_job(sched_job) \
61 container_of((sched_job), struct drm_sched_job, queue_node)
63 static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb);
66 * drm_sched_rq_init - initialize a given run queue struct
68 * @rq: scheduler run queue
70 * Initializes a scheduler runqueue.
72 static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
73 struct drm_sched_rq *rq)
75 spin_lock_init(&rq->lock);
76 INIT_LIST_HEAD(&rq->entities);
77 rq->current_entity = NULL;
82 * drm_sched_rq_add_entity - add an entity
84 * @rq: scheduler run queue
85 * @entity: scheduler entity
87 * Adds a scheduler entity to the run queue.
89 void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
90 struct drm_sched_entity *entity)
92 if (!list_empty(&entity->list))
95 atomic_inc(&rq->sched->score);
96 list_add_tail(&entity->list, &rq->entities);
97 spin_unlock(&rq->lock);
101 * drm_sched_rq_remove_entity - remove an entity
103 * @rq: scheduler run queue
104 * @entity: scheduler entity
106 * Removes a scheduler entity from the run queue.
108 void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
109 struct drm_sched_entity *entity)
111 if (list_empty(&entity->list))
113 spin_lock(&rq->lock);
114 atomic_dec(&rq->sched->score);
115 list_del_init(&entity->list);
116 if (rq->current_entity == entity)
117 rq->current_entity = NULL;
118 spin_unlock(&rq->lock);
122 * drm_sched_rq_select_entity - Select an entity which could provide a job to run
124 * @rq: scheduler run queue to check.
126 * Try to find a ready entity, returns NULL if none found.
128 static struct drm_sched_entity *
129 drm_sched_rq_select_entity(struct drm_sched_rq *rq)
131 struct drm_sched_entity *entity;
133 spin_lock(&rq->lock);
135 entity = rq->current_entity;
137 list_for_each_entry_continue(entity, &rq->entities, list) {
138 if (drm_sched_entity_is_ready(entity)) {
139 rq->current_entity = entity;
140 reinit_completion(&entity->entity_idle);
141 spin_unlock(&rq->lock);
147 list_for_each_entry(entity, &rq->entities, list) {
149 if (drm_sched_entity_is_ready(entity)) {
150 rq->current_entity = entity;
151 reinit_completion(&entity->entity_idle);
152 spin_unlock(&rq->lock);
156 if (entity == rq->current_entity)
160 spin_unlock(&rq->lock);
166 * drm_sched_dependency_optimized
168 * @fence: the dependency fence
169 * @entity: the entity which depends on the above fence
171 * Returns true if the dependency can be optimized and false otherwise
173 bool drm_sched_dependency_optimized(struct dma_fence* fence,
174 struct drm_sched_entity *entity)
176 struct drm_gpu_scheduler *sched = entity->rq->sched;
177 struct drm_sched_fence *s_fence;
179 if (!fence || dma_fence_is_signaled(fence))
181 if (fence->context == entity->fence_context)
183 s_fence = to_drm_sched_fence(fence);
184 if (s_fence && s_fence->sched == sched)
189 EXPORT_SYMBOL(drm_sched_dependency_optimized);
192 * drm_sched_start_timeout - start timeout for reset worker
194 * @sched: scheduler instance to start the worker for
196 * Start the timeout for the given scheduler.
198 static void drm_sched_start_timeout(struct drm_gpu_scheduler *sched)
200 if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
201 !list_empty(&sched->ring_mirror_list))
202 schedule_delayed_work(&sched->work_tdr, sched->timeout);
206 * drm_sched_fault - immediately start timeout handler
208 * @sched: scheduler where the timeout handling should be started.
210 * Start timeout handling immediately when the driver detects a hardware fault.
212 void drm_sched_fault(struct drm_gpu_scheduler *sched)
214 mod_delayed_work(system_wq, &sched->work_tdr, 0);
216 EXPORT_SYMBOL(drm_sched_fault);
219 * drm_sched_suspend_timeout - Suspend scheduler job timeout
221 * @sched: scheduler instance for which to suspend the timeout
223 * Suspend the delayed work timeout for the scheduler. This is done by
224 * modifying the delayed work timeout to an arbitrary large value,
225 * MAX_SCHEDULE_TIMEOUT in this case.
227 * Returns the timeout remaining
230 unsigned long drm_sched_suspend_timeout(struct drm_gpu_scheduler *sched)
232 unsigned long sched_timeout, now = jiffies;
234 sched_timeout = sched->work_tdr.timer.expires;
237 * Modify the timeout to an arbitrarily large value. This also prevents
238 * the timeout to be restarted when new submissions arrive
240 if (mod_delayed_work(system_wq, &sched->work_tdr, MAX_SCHEDULE_TIMEOUT)
241 && time_after(sched_timeout, now))
242 return sched_timeout - now;
244 return sched->timeout;
246 EXPORT_SYMBOL(drm_sched_suspend_timeout);
249 * drm_sched_resume_timeout - Resume scheduler job timeout
251 * @sched: scheduler instance for which to resume the timeout
252 * @remaining: remaining timeout
254 * Resume the delayed work timeout for the scheduler.
256 void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched,
257 unsigned long remaining)
259 spin_lock(&sched->job_list_lock);
261 if (list_empty(&sched->ring_mirror_list))
262 cancel_delayed_work(&sched->work_tdr);
264 mod_delayed_work(system_wq, &sched->work_tdr, remaining);
266 spin_unlock(&sched->job_list_lock);
268 EXPORT_SYMBOL(drm_sched_resume_timeout);
270 static void drm_sched_job_begin(struct drm_sched_job *s_job)
272 struct drm_gpu_scheduler *sched = s_job->sched;
274 spin_lock(&sched->job_list_lock);
275 list_add_tail(&s_job->node, &sched->ring_mirror_list);
276 drm_sched_start_timeout(sched);
277 spin_unlock(&sched->job_list_lock);
280 static void drm_sched_job_timedout(struct work_struct *work)
282 struct drm_gpu_scheduler *sched;
283 struct drm_sched_job *job;
285 sched = container_of(work, struct drm_gpu_scheduler, work_tdr.work);
287 /* Protects against concurrent deletion in drm_sched_get_cleanup_job */
288 spin_lock(&sched->job_list_lock);
289 job = list_first_entry_or_null(&sched->ring_mirror_list,
290 struct drm_sched_job, node);
294 * Remove the bad job so it cannot be freed by concurrent
295 * drm_sched_cleanup_jobs. It will be reinserted back after sched->thread
296 * is parked at which point it's safe.
298 list_del_init(&job->node);
299 spin_unlock(&sched->job_list_lock);
301 job->sched->ops->timedout_job(job);
304 * Guilty job did complete and hence needs to be manually removed
305 * See drm_sched_stop doc.
307 if (sched->free_guilty) {
308 job->sched->ops->free_job(job);
309 sched->free_guilty = false;
312 spin_unlock(&sched->job_list_lock);
315 spin_lock(&sched->job_list_lock);
316 drm_sched_start_timeout(sched);
317 spin_unlock(&sched->job_list_lock);
321 * drm_sched_increase_karma - Update sched_entity guilty flag
323 * @bad: The job guilty of time out
325 * Increment on every hang caused by the 'bad' job. If this exceeds the hang
326 * limit of the scheduler then the respective sched entity is marked guilty and
327 * jobs from it will not be scheduled further
329 void drm_sched_increase_karma(struct drm_sched_job *bad)
332 struct drm_sched_entity *tmp;
333 struct drm_sched_entity *entity;
334 struct drm_gpu_scheduler *sched = bad->sched;
336 /* don't increase @bad's karma if it's from KERNEL RQ,
337 * because sometimes GPU hang would cause kernel jobs (like VM updating jobs)
338 * corrupt but keep in mind that kernel jobs always considered good.
340 if (bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) {
341 atomic_inc(&bad->karma);
342 for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_KERNEL;
344 struct drm_sched_rq *rq = &sched->sched_rq[i];
346 spin_lock(&rq->lock);
347 list_for_each_entry_safe(entity, tmp, &rq->entities, list) {
348 if (bad->s_fence->scheduled.context ==
349 entity->fence_context) {
350 if (atomic_read(&bad->karma) >
351 bad->sched->hang_limit)
353 atomic_set(entity->guilty, 1);
357 spin_unlock(&rq->lock);
358 if (&entity->list != &rq->entities)
363 EXPORT_SYMBOL(drm_sched_increase_karma);
366 * drm_sched_stop - stop the scheduler
368 * @sched: scheduler instance
369 * @bad: job which caused the time out
371 * Stop the scheduler and also removes and frees all completed jobs.
372 * Note: bad job will not be freed as it might be used later and so it's
373 * callers responsibility to release it manually if it's not part of the
374 * mirror list any more.
377 void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
379 struct drm_sched_job *s_job, *tmp;
381 kthread_park(sched->thread);
384 * Reinsert back the bad job here - now it's safe as
385 * drm_sched_get_cleanup_job cannot race against us and release the
386 * bad job at this point - we parked (waited for) any in progress
387 * (earlier) cleanups and drm_sched_get_cleanup_job will not be called
388 * now until the scheduler thread is unparked.
390 if (bad && bad->sched == sched)
392 * Add at the head of the queue to reflect it was the earliest
395 list_add(&bad->node, &sched->ring_mirror_list);
398 * Iterate the job list from later to earlier one and either deactive
399 * their HW callbacks or remove them from mirror list if they already
401 * This iteration is thread safe as sched thread is stopped.
403 list_for_each_entry_safe_reverse(s_job, tmp, &sched->ring_mirror_list, node) {
404 if (s_job->s_fence->parent &&
405 dma_fence_remove_callback(s_job->s_fence->parent,
407 atomic_dec(&sched->hw_rq_count);
410 * remove job from ring_mirror_list.
411 * Locking here is for concurrent resume timeout
413 spin_lock(&sched->job_list_lock);
414 list_del_init(&s_job->node);
415 spin_unlock(&sched->job_list_lock);
418 * Wait for job's HW fence callback to finish using s_job
419 * before releasing it.
421 * Job is still alive so fence refcount at least 1
423 dma_fence_wait(&s_job->s_fence->finished, false);
426 * We must keep bad job alive for later use during
427 * recovery by some of the drivers but leave a hint
428 * that the guilty job must be released.
431 sched->ops->free_job(s_job);
433 sched->free_guilty = true;
438 * Stop pending timer in flight as we rearm it in drm_sched_start. This
439 * avoids the pending timeout work in progress to fire right away after
440 * this TDR finished and before the newly restarted jobs had a
441 * chance to complete.
443 cancel_delayed_work(&sched->work_tdr);
446 EXPORT_SYMBOL(drm_sched_stop);
449 * drm_sched_job_recovery - recover jobs after a reset
451 * @sched: scheduler instance
452 * @full_recovery: proceed with complete sched restart
455 void drm_sched_start(struct drm_gpu_scheduler *sched, bool full_recovery)
457 struct drm_sched_job *s_job, *tmp;
461 * Locking the list is not required here as the sched thread is parked
462 * so no new jobs are being inserted or removed. Also concurrent
463 * GPU recovers can't run in parallel.
465 list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
466 struct dma_fence *fence = s_job->s_fence->parent;
468 atomic_inc(&sched->hw_rq_count);
474 r = dma_fence_add_callback(fence, &s_job->cb,
475 drm_sched_process_job);
477 drm_sched_process_job(fence, &s_job->cb);
479 DRM_ERROR("fence add callback failed (%d)\n",
482 drm_sched_process_job(NULL, &s_job->cb);
486 spin_lock(&sched->job_list_lock);
487 drm_sched_start_timeout(sched);
488 spin_unlock(&sched->job_list_lock);
491 kthread_unpark(sched->thread);
493 EXPORT_SYMBOL(drm_sched_start);
496 * drm_sched_resubmit_jobs - helper to relunch job from mirror ring list
498 * @sched: scheduler instance
501 void drm_sched_resubmit_jobs(struct drm_gpu_scheduler *sched)
503 struct drm_sched_job *s_job, *tmp;
504 uint64_t guilty_context;
505 bool found_guilty = false;
506 struct dma_fence *fence;
508 list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
509 struct drm_sched_fence *s_fence = s_job->s_fence;
511 if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) {
513 guilty_context = s_job->s_fence->scheduled.context;
516 if (found_guilty && s_job->s_fence->scheduled.context == guilty_context)
517 dma_fence_set_error(&s_fence->finished, -ECANCELED);
519 dma_fence_put(s_job->s_fence->parent);
520 fence = sched->ops->run_job(s_job);
522 if (IS_ERR_OR_NULL(fence)) {
524 dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
526 s_job->s_fence->parent = NULL;
528 s_job->s_fence->parent = fence;
534 EXPORT_SYMBOL(drm_sched_resubmit_jobs);
537 * drm_sched_job_init - init a scheduler job
539 * @job: scheduler job to init
540 * @entity: scheduler entity to use
541 * @owner: job owner for debugging
543 * Refer to drm_sched_entity_push_job() documentation
544 * for locking considerations.
546 * Returns 0 for success, negative error code otherwise.
548 int drm_sched_job_init(struct drm_sched_job *job,
549 struct drm_sched_entity *entity,
552 struct drm_gpu_scheduler *sched;
554 drm_sched_entity_select_rq(entity);
558 sched = entity->rq->sched;
561 job->entity = entity;
562 job->s_priority = entity->rq - sched->sched_rq;
563 job->s_fence = drm_sched_fence_create(entity, owner);
566 job->id = atomic64_inc_return(&sched->job_id_count);
568 INIT_LIST_HEAD(&job->node);
572 EXPORT_SYMBOL(drm_sched_job_init);
575 * drm_sched_job_cleanup - clean up scheduler job resources
577 * @job: scheduler job to clean up
579 void drm_sched_job_cleanup(struct drm_sched_job *job)
581 dma_fence_put(&job->s_fence->finished);
584 EXPORT_SYMBOL(drm_sched_job_cleanup);
587 * drm_sched_ready - is the scheduler ready
589 * @sched: scheduler instance
591 * Return true if we can push more jobs to the hw, otherwise false.
593 static bool drm_sched_ready(struct drm_gpu_scheduler *sched)
595 return atomic_read(&sched->hw_rq_count) <
596 sched->hw_submission_limit;
600 * drm_sched_wakeup - Wake up the scheduler when it is ready
602 * @sched: scheduler instance
605 void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
607 if (drm_sched_ready(sched))
608 wake_up_interruptible(&sched->wake_up_worker);
612 * drm_sched_select_entity - Select next entity to process
614 * @sched: scheduler instance
616 * Returns the entity to process or NULL if none are found.
618 static struct drm_sched_entity *
619 drm_sched_select_entity(struct drm_gpu_scheduler *sched)
621 struct drm_sched_entity *entity;
624 if (!drm_sched_ready(sched))
627 /* Kernel run queue has higher priority than normal run queue*/
628 for (i = DRM_SCHED_PRIORITY_MAX - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
629 entity = drm_sched_rq_select_entity(&sched->sched_rq[i]);
638 * drm_sched_process_job - process a job
641 * @cb: fence callbacks
643 * Called after job has finished execution.
645 static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
647 struct drm_sched_job *s_job = container_of(cb, struct drm_sched_job, cb);
648 struct drm_sched_fence *s_fence = s_job->s_fence;
649 struct drm_gpu_scheduler *sched = s_fence->sched;
651 atomic_dec(&sched->hw_rq_count);
652 atomic_dec(&sched->score);
654 trace_drm_sched_process_job(s_fence);
656 dma_fence_get(&s_fence->finished);
657 drm_sched_fence_finished(s_fence);
658 dma_fence_put(&s_fence->finished);
659 wake_up_interruptible(&sched->wake_up_worker);
663 * drm_sched_get_cleanup_job - fetch the next finished job to be destroyed
665 * @sched: scheduler instance
667 * Returns the next finished job from the mirror list (if there is one)
668 * ready for it to be destroyed.
670 static struct drm_sched_job *
671 drm_sched_get_cleanup_job(struct drm_gpu_scheduler *sched)
673 struct drm_sched_job *job;
676 * Don't destroy jobs while the timeout worker is running OR thread
677 * is being parked and hence assumed to not touch ring_mirror_list
679 if ((sched->timeout != MAX_SCHEDULE_TIMEOUT &&
680 !cancel_delayed_work(&sched->work_tdr)) ||
681 kthread_should_park())
684 spin_lock(&sched->job_list_lock);
686 job = list_first_entry_or_null(&sched->ring_mirror_list,
687 struct drm_sched_job, node);
689 if (job && dma_fence_is_signaled(&job->s_fence->finished)) {
690 /* remove job from ring_mirror_list */
691 list_del_init(&job->node);
694 /* queue timeout for next job */
695 drm_sched_start_timeout(sched);
698 spin_unlock(&sched->job_list_lock);
704 * drm_sched_pick_best - Get a drm sched from a sched_list with the least load
705 * @sched_list: list of drm_gpu_schedulers
706 * @num_sched_list: number of drm_gpu_schedulers in the sched_list
708 * Returns pointer of the sched with the least load or NULL if none of the
709 * drm_gpu_schedulers are ready
711 struct drm_gpu_scheduler *
712 drm_sched_pick_best(struct drm_gpu_scheduler **sched_list,
713 unsigned int num_sched_list)
715 struct drm_gpu_scheduler *sched, *picked_sched = NULL;
717 unsigned int min_score = UINT_MAX, num_score;
719 for (i = 0; i < num_sched_list; ++i) {
720 sched = sched_list[i];
723 DRM_WARN("scheduler %s is not ready, skipping",
728 num_score = atomic_read(&sched->score);
729 if (num_score < min_score) {
730 min_score = num_score;
731 picked_sched = sched;
737 EXPORT_SYMBOL(drm_sched_pick_best);
740 * drm_sched_blocked - check if the scheduler is blocked
742 * @sched: scheduler instance
744 * Returns true if blocked, otherwise false.
746 static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
748 if (kthread_should_park()) {
757 * drm_sched_main - main scheduler thread
759 * @param: scheduler instance
763 static int drm_sched_main(void *param)
765 struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
768 sched_set_fifo_low(current);
770 while (!kthread_should_stop()) {
771 struct drm_sched_entity *entity = NULL;
772 struct drm_sched_fence *s_fence;
773 struct drm_sched_job *sched_job;
774 struct dma_fence *fence;
775 struct drm_sched_job *cleanup_job = NULL;
777 wait_event_interruptible(sched->wake_up_worker,
778 (cleanup_job = drm_sched_get_cleanup_job(sched)) ||
779 (!drm_sched_blocked(sched) &&
780 (entity = drm_sched_select_entity(sched))) ||
781 kthread_should_stop());
784 sched->ops->free_job(cleanup_job);
785 /* queue timeout for next job */
786 drm_sched_start_timeout(sched);
792 sched_job = drm_sched_entity_pop_job(entity);
794 complete(&entity->entity_idle);
799 s_fence = sched_job->s_fence;
801 atomic_inc(&sched->hw_rq_count);
802 drm_sched_job_begin(sched_job);
804 trace_drm_run_job(sched_job, entity);
805 fence = sched->ops->run_job(sched_job);
806 drm_sched_fence_scheduled(s_fence);
808 if (!IS_ERR_OR_NULL(fence)) {
809 s_fence->parent = dma_fence_get(fence);
810 r = dma_fence_add_callback(fence, &sched_job->cb,
811 drm_sched_process_job);
813 drm_sched_process_job(fence, &sched_job->cb);
815 DRM_ERROR("fence add callback failed (%d)\n",
817 dma_fence_put(fence);
820 dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
822 drm_sched_process_job(NULL, &sched_job->cb);
825 wake_up(&sched->job_scheduled);
831 * drm_sched_init - Init a gpu scheduler instance
833 * @sched: scheduler instance
834 * @ops: backend operations for this scheduler
835 * @hw_submission: number of hw submissions that can be in flight
836 * @hang_limit: number of times to allow a job to hang before dropping it
837 * @timeout: timeout value in jiffies for the scheduler
838 * @name: name used for debugging
840 * Return 0 on success, otherwise error code.
842 int drm_sched_init(struct drm_gpu_scheduler *sched,
843 const struct drm_sched_backend_ops *ops,
844 unsigned hw_submission,
851 sched->hw_submission_limit = hw_submission;
853 sched->timeout = timeout;
854 sched->hang_limit = hang_limit;
855 for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_MAX; i++)
856 drm_sched_rq_init(sched, &sched->sched_rq[i]);
858 init_waitqueue_head(&sched->wake_up_worker);
859 init_waitqueue_head(&sched->job_scheduled);
860 INIT_LIST_HEAD(&sched->ring_mirror_list);
861 spin_lock_init(&sched->job_list_lock);
862 atomic_set(&sched->hw_rq_count, 0);
863 INIT_DELAYED_WORK(&sched->work_tdr, drm_sched_job_timedout);
864 atomic_set(&sched->score, 0);
865 atomic64_set(&sched->job_id_count, 0);
867 /* Each scheduler will run on a seperate kernel thread */
868 sched->thread = kthread_run(drm_sched_main, sched, sched->name);
869 if (IS_ERR(sched->thread)) {
870 ret = PTR_ERR(sched->thread);
871 sched->thread = NULL;
872 DRM_ERROR("Failed to create scheduler for %s.\n", name);
879 EXPORT_SYMBOL(drm_sched_init);
882 * drm_sched_fini - Destroy a gpu scheduler
884 * @sched: scheduler instance
886 * Tears down and cleans up the scheduler.
888 void drm_sched_fini(struct drm_gpu_scheduler *sched)
891 kthread_stop(sched->thread);
893 sched->ready = false;
895 EXPORT_SYMBOL(drm_sched_fini);