Loading...
1/*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 */
23
24/**
25 * DOC: Overview
26 *
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.
34 *
35 * The organisation of the scheduler is the following:
36 *
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
42 * the hardware.
43 *
44 * The jobs in a entity are always scheduled in the order that they were pushed.
45 */
46
47#include <linux/kthread.h>
48#include <linux/wait.h>
49#include <linux/sched.h>
50#include <uapi/linux/sched/types.h>
51
52#include <drm/drm_print.h>
53#include <drm/gpu_scheduler.h>
54#include <drm/spsc_queue.h>
55
56#define CREATE_TRACE_POINTS
57#include "gpu_scheduler_trace.h"
58
59#define to_drm_sched_job(sched_job) \
60 container_of((sched_job), struct drm_sched_job, queue_node)
61
62static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb);
63
64/**
65 * drm_sched_rq_init - initialize a given run queue struct
66 *
67 * @rq: scheduler run queue
68 *
69 * Initializes a scheduler runqueue.
70 */
71static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
72 struct drm_sched_rq *rq)
73{
74 spin_lock_init(&rq->lock);
75 INIT_LIST_HEAD(&rq->entities);
76 rq->current_entity = NULL;
77 rq->sched = sched;
78}
79
80/**
81 * drm_sched_rq_add_entity - add an entity
82 *
83 * @rq: scheduler run queue
84 * @entity: scheduler entity
85 *
86 * Adds a scheduler entity to the run queue.
87 */
88void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
89 struct drm_sched_entity *entity)
90{
91 if (!list_empty(&entity->list))
92 return;
93 spin_lock(&rq->lock);
94 list_add_tail(&entity->list, &rq->entities);
95 spin_unlock(&rq->lock);
96}
97
98/**
99 * drm_sched_rq_remove_entity - remove an entity
100 *
101 * @rq: scheduler run queue
102 * @entity: scheduler entity
103 *
104 * Removes a scheduler entity from the run queue.
105 */
106void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
107 struct drm_sched_entity *entity)
108{
109 if (list_empty(&entity->list))
110 return;
111 spin_lock(&rq->lock);
112 list_del_init(&entity->list);
113 if (rq->current_entity == entity)
114 rq->current_entity = NULL;
115 spin_unlock(&rq->lock);
116}
117
118/**
119 * drm_sched_rq_select_entity - Select an entity which could provide a job to run
120 *
121 * @rq: scheduler run queue to check.
122 *
123 * Try to find a ready entity, returns NULL if none found.
124 */
125static struct drm_sched_entity *
126drm_sched_rq_select_entity(struct drm_sched_rq *rq)
127{
128 struct drm_sched_entity *entity;
129
130 spin_lock(&rq->lock);
131
132 entity = rq->current_entity;
133 if (entity) {
134 list_for_each_entry_continue(entity, &rq->entities, list) {
135 if (drm_sched_entity_is_ready(entity)) {
136 rq->current_entity = entity;
137 spin_unlock(&rq->lock);
138 return entity;
139 }
140 }
141 }
142
143 list_for_each_entry(entity, &rq->entities, list) {
144
145 if (drm_sched_entity_is_ready(entity)) {
146 rq->current_entity = entity;
147 spin_unlock(&rq->lock);
148 return entity;
149 }
150
151 if (entity == rq->current_entity)
152 break;
153 }
154
155 spin_unlock(&rq->lock);
156
157 return NULL;
158}
159
160/**
161 * drm_sched_dependency_optimized
162 *
163 * @fence: the dependency fence
164 * @entity: the entity which depends on the above fence
165 *
166 * Returns true if the dependency can be optimized and false otherwise
167 */
168bool drm_sched_dependency_optimized(struct dma_fence* fence,
169 struct drm_sched_entity *entity)
170{
171 struct drm_gpu_scheduler *sched = entity->rq->sched;
172 struct drm_sched_fence *s_fence;
173
174 if (!fence || dma_fence_is_signaled(fence))
175 return false;
176 if (fence->context == entity->fence_context)
177 return true;
178 s_fence = to_drm_sched_fence(fence);
179 if (s_fence && s_fence->sched == sched)
180 return true;
181
182 return false;
183}
184EXPORT_SYMBOL(drm_sched_dependency_optimized);
185
186/**
187 * drm_sched_start_timeout - start timeout for reset worker
188 *
189 * @sched: scheduler instance to start the worker for
190 *
191 * Start the timeout for the given scheduler.
192 */
193static void drm_sched_start_timeout(struct drm_gpu_scheduler *sched)
194{
195 if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
196 !list_empty(&sched->ring_mirror_list))
197 schedule_delayed_work(&sched->work_tdr, sched->timeout);
198}
199
200/**
201 * drm_sched_fault - immediately start timeout handler
202 *
203 * @sched: scheduler where the timeout handling should be started.
204 *
205 * Start timeout handling immediately when the driver detects a hardware fault.
206 */
207void drm_sched_fault(struct drm_gpu_scheduler *sched)
208{
209 mod_delayed_work(system_wq, &sched->work_tdr, 0);
210}
211EXPORT_SYMBOL(drm_sched_fault);
212
213/**
214 * drm_sched_suspend_timeout - Suspend scheduler job timeout
215 *
216 * @sched: scheduler instance for which to suspend the timeout
217 *
218 * Suspend the delayed work timeout for the scheduler. This is done by
219 * modifying the delayed work timeout to an arbitrary large value,
220 * MAX_SCHEDULE_TIMEOUT in this case. Note that this function can be
221 * called from an IRQ context.
222 *
223 * Returns the timeout remaining
224 *
225 */
226unsigned long drm_sched_suspend_timeout(struct drm_gpu_scheduler *sched)
227{
228 unsigned long sched_timeout, now = jiffies;
229
230 sched_timeout = sched->work_tdr.timer.expires;
231
232 /*
233 * Modify the timeout to an arbitrarily large value. This also prevents
234 * the timeout to be restarted when new submissions arrive
235 */
236 if (mod_delayed_work(system_wq, &sched->work_tdr, MAX_SCHEDULE_TIMEOUT)
237 && time_after(sched_timeout, now))
238 return sched_timeout - now;
239 else
240 return sched->timeout;
241}
242EXPORT_SYMBOL(drm_sched_suspend_timeout);
243
244/**
245 * drm_sched_resume_timeout - Resume scheduler job timeout
246 *
247 * @sched: scheduler instance for which to resume the timeout
248 * @remaining: remaining timeout
249 *
250 * Resume the delayed work timeout for the scheduler. Note that
251 * this function can be called from an IRQ context.
252 */
253void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched,
254 unsigned long remaining)
255{
256 unsigned long flags;
257
258 spin_lock_irqsave(&sched->job_list_lock, flags);
259
260 if (list_empty(&sched->ring_mirror_list))
261 cancel_delayed_work(&sched->work_tdr);
262 else
263 mod_delayed_work(system_wq, &sched->work_tdr, remaining);
264
265 spin_unlock_irqrestore(&sched->job_list_lock, flags);
266}
267EXPORT_SYMBOL(drm_sched_resume_timeout);
268
269static void drm_sched_job_begin(struct drm_sched_job *s_job)
270{
271 struct drm_gpu_scheduler *sched = s_job->sched;
272 unsigned long flags;
273
274 spin_lock_irqsave(&sched->job_list_lock, flags);
275 list_add_tail(&s_job->node, &sched->ring_mirror_list);
276 drm_sched_start_timeout(sched);
277 spin_unlock_irqrestore(&sched->job_list_lock, flags);
278}
279
280static void drm_sched_job_timedout(struct work_struct *work)
281{
282 struct drm_gpu_scheduler *sched;
283 struct drm_sched_job *job;
284 unsigned long flags;
285
286 sched = container_of(work, struct drm_gpu_scheduler, work_tdr.work);
287 job = list_first_entry_or_null(&sched->ring_mirror_list,
288 struct drm_sched_job, node);
289
290 if (job) {
291 job->sched->ops->timedout_job(job);
292
293 /*
294 * Guilty job did complete and hence needs to be manually removed
295 * See drm_sched_stop doc.
296 */
297 if (sched->free_guilty) {
298 job->sched->ops->free_job(job);
299 sched->free_guilty = false;
300 }
301 }
302
303 spin_lock_irqsave(&sched->job_list_lock, flags);
304 drm_sched_start_timeout(sched);
305 spin_unlock_irqrestore(&sched->job_list_lock, flags);
306}
307
308 /**
309 * drm_sched_increase_karma - Update sched_entity guilty flag
310 *
311 * @bad: The job guilty of time out
312 *
313 * Increment on every hang caused by the 'bad' job. If this exceeds the hang
314 * limit of the scheduler then the respective sched entity is marked guilty and
315 * jobs from it will not be scheduled further
316 */
317void drm_sched_increase_karma(struct drm_sched_job *bad)
318{
319 int i;
320 struct drm_sched_entity *tmp;
321 struct drm_sched_entity *entity;
322 struct drm_gpu_scheduler *sched = bad->sched;
323
324 /* don't increase @bad's karma if it's from KERNEL RQ,
325 * because sometimes GPU hang would cause kernel jobs (like VM updating jobs)
326 * corrupt but keep in mind that kernel jobs always considered good.
327 */
328 if (bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) {
329 atomic_inc(&bad->karma);
330 for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_KERNEL;
331 i++) {
332 struct drm_sched_rq *rq = &sched->sched_rq[i];
333
334 spin_lock(&rq->lock);
335 list_for_each_entry_safe(entity, tmp, &rq->entities, list) {
336 if (bad->s_fence->scheduled.context ==
337 entity->fence_context) {
338 if (atomic_read(&bad->karma) >
339 bad->sched->hang_limit)
340 if (entity->guilty)
341 atomic_set(entity->guilty, 1);
342 break;
343 }
344 }
345 spin_unlock(&rq->lock);
346 if (&entity->list != &rq->entities)
347 break;
348 }
349 }
350}
351EXPORT_SYMBOL(drm_sched_increase_karma);
352
353/**
354 * drm_sched_stop - stop the scheduler
355 *
356 * @sched: scheduler instance
357 * @bad: job which caused the time out
358 *
359 * Stop the scheduler and also removes and frees all completed jobs.
360 * Note: bad job will not be freed as it might be used later and so it's
361 * callers responsibility to release it manually if it's not part of the
362 * mirror list any more.
363 *
364 */
365void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
366{
367 struct drm_sched_job *s_job, *tmp;
368 unsigned long flags;
369
370 kthread_park(sched->thread);
371
372 /*
373 * Iterate the job list from later to earlier one and either deactive
374 * their HW callbacks or remove them from mirror list if they already
375 * signaled.
376 * This iteration is thread safe as sched thread is stopped.
377 */
378 list_for_each_entry_safe_reverse(s_job, tmp, &sched->ring_mirror_list, node) {
379 if (s_job->s_fence->parent &&
380 dma_fence_remove_callback(s_job->s_fence->parent,
381 &s_job->cb)) {
382 atomic_dec(&sched->hw_rq_count);
383 } else {
384 /*
385 * remove job from ring_mirror_list.
386 * Locking here is for concurrent resume timeout
387 */
388 spin_lock_irqsave(&sched->job_list_lock, flags);
389 list_del_init(&s_job->node);
390 spin_unlock_irqrestore(&sched->job_list_lock, flags);
391
392 /*
393 * Wait for job's HW fence callback to finish using s_job
394 * before releasing it.
395 *
396 * Job is still alive so fence refcount at least 1
397 */
398 dma_fence_wait(&s_job->s_fence->finished, false);
399
400 /*
401 * We must keep bad job alive for later use during
402 * recovery by some of the drivers but leave a hint
403 * that the guilty job must be released.
404 */
405 if (bad != s_job)
406 sched->ops->free_job(s_job);
407 else
408 sched->free_guilty = true;
409 }
410 }
411
412 /*
413 * Stop pending timer in flight as we rearm it in drm_sched_start. This
414 * avoids the pending timeout work in progress to fire right away after
415 * this TDR finished and before the newly restarted jobs had a
416 * chance to complete.
417 */
418 cancel_delayed_work(&sched->work_tdr);
419}
420
421EXPORT_SYMBOL(drm_sched_stop);
422
423/**
424 * drm_sched_job_recovery - recover jobs after a reset
425 *
426 * @sched: scheduler instance
427 * @full_recovery: proceed with complete sched restart
428 *
429 */
430void drm_sched_start(struct drm_gpu_scheduler *sched, bool full_recovery)
431{
432 struct drm_sched_job *s_job, *tmp;
433 unsigned long flags;
434 int r;
435
436 /*
437 * Locking the list is not required here as the sched thread is parked
438 * so no new jobs are being inserted or removed. Also concurrent
439 * GPU recovers can't run in parallel.
440 */
441 list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
442 struct dma_fence *fence = s_job->s_fence->parent;
443
444 atomic_inc(&sched->hw_rq_count);
445
446 if (!full_recovery)
447 continue;
448
449 if (fence) {
450 r = dma_fence_add_callback(fence, &s_job->cb,
451 drm_sched_process_job);
452 if (r == -ENOENT)
453 drm_sched_process_job(fence, &s_job->cb);
454 else if (r)
455 DRM_ERROR("fence add callback failed (%d)\n",
456 r);
457 } else
458 drm_sched_process_job(NULL, &s_job->cb);
459 }
460
461 if (full_recovery) {
462 spin_lock_irqsave(&sched->job_list_lock, flags);
463 drm_sched_start_timeout(sched);
464 spin_unlock_irqrestore(&sched->job_list_lock, flags);
465 }
466
467 kthread_unpark(sched->thread);
468}
469EXPORT_SYMBOL(drm_sched_start);
470
471/**
472 * drm_sched_resubmit_jobs - helper to relunch job from mirror ring list
473 *
474 * @sched: scheduler instance
475 *
476 */
477void drm_sched_resubmit_jobs(struct drm_gpu_scheduler *sched)
478{
479 struct drm_sched_job *s_job, *tmp;
480 uint64_t guilty_context;
481 bool found_guilty = false;
482 struct dma_fence *fence;
483
484 list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
485 struct drm_sched_fence *s_fence = s_job->s_fence;
486
487 if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) {
488 found_guilty = true;
489 guilty_context = s_job->s_fence->scheduled.context;
490 }
491
492 if (found_guilty && s_job->s_fence->scheduled.context == guilty_context)
493 dma_fence_set_error(&s_fence->finished, -ECANCELED);
494
495 dma_fence_put(s_job->s_fence->parent);
496 fence = sched->ops->run_job(s_job);
497
498 if (IS_ERR_OR_NULL(fence)) {
499 s_job->s_fence->parent = NULL;
500 dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
501 } else {
502 s_job->s_fence->parent = fence;
503 }
504
505
506 }
507}
508EXPORT_SYMBOL(drm_sched_resubmit_jobs);
509
510/**
511 * drm_sched_job_init - init a scheduler job
512 *
513 * @job: scheduler job to init
514 * @entity: scheduler entity to use
515 * @owner: job owner for debugging
516 *
517 * Refer to drm_sched_entity_push_job() documentation
518 * for locking considerations.
519 *
520 * Returns 0 for success, negative error code otherwise.
521 */
522int drm_sched_job_init(struct drm_sched_job *job,
523 struct drm_sched_entity *entity,
524 void *owner)
525{
526 struct drm_gpu_scheduler *sched;
527
528 drm_sched_entity_select_rq(entity);
529 if (!entity->rq)
530 return -ENOENT;
531
532 sched = entity->rq->sched;
533
534 job->sched = sched;
535 job->entity = entity;
536 job->s_priority = entity->rq - sched->sched_rq;
537 job->s_fence = drm_sched_fence_create(entity, owner);
538 if (!job->s_fence)
539 return -ENOMEM;
540 job->id = atomic64_inc_return(&sched->job_id_count);
541
542 INIT_LIST_HEAD(&job->node);
543
544 return 0;
545}
546EXPORT_SYMBOL(drm_sched_job_init);
547
548/**
549 * drm_sched_job_cleanup - clean up scheduler job resources
550 *
551 * @job: scheduler job to clean up
552 */
553void drm_sched_job_cleanup(struct drm_sched_job *job)
554{
555 dma_fence_put(&job->s_fence->finished);
556 job->s_fence = NULL;
557}
558EXPORT_SYMBOL(drm_sched_job_cleanup);
559
560/**
561 * drm_sched_ready - is the scheduler ready
562 *
563 * @sched: scheduler instance
564 *
565 * Return true if we can push more jobs to the hw, otherwise false.
566 */
567static bool drm_sched_ready(struct drm_gpu_scheduler *sched)
568{
569 return atomic_read(&sched->hw_rq_count) <
570 sched->hw_submission_limit;
571}
572
573/**
574 * drm_sched_wakeup - Wake up the scheduler when it is ready
575 *
576 * @sched: scheduler instance
577 *
578 */
579void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
580{
581 if (drm_sched_ready(sched))
582 wake_up_interruptible(&sched->wake_up_worker);
583}
584
585/**
586 * drm_sched_select_entity - Select next entity to process
587 *
588 * @sched: scheduler instance
589 *
590 * Returns the entity to process or NULL if none are found.
591 */
592static struct drm_sched_entity *
593drm_sched_select_entity(struct drm_gpu_scheduler *sched)
594{
595 struct drm_sched_entity *entity;
596 int i;
597
598 if (!drm_sched_ready(sched))
599 return NULL;
600
601 /* Kernel run queue has higher priority than normal run queue*/
602 for (i = DRM_SCHED_PRIORITY_MAX - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
603 entity = drm_sched_rq_select_entity(&sched->sched_rq[i]);
604 if (entity)
605 break;
606 }
607
608 return entity;
609}
610
611/**
612 * drm_sched_process_job - process a job
613 *
614 * @f: fence
615 * @cb: fence callbacks
616 *
617 * Called after job has finished execution.
618 */
619static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
620{
621 struct drm_sched_job *s_job = container_of(cb, struct drm_sched_job, cb);
622 struct drm_sched_fence *s_fence = s_job->s_fence;
623 struct drm_gpu_scheduler *sched = s_fence->sched;
624
625 atomic_dec(&sched->hw_rq_count);
626 atomic_dec(&sched->num_jobs);
627
628 trace_drm_sched_process_job(s_fence);
629
630 drm_sched_fence_finished(s_fence);
631 wake_up_interruptible(&sched->wake_up_worker);
632}
633
634/**
635 * drm_sched_cleanup_jobs - destroy finished jobs
636 *
637 * @sched: scheduler instance
638 *
639 * Remove all finished jobs from the mirror list and destroy them.
640 */
641static void drm_sched_cleanup_jobs(struct drm_gpu_scheduler *sched)
642{
643 unsigned long flags;
644
645 /* Don't destroy jobs while the timeout worker is running */
646 if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
647 !cancel_delayed_work(&sched->work_tdr))
648 return;
649
650
651 while (!list_empty(&sched->ring_mirror_list)) {
652 struct drm_sched_job *job;
653
654 job = list_first_entry(&sched->ring_mirror_list,
655 struct drm_sched_job, node);
656 if (!dma_fence_is_signaled(&job->s_fence->finished))
657 break;
658
659 spin_lock_irqsave(&sched->job_list_lock, flags);
660 /* remove job from ring_mirror_list */
661 list_del_init(&job->node);
662 spin_unlock_irqrestore(&sched->job_list_lock, flags);
663
664 sched->ops->free_job(job);
665 }
666
667 /* queue timeout for next job */
668 spin_lock_irqsave(&sched->job_list_lock, flags);
669 drm_sched_start_timeout(sched);
670 spin_unlock_irqrestore(&sched->job_list_lock, flags);
671
672}
673
674/**
675 * drm_sched_blocked - check if the scheduler is blocked
676 *
677 * @sched: scheduler instance
678 *
679 * Returns true if blocked, otherwise false.
680 */
681static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
682{
683 if (kthread_should_park()) {
684 kthread_parkme();
685 return true;
686 }
687
688 return false;
689}
690
691/**
692 * drm_sched_main - main scheduler thread
693 *
694 * @param: scheduler instance
695 *
696 * Returns 0.
697 */
698static int drm_sched_main(void *param)
699{
700 struct sched_param sparam = {.sched_priority = 1};
701 struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
702 int r;
703
704 sched_setscheduler(current, SCHED_FIFO, &sparam);
705
706 while (!kthread_should_stop()) {
707 struct drm_sched_entity *entity = NULL;
708 struct drm_sched_fence *s_fence;
709 struct drm_sched_job *sched_job;
710 struct dma_fence *fence;
711
712 wait_event_interruptible(sched->wake_up_worker,
713 (drm_sched_cleanup_jobs(sched),
714 (!drm_sched_blocked(sched) &&
715 (entity = drm_sched_select_entity(sched))) ||
716 kthread_should_stop()));
717
718 if (!entity)
719 continue;
720
721 sched_job = drm_sched_entity_pop_job(entity);
722 if (!sched_job)
723 continue;
724
725 s_fence = sched_job->s_fence;
726
727 atomic_inc(&sched->hw_rq_count);
728 drm_sched_job_begin(sched_job);
729
730 fence = sched->ops->run_job(sched_job);
731 drm_sched_fence_scheduled(s_fence);
732
733 if (!IS_ERR_OR_NULL(fence)) {
734 s_fence->parent = dma_fence_get(fence);
735 r = dma_fence_add_callback(fence, &sched_job->cb,
736 drm_sched_process_job);
737 if (r == -ENOENT)
738 drm_sched_process_job(fence, &sched_job->cb);
739 else if (r)
740 DRM_ERROR("fence add callback failed (%d)\n",
741 r);
742 dma_fence_put(fence);
743 } else {
744
745 dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
746 drm_sched_process_job(NULL, &sched_job->cb);
747 }
748
749 wake_up(&sched->job_scheduled);
750 }
751 return 0;
752}
753
754/**
755 * drm_sched_init - Init a gpu scheduler instance
756 *
757 * @sched: scheduler instance
758 * @ops: backend operations for this scheduler
759 * @hw_submission: number of hw submissions that can be in flight
760 * @hang_limit: number of times to allow a job to hang before dropping it
761 * @timeout: timeout value in jiffies for the scheduler
762 * @name: name used for debugging
763 *
764 * Return 0 on success, otherwise error code.
765 */
766int drm_sched_init(struct drm_gpu_scheduler *sched,
767 const struct drm_sched_backend_ops *ops,
768 unsigned hw_submission,
769 unsigned hang_limit,
770 long timeout,
771 const char *name)
772{
773 int i, ret;
774 sched->ops = ops;
775 sched->hw_submission_limit = hw_submission;
776 sched->name = name;
777 sched->timeout = timeout;
778 sched->hang_limit = hang_limit;
779 for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_MAX; i++)
780 drm_sched_rq_init(sched, &sched->sched_rq[i]);
781
782 init_waitqueue_head(&sched->wake_up_worker);
783 init_waitqueue_head(&sched->job_scheduled);
784 INIT_LIST_HEAD(&sched->ring_mirror_list);
785 spin_lock_init(&sched->job_list_lock);
786 atomic_set(&sched->hw_rq_count, 0);
787 INIT_DELAYED_WORK(&sched->work_tdr, drm_sched_job_timedout);
788 atomic_set(&sched->num_jobs, 0);
789 atomic64_set(&sched->job_id_count, 0);
790
791 /* Each scheduler will run on a seperate kernel thread */
792 sched->thread = kthread_run(drm_sched_main, sched, sched->name);
793 if (IS_ERR(sched->thread)) {
794 ret = PTR_ERR(sched->thread);
795 sched->thread = NULL;
796 DRM_ERROR("Failed to create scheduler for %s.\n", name);
797 return ret;
798 }
799
800 sched->ready = true;
801 return 0;
802}
803EXPORT_SYMBOL(drm_sched_init);
804
805/**
806 * drm_sched_fini - Destroy a gpu scheduler
807 *
808 * @sched: scheduler instance
809 *
810 * Tears down and cleans up the scheduler.
811 */
812void drm_sched_fini(struct drm_gpu_scheduler *sched)
813{
814 if (sched->thread)
815 kthread_stop(sched->thread);
816
817 sched->ready = false;
818}
819EXPORT_SYMBOL(drm_sched_fini);
1/*
2 * Copyright 2015 Advanced Micro Devices, Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 */
23
24/**
25 * DOC: Overview
26 *
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.
34 *
35 * The organisation of the scheduler is the following:
36 *
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
42 * the hardware.
43 *
44 * The jobs in a entity are always scheduled in the order that they were pushed.
45 */
46
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>
52
53#include <drm/drm_print.h>
54#include <drm/gpu_scheduler.h>
55#include <drm/spsc_queue.h>
56
57#define CREATE_TRACE_POINTS
58#include "gpu_scheduler_trace.h"
59
60#define to_drm_sched_job(sched_job) \
61 container_of((sched_job), struct drm_sched_job, queue_node)
62
63static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb);
64
65/**
66 * drm_sched_rq_init - initialize a given run queue struct
67 *
68 * @rq: scheduler run queue
69 *
70 * Initializes a scheduler runqueue.
71 */
72static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
73 struct drm_sched_rq *rq)
74{
75 spin_lock_init(&rq->lock);
76 INIT_LIST_HEAD(&rq->entities);
77 rq->current_entity = NULL;
78 rq->sched = sched;
79}
80
81/**
82 * drm_sched_rq_add_entity - add an entity
83 *
84 * @rq: scheduler run queue
85 * @entity: scheduler entity
86 *
87 * Adds a scheduler entity to the run queue.
88 */
89void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
90 struct drm_sched_entity *entity)
91{
92 if (!list_empty(&entity->list))
93 return;
94 spin_lock(&rq->lock);
95 atomic_inc(&rq->sched->score);
96 list_add_tail(&entity->list, &rq->entities);
97 spin_unlock(&rq->lock);
98}
99
100/**
101 * drm_sched_rq_remove_entity - remove an entity
102 *
103 * @rq: scheduler run queue
104 * @entity: scheduler entity
105 *
106 * Removes a scheduler entity from the run queue.
107 */
108void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
109 struct drm_sched_entity *entity)
110{
111 if (list_empty(&entity->list))
112 return;
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);
119}
120
121/**
122 * drm_sched_rq_select_entity - Select an entity which could provide a job to run
123 *
124 * @rq: scheduler run queue to check.
125 *
126 * Try to find a ready entity, returns NULL if none found.
127 */
128static struct drm_sched_entity *
129drm_sched_rq_select_entity(struct drm_sched_rq *rq)
130{
131 struct drm_sched_entity *entity;
132
133 spin_lock(&rq->lock);
134
135 entity = rq->current_entity;
136 if (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);
142 return entity;
143 }
144 }
145 }
146
147 list_for_each_entry(entity, &rq->entities, list) {
148
149 if (drm_sched_entity_is_ready(entity)) {
150 rq->current_entity = entity;
151 reinit_completion(&entity->entity_idle);
152 spin_unlock(&rq->lock);
153 return entity;
154 }
155
156 if (entity == rq->current_entity)
157 break;
158 }
159
160 spin_unlock(&rq->lock);
161
162 return NULL;
163}
164
165/**
166 * drm_sched_dependency_optimized
167 *
168 * @fence: the dependency fence
169 * @entity: the entity which depends on the above fence
170 *
171 * Returns true if the dependency can be optimized and false otherwise
172 */
173bool drm_sched_dependency_optimized(struct dma_fence* fence,
174 struct drm_sched_entity *entity)
175{
176 struct drm_gpu_scheduler *sched = entity->rq->sched;
177 struct drm_sched_fence *s_fence;
178
179 if (!fence || dma_fence_is_signaled(fence))
180 return false;
181 if (fence->context == entity->fence_context)
182 return true;
183 s_fence = to_drm_sched_fence(fence);
184 if (s_fence && s_fence->sched == sched)
185 return true;
186
187 return false;
188}
189EXPORT_SYMBOL(drm_sched_dependency_optimized);
190
191/**
192 * drm_sched_start_timeout - start timeout for reset worker
193 *
194 * @sched: scheduler instance to start the worker for
195 *
196 * Start the timeout for the given scheduler.
197 */
198static void drm_sched_start_timeout(struct drm_gpu_scheduler *sched)
199{
200 if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
201 !list_empty(&sched->ring_mirror_list))
202 schedule_delayed_work(&sched->work_tdr, sched->timeout);
203}
204
205/**
206 * drm_sched_fault - immediately start timeout handler
207 *
208 * @sched: scheduler where the timeout handling should be started.
209 *
210 * Start timeout handling immediately when the driver detects a hardware fault.
211 */
212void drm_sched_fault(struct drm_gpu_scheduler *sched)
213{
214 mod_delayed_work(system_wq, &sched->work_tdr, 0);
215}
216EXPORT_SYMBOL(drm_sched_fault);
217
218/**
219 * drm_sched_suspend_timeout - Suspend scheduler job timeout
220 *
221 * @sched: scheduler instance for which to suspend the timeout
222 *
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.
226 *
227 * Returns the timeout remaining
228 *
229 */
230unsigned long drm_sched_suspend_timeout(struct drm_gpu_scheduler *sched)
231{
232 unsigned long sched_timeout, now = jiffies;
233
234 sched_timeout = sched->work_tdr.timer.expires;
235
236 /*
237 * Modify the timeout to an arbitrarily large value. This also prevents
238 * the timeout to be restarted when new submissions arrive
239 */
240 if (mod_delayed_work(system_wq, &sched->work_tdr, MAX_SCHEDULE_TIMEOUT)
241 && time_after(sched_timeout, now))
242 return sched_timeout - now;
243 else
244 return sched->timeout;
245}
246EXPORT_SYMBOL(drm_sched_suspend_timeout);
247
248/**
249 * drm_sched_resume_timeout - Resume scheduler job timeout
250 *
251 * @sched: scheduler instance for which to resume the timeout
252 * @remaining: remaining timeout
253 *
254 * Resume the delayed work timeout for the scheduler.
255 */
256void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched,
257 unsigned long remaining)
258{
259 spin_lock(&sched->job_list_lock);
260
261 if (list_empty(&sched->ring_mirror_list))
262 cancel_delayed_work(&sched->work_tdr);
263 else
264 mod_delayed_work(system_wq, &sched->work_tdr, remaining);
265
266 spin_unlock(&sched->job_list_lock);
267}
268EXPORT_SYMBOL(drm_sched_resume_timeout);
269
270static void drm_sched_job_begin(struct drm_sched_job *s_job)
271{
272 struct drm_gpu_scheduler *sched = s_job->sched;
273
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);
278}
279
280static void drm_sched_job_timedout(struct work_struct *work)
281{
282 struct drm_gpu_scheduler *sched;
283 struct drm_sched_job *job;
284
285 sched = container_of(work, struct drm_gpu_scheduler, work_tdr.work);
286
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);
291
292 if (job) {
293 /*
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.
297 */
298 list_del_init(&job->node);
299 spin_unlock(&sched->job_list_lock);
300
301 job->sched->ops->timedout_job(job);
302
303 /*
304 * Guilty job did complete and hence needs to be manually removed
305 * See drm_sched_stop doc.
306 */
307 if (sched->free_guilty) {
308 job->sched->ops->free_job(job);
309 sched->free_guilty = false;
310 }
311 } else {
312 spin_unlock(&sched->job_list_lock);
313 }
314
315 spin_lock(&sched->job_list_lock);
316 drm_sched_start_timeout(sched);
317 spin_unlock(&sched->job_list_lock);
318}
319
320 /**
321 * drm_sched_increase_karma - Update sched_entity guilty flag
322 *
323 * @bad: The job guilty of time out
324 *
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
328 */
329void drm_sched_increase_karma(struct drm_sched_job *bad)
330{
331 int i;
332 struct drm_sched_entity *tmp;
333 struct drm_sched_entity *entity;
334 struct drm_gpu_scheduler *sched = bad->sched;
335
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.
339 */
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;
343 i++) {
344 struct drm_sched_rq *rq = &sched->sched_rq[i];
345
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)
352 if (entity->guilty)
353 atomic_set(entity->guilty, 1);
354 break;
355 }
356 }
357 spin_unlock(&rq->lock);
358 if (&entity->list != &rq->entities)
359 break;
360 }
361 }
362}
363EXPORT_SYMBOL(drm_sched_increase_karma);
364
365/**
366 * drm_sched_stop - stop the scheduler
367 *
368 * @sched: scheduler instance
369 * @bad: job which caused the time out
370 *
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.
375 *
376 */
377void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
378{
379 struct drm_sched_job *s_job, *tmp;
380
381 kthread_park(sched->thread);
382
383 /*
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.
389 */
390 if (bad && bad->sched == sched)
391 /*
392 * Add at the head of the queue to reflect it was the earliest
393 * job extracted.
394 */
395 list_add(&bad->node, &sched->ring_mirror_list);
396
397 /*
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
400 * signaled.
401 * This iteration is thread safe as sched thread is stopped.
402 */
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,
406 &s_job->cb)) {
407 atomic_dec(&sched->hw_rq_count);
408 } else {
409 /*
410 * remove job from ring_mirror_list.
411 * Locking here is for concurrent resume timeout
412 */
413 spin_lock(&sched->job_list_lock);
414 list_del_init(&s_job->node);
415 spin_unlock(&sched->job_list_lock);
416
417 /*
418 * Wait for job's HW fence callback to finish using s_job
419 * before releasing it.
420 *
421 * Job is still alive so fence refcount at least 1
422 */
423 dma_fence_wait(&s_job->s_fence->finished, false);
424
425 /*
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.
429 */
430 if (bad != s_job)
431 sched->ops->free_job(s_job);
432 else
433 sched->free_guilty = true;
434 }
435 }
436
437 /*
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.
442 */
443 cancel_delayed_work(&sched->work_tdr);
444}
445
446EXPORT_SYMBOL(drm_sched_stop);
447
448/**
449 * drm_sched_job_recovery - recover jobs after a reset
450 *
451 * @sched: scheduler instance
452 * @full_recovery: proceed with complete sched restart
453 *
454 */
455void drm_sched_start(struct drm_gpu_scheduler *sched, bool full_recovery)
456{
457 struct drm_sched_job *s_job, *tmp;
458 int r;
459
460 /*
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.
464 */
465 list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
466 struct dma_fence *fence = s_job->s_fence->parent;
467
468 atomic_inc(&sched->hw_rq_count);
469
470 if (!full_recovery)
471 continue;
472
473 if (fence) {
474 r = dma_fence_add_callback(fence, &s_job->cb,
475 drm_sched_process_job);
476 if (r == -ENOENT)
477 drm_sched_process_job(fence, &s_job->cb);
478 else if (r)
479 DRM_ERROR("fence add callback failed (%d)\n",
480 r);
481 } else
482 drm_sched_process_job(NULL, &s_job->cb);
483 }
484
485 if (full_recovery) {
486 spin_lock(&sched->job_list_lock);
487 drm_sched_start_timeout(sched);
488 spin_unlock(&sched->job_list_lock);
489 }
490
491 kthread_unpark(sched->thread);
492}
493EXPORT_SYMBOL(drm_sched_start);
494
495/**
496 * drm_sched_resubmit_jobs - helper to relunch job from mirror ring list
497 *
498 * @sched: scheduler instance
499 *
500 */
501void drm_sched_resubmit_jobs(struct drm_gpu_scheduler *sched)
502{
503 struct drm_sched_job *s_job, *tmp;
504 uint64_t guilty_context;
505 bool found_guilty = false;
506 struct dma_fence *fence;
507
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;
510
511 if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) {
512 found_guilty = true;
513 guilty_context = s_job->s_fence->scheduled.context;
514 }
515
516 if (found_guilty && s_job->s_fence->scheduled.context == guilty_context)
517 dma_fence_set_error(&s_fence->finished, -ECANCELED);
518
519 dma_fence_put(s_job->s_fence->parent);
520 fence = sched->ops->run_job(s_job);
521
522 if (IS_ERR_OR_NULL(fence)) {
523 if (IS_ERR(fence))
524 dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
525
526 s_job->s_fence->parent = NULL;
527 } else {
528 s_job->s_fence->parent = fence;
529 }
530
531
532 }
533}
534EXPORT_SYMBOL(drm_sched_resubmit_jobs);
535
536/**
537 * drm_sched_job_init - init a scheduler job
538 *
539 * @job: scheduler job to init
540 * @entity: scheduler entity to use
541 * @owner: job owner for debugging
542 *
543 * Refer to drm_sched_entity_push_job() documentation
544 * for locking considerations.
545 *
546 * Returns 0 for success, negative error code otherwise.
547 */
548int drm_sched_job_init(struct drm_sched_job *job,
549 struct drm_sched_entity *entity,
550 void *owner)
551{
552 struct drm_gpu_scheduler *sched;
553
554 drm_sched_entity_select_rq(entity);
555 if (!entity->rq)
556 return -ENOENT;
557
558 sched = entity->rq->sched;
559
560 job->sched = sched;
561 job->entity = entity;
562 job->s_priority = entity->rq - sched->sched_rq;
563 job->s_fence = drm_sched_fence_create(entity, owner);
564 if (!job->s_fence)
565 return -ENOMEM;
566 job->id = atomic64_inc_return(&sched->job_id_count);
567
568 INIT_LIST_HEAD(&job->node);
569
570 return 0;
571}
572EXPORT_SYMBOL(drm_sched_job_init);
573
574/**
575 * drm_sched_job_cleanup - clean up scheduler job resources
576 *
577 * @job: scheduler job to clean up
578 */
579void drm_sched_job_cleanup(struct drm_sched_job *job)
580{
581 dma_fence_put(&job->s_fence->finished);
582 job->s_fence = NULL;
583}
584EXPORT_SYMBOL(drm_sched_job_cleanup);
585
586/**
587 * drm_sched_ready - is the scheduler ready
588 *
589 * @sched: scheduler instance
590 *
591 * Return true if we can push more jobs to the hw, otherwise false.
592 */
593static bool drm_sched_ready(struct drm_gpu_scheduler *sched)
594{
595 return atomic_read(&sched->hw_rq_count) <
596 sched->hw_submission_limit;
597}
598
599/**
600 * drm_sched_wakeup - Wake up the scheduler when it is ready
601 *
602 * @sched: scheduler instance
603 *
604 */
605void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
606{
607 if (drm_sched_ready(sched))
608 wake_up_interruptible(&sched->wake_up_worker);
609}
610
611/**
612 * drm_sched_select_entity - Select next entity to process
613 *
614 * @sched: scheduler instance
615 *
616 * Returns the entity to process or NULL if none are found.
617 */
618static struct drm_sched_entity *
619drm_sched_select_entity(struct drm_gpu_scheduler *sched)
620{
621 struct drm_sched_entity *entity;
622 int i;
623
624 if (!drm_sched_ready(sched))
625 return NULL;
626
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]);
630 if (entity)
631 break;
632 }
633
634 return entity;
635}
636
637/**
638 * drm_sched_process_job - process a job
639 *
640 * @f: fence
641 * @cb: fence callbacks
642 *
643 * Called after job has finished execution.
644 */
645static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
646{
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;
650
651 atomic_dec(&sched->hw_rq_count);
652 atomic_dec(&sched->score);
653
654 trace_drm_sched_process_job(s_fence);
655
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);
660}
661
662/**
663 * drm_sched_get_cleanup_job - fetch the next finished job to be destroyed
664 *
665 * @sched: scheduler instance
666 *
667 * Returns the next finished job from the mirror list (if there is one)
668 * ready for it to be destroyed.
669 */
670static struct drm_sched_job *
671drm_sched_get_cleanup_job(struct drm_gpu_scheduler *sched)
672{
673 struct drm_sched_job *job;
674
675 /*
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
678 */
679 if ((sched->timeout != MAX_SCHEDULE_TIMEOUT &&
680 !cancel_delayed_work(&sched->work_tdr)) ||
681 kthread_should_park())
682 return NULL;
683
684 spin_lock(&sched->job_list_lock);
685
686 job = list_first_entry_or_null(&sched->ring_mirror_list,
687 struct drm_sched_job, node);
688
689 if (job && dma_fence_is_signaled(&job->s_fence->finished)) {
690 /* remove job from ring_mirror_list */
691 list_del_init(&job->node);
692 } else {
693 job = NULL;
694 /* queue timeout for next job */
695 drm_sched_start_timeout(sched);
696 }
697
698 spin_unlock(&sched->job_list_lock);
699
700 return job;
701}
702
703/**
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
707 *
708 * Returns pointer of the sched with the least load or NULL if none of the
709 * drm_gpu_schedulers are ready
710 */
711struct drm_gpu_scheduler *
712drm_sched_pick_best(struct drm_gpu_scheduler **sched_list,
713 unsigned int num_sched_list)
714{
715 struct drm_gpu_scheduler *sched, *picked_sched = NULL;
716 int i;
717 unsigned int min_score = UINT_MAX, num_score;
718
719 for (i = 0; i < num_sched_list; ++i) {
720 sched = sched_list[i];
721
722 if (!sched->ready) {
723 DRM_WARN("scheduler %s is not ready, skipping",
724 sched->name);
725 continue;
726 }
727
728 num_score = atomic_read(&sched->score);
729 if (num_score < min_score) {
730 min_score = num_score;
731 picked_sched = sched;
732 }
733 }
734
735 return picked_sched;
736}
737EXPORT_SYMBOL(drm_sched_pick_best);
738
739/**
740 * drm_sched_blocked - check if the scheduler is blocked
741 *
742 * @sched: scheduler instance
743 *
744 * Returns true if blocked, otherwise false.
745 */
746static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
747{
748 if (kthread_should_park()) {
749 kthread_parkme();
750 return true;
751 }
752
753 return false;
754}
755
756/**
757 * drm_sched_main - main scheduler thread
758 *
759 * @param: scheduler instance
760 *
761 * Returns 0.
762 */
763static int drm_sched_main(void *param)
764{
765 struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
766 int r;
767
768 sched_set_fifo_low(current);
769
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;
776
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());
782
783 if (cleanup_job) {
784 sched->ops->free_job(cleanup_job);
785 /* queue timeout for next job */
786 drm_sched_start_timeout(sched);
787 }
788
789 if (!entity)
790 continue;
791
792 sched_job = drm_sched_entity_pop_job(entity);
793
794 complete(&entity->entity_idle);
795
796 if (!sched_job)
797 continue;
798
799 s_fence = sched_job->s_fence;
800
801 atomic_inc(&sched->hw_rq_count);
802 drm_sched_job_begin(sched_job);
803
804 trace_drm_run_job(sched_job, entity);
805 fence = sched->ops->run_job(sched_job);
806 drm_sched_fence_scheduled(s_fence);
807
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);
812 if (r == -ENOENT)
813 drm_sched_process_job(fence, &sched_job->cb);
814 else if (r)
815 DRM_ERROR("fence add callback failed (%d)\n",
816 r);
817 dma_fence_put(fence);
818 } else {
819 if (IS_ERR(fence))
820 dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
821
822 drm_sched_process_job(NULL, &sched_job->cb);
823 }
824
825 wake_up(&sched->job_scheduled);
826 }
827 return 0;
828}
829
830/**
831 * drm_sched_init - Init a gpu scheduler instance
832 *
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
839 *
840 * Return 0 on success, otherwise error code.
841 */
842int drm_sched_init(struct drm_gpu_scheduler *sched,
843 const struct drm_sched_backend_ops *ops,
844 unsigned hw_submission,
845 unsigned hang_limit,
846 long timeout,
847 const char *name)
848{
849 int i, ret;
850 sched->ops = ops;
851 sched->hw_submission_limit = hw_submission;
852 sched->name = name;
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]);
857
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);
866
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);
873 return ret;
874 }
875
876 sched->ready = true;
877 return 0;
878}
879EXPORT_SYMBOL(drm_sched_init);
880
881/**
882 * drm_sched_fini - Destroy a gpu scheduler
883 *
884 * @sched: scheduler instance
885 *
886 * Tears down and cleans up the scheduler.
887 */
888void drm_sched_fini(struct drm_gpu_scheduler *sched)
889{
890 if (sched->thread)
891 kthread_stop(sched->thread);
892
893 sched->ready = false;
894}
895EXPORT_SYMBOL(drm_sched_fini);