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  * Note that once a job was taken from the entities queue and pushed to the
47  * hardware, i.e. the pending queue, the entity must not be referenced anymore
48  * through the jobs entity pointer.
49  */
50 
51 #include <linux/kthread.h>
52 #include <linux/wait.h>
53 #include <linux/sched.h>
54 #include <linux/completion.h>
55 #include <linux/dma-resv.h>
56 #include <uapi/linux/sched/types.h>
57 
58 #include <drm/drm_print.h>
59 #include <drm/drm_gem.h>
60 #include <drm/drm_syncobj.h>
61 #include <drm/gpu_scheduler.h>
62 #include <drm/spsc_queue.h>
63 
64 #define CREATE_TRACE_POINTS
65 #include "gpu_scheduler_trace.h"
66 
67 #define to_drm_sched_job(sched_job)		\
68 		container_of((sched_job), struct drm_sched_job, queue_node)
69 
70 int drm_sched_policy = DRM_SCHED_POLICY_FIFO;
71 
72 /**
73  * DOC: sched_policy (int)
74  * Used to override default entities scheduling policy in a run queue.
75  */
76 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).");
77 module_param_named(sched_policy, drm_sched_policy, int, 0444);
78 
drm_sched_entity_compare_before(struct rb_node * a,const struct rb_node * b)79 static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a,
80 							    const struct rb_node *b)
81 {
82 	struct drm_sched_entity *ent_a =  rb_entry((a), struct drm_sched_entity, rb_tree_node);
83 	struct drm_sched_entity *ent_b =  rb_entry((b), struct drm_sched_entity, rb_tree_node);
84 
85 	return ktime_before(ent_a->oldest_job_waiting, ent_b->oldest_job_waiting);
86 }
87 
drm_sched_rq_remove_fifo_locked(struct drm_sched_entity * entity)88 static inline void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *entity)
89 {
90 	struct drm_sched_rq *rq = entity->rq;
91 
92 	if (!RB_EMPTY_NODE(&entity->rb_tree_node)) {
93 		rb_erase_cached(&entity->rb_tree_node, &rq->rb_tree_root);
94 		RB_CLEAR_NODE(&entity->rb_tree_node);
95 	}
96 }
97 
drm_sched_rq_update_fifo(struct drm_sched_entity * entity,ktime_t ts)98 void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts)
99 {
100 	/*
101 	 * Both locks need to be grabbed, one to protect from entity->rq change
102 	 * for entity from within concurrent drm_sched_entity_select_rq and the
103 	 * other to update the rb tree structure.
104 	 */
105 	spin_lock(&entity->rq_lock);
106 	spin_lock(&entity->rq->lock);
107 
108 	drm_sched_rq_remove_fifo_locked(entity);
109 
110 	entity->oldest_job_waiting = ts;
111 
112 	rb_add_cached(&entity->rb_tree_node, &entity->rq->rb_tree_root,
113 		      drm_sched_entity_compare_before);
114 
115 	spin_unlock(&entity->rq->lock);
116 	spin_unlock(&entity->rq_lock);
117 }
118 
119 /**
120  * drm_sched_rq_init - initialize a given run queue struct
121  *
122  * @sched: scheduler instance to associate with this run queue
123  * @rq: scheduler run queue
124  *
125  * Initializes a scheduler runqueue.
126  */
drm_sched_rq_init(struct drm_gpu_scheduler * sched,struct drm_sched_rq * rq)127 static void drm_sched_rq_init(struct drm_gpu_scheduler *sched,
128 			      struct drm_sched_rq *rq)
129 {
130 	spin_lock_init(&rq->lock);
131 	INIT_LIST_HEAD(&rq->entities);
132 	rq->rb_tree_root = RB_ROOT_CACHED;
133 	rq->current_entity = NULL;
134 	rq->sched = sched;
135 }
136 
137 /**
138  * drm_sched_rq_add_entity - add an entity
139  *
140  * @rq: scheduler run queue
141  * @entity: scheduler entity
142  *
143  * Adds a scheduler entity to the run queue.
144  */
drm_sched_rq_add_entity(struct drm_sched_rq * rq,struct drm_sched_entity * entity)145 void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
146 			     struct drm_sched_entity *entity)
147 {
148 	if (!list_empty(&entity->list))
149 		return;
150 
151 	spin_lock(&rq->lock);
152 
153 	atomic_inc(rq->sched->score);
154 	list_add_tail(&entity->list, &rq->entities);
155 
156 	spin_unlock(&rq->lock);
157 }
158 
159 /**
160  * drm_sched_rq_remove_entity - remove an entity
161  *
162  * @rq: scheduler run queue
163  * @entity: scheduler entity
164  *
165  * Removes a scheduler entity from the run queue.
166  */
drm_sched_rq_remove_entity(struct drm_sched_rq * rq,struct drm_sched_entity * entity)167 void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
168 				struct drm_sched_entity *entity)
169 {
170 	if (list_empty(&entity->list))
171 		return;
172 
173 	spin_lock(&rq->lock);
174 
175 	atomic_dec(rq->sched->score);
176 	list_del_init(&entity->list);
177 
178 	if (rq->current_entity == entity)
179 		rq->current_entity = NULL;
180 
181 	if (drm_sched_policy == DRM_SCHED_POLICY_FIFO)
182 		drm_sched_rq_remove_fifo_locked(entity);
183 
184 	spin_unlock(&rq->lock);
185 }
186 
187 /**
188  * drm_sched_rq_select_entity_rr - Select an entity which could provide a job to run
189  *
190  * @rq: scheduler run queue to check.
191  *
192  * Try to find a ready entity, returns NULL if none found.
193  */
194 static struct drm_sched_entity *
drm_sched_rq_select_entity_rr(struct drm_sched_rq * rq)195 drm_sched_rq_select_entity_rr(struct drm_sched_rq *rq)
196 {
197 	struct drm_sched_entity *entity;
198 
199 	spin_lock(&rq->lock);
200 
201 	entity = rq->current_entity;
202 	if (entity) {
203 		list_for_each_entry_continue(entity, &rq->entities, list) {
204 			if (drm_sched_entity_is_ready(entity)) {
205 				rq->current_entity = entity;
206 				reinit_completion(&entity->entity_idle);
207 				spin_unlock(&rq->lock);
208 				return entity;
209 			}
210 		}
211 	}
212 
213 	list_for_each_entry(entity, &rq->entities, list) {
214 
215 		if (drm_sched_entity_is_ready(entity)) {
216 			rq->current_entity = entity;
217 			reinit_completion(&entity->entity_idle);
218 			spin_unlock(&rq->lock);
219 			return entity;
220 		}
221 
222 		if (entity == rq->current_entity)
223 			break;
224 	}
225 
226 	spin_unlock(&rq->lock);
227 
228 	return NULL;
229 }
230 
231 /**
232  * drm_sched_rq_select_entity_fifo - Select an entity which provides a job to run
233  *
234  * @rq: scheduler run queue to check.
235  *
236  * Find oldest waiting ready entity, returns NULL if none found.
237  */
238 static struct drm_sched_entity *
drm_sched_rq_select_entity_fifo(struct drm_sched_rq * rq)239 drm_sched_rq_select_entity_fifo(struct drm_sched_rq *rq)
240 {
241 	struct rb_node *rb;
242 
243 	spin_lock(&rq->lock);
244 	for (rb = rb_first_cached(&rq->rb_tree_root); rb; rb = rb_next(rb)) {
245 		struct drm_sched_entity *entity;
246 
247 		entity = rb_entry(rb, struct drm_sched_entity, rb_tree_node);
248 		if (drm_sched_entity_is_ready(entity)) {
249 			rq->current_entity = entity;
250 			reinit_completion(&entity->entity_idle);
251 			break;
252 		}
253 	}
254 	spin_unlock(&rq->lock);
255 
256 	return rb ? rb_entry(rb, struct drm_sched_entity, rb_tree_node) : NULL;
257 }
258 
259 /**
260  * drm_sched_job_done - complete a job
261  * @s_job: pointer to the job which is done
262  *
263  * Finish the job's fence and wake up the worker thread.
264  */
drm_sched_job_done(struct drm_sched_job * s_job,int result)265 static void drm_sched_job_done(struct drm_sched_job *s_job, int result)
266 {
267 	struct drm_sched_fence *s_fence = s_job->s_fence;
268 	struct drm_gpu_scheduler *sched = s_fence->sched;
269 
270 	atomic_dec(&sched->hw_rq_count);
271 	atomic_dec(sched->score);
272 
273 	trace_drm_sched_process_job(s_fence);
274 
275 	dma_fence_get(&s_fence->finished);
276 	drm_sched_fence_finished(s_fence, result);
277 	dma_fence_put(&s_fence->finished);
278 	wake_up_interruptible(&sched->wake_up_worker);
279 }
280 
281 /**
282  * drm_sched_job_done_cb - the callback for a done job
283  * @f: fence
284  * @cb: fence callbacks
285  */
drm_sched_job_done_cb(struct dma_fence * f,struct dma_fence_cb * cb)286 static void drm_sched_job_done_cb(struct dma_fence *f, struct dma_fence_cb *cb)
287 {
288 	struct drm_sched_job *s_job = container_of(cb, struct drm_sched_job, cb);
289 
290 	drm_sched_job_done(s_job, f->error);
291 }
292 
293 /**
294  * drm_sched_start_timeout - start timeout for reset worker
295  *
296  * @sched: scheduler instance to start the worker for
297  *
298  * Start the timeout for the given scheduler.
299  */
drm_sched_start_timeout(struct drm_gpu_scheduler * sched)300 static void drm_sched_start_timeout(struct drm_gpu_scheduler *sched)
301 {
302 	if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
303 	    !list_empty(&sched->pending_list))
304 		queue_delayed_work(sched->timeout_wq, &sched->work_tdr, sched->timeout);
305 }
306 
307 /**
308  * drm_sched_fault - immediately start timeout handler
309  *
310  * @sched: scheduler where the timeout handling should be started.
311  *
312  * Start timeout handling immediately when the driver detects a hardware fault.
313  */
drm_sched_fault(struct drm_gpu_scheduler * sched)314 void drm_sched_fault(struct drm_gpu_scheduler *sched)
315 {
316 	if (sched->timeout_wq)
317 		mod_delayed_work(sched->timeout_wq, &sched->work_tdr, 0);
318 }
319 EXPORT_SYMBOL(drm_sched_fault);
320 
321 /**
322  * drm_sched_suspend_timeout - Suspend scheduler job timeout
323  *
324  * @sched: scheduler instance for which to suspend the timeout
325  *
326  * Suspend the delayed work timeout for the scheduler. This is done by
327  * modifying the delayed work timeout to an arbitrary large value,
328  * MAX_SCHEDULE_TIMEOUT in this case.
329  *
330  * Returns the timeout remaining
331  *
332  */
drm_sched_suspend_timeout(struct drm_gpu_scheduler * sched)333 unsigned long drm_sched_suspend_timeout(struct drm_gpu_scheduler *sched)
334 {
335 	unsigned long sched_timeout, now = jiffies;
336 
337 	sched_timeout = sched->work_tdr.timer.expires;
338 
339 	/*
340 	 * Modify the timeout to an arbitrarily large value. This also prevents
341 	 * the timeout to be restarted when new submissions arrive
342 	 */
343 	if (mod_delayed_work(sched->timeout_wq, &sched->work_tdr, MAX_SCHEDULE_TIMEOUT)
344 			&& time_after(sched_timeout, now))
345 		return sched_timeout - now;
346 	else
347 		return sched->timeout;
348 }
349 EXPORT_SYMBOL(drm_sched_suspend_timeout);
350 
351 /**
352  * drm_sched_resume_timeout - Resume scheduler job timeout
353  *
354  * @sched: scheduler instance for which to resume the timeout
355  * @remaining: remaining timeout
356  *
357  * Resume the delayed work timeout for the scheduler.
358  */
drm_sched_resume_timeout(struct drm_gpu_scheduler * sched,unsigned long remaining)359 void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched,
360 		unsigned long remaining)
361 {
362 	spin_lock(&sched->job_list_lock);
363 
364 	if (list_empty(&sched->pending_list))
365 		cancel_delayed_work(&sched->work_tdr);
366 	else
367 		mod_delayed_work(sched->timeout_wq, &sched->work_tdr, remaining);
368 
369 	spin_unlock(&sched->job_list_lock);
370 }
371 EXPORT_SYMBOL(drm_sched_resume_timeout);
372 
drm_sched_job_begin(struct drm_sched_job * s_job)373 static void drm_sched_job_begin(struct drm_sched_job *s_job)
374 {
375 	struct drm_gpu_scheduler *sched = s_job->sched;
376 
377 	spin_lock(&sched->job_list_lock);
378 	list_add_tail(&s_job->list, &sched->pending_list);
379 	drm_sched_start_timeout(sched);
380 	spin_unlock(&sched->job_list_lock);
381 }
382 
drm_sched_job_timedout(struct work_struct * work)383 static void drm_sched_job_timedout(struct work_struct *work)
384 {
385 	struct drm_gpu_scheduler *sched;
386 	struct drm_sched_job *job;
387 	enum drm_gpu_sched_stat status = DRM_GPU_SCHED_STAT_NOMINAL;
388 
389 	sched = container_of(work, struct drm_gpu_scheduler, work_tdr.work);
390 
391 	/* Protects against concurrent deletion in drm_sched_get_cleanup_job */
392 	spin_lock(&sched->job_list_lock);
393 	job = list_first_entry_or_null(&sched->pending_list,
394 				       struct drm_sched_job, list);
395 
396 	if (job) {
397 		/*
398 		 * Remove the bad job so it cannot be freed by concurrent
399 		 * drm_sched_cleanup_jobs. It will be reinserted back after sched->thread
400 		 * is parked at which point it's safe.
401 		 */
402 		list_del_init(&job->list);
403 		spin_unlock(&sched->job_list_lock);
404 
405 		status = job->sched->ops->timedout_job(job);
406 
407 		/*
408 		 * Guilty job did complete and hence needs to be manually removed
409 		 * See drm_sched_stop doc.
410 		 */
411 		if (sched->free_guilty) {
412 			job->sched->ops->free_job(job);
413 			sched->free_guilty = false;
414 		}
415 	} else {
416 		spin_unlock(&sched->job_list_lock);
417 	}
418 
419 	if (status != DRM_GPU_SCHED_STAT_ENODEV) {
420 		spin_lock(&sched->job_list_lock);
421 		drm_sched_start_timeout(sched);
422 		spin_unlock(&sched->job_list_lock);
423 	}
424 }
425 
426 /**
427  * drm_sched_stop - stop the scheduler
428  *
429  * @sched: scheduler instance
430  * @bad: job which caused the time out
431  *
432  * Stop the scheduler and also removes and frees all completed jobs.
433  * Note: bad job will not be freed as it might be used later and so it's
434  * callers responsibility to release it manually if it's not part of the
435  * pending list any more.
436  *
437  */
drm_sched_stop(struct drm_gpu_scheduler * sched,struct drm_sched_job * bad)438 void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
439 {
440 	struct drm_sched_job *s_job, *tmp;
441 
442 	kthread_park(sched->thread);
443 
444 	/*
445 	 * Reinsert back the bad job here - now it's safe as
446 	 * drm_sched_get_cleanup_job cannot race against us and release the
447 	 * bad job at this point - we parked (waited for) any in progress
448 	 * (earlier) cleanups and drm_sched_get_cleanup_job will not be called
449 	 * now until the scheduler thread is unparked.
450 	 */
451 	if (bad && bad->sched == sched)
452 		/*
453 		 * Add at the head of the queue to reflect it was the earliest
454 		 * job extracted.
455 		 */
456 		list_add(&bad->list, &sched->pending_list);
457 
458 	/*
459 	 * Iterate the job list from later to  earlier one and either deactive
460 	 * their HW callbacks or remove them from pending list if they already
461 	 * signaled.
462 	 * This iteration is thread safe as sched thread is stopped.
463 	 */
464 	list_for_each_entry_safe_reverse(s_job, tmp, &sched->pending_list,
465 					 list) {
466 		if (s_job->s_fence->parent &&
467 		    dma_fence_remove_callback(s_job->s_fence->parent,
468 					      &s_job->cb)) {
469 			dma_fence_put(s_job->s_fence->parent);
470 			s_job->s_fence->parent = NULL;
471 			atomic_dec(&sched->hw_rq_count);
472 		} else {
473 			/*
474 			 * remove job from pending_list.
475 			 * Locking here is for concurrent resume timeout
476 			 */
477 			spin_lock(&sched->job_list_lock);
478 			list_del_init(&s_job->list);
479 			spin_unlock(&sched->job_list_lock);
480 
481 			/*
482 			 * Wait for job's HW fence callback to finish using s_job
483 			 * before releasing it.
484 			 *
485 			 * Job is still alive so fence refcount at least 1
486 			 */
487 			dma_fence_wait(&s_job->s_fence->finished, false);
488 
489 			/*
490 			 * We must keep bad job alive for later use during
491 			 * recovery by some of the drivers but leave a hint
492 			 * that the guilty job must be released.
493 			 */
494 			if (bad != s_job)
495 				sched->ops->free_job(s_job);
496 			else
497 				sched->free_guilty = true;
498 		}
499 	}
500 
501 	/*
502 	 * Stop pending timer in flight as we rearm it in  drm_sched_start. This
503 	 * avoids the pending timeout work in progress to fire right away after
504 	 * this TDR finished and before the newly restarted jobs had a
505 	 * chance to complete.
506 	 */
507 	cancel_delayed_work(&sched->work_tdr);
508 }
509 
510 EXPORT_SYMBOL(drm_sched_stop);
511 
512 /**
513  * drm_sched_start - recover jobs after a reset
514  *
515  * @sched: scheduler instance
516  * @full_recovery: proceed with complete sched restart
517  *
518  */
drm_sched_start(struct drm_gpu_scheduler * sched,bool full_recovery)519 void drm_sched_start(struct drm_gpu_scheduler *sched, bool full_recovery)
520 {
521 	struct drm_sched_job *s_job, *tmp;
522 	int r;
523 
524 	/*
525 	 * Locking the list is not required here as the sched thread is parked
526 	 * so no new jobs are being inserted or removed. Also concurrent
527 	 * GPU recovers can't run in parallel.
528 	 */
529 	list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
530 		struct dma_fence *fence = s_job->s_fence->parent;
531 
532 		atomic_inc(&sched->hw_rq_count);
533 
534 		if (!full_recovery)
535 			continue;
536 
537 		if (fence) {
538 			r = dma_fence_add_callback(fence, &s_job->cb,
539 						   drm_sched_job_done_cb);
540 			if (r == -ENOENT)
541 				drm_sched_job_done(s_job, fence->error);
542 			else if (r)
543 				DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n",
544 					  r);
545 		} else
546 			drm_sched_job_done(s_job, -ECANCELED);
547 	}
548 
549 	if (full_recovery) {
550 		spin_lock(&sched->job_list_lock);
551 		drm_sched_start_timeout(sched);
552 		spin_unlock(&sched->job_list_lock);
553 	}
554 
555 	kthread_unpark(sched->thread);
556 }
557 EXPORT_SYMBOL(drm_sched_start);
558 
559 /**
560  * drm_sched_resubmit_jobs - Deprecated, don't use in new code!
561  *
562  * @sched: scheduler instance
563  *
564  * Re-submitting jobs was a concept AMD came up as cheap way to implement
565  * recovery after a job timeout.
566  *
567  * This turned out to be not working very well. First of all there are many
568  * problem with the dma_fence implementation and requirements. Either the
569  * implementation is risking deadlocks with core memory management or violating
570  * documented implementation details of the dma_fence object.
571  *
572  * Drivers can still save and restore their state for recovery operations, but
573  * we shouldn't make this a general scheduler feature around the dma_fence
574  * interface.
575  */
drm_sched_resubmit_jobs(struct drm_gpu_scheduler * sched)576 void drm_sched_resubmit_jobs(struct drm_gpu_scheduler *sched)
577 {
578 	struct drm_sched_job *s_job, *tmp;
579 	uint64_t guilty_context;
580 	bool found_guilty = false;
581 	struct dma_fence *fence;
582 
583 	list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
584 		struct drm_sched_fence *s_fence = s_job->s_fence;
585 
586 		if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) {
587 			found_guilty = true;
588 			guilty_context = s_job->s_fence->scheduled.context;
589 		}
590 
591 		if (found_guilty && s_job->s_fence->scheduled.context == guilty_context)
592 			dma_fence_set_error(&s_fence->finished, -ECANCELED);
593 
594 		fence = sched->ops->run_job(s_job);
595 
596 		if (IS_ERR_OR_NULL(fence)) {
597 			if (IS_ERR(fence))
598 				dma_fence_set_error(&s_fence->finished, PTR_ERR(fence));
599 
600 			s_job->s_fence->parent = NULL;
601 		} else {
602 
603 			s_job->s_fence->parent = dma_fence_get(fence);
604 
605 			/* Drop for orignal kref_init */
606 			dma_fence_put(fence);
607 		}
608 	}
609 }
610 EXPORT_SYMBOL(drm_sched_resubmit_jobs);
611 
612 /**
613  * drm_sched_job_init - init a scheduler job
614  * @job: scheduler job to init
615  * @entity: scheduler entity to use
616  * @owner: job owner for debugging
617  *
618  * Refer to drm_sched_entity_push_job() documentation
619  * for locking considerations.
620  *
621  * Drivers must make sure drm_sched_job_cleanup() if this function returns
622  * successfully, even when @job is aborted before drm_sched_job_arm() is called.
623  *
624  * WARNING: amdgpu abuses &drm_sched.ready to signal when the hardware
625  * has died, which can mean that there's no valid runqueue for a @entity.
626  * This function returns -ENOENT in this case (which probably should be -EIO as
627  * a more meanigful return value).
628  *
629  * Returns 0 for success, negative error code otherwise.
630  */
drm_sched_job_init(struct drm_sched_job * job,struct drm_sched_entity * entity,void * owner)631 int drm_sched_job_init(struct drm_sched_job *job,
632 		       struct drm_sched_entity *entity,
633 		       void *owner)
634 {
635 	if (!entity->rq)
636 		return -ENOENT;
637 
638 	/*
639 	 * We don't know for sure how the user has allocated. Thus, zero the
640 	 * struct so that unallowed (i.e., too early) usage of pointers that
641 	 * this function does not set is guaranteed to lead to a NULL pointer
642 	 * exception instead of UB.
643 	 */
644 	memset(job, 0, sizeof(*job));
645 
646 	job->entity = entity;
647 	job->s_fence = drm_sched_fence_alloc(entity, owner);
648 	if (!job->s_fence)
649 		return -ENOMEM;
650 
651 	INIT_LIST_HEAD(&job->list);
652 
653 	xa_init_flags(&job->dependencies, XA_FLAGS_ALLOC);
654 
655 	return 0;
656 }
657 EXPORT_SYMBOL(drm_sched_job_init);
658 
659 /**
660  * drm_sched_job_arm - arm a scheduler job for execution
661  * @job: scheduler job to arm
662  *
663  * This arms a scheduler job for execution. Specifically it initializes the
664  * &drm_sched_job.s_fence of @job, so that it can be attached to struct dma_resv
665  * or other places that need to track the completion of this job.
666  *
667  * Refer to drm_sched_entity_push_job() documentation for locking
668  * considerations.
669  *
670  * This can only be called if drm_sched_job_init() succeeded.
671  */
drm_sched_job_arm(struct drm_sched_job * job)672 void drm_sched_job_arm(struct drm_sched_job *job)
673 {
674 	struct drm_gpu_scheduler *sched;
675 	struct drm_sched_entity *entity = job->entity;
676 
677 	BUG_ON(!entity);
678 	drm_sched_entity_select_rq(entity);
679 	sched = entity->rq->sched;
680 
681 	job->sched = sched;
682 	job->s_priority = entity->rq - sched->sched_rq;
683 	job->id = atomic64_inc_return(&sched->job_id_count);
684 
685 	drm_sched_fence_init(job->s_fence, job->entity);
686 }
687 EXPORT_SYMBOL(drm_sched_job_arm);
688 
689 /**
690  * drm_sched_job_add_dependency - adds the fence as a job dependency
691  * @job: scheduler job to add the dependencies to
692  * @fence: the dma_fence to add to the list of dependencies.
693  *
694  * Note that @fence is consumed in both the success and error cases.
695  *
696  * Returns:
697  * 0 on success, or an error on failing to expand the array.
698  */
drm_sched_job_add_dependency(struct drm_sched_job * job,struct dma_fence * fence)699 int drm_sched_job_add_dependency(struct drm_sched_job *job,
700 				 struct dma_fence *fence)
701 {
702 	struct dma_fence *entry;
703 	unsigned long index;
704 	u32 id = 0;
705 	int ret;
706 
707 	if (!fence)
708 		return 0;
709 
710 	/* Deduplicate if we already depend on a fence from the same context.
711 	 * This lets the size of the array of deps scale with the number of
712 	 * engines involved, rather than the number of BOs.
713 	 */
714 	xa_for_each(&job->dependencies, index, entry) {
715 		if (entry->context != fence->context)
716 			continue;
717 
718 		if (dma_fence_is_later(fence, entry)) {
719 			dma_fence_put(entry);
720 			xa_store(&job->dependencies, index, fence, GFP_KERNEL);
721 		} else {
722 			dma_fence_put(fence);
723 		}
724 		return 0;
725 	}
726 
727 	ret = xa_alloc(&job->dependencies, &id, fence, xa_limit_32b, GFP_KERNEL);
728 	if (ret != 0)
729 		dma_fence_put(fence);
730 
731 	return ret;
732 }
733 EXPORT_SYMBOL(drm_sched_job_add_dependency);
734 
735 /**
736  * drm_sched_job_add_syncobj_dependency - adds a syncobj's fence as a job dependency
737  * @job: scheduler job to add the dependencies to
738  * @file: drm file private pointer
739  * @handle: syncobj handle to lookup
740  * @point: timeline point
741  *
742  * This adds the fence matching the given syncobj to @job.
743  *
744  * Returns:
745  * 0 on success, or an error on failing to expand the array.
746  */
drm_sched_job_add_syncobj_dependency(struct drm_sched_job * job,struct drm_file * file,u32 handle,u32 point)747 int drm_sched_job_add_syncobj_dependency(struct drm_sched_job *job,
748 					 struct drm_file *file,
749 					 u32 handle,
750 					 u32 point)
751 {
752 	struct dma_fence *fence;
753 	int ret;
754 
755 	ret = drm_syncobj_find_fence(file, handle, point, 0, &fence);
756 	if (ret)
757 		return ret;
758 
759 	return drm_sched_job_add_dependency(job, fence);
760 }
761 EXPORT_SYMBOL(drm_sched_job_add_syncobj_dependency);
762 
763 /**
764  * drm_sched_job_add_resv_dependencies - add all fences from the resv to the job
765  * @job: scheduler job to add the dependencies to
766  * @resv: the dma_resv object to get the fences from
767  * @usage: the dma_resv_usage to use to filter the fences
768  *
769  * This adds all fences matching the given usage from @resv to @job.
770  * Must be called with the @resv lock held.
771  *
772  * Returns:
773  * 0 on success, or an error on failing to expand the array.
774  */
drm_sched_job_add_resv_dependencies(struct drm_sched_job * job,struct dma_resv * resv,enum dma_resv_usage usage)775 int drm_sched_job_add_resv_dependencies(struct drm_sched_job *job,
776 					struct dma_resv *resv,
777 					enum dma_resv_usage usage)
778 {
779 	struct dma_resv_iter cursor;
780 	struct dma_fence *fence;
781 	int ret;
782 
783 	dma_resv_assert_held(resv);
784 
785 	dma_resv_for_each_fence(&cursor, resv, usage, fence) {
786 		/* Make sure to grab an additional ref on the added fence */
787 		dma_fence_get(fence);
788 		ret = drm_sched_job_add_dependency(job, fence);
789 		if (ret) {
790 			dma_fence_put(fence);
791 			return ret;
792 		}
793 	}
794 	return 0;
795 }
796 EXPORT_SYMBOL(drm_sched_job_add_resv_dependencies);
797 
798 /**
799  * drm_sched_job_add_implicit_dependencies - adds implicit dependencies as job
800  *   dependencies
801  * @job: scheduler job to add the dependencies to
802  * @obj: the gem object to add new dependencies from.
803  * @write: whether the job might write the object (so we need to depend on
804  * shared fences in the reservation object).
805  *
806  * This should be called after drm_gem_lock_reservations() on your array of
807  * GEM objects used in the job but before updating the reservations with your
808  * own fences.
809  *
810  * Returns:
811  * 0 on success, or an error on failing to expand the array.
812  */
drm_sched_job_add_implicit_dependencies(struct drm_sched_job * job,struct drm_gem_object * obj,bool write)813 int drm_sched_job_add_implicit_dependencies(struct drm_sched_job *job,
814 					    struct drm_gem_object *obj,
815 					    bool write)
816 {
817 	return drm_sched_job_add_resv_dependencies(job, obj->resv,
818 						   dma_resv_usage_rw(write));
819 }
820 EXPORT_SYMBOL(drm_sched_job_add_implicit_dependencies);
821 
822 /**
823  * drm_sched_job_cleanup - clean up scheduler job resources
824  * @job: scheduler job to clean up
825  *
826  * Cleans up the resources allocated with drm_sched_job_init().
827  *
828  * Drivers should call this from their error unwind code if @job is aborted
829  * before drm_sched_job_arm() is called.
830  *
831  * After that point of no return @job is committed to be executed by the
832  * scheduler, and this function should be called from the
833  * &drm_sched_backend_ops.free_job callback.
834  */
drm_sched_job_cleanup(struct drm_sched_job * job)835 void drm_sched_job_cleanup(struct drm_sched_job *job)
836 {
837 	struct dma_fence *fence;
838 	unsigned long index;
839 
840 	if (kref_read(&job->s_fence->finished.refcount)) {
841 		/* drm_sched_job_arm() has been called */
842 		dma_fence_put(&job->s_fence->finished);
843 	} else {
844 		/* aborted job before committing to run it */
845 		drm_sched_fence_free(job->s_fence);
846 	}
847 
848 	job->s_fence = NULL;
849 
850 	xa_for_each(&job->dependencies, index, fence) {
851 		dma_fence_put(fence);
852 	}
853 	xa_destroy(&job->dependencies);
854 
855 }
856 EXPORT_SYMBOL(drm_sched_job_cleanup);
857 
858 /**
859  * drm_sched_can_queue -- Can we queue more to the hardware?
860  * @sched: scheduler instance
861  *
862  * Return true if we can push more jobs to the hw, otherwise false.
863  */
drm_sched_can_queue(struct drm_gpu_scheduler * sched)864 static bool drm_sched_can_queue(struct drm_gpu_scheduler *sched)
865 {
866 	return atomic_read(&sched->hw_rq_count) <
867 		sched->hw_submission_limit;
868 }
869 
870 /**
871  * drm_sched_wakeup_if_can_queue - Wake up the scheduler
872  * @sched: scheduler instance
873  *
874  * Wake up the scheduler if we can queue jobs.
875  */
drm_sched_wakeup_if_can_queue(struct drm_gpu_scheduler * sched)876 void drm_sched_wakeup_if_can_queue(struct drm_gpu_scheduler *sched)
877 {
878 	if (drm_sched_can_queue(sched))
879 		wake_up_interruptible(&sched->wake_up_worker);
880 }
881 
882 /**
883  * drm_sched_select_entity - Select next entity to process
884  *
885  * @sched: scheduler instance
886  *
887  * Returns the entity to process or NULL if none are found.
888  */
889 static struct drm_sched_entity *
drm_sched_select_entity(struct drm_gpu_scheduler * sched)890 drm_sched_select_entity(struct drm_gpu_scheduler *sched)
891 {
892 	struct drm_sched_entity *entity;
893 	int i;
894 
895 	if (!drm_sched_can_queue(sched))
896 		return NULL;
897 
898 	/* Kernel run queue has higher priority than normal run queue*/
899 	for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
900 		entity = drm_sched_policy == DRM_SCHED_POLICY_FIFO ?
901 			drm_sched_rq_select_entity_fifo(&sched->sched_rq[i]) :
902 			drm_sched_rq_select_entity_rr(&sched->sched_rq[i]);
903 		if (entity)
904 			break;
905 	}
906 
907 	return entity;
908 }
909 
910 /**
911  * drm_sched_get_cleanup_job - fetch the next finished job to be destroyed
912  *
913  * @sched: scheduler instance
914  *
915  * Returns the next finished job from the pending list (if there is one)
916  * ready for it to be destroyed.
917  */
918 static struct drm_sched_job *
drm_sched_get_cleanup_job(struct drm_gpu_scheduler * sched)919 drm_sched_get_cleanup_job(struct drm_gpu_scheduler *sched)
920 {
921 	struct drm_sched_job *job, *next;
922 
923 	spin_lock(&sched->job_list_lock);
924 
925 	job = list_first_entry_or_null(&sched->pending_list,
926 				       struct drm_sched_job, list);
927 
928 	if (job && dma_fence_is_signaled(&job->s_fence->finished)) {
929 		/* remove job from pending_list */
930 		list_del_init(&job->list);
931 
932 		/* cancel this job's TO timer */
933 		cancel_delayed_work(&sched->work_tdr);
934 		/* make the scheduled timestamp more accurate */
935 		next = list_first_entry_or_null(&sched->pending_list,
936 						typeof(*next), list);
937 
938 		if (next) {
939 			next->s_fence->scheduled.timestamp =
940 				dma_fence_timestamp(&job->s_fence->finished);
941 			/* start TO timer for next job */
942 			drm_sched_start_timeout(sched);
943 		}
944 	} else {
945 		job = NULL;
946 	}
947 
948 	spin_unlock(&sched->job_list_lock);
949 
950 	return job;
951 }
952 
953 /**
954  * drm_sched_pick_best - Get a drm sched from a sched_list with the least load
955  * @sched_list: list of drm_gpu_schedulers
956  * @num_sched_list: number of drm_gpu_schedulers in the sched_list
957  *
958  * Returns pointer of the sched with the least load or NULL if none of the
959  * drm_gpu_schedulers are ready
960  */
961 struct drm_gpu_scheduler *
drm_sched_pick_best(struct drm_gpu_scheduler ** sched_list,unsigned int num_sched_list)962 drm_sched_pick_best(struct drm_gpu_scheduler **sched_list,
963 		     unsigned int num_sched_list)
964 {
965 	struct drm_gpu_scheduler *sched, *picked_sched = NULL;
966 	int i;
967 	unsigned int min_score = UINT_MAX, num_score;
968 
969 	for (i = 0; i < num_sched_list; ++i) {
970 		sched = sched_list[i];
971 
972 		if (!sched->ready) {
973 			DRM_WARN("scheduler %s is not ready, skipping",
974 				 sched->name);
975 			continue;
976 		}
977 
978 		num_score = atomic_read(sched->score);
979 		if (num_score < min_score) {
980 			min_score = num_score;
981 			picked_sched = sched;
982 		}
983 	}
984 
985 	return picked_sched;
986 }
987 EXPORT_SYMBOL(drm_sched_pick_best);
988 
989 /**
990  * drm_sched_blocked - check if the scheduler is blocked
991  *
992  * @sched: scheduler instance
993  *
994  * Returns true if blocked, otherwise false.
995  */
drm_sched_blocked(struct drm_gpu_scheduler * sched)996 static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
997 {
998 	if (kthread_should_park()) {
999 		kthread_parkme();
1000 		return true;
1001 	}
1002 
1003 	return false;
1004 }
1005 
1006 /**
1007  * drm_sched_main - main scheduler thread
1008  *
1009  * @param: scheduler instance
1010  *
1011  * Returns 0.
1012  */
drm_sched_main(void * param)1013 static int drm_sched_main(void *param)
1014 {
1015 	struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
1016 	int r;
1017 
1018 	sched_set_fifo_low(current);
1019 
1020 	while (!kthread_should_stop()) {
1021 		struct drm_sched_entity *entity = NULL;
1022 		struct drm_sched_fence *s_fence;
1023 		struct drm_sched_job *sched_job;
1024 		struct dma_fence *fence;
1025 		struct drm_sched_job *cleanup_job = NULL;
1026 
1027 		wait_event_interruptible(sched->wake_up_worker,
1028 					 (cleanup_job = drm_sched_get_cleanup_job(sched)) ||
1029 					 (!drm_sched_blocked(sched) &&
1030 					  (entity = drm_sched_select_entity(sched))) ||
1031 					 kthread_should_stop());
1032 
1033 		if (cleanup_job)
1034 			sched->ops->free_job(cleanup_job);
1035 
1036 		if (!entity)
1037 			continue;
1038 
1039 		sched_job = drm_sched_entity_pop_job(entity);
1040 
1041 		if (!sched_job) {
1042 			complete_all(&entity->entity_idle);
1043 			continue;
1044 		}
1045 
1046 		s_fence = sched_job->s_fence;
1047 
1048 		atomic_inc(&sched->hw_rq_count);
1049 		drm_sched_job_begin(sched_job);
1050 
1051 		trace_drm_run_job(sched_job, entity);
1052 		fence = sched->ops->run_job(sched_job);
1053 		complete_all(&entity->entity_idle);
1054 		drm_sched_fence_scheduled(s_fence, fence);
1055 
1056 		if (!IS_ERR_OR_NULL(fence)) {
1057 			/* Drop for original kref_init of the fence */
1058 			dma_fence_put(fence);
1059 
1060 			r = dma_fence_add_callback(fence, &sched_job->cb,
1061 						   drm_sched_job_done_cb);
1062 			if (r == -ENOENT)
1063 				drm_sched_job_done(sched_job, fence->error);
1064 			else if (r)
1065 				DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n",
1066 					  r);
1067 		} else {
1068 			drm_sched_job_done(sched_job, IS_ERR(fence) ?
1069 					   PTR_ERR(fence) : 0);
1070 		}
1071 
1072 		wake_up(&sched->job_scheduled);
1073 	}
1074 	return 0;
1075 }
1076 
1077 /**
1078  * drm_sched_init - Init a gpu scheduler instance
1079  *
1080  * @sched: scheduler instance
1081  * @ops: backend operations for this scheduler
1082  * @hw_submission: number of hw submissions that can be in flight
1083  * @hang_limit: number of times to allow a job to hang before dropping it
1084  * @timeout: timeout value in jiffies for the scheduler
1085  * @timeout_wq: workqueue to use for timeout work. If NULL, the system_wq is
1086  *		used
1087  * @score: optional score atomic shared with other schedulers
1088  * @name: name used for debugging
1089  * @dev: target &struct device
1090  *
1091  * Return 0 on success, otherwise error code.
1092  */
drm_sched_init(struct drm_gpu_scheduler * sched,const struct drm_sched_backend_ops * ops,unsigned hw_submission,unsigned hang_limit,long timeout,struct workqueue_struct * timeout_wq,atomic_t * score,const char * name,struct device * dev)1093 int drm_sched_init(struct drm_gpu_scheduler *sched,
1094 		   const struct drm_sched_backend_ops *ops,
1095 		   unsigned hw_submission, unsigned hang_limit,
1096 		   long timeout, struct workqueue_struct *timeout_wq,
1097 		   atomic_t *score, const char *name, struct device *dev)
1098 {
1099 	int i, ret;
1100 	sched->ops = ops;
1101 	sched->hw_submission_limit = hw_submission;
1102 	sched->name = name;
1103 	sched->timeout = timeout;
1104 	sched->timeout_wq = timeout_wq ? : system_wq;
1105 	sched->hang_limit = hang_limit;
1106 	sched->score = score ? score : &sched->_score;
1107 	sched->dev = dev;
1108 	for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_COUNT; i++)
1109 		drm_sched_rq_init(sched, &sched->sched_rq[i]);
1110 
1111 	init_waitqueue_head(&sched->wake_up_worker);
1112 	init_waitqueue_head(&sched->job_scheduled);
1113 	INIT_LIST_HEAD(&sched->pending_list);
1114 	spin_lock_init(&sched->job_list_lock);
1115 	atomic_set(&sched->hw_rq_count, 0);
1116 	INIT_DELAYED_WORK(&sched->work_tdr, drm_sched_job_timedout);
1117 	atomic_set(&sched->_score, 0);
1118 	atomic64_set(&sched->job_id_count, 0);
1119 
1120 	/* Each scheduler will run on a seperate kernel thread */
1121 	sched->thread = kthread_run(drm_sched_main, sched, sched->name);
1122 	if (IS_ERR(sched->thread)) {
1123 		ret = PTR_ERR(sched->thread);
1124 		sched->thread = NULL;
1125 		DRM_DEV_ERROR(sched->dev, "Failed to create scheduler for %s.\n", name);
1126 		return ret;
1127 	}
1128 
1129 	sched->ready = true;
1130 	return 0;
1131 }
1132 EXPORT_SYMBOL(drm_sched_init);
1133 
1134 /**
1135  * drm_sched_fini - Destroy a gpu scheduler
1136  *
1137  * @sched: scheduler instance
1138  *
1139  * Tears down and cleans up the scheduler.
1140  */
drm_sched_fini(struct drm_gpu_scheduler * sched)1141 void drm_sched_fini(struct drm_gpu_scheduler *sched)
1142 {
1143 	struct drm_sched_entity *s_entity;
1144 	int i;
1145 
1146 	if (sched->thread)
1147 		kthread_stop(sched->thread);
1148 
1149 	for (i = DRM_SCHED_PRIORITY_COUNT - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
1150 		struct drm_sched_rq *rq = &sched->sched_rq[i];
1151 
1152 		spin_lock(&rq->lock);
1153 		list_for_each_entry(s_entity, &rq->entities, list)
1154 			/*
1155 			 * Prevents reinsertion and marks job_queue as idle,
1156 			 * it will removed from rq in drm_sched_entity_fini
1157 			 * eventually
1158 			 */
1159 			s_entity->stopped = true;
1160 		spin_unlock(&rq->lock);
1161 
1162 	}
1163 
1164 	/* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */
1165 	wake_up_all(&sched->job_scheduled);
1166 
1167 	/* Confirm no work left behind accessing device structures */
1168 	cancel_delayed_work_sync(&sched->work_tdr);
1169 
1170 	sched->ready = false;
1171 }
1172 EXPORT_SYMBOL(drm_sched_fini);
1173 
1174 /**
1175  * drm_sched_increase_karma - Update sched_entity guilty flag
1176  *
1177  * @bad: The job guilty of time out
1178  *
1179  * Increment on every hang caused by the 'bad' job. If this exceeds the hang
1180  * limit of the scheduler then the respective sched entity is marked guilty and
1181  * jobs from it will not be scheduled further
1182  */
drm_sched_increase_karma(struct drm_sched_job * bad)1183 void drm_sched_increase_karma(struct drm_sched_job *bad)
1184 {
1185 	int i;
1186 	struct drm_sched_entity *tmp;
1187 	struct drm_sched_entity *entity;
1188 	struct drm_gpu_scheduler *sched = bad->sched;
1189 
1190 	/* don't change @bad's karma if it's from KERNEL RQ,
1191 	 * because sometimes GPU hang would cause kernel jobs (like VM updating jobs)
1192 	 * corrupt but keep in mind that kernel jobs always considered good.
1193 	 */
1194 	if (bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) {
1195 		atomic_inc(&bad->karma);
1196 
1197 		for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_KERNEL;
1198 		     i++) {
1199 			struct drm_sched_rq *rq = &sched->sched_rq[i];
1200 
1201 			spin_lock(&rq->lock);
1202 			list_for_each_entry_safe(entity, tmp, &rq->entities, list) {
1203 				if (bad->s_fence->scheduled.context ==
1204 				    entity->fence_context) {
1205 					if (entity->guilty)
1206 						atomic_set(entity->guilty, 1);
1207 					break;
1208 				}
1209 			}
1210 			spin_unlock(&rq->lock);
1211 			if (&entity->list != &rq->entities)
1212 				break;
1213 		}
1214 	}
1215 }
1216 EXPORT_SYMBOL(drm_sched_increase_karma);
1217