gpu_scheduler.c 19.6 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 <drm/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
 * @kernel	If this is an entity for the kernel
121
 * @jobs	The max number of jobs in the job queue
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
			  uint32_t jobs, 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
	spin_lock_init(&entity->rq_lock);
140
	spin_lock_init(&entity->queue_lock);
141
	spsc_queue_init(&entity->job_queue);
142

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

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

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

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

	return false;
}

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

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

	return true;
}

199 200 201 202 203 204
/**
 * Destroy a context entity
 *
 * @sched       Pointer to scheduler instance
 * @entity	The pointer to a valid scheduler entity
 *
205
 * Cleanup and free the allocated resources.
206
 */
207 208
void drm_sched_entity_fini(struct drm_gpu_scheduler *sched,
			   struct drm_sched_entity *entity)
209
{
210
	int r;
211

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

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

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

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

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

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

	spin_lock(&entity->rq_lock);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

352
	if (!sched_job)
353 354
		return NULL;

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

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

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

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

380
	trace_drm_sched_job(sched_job, entity);
381

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

	spin_unlock(&entity->queue_lock);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

609
	return entity;
610 611
}

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

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

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

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

	return false;
}

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

	sched_setscheduler(current, SCHED_FIFO, &sparam);

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

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

656 657 658
		if (!entity)
			continue;

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

663
		s_fence = sched_job->s_fence;
664

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

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

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

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

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

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

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

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

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