gpu_scheduler.c 21.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright 2015 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */
23

24 25 26
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/sched.h>
27
#include <uapi/linux/sched/types.h>
28
#include <drm/drmP.h>
29 30
#include <drm/gpu_scheduler.h>
#include <drm/spsc_queue.h>
31

32
#define CREATE_TRACE_POINTS
33
#include "gpu_scheduler_trace.h"
34

35 36
#define to_drm_sched_job(sched_job)		\
		container_of((sched_job), struct drm_sched_job, queue_node)
37

38 39 40
static bool drm_sched_entity_is_ready(struct drm_sched_entity *entity);
static void drm_sched_wakeup(struct drm_gpu_scheduler *sched);
static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb);
41

42
/* Initialize a given run queue struct */
43
static void drm_sched_rq_init(struct drm_sched_rq *rq)
44
{
45
	spin_lock_init(&rq->lock);
46 47
	INIT_LIST_HEAD(&rq->entities);
	rq->current_entity = NULL;
48 49
}

50 51
static void drm_sched_rq_add_entity(struct drm_sched_rq *rq,
				    struct drm_sched_entity *entity)
52
{
53 54
	if (!list_empty(&entity->list))
		return;
55
	spin_lock(&rq->lock);
56
	list_add_tail(&entity->list, &rq->entities);
57
	spin_unlock(&rq->lock);
58 59
}

60 61
static void drm_sched_rq_remove_entity(struct drm_sched_rq *rq,
				       struct drm_sched_entity *entity)
62
{
63 64
	if (list_empty(&entity->list))
		return;
65
	spin_lock(&rq->lock);
66 67 68
	list_del_init(&entity->list);
	if (rq->current_entity == entity)
		rq->current_entity = NULL;
69
	spin_unlock(&rq->lock);
70 71 72
}

/**
73 74 75 76 77
 * Select an entity which could provide a job to run
 *
 * @rq		The run queue to check.
 *
 * Try to find a ready entity, returns NULL if none found.
78
 */
79 80
static struct drm_sched_entity *
drm_sched_rq_select_entity(struct drm_sched_rq *rq)
81
{
82
	struct drm_sched_entity *entity;
83

84 85 86
	spin_lock(&rq->lock);

	entity = rq->current_entity;
87 88
	if (entity) {
		list_for_each_entry_continue(entity, &rq->entities, list) {
89
			if (drm_sched_entity_is_ready(entity)) {
90
				rq->current_entity = entity;
91
				spin_unlock(&rq->lock);
92
				return entity;
93
			}
94 95 96
		}
	}

97
	list_for_each_entry(entity, &rq->entities, list) {
98

99
		if (drm_sched_entity_is_ready(entity)) {
100
			rq->current_entity = entity;
101
			spin_unlock(&rq->lock);
102
			return entity;
103
		}
104

105 106 107
		if (entity == rq->current_entity)
			break;
	}
108

109 110
	spin_unlock(&rq->lock);

111
	return NULL;
112 113 114 115 116 117
}

/**
 * Init a context entity used by scheduler when submit to HW ring.
 *
 * @sched	The pointer to the scheduler
118
 * @entity	The pointer to a valid drm_sched_entity
119
 * @rq		The run queue this entity belongs
120 121
 * @guilty      atomic_t set to 1 when a job on this queue
 *              is found to be guilty causing a timeout
122 123 124
 *
 * return 0 if succeed. negative error code on failure
*/
125 126 127
int drm_sched_entity_init(struct drm_gpu_scheduler *sched,
			  struct drm_sched_entity *entity,
			  struct drm_sched_rq *rq,
128
			  atomic_t *guilty)
129 130 131 132
{
	if (!(sched && entity && rq))
		return -EINVAL;

133
	memset(entity, 0, sizeof(struct drm_sched_entity));
134 135 136
	INIT_LIST_HEAD(&entity->list);
	entity->rq = rq;
	entity->sched = sched;
137
	entity->guilty = guilty;
138 139
	entity->fini_status = 0;
	entity->last_scheduled = NULL;
140

141
	spin_lock_init(&entity->rq_lock);
142
	spsc_queue_init(&entity->job_queue);
143

144
	atomic_set(&entity->fence_seq, 0);
145
	entity->fence_context = dma_fence_context_alloc(2);
146 147 148

	return 0;
}
149
EXPORT_SYMBOL(drm_sched_entity_init);
150 151 152 153 154 155 156 157 158

/**
 * Query if entity is initialized
 *
 * @sched       Pointer to scheduler instance
 * @entity	The pointer to a valid scheduler entity
 *
 * return true if entity is initialized, false otherwise
*/
159 160
static bool drm_sched_entity_is_initialized(struct drm_gpu_scheduler *sched,
					    struct drm_sched_entity *entity)
161
{
162 163
	return entity->sched == sched &&
		entity->rq != NULL;
164 165
}

166 167 168 169 170 171 172
/**
 * Check if entity is idle
 *
 * @entity	The pointer to a valid scheduler entity
 *
 * Return true if entity don't has any unscheduled jobs.
 */
173
static bool drm_sched_entity_is_idle(struct drm_sched_entity *entity)
174
{
175
	rmb();
176
	if (spsc_queue_peek(&entity->job_queue) == NULL)
177 178 179 180 181
		return true;

	return false;
}

182 183 184 185 186 187 188
/**
 * Check if entity is ready
 *
 * @entity	The pointer to a valid scheduler entity
 *
 * Return true if entity could provide a job.
 */
189
static bool drm_sched_entity_is_ready(struct drm_sched_entity *entity)
190
{
191
	if (spsc_queue_peek(&entity->job_queue) == NULL)
192 193
		return false;

194
	if (READ_ONCE(entity->dependency))
195 196 197 198 199
		return false;

	return true;
}

200 201 202 203 204 205 206 207 208 209 210 211
static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f,
				    struct dma_fence_cb *cb)
{
	struct drm_sched_job *job = container_of(cb, struct drm_sched_job,
						 finish_cb);
	drm_sched_fence_finished(job->s_fence);
	WARN_ON(job->s_fence->parent);
	dma_fence_put(&job->s_fence->finished);
	job->sched->ops->free_job(job);
}


212 213 214 215 216 217
/**
 * Destroy a context entity
 *
 * @sched       Pointer to scheduler instance
 * @entity	The pointer to a valid scheduler entity
 *
218 219
 * Splitting drm_sched_entity_fini() into two functions, The first one is does the waiting,
 * removes the entity from the runqueue and returns an error when the process was killed.
220
 */
221
void drm_sched_entity_do_release(struct drm_gpu_scheduler *sched,
222
			   struct drm_sched_entity *entity)
223
{
224
	if (!drm_sched_entity_is_initialized(sched, entity))
225
		return;
226 227
	/**
	 * The client will not queue more IBs during this fini, consume existing
228
	 * queued IBs or discard them on SIGKILL
229
	*/
230
	if ((current->flags & PF_SIGNALED) && current->exit_code == SIGKILL)
231
		entity->fini_status = -ERESTARTSYS;
232
	else
233
		entity->fini_status = wait_event_killable(sched->job_scheduled,
234 235
					drm_sched_entity_is_idle(entity));
	drm_sched_entity_set_rq(entity, NULL);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
}
EXPORT_SYMBOL(drm_sched_entity_do_release);

/**
 * Destroy a context entity
 *
 * @sched       Pointer to scheduler instance
 * @entity	The pointer to a valid scheduler entity
 *
 * The second one then goes over the entity and signals all jobs with an error code.
 */
void drm_sched_entity_cleanup(struct drm_gpu_scheduler *sched,
			   struct drm_sched_entity *entity)
{
	if (entity->fini_status) {
251
		struct drm_sched_job *job;
252
		int r;
253 254 255 256 257 258

		/* Park the kernel for a moment to make sure it isn't processing
		 * our enity.
		 */
		kthread_park(sched->thread);
		kthread_unpark(sched->thread);
259 260 261 262 263 264 265
		if (entity->dependency) {
			dma_fence_remove_callback(entity->dependency,
						  &entity->cb);
			dma_fence_put(entity->dependency);
			entity->dependency = NULL;
		}

266 267 268
		while ((job = to_drm_sched_job(spsc_queue_pop(&entity->job_queue)))) {
			struct drm_sched_fence *s_fence = job->s_fence;
			drm_sched_fence_scheduled(s_fence);
269
			dma_fence_set_error(&s_fence->finished, -ESRCH);
270 271 272 273 274 275
			r = dma_fence_add_callback(entity->last_scheduled, &job->finish_cb,
							drm_sched_entity_kill_jobs_cb);
			if (r == -ENOENT)
				drm_sched_entity_kill_jobs_cb(NULL, &job->finish_cb);
			else if (r)
				DRM_ERROR("fence add callback failed (%d)\n", r);
276
		}
277
	}
278 279 280

	dma_fence_put(entity->last_scheduled);
	entity->last_scheduled = NULL;
281
}
282 283 284 285 286 287 288 289
EXPORT_SYMBOL(drm_sched_entity_cleanup);

void drm_sched_entity_fini(struct drm_gpu_scheduler *sched,
				struct drm_sched_entity *entity)
{
	drm_sched_entity_do_release(sched, entity);
	drm_sched_entity_cleanup(sched, entity);
}
290
EXPORT_SYMBOL(drm_sched_entity_fini);
291

292
static void drm_sched_entity_wakeup(struct dma_fence *f, struct dma_fence_cb *cb)
293
{
294 295
	struct drm_sched_entity *entity =
		container_of(cb, struct drm_sched_entity, cb);
296
	entity->dependency = NULL;
297
	dma_fence_put(f);
298
	drm_sched_wakeup(entity->sched);
299 300
}

301
static void drm_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb *cb)
302
{
303 304
	struct drm_sched_entity *entity =
		container_of(cb, struct drm_sched_entity, cb);
305
	entity->dependency = NULL;
306
	dma_fence_put(f);
307 308
}

309 310
void drm_sched_entity_set_rq(struct drm_sched_entity *entity,
			     struct drm_sched_rq *rq)
311 312 313 314 315 316 317
{
	if (entity->rq == rq)
		return;

	spin_lock(&entity->rq_lock);

	if (entity->rq)
318
		drm_sched_rq_remove_entity(entity->rq, entity);
319 320 321

	entity->rq = rq;
	if (rq)
322
		drm_sched_rq_add_entity(rq, entity);
323 324 325

	spin_unlock(&entity->rq_lock);
}
326
EXPORT_SYMBOL(drm_sched_entity_set_rq);
327

328 329
bool drm_sched_dependency_optimized(struct dma_fence* fence,
				    struct drm_sched_entity *entity)
C
Chunming Zhou 已提交
330
{
331 332
	struct drm_gpu_scheduler *sched = entity->sched;
	struct drm_sched_fence *s_fence;
C
Chunming Zhou 已提交
333 334 335 336 337

	if (!fence || dma_fence_is_signaled(fence))
		return false;
	if (fence->context == entity->fence_context)
		return true;
338
	s_fence = to_drm_sched_fence(fence);
C
Chunming Zhou 已提交
339 340 341 342 343
	if (s_fence && s_fence->sched == sched)
		return true;

	return false;
}
344
EXPORT_SYMBOL(drm_sched_dependency_optimized);
C
Chunming Zhou 已提交
345

346
static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity)
347
{
348
	struct drm_gpu_scheduler *sched = entity->sched;
349
	struct dma_fence * fence = entity->dependency;
350
	struct drm_sched_fence *s_fence;
351

352 353 354 355 356 357 358
	if (fence->context == entity->fence_context ||
            fence->context == entity->fence_context + 1) {
                /*
                 * Fence is a scheduled/finished fence from a job
                 * which belongs to the same entity, we can ignore
                 * fences from ourself
                 */
359
		dma_fence_put(entity->dependency);
360 361 362
		return false;
	}

363
	s_fence = to_drm_sched_fence(fence);
364 365
	if (s_fence && s_fence->sched == sched) {

366 367 368 369
		/*
		 * Fence is from the same scheduler, only need to wait for
		 * it to be scheduled
		 */
370 371
		fence = dma_fence_get(&s_fence->scheduled);
		dma_fence_put(entity->dependency);
372
		entity->dependency = fence;
373
		if (!dma_fence_add_callback(fence, &entity->cb,
374
					    drm_sched_entity_clear_dep))
375 376 377
			return true;

		/* Ignore it when it is already scheduled */
378
		dma_fence_put(fence);
379
		return false;
380 381
	}

382
	if (!dma_fence_add_callback(entity->dependency, &entity->cb,
383
				    drm_sched_entity_wakeup))
384 385
		return true;

386
	dma_fence_put(entity->dependency);
387 388 389
	return false;
}

390 391
static struct drm_sched_job *
drm_sched_entity_pop_job(struct drm_sched_entity *entity)
392
{
393 394
	struct drm_gpu_scheduler *sched = entity->sched;
	struct drm_sched_job *sched_job = to_drm_sched_job(
395
						spsc_queue_peek(&entity->job_queue));
396

397
	if (!sched_job)
398 399
		return NULL;

400
	while ((entity->dependency = sched->ops->dependency(sched_job, entity)))
401
		if (drm_sched_entity_add_dependency_cb(entity))
402 403
			return NULL;

404 405 406 407
	/* skip jobs from entity that marked guilty */
	if (entity->guilty && atomic_read(entity->guilty))
		dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED);

408 409 410
	dma_fence_put(entity->last_scheduled);
	entity->last_scheduled = dma_fence_get(&sched_job->s_fence->finished);

411
	spsc_queue_pop(&entity->job_queue);
412
	return sched_job;
413 414
}

415
/**
416
 * Submit a job to the job queue
417
 *
418
 * @sched_job		The pointer to job required to submit
419
 *
420 421 422 423
 * Note: To guarantee that the order of insertion to queue matches
 * the job's fence sequence number this function should be
 * called with drm_sched_job_init under common lock.
 *
424
 * Returns 0 for success, negative error code otherwise.
425
 */
426 427
void drm_sched_entity_push_job(struct drm_sched_job *sched_job,
			       struct drm_sched_entity *entity)
428
{
429
	struct drm_gpu_scheduler *sched = sched_job->sched;
430
	bool first = false;
431

432
	trace_drm_sched_job(sched_job, entity);
433

434
	first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
435 436

	/* first job wakes up scheduler */
437 438
	if (first) {
		/* Add the entity to the run queue */
439
		spin_lock(&entity->rq_lock);
440
		drm_sched_rq_add_entity(entity->rq, entity);
441
		spin_unlock(&entity->rq_lock);
442
		drm_sched_wakeup(sched);
443
	}
444
}
445
EXPORT_SYMBOL(drm_sched_entity_push_job);
446

447
/* job_finish is called after hw fence signaled
448
 */
449
static void drm_sched_job_finish(struct work_struct *work)
450
{
451
	struct drm_sched_job *s_job = container_of(work, struct drm_sched_job,
452
						   finish_work);
453
	struct drm_gpu_scheduler *sched = s_job->sched;
454

455
	/* remove job from ring_mirror_list */
456
	spin_lock(&sched->job_list_lock);
457
	list_del_init(&s_job->node);
458
	if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
459
		struct drm_sched_job *next;
460

461
		spin_unlock(&sched->job_list_lock);
462
		cancel_delayed_work_sync(&s_job->work_tdr);
463
		spin_lock(&sched->job_list_lock);
464 465 466

		/* queue TDR for next job */
		next = list_first_entry_or_null(&sched->ring_mirror_list,
467
						struct drm_sched_job, node);
468

469
		if (next)
470 471
			schedule_delayed_work(&next->work_tdr, sched->timeout);
	}
472
	spin_unlock(&sched->job_list_lock);
473
	dma_fence_put(&s_job->s_fence->finished);
474 475 476
	sched->ops->free_job(s_job);
}

477
static void drm_sched_job_finish_cb(struct dma_fence *f,
478
				    struct dma_fence_cb *cb)
479
{
480
	struct drm_sched_job *job = container_of(cb, struct drm_sched_job,
481 482
						 finish_cb);
	schedule_work(&job->finish_work);
483 484
}

485
static void drm_sched_job_begin(struct drm_sched_job *s_job)
486
{
487
	struct drm_gpu_scheduler *sched = s_job->sched;
488

489
	dma_fence_add_callback(&s_job->s_fence->finished, &s_job->finish_cb,
490
			       drm_sched_job_finish_cb);
491

492
	spin_lock(&sched->job_list_lock);
493
	list_add_tail(&s_job->node, &sched->ring_mirror_list);
494
	if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
495
	    list_first_entry_or_null(&sched->ring_mirror_list,
496
				     struct drm_sched_job, node) == s_job)
497
		schedule_delayed_work(&s_job->work_tdr, sched->timeout);
498
	spin_unlock(&sched->job_list_lock);
499 500
}

501
static void drm_sched_job_timedout(struct work_struct *work)
502
{
503
	struct drm_sched_job *job = container_of(work, struct drm_sched_job,
504 505 506 507 508
						 work_tdr.work);

	job->sched->ops->timedout_job(job);
}

509
void drm_sched_hw_job_reset(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
510
{
511 512
	struct drm_sched_job *s_job;
	struct drm_sched_entity *entity, *tmp;
513
	int i;
514 515 516

	spin_lock(&sched->job_list_lock);
	list_for_each_entry_reverse(s_job, &sched->ring_mirror_list, node) {
517 518 519
		if (s_job->s_fence->parent &&
		    dma_fence_remove_callback(s_job->s_fence->parent,
					      &s_job->s_fence->cb)) {
520
			dma_fence_put(s_job->s_fence->parent);
521
			s_job->s_fence->parent = NULL;
522
			atomic_dec(&sched->hw_rq_count);
523 524
		}
	}
525
	spin_unlock(&sched->job_list_lock);
526

527
	if (bad && bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) {
528
		atomic_inc(&bad->karma);
529 530 531 532
		/* don't increase @bad's karma if it's from KERNEL RQ,
		 * becuase sometimes GPU hang would cause kernel jobs (like VM updating jobs)
		 * corrupt but keep in mind that kernel jobs always considered good.
		 */
533 534
		for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_KERNEL; i++ ) {
			struct drm_sched_rq *rq = &sched->sched_rq[i];
535 536 537 538

			spin_lock(&rq->lock);
			list_for_each_entry_safe(entity, tmp, &rq->entities, list) {
				if (bad->s_fence->scheduled.context == entity->fence_context) {
539
				    if (atomic_read(&bad->karma) > bad->sched->hang_limit)
540 541
						if (entity->guilty)
							atomic_set(entity->guilty, 1);
542 543 544 545
					break;
				}
			}
			spin_unlock(&rq->lock);
546
			if (&entity->list != &rq->entities)
547 548 549
				break;
		}
	}
550
}
551
EXPORT_SYMBOL(drm_sched_hw_job_reset);
552

553
void drm_sched_job_recovery(struct drm_gpu_scheduler *sched)
554
{
555
	struct drm_sched_job *s_job, *tmp;
556
	bool found_guilty = false;
557 558 559 560
	int r;

	spin_lock(&sched->job_list_lock);
	s_job = list_first_entry_or_null(&sched->ring_mirror_list,
561
					 struct drm_sched_job, node);
562
	if (s_job && sched->timeout != MAX_SCHEDULE_TIMEOUT)
563 564
		schedule_delayed_work(&s_job->work_tdr, sched->timeout);

565
	list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
566
		struct drm_sched_fence *s_fence = s_job->s_fence;
567
		struct dma_fence *fence;
568 569 570 571 572 573 574 575 576
		uint64_t guilty_context;

		if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) {
			found_guilty = true;
			guilty_context = s_job->s_fence->scheduled.context;
		}

		if (found_guilty && s_job->s_fence->scheduled.context == guilty_context)
			dma_fence_set_error(&s_fence->finished, -ECANCELED);
577

578 579
		spin_unlock(&sched->job_list_lock);
		fence = sched->ops->run_job(s_job);
580
		atomic_inc(&sched->hw_rq_count);
581

582
		if (fence) {
583 584
			s_fence->parent = dma_fence_get(fence);
			r = dma_fence_add_callback(fence, &s_fence->cb,
585
						   drm_sched_process_job);
586
			if (r == -ENOENT)
587
				drm_sched_process_job(fence, &s_fence->cb);
588 589 590
			else if (r)
				DRM_ERROR("fence add callback failed (%d)\n",
					  r);
591
			dma_fence_put(fence);
592
		} else {
593
			drm_sched_process_job(NULL, &s_fence->cb);
594
		}
595
		spin_lock(&sched->job_list_lock);
596 597 598
	}
	spin_unlock(&sched->job_list_lock);
}
599
EXPORT_SYMBOL(drm_sched_job_recovery);
600

601 602 603 604 605 606
/**
 * Init a sched_job with basic field
 *
 * Note: Refer to drm_sched_entity_push_job documentation
 * for locking considerations.
 */
607 608 609
int drm_sched_job_init(struct drm_sched_job *job,
		       struct drm_gpu_scheduler *sched,
		       struct drm_sched_entity *entity,
610
		       void *owner)
611 612
{
	job->sched = sched;
613
	job->entity = entity;
614
	job->s_priority = entity->rq - sched->sched_rq;
615
	job->s_fence = drm_sched_fence_create(entity, owner);
616 617
	if (!job->s_fence)
		return -ENOMEM;
618
	job->id = atomic64_inc_return(&sched->job_id_count);
619

620
	INIT_WORK(&job->finish_work, drm_sched_job_finish);
621
	INIT_LIST_HEAD(&job->node);
622
	INIT_DELAYED_WORK(&job->work_tdr, drm_sched_job_timedout);
623

624 625
	return 0;
}
626
EXPORT_SYMBOL(drm_sched_job_init);
627

628 629 630
/**
 * Return ture if we can push more jobs to the hw.
 */
631
static bool drm_sched_ready(struct drm_gpu_scheduler *sched)
632 633 634 635 636
{
	return atomic_read(&sched->hw_rq_count) <
		sched->hw_submission_limit;
}

637 638 639
/**
 * Wake up the scheduler when it is ready
 */
640
static void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
641
{
642
	if (drm_sched_ready(sched))
643
		wake_up_interruptible(&sched->wake_up_worker);
644 645
}

646
/**
647
 * Select next entity to process
648
*/
649 650
static struct drm_sched_entity *
drm_sched_select_entity(struct drm_gpu_scheduler *sched)
651
{
652
	struct drm_sched_entity *entity;
653
	int i;
654

655
	if (!drm_sched_ready(sched))
656 657 658
		return NULL;

	/* Kernel run queue has higher priority than normal run queue*/
659 660
	for (i = DRM_SCHED_PRIORITY_MAX - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
		entity = drm_sched_rq_select_entity(&sched->sched_rq[i]);
661 662 663
		if (entity)
			break;
	}
664

665
	return entity;
666 667
}

668
static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
669
{
670 671 672
	struct drm_sched_fence *s_fence =
		container_of(cb, struct drm_sched_fence, cb);
	struct drm_gpu_scheduler *sched = s_fence->sched;
673

674
	dma_fence_get(&s_fence->finished);
675
	atomic_dec(&sched->hw_rq_count);
676
	drm_sched_fence_finished(s_fence);
M
Monk Liu 已提交
677

678
	trace_drm_sched_process_job(s_fence);
679
	dma_fence_put(&s_fence->finished);
680
	wake_up_interruptible(&sched->wake_up_worker);
681 682
}

683
static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
684 685 686 687 688 689 690 691 692
{
	if (kthread_should_park()) {
		kthread_parkme();
		return true;
	}

	return false;
}

693
static int drm_sched_main(void *param)
694 695
{
	struct sched_param sparam = {.sched_priority = 1};
696
	struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
697
	int r;
698 699 700 701

	sched_setscheduler(current, SCHED_FIFO, &sparam);

	while (!kthread_should_stop()) {
702 703 704
		struct drm_sched_entity *entity = NULL;
		struct drm_sched_fence *s_fence;
		struct drm_sched_job *sched_job;
705
		struct dma_fence *fence;
706

707
		wait_event_interruptible(sched->wake_up_worker,
708 709
					 (!drm_sched_blocked(sched) &&
					  (entity = drm_sched_select_entity(sched))) ||
710
					 kthread_should_stop());
711

712 713 714
		if (!entity)
			continue;

715
		sched_job = drm_sched_entity_pop_job(entity);
716
		if (!sched_job)
717 718
			continue;

719
		s_fence = sched_job->s_fence;
720

721
		atomic_inc(&sched->hw_rq_count);
722
		drm_sched_job_begin(sched_job);
723

724
		fence = sched->ops->run_job(sched_job);
725
		drm_sched_fence_scheduled(s_fence);
726

727
		if (fence) {
728 729
			s_fence->parent = dma_fence_get(fence);
			r = dma_fence_add_callback(fence, &s_fence->cb,
730
						   drm_sched_process_job);
731
			if (r == -ENOENT)
732
				drm_sched_process_job(fence, &s_fence->cb);
733
			else if (r)
734 735
				DRM_ERROR("fence add callback failed (%d)\n",
					  r);
736
			dma_fence_put(fence);
737
		} else {
738
			drm_sched_process_job(NULL, &s_fence->cb);
739
		}
740

741
		wake_up(&sched->job_scheduled);
742 743 744 745 746
	}
	return 0;
}

/**
747
 * Init a gpu scheduler instance
748
 *
749
 * @sched		The pointer to the scheduler
750 751
 * @ops			The backend operations for this scheduler.
 * @hw_submissions	Number of hw submissions to do.
752
 * @name		Name used for debugging
753
 *
754
 * Return 0 on success, otherwise error code.
755
*/
756 757
int drm_sched_init(struct drm_gpu_scheduler *sched,
		   const struct drm_sched_backend_ops *ops,
M
Monk Liu 已提交
758 759 760 761
		   unsigned hw_submission,
		   unsigned hang_limit,
		   long timeout,
		   const char *name)
762
{
763
	int i;
764
	sched->ops = ops;
765
	sched->hw_submission_limit = hw_submission;
766
	sched->name = name;
767
	sched->timeout = timeout;
M
Monk Liu 已提交
768
	sched->hang_limit = hang_limit;
769 770
	for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_MAX; i++)
		drm_sched_rq_init(&sched->sched_rq[i]);
771

772 773
	init_waitqueue_head(&sched->wake_up_worker);
	init_waitqueue_head(&sched->job_scheduled);
774 775
	INIT_LIST_HEAD(&sched->ring_mirror_list);
	spin_lock_init(&sched->job_list_lock);
776
	atomic_set(&sched->hw_rq_count, 0);
777
	atomic64_set(&sched->job_id_count, 0);
778

779
	/* Each scheduler will run on a seperate kernel thread */
780
	sched->thread = kthread_run(drm_sched_main, sched, sched->name);
781
	if (IS_ERR(sched->thread)) {
782 783
		DRM_ERROR("Failed to create scheduler for %s.\n", name);
		return PTR_ERR(sched->thread);
784 785
	}

786
	return 0;
787
}
788
EXPORT_SYMBOL(drm_sched_init);
789 790 791 792 793 794

/**
 * Destroy a gpu scheduler
 *
 * @sched	The pointer to the scheduler
 */
795
void drm_sched_fini(struct drm_gpu_scheduler *sched)
796
{
797 798
	if (sched->thread)
		kthread_stop(sched->thread);
799
}
800
EXPORT_SYMBOL(drm_sched_fini);