gpu_scheduler.c 21.2 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
 * @jobs	The max number of jobs in the job queue
121 122
 * @guilty      atomic_t set to 1 when a job on this queue
 *              is found to be guilty causing a timeout
123 124 125
 *
 * return 0 if succeed. negative error code on failure
*/
126 127 128
int drm_sched_entity_init(struct drm_gpu_scheduler *sched,
			  struct drm_sched_entity *entity,
			  struct drm_sched_rq *rq,
129
			  uint32_t jobs, atomic_t *guilty)
130 131 132 133
{
	if (!(sched && entity && rq))
		return -EINVAL;

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

142
	spin_lock_init(&entity->rq_lock);
143
	spin_lock_init(&entity->queue_lock);
144
	spsc_queue_init(&entity->job_queue);
145

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

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

/**
 * 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
*/
161 162
static bool drm_sched_entity_is_initialized(struct drm_gpu_scheduler *sched,
					    struct drm_sched_entity *entity)
163
{
164 165
	return entity->sched == sched &&
		entity->rq != NULL;
166 167
}

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

	return false;
}

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

196
	if (READ_ONCE(entity->dependency))
197 198 199 200 201
		return false;

	return true;
}

202 203 204 205 206 207 208 209 210 211 212 213
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);
}


214 215 216 217 218 219
/**
 * Destroy a context entity
 *
 * @sched       Pointer to scheduler instance
 * @entity	The pointer to a valid scheduler entity
 *
220 221
 * 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.
222
 */
223
void drm_sched_entity_do_release(struct drm_gpu_scheduler *sched,
224
			   struct drm_sched_entity *entity)
225
{
226
	if (!drm_sched_entity_is_initialized(sched, entity))
227
		return;
228 229
	/**
	 * The client will not queue more IBs during this fini, consume existing
230
	 * queued IBs or discard them on SIGKILL
231
	*/
232
	if ((current->flags & PF_SIGNALED) && current->exit_code == SIGKILL)
233
		entity->fini_status = -ERESTARTSYS;
234
	else
235
		entity->fini_status = wait_event_killable(sched->job_scheduled,
236 237
					drm_sched_entity_is_idle(entity));
	drm_sched_entity_set_rq(entity, NULL);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
}
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) {
253
		struct drm_sched_job *job;
254
		int r;
255 256 257 258 259 260

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

268 269 270
		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);
271
			dma_fence_set_error(&s_fence->finished, -ESRCH);
272 273 274 275 276 277
			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);
278
		}
279
	}
280 281 282

	dma_fence_put(entity->last_scheduled);
	entity->last_scheduled = NULL;
283
}
284 285 286 287 288 289 290 291
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);
}
292
EXPORT_SYMBOL(drm_sched_entity_fini);
293

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

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

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

	spin_lock(&entity->rq_lock);

	if (entity->rq)
320
		drm_sched_rq_remove_entity(entity->rq, entity);
321 322 323

	entity->rq = rq;
	if (rq)
324
		drm_sched_rq_add_entity(rq, entity);
325 326 327

	spin_unlock(&entity->rq_lock);
}
328
EXPORT_SYMBOL(drm_sched_entity_set_rq);
329

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

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

	return false;
}
346
EXPORT_SYMBOL(drm_sched_dependency_optimized);
C
Chunming Zhou 已提交
347

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

	if (fence->context == entity->fence_context) {
		/* We can ignore fences from ourself */
356
		dma_fence_put(entity->dependency);
357 358 359
		return false;
	}

360
	s_fence = to_drm_sched_fence(fence);
361 362
	if (s_fence && s_fence->sched == sched) {

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

		/* Ignore it when it is already scheduled */
375
		dma_fence_put(fence);
376
		return false;
377 378
	}

379
	if (!dma_fence_add_callback(entity->dependency, &entity->cb,
380
				    drm_sched_entity_wakeup))
381 382
		return true;

383
	dma_fence_put(entity->dependency);
384 385 386
	return false;
}

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

394
	if (!sched_job)
395 396
		return NULL;

397
	while ((entity->dependency = sched->ops->dependency(sched_job, entity)))
398
		if (drm_sched_entity_add_dependency_cb(entity))
399 400
			return NULL;

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

405 406 407
	dma_fence_put(entity->last_scheduled);
	entity->last_scheduled = dma_fence_get(&sched_job->s_fence->finished);

408
	spsc_queue_pop(&entity->job_queue);
409
	return sched_job;
410 411
}

412
/**
413
 * Submit a job to the job queue
414
 *
415
 * @sched_job		The pointer to job required to submit
416
 *
417
 * Returns 0 for success, negative error code otherwise.
418
 */
419 420
void drm_sched_entity_push_job(struct drm_sched_job *sched_job,
			       struct drm_sched_entity *entity)
421
{
422
	struct drm_gpu_scheduler *sched = sched_job->sched;
423
	bool first = false;
424

425
	trace_drm_sched_job(sched_job, entity);
426

427 428
	spin_lock(&entity->queue_lock);
	first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
429 430 431 432

	spin_unlock(&entity->queue_lock);

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

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

451
	/* remove job from ring_mirror_list */
452
	spin_lock(&sched->job_list_lock);
453
	list_del_init(&s_job->node);
454
	if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
455
		struct drm_sched_job *next;
456

457
		spin_unlock(&sched->job_list_lock);
458
		cancel_delayed_work_sync(&s_job->work_tdr);
459
		spin_lock(&sched->job_list_lock);
460 461 462

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

465
		if (next)
466 467
			schedule_delayed_work(&next->work_tdr, sched->timeout);
	}
468
	spin_unlock(&sched->job_list_lock);
469
	dma_fence_put(&s_job->s_fence->finished);
470 471 472
	sched->ops->free_job(s_job);
}

473
static void drm_sched_job_finish_cb(struct dma_fence *f,
474
				    struct dma_fence_cb *cb)
475
{
476
	struct drm_sched_job *job = container_of(cb, struct drm_sched_job,
477 478
						 finish_cb);
	schedule_work(&job->finish_work);
479 480
}

481
static void drm_sched_job_begin(struct drm_sched_job *s_job)
482
{
483
	struct drm_gpu_scheduler *sched = s_job->sched;
484

485
	dma_fence_add_callback(&s_job->s_fence->finished, &s_job->finish_cb,
486
			       drm_sched_job_finish_cb);
487

488
	spin_lock(&sched->job_list_lock);
489
	list_add_tail(&s_job->node, &sched->ring_mirror_list);
490
	if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
491
	    list_first_entry_or_null(&sched->ring_mirror_list,
492
				     struct drm_sched_job, node) == s_job)
493
		schedule_delayed_work(&s_job->work_tdr, sched->timeout);
494
	spin_unlock(&sched->job_list_lock);
495 496
}

497
static void drm_sched_job_timedout(struct work_struct *work)
498
{
499
	struct drm_sched_job *job = container_of(work, struct drm_sched_job,
500 501 502 503 504
						 work_tdr.work);

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

505
void drm_sched_hw_job_reset(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
506
{
507 508
	struct drm_sched_job *s_job;
	struct drm_sched_entity *entity, *tmp;
509
	int i;
510 511 512

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

523
	if (bad && bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) {
524
		atomic_inc(&bad->karma);
525 526 527 528
		/* 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.
		 */
529 530
		for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_KERNEL; i++ ) {
			struct drm_sched_rq *rq = &sched->sched_rq[i];
531 532 533 534

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

549
void drm_sched_job_recovery(struct drm_gpu_scheduler *sched)
550
{
551
	struct drm_sched_job *s_job, *tmp;
552
	bool found_guilty = false;
553 554 555 556
	int r;

	spin_lock(&sched->job_list_lock);
	s_job = list_first_entry_or_null(&sched->ring_mirror_list,
557
					 struct drm_sched_job, node);
558
	if (s_job && sched->timeout != MAX_SCHEDULE_TIMEOUT)
559 560
		schedule_delayed_work(&s_job->work_tdr, sched->timeout);

561
	list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
562
		struct drm_sched_fence *s_fence = s_job->s_fence;
563
		struct dma_fence *fence;
564 565 566 567 568 569 570 571 572
		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);
573

574 575
		spin_unlock(&sched->job_list_lock);
		fence = sched->ops->run_job(s_job);
576
		atomic_inc(&sched->hw_rq_count);
577

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

597
/* init a sched_job with basic field */
598 599 600
int drm_sched_job_init(struct drm_sched_job *job,
		       struct drm_gpu_scheduler *sched,
		       struct drm_sched_entity *entity,
601
		       void *owner)
602 603
{
	job->sched = sched;
604
	job->entity = entity;
605
	job->s_priority = entity->rq - sched->sched_rq;
606
	job->s_fence = drm_sched_fence_create(entity, owner);
607 608
	if (!job->s_fence)
		return -ENOMEM;
609
	job->id = atomic64_inc_return(&sched->job_id_count);
610

611
	INIT_WORK(&job->finish_work, drm_sched_job_finish);
612
	INIT_LIST_HEAD(&job->node);
613
	INIT_DELAYED_WORK(&job->work_tdr, drm_sched_job_timedout);
614

615 616
	return 0;
}
617
EXPORT_SYMBOL(drm_sched_job_init);
618

619 620 621
/**
 * Return ture if we can push more jobs to the hw.
 */
622
static bool drm_sched_ready(struct drm_gpu_scheduler *sched)
623 624 625 626 627
{
	return atomic_read(&sched->hw_rq_count) <
		sched->hw_submission_limit;
}

628 629 630
/**
 * Wake up the scheduler when it is ready
 */
631
static void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
632
{
633
	if (drm_sched_ready(sched))
634
		wake_up_interruptible(&sched->wake_up_worker);
635 636
}

637
/**
638
 * Select next entity to process
639
*/
640 641
static struct drm_sched_entity *
drm_sched_select_entity(struct drm_gpu_scheduler *sched)
642
{
643
	struct drm_sched_entity *entity;
644
	int i;
645

646
	if (!drm_sched_ready(sched))
647 648 649
		return NULL;

	/* Kernel run queue has higher priority than normal run queue*/
650 651
	for (i = DRM_SCHED_PRIORITY_MAX - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
		entity = drm_sched_rq_select_entity(&sched->sched_rq[i]);
652 653 654
		if (entity)
			break;
	}
655

656
	return entity;
657 658
}

659
static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
660
{
661 662 663
	struct drm_sched_fence *s_fence =
		container_of(cb, struct drm_sched_fence, cb);
	struct drm_gpu_scheduler *sched = s_fence->sched;
664

665
	dma_fence_get(&s_fence->finished);
666
	atomic_dec(&sched->hw_rq_count);
667
	drm_sched_fence_finished(s_fence);
M
Monk Liu 已提交
668

669
	trace_drm_sched_process_job(s_fence);
670
	dma_fence_put(&s_fence->finished);
671
	wake_up_interruptible(&sched->wake_up_worker);
672 673
}

674
static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
675 676 677 678 679 680 681 682 683
{
	if (kthread_should_park()) {
		kthread_parkme();
		return true;
	}

	return false;
}

684
static int drm_sched_main(void *param)
685 686
{
	struct sched_param sparam = {.sched_priority = 1};
687
	struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
688
	int r;
689 690 691 692

	sched_setscheduler(current, SCHED_FIFO, &sparam);

	while (!kthread_should_stop()) {
693 694 695
		struct drm_sched_entity *entity = NULL;
		struct drm_sched_fence *s_fence;
		struct drm_sched_job *sched_job;
696
		struct dma_fence *fence;
697

698
		wait_event_interruptible(sched->wake_up_worker,
699 700
					 (!drm_sched_blocked(sched) &&
					  (entity = drm_sched_select_entity(sched))) ||
701
					 kthread_should_stop());
702

703 704 705
		if (!entity)
			continue;

706
		sched_job = drm_sched_entity_pop_job(entity);
707
		if (!sched_job)
708 709
			continue;

710
		s_fence = sched_job->s_fence;
711

712
		atomic_inc(&sched->hw_rq_count);
713
		drm_sched_job_begin(sched_job);
714

715
		fence = sched->ops->run_job(sched_job);
716
		drm_sched_fence_scheduled(s_fence);
717

718
		if (fence) {
719 720
			s_fence->parent = dma_fence_get(fence);
			r = dma_fence_add_callback(fence, &s_fence->cb,
721
						   drm_sched_process_job);
722
			if (r == -ENOENT)
723
				drm_sched_process_job(fence, &s_fence->cb);
724
			else if (r)
725 726
				DRM_ERROR("fence add callback failed (%d)\n",
					  r);
727
			dma_fence_put(fence);
728
		} else {
729
			drm_sched_process_job(NULL, &s_fence->cb);
730
		}
731

732
		wake_up(&sched->job_scheduled);
733 734 735 736 737
	}
	return 0;
}

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

763 764
	init_waitqueue_head(&sched->wake_up_worker);
	init_waitqueue_head(&sched->job_scheduled);
765 766
	INIT_LIST_HEAD(&sched->ring_mirror_list);
	spin_lock_init(&sched->job_list_lock);
767
	atomic_set(&sched->hw_rq_count, 0);
768
	atomic64_set(&sched->job_id_count, 0);
769

770
	/* Each scheduler will run on a seperate kernel thread */
771
	sched->thread = kthread_run(drm_sched_main, sched, sched->name);
772
	if (IS_ERR(sched->thread)) {
773 774
		DRM_ERROR("Failed to create scheduler for %s.\n", name);
		return PTR_ERR(sched->thread);
775 776
	}

777
	return 0;
778
}
779
EXPORT_SYMBOL(drm_sched_init);
780 781 782 783 784 785

/**
 * Destroy a gpu scheduler
 *
 * @sched	The pointer to the scheduler
 */
786
void drm_sched_fini(struct drm_gpu_scheduler *sched)
787
{
788 789
	if (sched->thread)
		kthread_stop(sched->thread);
790
}
791
EXPORT_SYMBOL(drm_sched_fini);