gpu_scheduler.c 19.7 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
	spin_lock_init(&entity->rq_lock);
141
	spin_lock_init(&entity->queue_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
/**
 * Destroy a context entity
 *
 * @sched       Pointer to scheduler instance
 * @entity	The pointer to a valid scheduler entity
 *
206
 * Cleanup and free the allocated resources.
207
 */
208 209
void drm_sched_entity_fini(struct drm_gpu_scheduler *sched,
			   struct drm_sched_entity *entity)
210
{
211
	int r;
212

213
	if (!drm_sched_entity_is_initialized(sched, entity))
214
		return;
215 216
	/**
	 * The client will not queue more IBs during this fini, consume existing
217
	 * queued IBs or discard them on SIGKILL
218
	*/
219 220 221 222
	if ((current->flags & PF_SIGNALED) && current->exit_code == SIGKILL)
		r = -ERESTARTSYS;
	else
		r = wait_event_killable(sched->job_scheduled,
223 224
					drm_sched_entity_is_idle(entity));
	drm_sched_entity_set_rq(entity, NULL);
225
	if (r) {
226
		struct drm_sched_job *job;
227 228 229 230 231 232

		/* Park the kernel for a moment to make sure it isn't processing
		 * our enity.
		 */
		kthread_park(sched->thread);
		kthread_unpark(sched->thread);
233 234 235 236 237 238 239
		if (entity->dependency) {
			dma_fence_remove_callback(entity->dependency,
						  &entity->cb);
			dma_fence_put(entity->dependency);
			entity->dependency = NULL;
		}

240 241 242
		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);
243
			dma_fence_set_error(&s_fence->finished, -ESRCH);
244
			drm_sched_fence_finished(s_fence);
245
			WARN_ON(s_fence->parent);
246
			dma_fence_put(&s_fence->finished);
247
			sched->ops->free_job(job);
248
		}
249
	}
250
}
251
EXPORT_SYMBOL(drm_sched_entity_fini);
252

253
static void drm_sched_entity_wakeup(struct dma_fence *f, struct dma_fence_cb *cb)
254
{
255 256
	struct drm_sched_entity *entity =
		container_of(cb, struct drm_sched_entity, cb);
257
	entity->dependency = NULL;
258
	dma_fence_put(f);
259
	drm_sched_wakeup(entity->sched);
260 261
}

262
static void drm_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb *cb)
263
{
264 265
	struct drm_sched_entity *entity =
		container_of(cb, struct drm_sched_entity, cb);
266
	entity->dependency = NULL;
267
	dma_fence_put(f);
268 269
}

270 271
void drm_sched_entity_set_rq(struct drm_sched_entity *entity,
			     struct drm_sched_rq *rq)
272 273 274 275 276 277 278
{
	if (entity->rq == rq)
		return;

	spin_lock(&entity->rq_lock);

	if (entity->rq)
279
		drm_sched_rq_remove_entity(entity->rq, entity);
280 281 282

	entity->rq = rq;
	if (rq)
283
		drm_sched_rq_add_entity(rq, entity);
284 285 286

	spin_unlock(&entity->rq_lock);
}
287
EXPORT_SYMBOL(drm_sched_entity_set_rq);
288

289 290
bool drm_sched_dependency_optimized(struct dma_fence* fence,
				    struct drm_sched_entity *entity)
C
Chunming Zhou 已提交
291
{
292 293
	struct drm_gpu_scheduler *sched = entity->sched;
	struct drm_sched_fence *s_fence;
C
Chunming Zhou 已提交
294 295 296 297 298

	if (!fence || dma_fence_is_signaled(fence))
		return false;
	if (fence->context == entity->fence_context)
		return true;
299
	s_fence = to_drm_sched_fence(fence);
C
Chunming Zhou 已提交
300 301 302 303 304
	if (s_fence && s_fence->sched == sched)
		return true;

	return false;
}
305
EXPORT_SYMBOL(drm_sched_dependency_optimized);
C
Chunming Zhou 已提交
306

307
static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity)
308
{
309
	struct drm_gpu_scheduler *sched = entity->sched;
310
	struct dma_fence * fence = entity->dependency;
311
	struct drm_sched_fence *s_fence;
312 313 314

	if (fence->context == entity->fence_context) {
		/* We can ignore fences from ourself */
315
		dma_fence_put(entity->dependency);
316 317 318
		return false;
	}

319
	s_fence = to_drm_sched_fence(fence);
320 321
	if (s_fence && s_fence->sched == sched) {

322 323 324 325
		/*
		 * Fence is from the same scheduler, only need to wait for
		 * it to be scheduled
		 */
326 327
		fence = dma_fence_get(&s_fence->scheduled);
		dma_fence_put(entity->dependency);
328
		entity->dependency = fence;
329
		if (!dma_fence_add_callback(fence, &entity->cb,
330
					    drm_sched_entity_clear_dep))
331 332 333
			return true;

		/* Ignore it when it is already scheduled */
334
		dma_fence_put(fence);
335
		return false;
336 337
	}

338
	if (!dma_fence_add_callback(entity->dependency, &entity->cb,
339
				    drm_sched_entity_wakeup))
340 341
		return true;

342
	dma_fence_put(entity->dependency);
343 344 345
	return false;
}

346 347
static struct drm_sched_job *
drm_sched_entity_pop_job(struct drm_sched_entity *entity)
348
{
349 350
	struct drm_gpu_scheduler *sched = entity->sched;
	struct drm_sched_job *sched_job = to_drm_sched_job(
351
						spsc_queue_peek(&entity->job_queue));
352

353
	if (!sched_job)
354 355
		return NULL;

356
	while ((entity->dependency = sched->ops->dependency(sched_job, entity)))
357
		if (drm_sched_entity_add_dependency_cb(entity))
358 359
			return NULL;

360 361 362 363
	/* skip jobs from entity that marked guilty */
	if (entity->guilty && atomic_read(entity->guilty))
		dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED);

364
	spsc_queue_pop(&entity->job_queue);
365
	return sched_job;
366 367
}

368
/**
369
 * Submit a job to the job queue
370
 *
371
 * @sched_job		The pointer to job required to submit
372
 *
373
 * Returns 0 for success, negative error code otherwise.
374
 */
375 376
void drm_sched_entity_push_job(struct drm_sched_job *sched_job,
			       struct drm_sched_entity *entity)
377
{
378
	struct drm_gpu_scheduler *sched = sched_job->sched;
379
	bool first = false;
380

381
	trace_drm_sched_job(sched_job, entity);
382

383 384
	spin_lock(&entity->queue_lock);
	first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
385 386 387 388

	spin_unlock(&entity->queue_lock);

	/* first job wakes up scheduler */
389 390
	if (first) {
		/* Add the entity to the run queue */
391
		spin_lock(&entity->rq_lock);
392
		drm_sched_rq_add_entity(entity->rq, entity);
393
		spin_unlock(&entity->rq_lock);
394
		drm_sched_wakeup(sched);
395
	}
396
}
397
EXPORT_SYMBOL(drm_sched_entity_push_job);
398

399
/* job_finish is called after hw fence signaled
400
 */
401
static void drm_sched_job_finish(struct work_struct *work)
402
{
403
	struct drm_sched_job *s_job = container_of(work, struct drm_sched_job,
404
						   finish_work);
405
	struct drm_gpu_scheduler *sched = s_job->sched;
406

407
	/* remove job from ring_mirror_list */
408
	spin_lock(&sched->job_list_lock);
409
	list_del_init(&s_job->node);
410
	if (sched->timeout != MAX_SCHEDULE_TIMEOUT) {
411
		struct drm_sched_job *next;
412

413
		spin_unlock(&sched->job_list_lock);
414
		cancel_delayed_work_sync(&s_job->work_tdr);
415
		spin_lock(&sched->job_list_lock);
416 417 418

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

421
		if (next)
422 423
			schedule_delayed_work(&next->work_tdr, sched->timeout);
	}
424
	spin_unlock(&sched->job_list_lock);
425
	dma_fence_put(&s_job->s_fence->finished);
426 427 428
	sched->ops->free_job(s_job);
}

429
static void drm_sched_job_finish_cb(struct dma_fence *f,
430
				    struct dma_fence_cb *cb)
431
{
432
	struct drm_sched_job *job = container_of(cb, struct drm_sched_job,
433 434
						 finish_cb);
	schedule_work(&job->finish_work);
435 436
}

437
static void drm_sched_job_begin(struct drm_sched_job *s_job)
438
{
439
	struct drm_gpu_scheduler *sched = s_job->sched;
440

441
	dma_fence_add_callback(&s_job->s_fence->finished, &s_job->finish_cb,
442
			       drm_sched_job_finish_cb);
443

444
	spin_lock(&sched->job_list_lock);
445
	list_add_tail(&s_job->node, &sched->ring_mirror_list);
446
	if (sched->timeout != MAX_SCHEDULE_TIMEOUT &&
447
	    list_first_entry_or_null(&sched->ring_mirror_list,
448
				     struct drm_sched_job, node) == s_job)
449
		schedule_delayed_work(&s_job->work_tdr, sched->timeout);
450
	spin_unlock(&sched->job_list_lock);
451 452
}

453
static void drm_sched_job_timedout(struct work_struct *work)
454
{
455
	struct drm_sched_job *job = container_of(work, struct drm_sched_job,
456 457 458 459 460
						 work_tdr.work);

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

461
void drm_sched_hw_job_reset(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad)
462
{
463 464
	struct drm_sched_job *s_job;
	struct drm_sched_entity *entity, *tmp;
465
	int i;
466 467 468

	spin_lock(&sched->job_list_lock);
	list_for_each_entry_reverse(s_job, &sched->ring_mirror_list, node) {
469 470 471
		if (s_job->s_fence->parent &&
		    dma_fence_remove_callback(s_job->s_fence->parent,
					      &s_job->s_fence->cb)) {
472
			dma_fence_put(s_job->s_fence->parent);
473
			s_job->s_fence->parent = NULL;
474
			atomic_dec(&sched->hw_rq_count);
475 476
		}
	}
477
	spin_unlock(&sched->job_list_lock);
478

479
	if (bad && bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) {
480
		atomic_inc(&bad->karma);
481 482 483 484
		/* 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.
		 */
485 486
		for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_KERNEL; i++ ) {
			struct drm_sched_rq *rq = &sched->sched_rq[i];
487 488 489 490

			spin_lock(&rq->lock);
			list_for_each_entry_safe(entity, tmp, &rq->entities, list) {
				if (bad->s_fence->scheduled.context == entity->fence_context) {
491
				    if (atomic_read(&bad->karma) > bad->sched->hang_limit)
492 493
						if (entity->guilty)
							atomic_set(entity->guilty, 1);
494 495 496 497
					break;
				}
			}
			spin_unlock(&rq->lock);
498
			if (&entity->list != &rq->entities)
499 500 501
				break;
		}
	}
502
}
503
EXPORT_SYMBOL(drm_sched_hw_job_reset);
504

505
void drm_sched_job_recovery(struct drm_gpu_scheduler *sched)
506
{
507
	struct drm_sched_job *s_job, *tmp;
508
	bool found_guilty = false;
509 510 511 512
	int r;

	spin_lock(&sched->job_list_lock);
	s_job = list_first_entry_or_null(&sched->ring_mirror_list,
513
					 struct drm_sched_job, node);
514
	if (s_job && sched->timeout != MAX_SCHEDULE_TIMEOUT)
515 516
		schedule_delayed_work(&s_job->work_tdr, sched->timeout);

517
	list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) {
518
		struct drm_sched_fence *s_fence = s_job->s_fence;
519
		struct dma_fence *fence;
520 521 522 523 524 525 526 527 528
		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);
529

530 531
		spin_unlock(&sched->job_list_lock);
		fence = sched->ops->run_job(s_job);
532
		atomic_inc(&sched->hw_rq_count);
533
		if (fence) {
534 535
			s_fence->parent = dma_fence_get(fence);
			r = dma_fence_add_callback(fence, &s_fence->cb,
536
						   drm_sched_process_job);
537
			if (r == -ENOENT)
538
				drm_sched_process_job(fence, &s_fence->cb);
539 540 541
			else if (r)
				DRM_ERROR("fence add callback failed (%d)\n",
					  r);
542
			dma_fence_put(fence);
543
		} else {
544
			drm_sched_process_job(NULL, &s_fence->cb);
545
		}
546
		spin_lock(&sched->job_list_lock);
547 548 549
	}
	spin_unlock(&sched->job_list_lock);
}
550
EXPORT_SYMBOL(drm_sched_job_recovery);
551

552
/* init a sched_job with basic field */
553 554 555
int drm_sched_job_init(struct drm_sched_job *job,
		       struct drm_gpu_scheduler *sched,
		       struct drm_sched_entity *entity,
556
		       void *owner)
557 558
{
	job->sched = sched;
559
	job->s_priority = entity->rq - sched->sched_rq;
560
	job->s_fence = drm_sched_fence_create(entity, owner);
561 562
	if (!job->s_fence)
		return -ENOMEM;
563
	job->id = atomic64_inc_return(&sched->job_id_count);
564

565
	INIT_WORK(&job->finish_work, drm_sched_job_finish);
566
	INIT_LIST_HEAD(&job->node);
567
	INIT_DELAYED_WORK(&job->work_tdr, drm_sched_job_timedout);
568

569 570
	return 0;
}
571
EXPORT_SYMBOL(drm_sched_job_init);
572

573 574 575
/**
 * Return ture if we can push more jobs to the hw.
 */
576
static bool drm_sched_ready(struct drm_gpu_scheduler *sched)
577 578 579 580 581
{
	return atomic_read(&sched->hw_rq_count) <
		sched->hw_submission_limit;
}

582 583 584
/**
 * Wake up the scheduler when it is ready
 */
585
static void drm_sched_wakeup(struct drm_gpu_scheduler *sched)
586
{
587
	if (drm_sched_ready(sched))
588
		wake_up_interruptible(&sched->wake_up_worker);
589 590
}

591
/**
592
 * Select next entity to process
593
*/
594 595
static struct drm_sched_entity *
drm_sched_select_entity(struct drm_gpu_scheduler *sched)
596
{
597
	struct drm_sched_entity *entity;
598
	int i;
599

600
	if (!drm_sched_ready(sched))
601 602 603
		return NULL;

	/* Kernel run queue has higher priority than normal run queue*/
604 605
	for (i = DRM_SCHED_PRIORITY_MAX - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) {
		entity = drm_sched_rq_select_entity(&sched->sched_rq[i]);
606 607 608
		if (entity)
			break;
	}
609

610
	return entity;
611 612
}

613
static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
614
{
615 616 617
	struct drm_sched_fence *s_fence =
		container_of(cb, struct drm_sched_fence, cb);
	struct drm_gpu_scheduler *sched = s_fence->sched;
618

619
	dma_fence_get(&s_fence->finished);
620
	atomic_dec(&sched->hw_rq_count);
621
	drm_sched_fence_finished(s_fence);
M
Monk Liu 已提交
622

623
	trace_drm_sched_process_job(s_fence);
624
	dma_fence_put(&s_fence->finished);
625
	wake_up_interruptible(&sched->wake_up_worker);
626 627
}

628
static bool drm_sched_blocked(struct drm_gpu_scheduler *sched)
629 630 631 632 633 634 635 636 637
{
	if (kthread_should_park()) {
		kthread_parkme();
		return true;
	}

	return false;
}

638
static int drm_sched_main(void *param)
639 640
{
	struct sched_param sparam = {.sched_priority = 1};
641
	struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param;
642
	int r;
643 644 645 646

	sched_setscheduler(current, SCHED_FIFO, &sparam);

	while (!kthread_should_stop()) {
647 648 649
		struct drm_sched_entity *entity = NULL;
		struct drm_sched_fence *s_fence;
		struct drm_sched_job *sched_job;
650
		struct dma_fence *fence;
651

652
		wait_event_interruptible(sched->wake_up_worker,
653 654
					 (!drm_sched_blocked(sched) &&
					  (entity = drm_sched_select_entity(sched))) ||
655
					 kthread_should_stop());
656

657 658 659
		if (!entity)
			continue;

660
		sched_job = drm_sched_entity_pop_job(entity);
661
		if (!sched_job)
662 663
			continue;

664
		s_fence = sched_job->s_fence;
665

666
		atomic_inc(&sched->hw_rq_count);
667
		drm_sched_job_begin(sched_job);
668

669
		fence = sched->ops->run_job(sched_job);
670
		drm_sched_fence_scheduled(s_fence);
671

672
		if (fence) {
673 674
			s_fence->parent = dma_fence_get(fence);
			r = dma_fence_add_callback(fence, &s_fence->cb,
675
						   drm_sched_process_job);
676
			if (r == -ENOENT)
677
				drm_sched_process_job(fence, &s_fence->cb);
678
			else if (r)
679 680
				DRM_ERROR("fence add callback failed (%d)\n",
					  r);
681
			dma_fence_put(fence);
682
		} else {
683
			drm_sched_process_job(NULL, &s_fence->cb);
684
		}
685

686
		wake_up(&sched->job_scheduled);
687 688 689 690 691
	}
	return 0;
}

/**
692
 * Init a gpu scheduler instance
693
 *
694
 * @sched		The pointer to the scheduler
695 696
 * @ops			The backend operations for this scheduler.
 * @hw_submissions	Number of hw submissions to do.
697
 * @name		Name used for debugging
698
 *
699
 * Return 0 on success, otherwise error code.
700
*/
701 702
int drm_sched_init(struct drm_gpu_scheduler *sched,
		   const struct drm_sched_backend_ops *ops,
M
Monk Liu 已提交
703 704 705 706
		   unsigned hw_submission,
		   unsigned hang_limit,
		   long timeout,
		   const char *name)
707
{
708
	int i;
709
	sched->ops = ops;
710
	sched->hw_submission_limit = hw_submission;
711
	sched->name = name;
712
	sched->timeout = timeout;
M
Monk Liu 已提交
713
	sched->hang_limit = hang_limit;
714 715
	for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_MAX; i++)
		drm_sched_rq_init(&sched->sched_rq[i]);
716

717 718
	init_waitqueue_head(&sched->wake_up_worker);
	init_waitqueue_head(&sched->job_scheduled);
719 720
	INIT_LIST_HEAD(&sched->ring_mirror_list);
	spin_lock_init(&sched->job_list_lock);
721
	atomic_set(&sched->hw_rq_count, 0);
722
	atomic64_set(&sched->job_id_count, 0);
723

724
	/* Each scheduler will run on a seperate kernel thread */
725
	sched->thread = kthread_run(drm_sched_main, sched, sched->name);
726
	if (IS_ERR(sched->thread)) {
727 728
		DRM_ERROR("Failed to create scheduler for %s.\n", name);
		return PTR_ERR(sched->thread);
729 730
	}

731
	return 0;
732
}
733
EXPORT_SYMBOL(drm_sched_init);
734 735 736 737 738 739

/**
 * Destroy a gpu scheduler
 *
 * @sched	The pointer to the scheduler
 */
740
void drm_sched_fini(struct drm_gpu_scheduler *sched)
741
{
742 743
	if (sched->thread)
		kthread_stop(sched->thread);
744
}
745
EXPORT_SYMBOL(drm_sched_fini);