1 /* 2 * Copyright 2015 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 */ 23 24 /** 25 * DOC: Overview 26 * 27 * The GPU scheduler provides entities which allow userspace to push jobs 28 * into software queues which are then scheduled on a hardware run queue. 29 * The software queues have a priority among them. The scheduler selects the entities 30 * from the run queue using a FIFO. The scheduler provides dependency handling 31 * features among jobs. The driver is supposed to provide callback functions for 32 * backend operations to the scheduler like submitting a job to hardware run queue, 33 * returning the dependencies of a job etc. 34 * 35 * The organisation of the scheduler is the following: 36 * 37 * 1. Each hw run queue has one scheduler 38 * 2. Each scheduler has multiple run queues with different priorities 39 * (e.g., HIGH_HW,HIGH_SW, KERNEL, NORMAL) 40 * 3. Each scheduler run queue has a queue of entities to schedule 41 * 4. Entities themselves maintain a queue of jobs that will be scheduled on 42 * the hardware. 43 * 44 * The jobs in an entity are always scheduled in the order in which they were pushed. 45 * 46 * Note that once a job was taken from the entities queue and pushed to the 47 * hardware, i.e. the pending queue, the entity must not be referenced anymore 48 * through the jobs entity pointer. 49 */ 50 51 /** 52 * DOC: Flow Control 53 * 54 * The DRM GPU scheduler provides a flow control mechanism to regulate the rate 55 * in which the jobs fetched from scheduler entities are executed. 56 * 57 * In this context the &drm_gpu_scheduler keeps track of a driver specified 58 * credit limit representing the capacity of this scheduler and a credit count; 59 * every &drm_sched_job carries a driver specified number of credits. 60 * 61 * Once a job is executed (but not yet finished), the job's credits contribute 62 * to the scheduler's credit count until the job is finished. If by executing 63 * one more job the scheduler's credit count would exceed the scheduler's 64 * credit limit, the job won't be executed. Instead, the scheduler will wait 65 * until the credit count has decreased enough to not overflow its credit limit. 66 * This implies waiting for previously executed jobs. 67 */ 68 69 #include <linux/wait.h> 70 #include <linux/sched.h> 71 #include <linux/completion.h> 72 #include <linux/dma-resv.h> 73 #include <uapi/linux/sched/types.h> 74 75 #include <drm/drm_print.h> 76 #include <drm/drm_gem.h> 77 #include <drm/drm_syncobj.h> 78 #include <drm/gpu_scheduler.h> 79 #include <drm/spsc_queue.h> 80 81 #include "sched_internal.h" 82 83 #define CREATE_TRACE_POINTS 84 #include "gpu_scheduler_trace.h" 85 86 #ifdef CONFIG_LOCKDEP 87 static struct lockdep_map drm_sched_lockdep_map = { 88 .name = "drm_sched_lockdep_map" 89 }; 90 #endif 91 92 int drm_sched_policy = DRM_SCHED_POLICY_FIFO; 93 94 /** 95 * DOC: sched_policy (int) 96 * Used to override default entities scheduling policy in a run queue. 97 */ 98 MODULE_PARM_DESC(sched_policy, "Specify the scheduling policy for entities on a run-queue, " __stringify(DRM_SCHED_POLICY_RR) " = Round Robin, " __stringify(DRM_SCHED_POLICY_FIFO) " = FIFO (default)."); 99 module_param_named(sched_policy, drm_sched_policy, int, 0444); 100 101 static u32 drm_sched_available_credits(struct drm_gpu_scheduler *sched) 102 { 103 u32 credits; 104 105 WARN_ON(check_sub_overflow(sched->credit_limit, 106 atomic_read(&sched->credit_count), 107 &credits)); 108 109 return credits; 110 } 111 112 /** 113 * drm_sched_can_queue -- Can we queue more to the hardware? 114 * @sched: scheduler instance 115 * @entity: the scheduler entity 116 * 117 * Return true if we can push at least one more job from @entity, false 118 * otherwise. 119 */ 120 static bool drm_sched_can_queue(struct drm_gpu_scheduler *sched, 121 struct drm_sched_entity *entity) 122 { 123 struct drm_sched_job *s_job; 124 125 s_job = drm_sched_entity_queue_peek(entity); 126 if (!s_job) 127 return false; 128 129 /* If a job exceeds the credit limit, truncate it to the credit limit 130 * itself to guarantee forward progress. 131 */ 132 if (s_job->credits > sched->credit_limit) { 133 dev_WARN(sched->dev, 134 "Jobs may not exceed the credit limit, truncate.\n"); 135 s_job->credits = sched->credit_limit; 136 } 137 138 return drm_sched_available_credits(sched) >= s_job->credits; 139 } 140 141 static __always_inline bool drm_sched_entity_compare_before(struct rb_node *a, 142 const struct rb_node *b) 143 { 144 struct drm_sched_entity *ent_a = rb_entry((a), struct drm_sched_entity, rb_tree_node); 145 struct drm_sched_entity *ent_b = rb_entry((b), struct drm_sched_entity, rb_tree_node); 146 147 return ktime_before(ent_a->oldest_job_waiting, ent_b->oldest_job_waiting); 148 } 149 150 static void drm_sched_rq_remove_fifo_locked(struct drm_sched_entity *entity, 151 struct drm_sched_rq *rq) 152 { 153 if (!RB_EMPTY_NODE(&entity->rb_tree_node)) { 154 rb_erase_cached(&entity->rb_tree_node, &rq->rb_tree_root); 155 RB_CLEAR_NODE(&entity->rb_tree_node); 156 } 157 } 158 159 void drm_sched_rq_update_fifo_locked(struct drm_sched_entity *entity, 160 struct drm_sched_rq *rq, 161 ktime_t ts) 162 { 163 /* 164 * Both locks need to be grabbed, one to protect from entity->rq change 165 * for entity from within concurrent drm_sched_entity_select_rq and the 166 * other to update the rb tree structure. 167 */ 168 lockdep_assert_held(&entity->lock); 169 lockdep_assert_held(&rq->lock); 170 171 drm_sched_rq_remove_fifo_locked(entity, rq); 172 173 entity->oldest_job_waiting = ts; 174 175 rb_add_cached(&entity->rb_tree_node, &rq->rb_tree_root, 176 drm_sched_entity_compare_before); 177 } 178 179 /** 180 * drm_sched_rq_init - initialize a given run queue struct 181 * 182 * @sched: scheduler instance to associate with this run queue 183 * @rq: scheduler run queue 184 * 185 * Initializes a scheduler runqueue. 186 */ 187 static void drm_sched_rq_init(struct drm_gpu_scheduler *sched, 188 struct drm_sched_rq *rq) 189 { 190 spin_lock_init(&rq->lock); 191 INIT_LIST_HEAD(&rq->entities); 192 rq->rb_tree_root = RB_ROOT_CACHED; 193 rq->current_entity = NULL; 194 rq->sched = sched; 195 } 196 197 /** 198 * drm_sched_rq_add_entity - add an entity 199 * 200 * @rq: scheduler run queue 201 * @entity: scheduler entity 202 * 203 * Adds a scheduler entity to the run queue. 204 */ 205 void drm_sched_rq_add_entity(struct drm_sched_rq *rq, 206 struct drm_sched_entity *entity) 207 { 208 lockdep_assert_held(&entity->lock); 209 lockdep_assert_held(&rq->lock); 210 211 if (!list_empty(&entity->list)) 212 return; 213 214 atomic_inc(rq->sched->score); 215 list_add_tail(&entity->list, &rq->entities); 216 } 217 218 /** 219 * drm_sched_rq_remove_entity - remove an entity 220 * 221 * @rq: scheduler run queue 222 * @entity: scheduler entity 223 * 224 * Removes a scheduler entity from the run queue. 225 */ 226 void drm_sched_rq_remove_entity(struct drm_sched_rq *rq, 227 struct drm_sched_entity *entity) 228 { 229 lockdep_assert_held(&entity->lock); 230 231 if (list_empty(&entity->list)) 232 return; 233 234 spin_lock(&rq->lock); 235 236 atomic_dec(rq->sched->score); 237 list_del_init(&entity->list); 238 239 if (rq->current_entity == entity) 240 rq->current_entity = NULL; 241 242 if (drm_sched_policy == DRM_SCHED_POLICY_FIFO) 243 drm_sched_rq_remove_fifo_locked(entity, rq); 244 245 spin_unlock(&rq->lock); 246 } 247 248 /** 249 * drm_sched_rq_select_entity_rr - Select an entity which could provide a job to run 250 * 251 * @sched: the gpu scheduler 252 * @rq: scheduler run queue to check. 253 * 254 * Try to find the next ready entity. 255 * 256 * Return an entity if one is found; return an error-pointer (!NULL) if an 257 * entity was ready, but the scheduler had insufficient credits to accommodate 258 * its job; return NULL, if no ready entity was found. 259 */ 260 static struct drm_sched_entity * 261 drm_sched_rq_select_entity_rr(struct drm_gpu_scheduler *sched, 262 struct drm_sched_rq *rq) 263 { 264 struct drm_sched_entity *entity; 265 266 spin_lock(&rq->lock); 267 268 entity = rq->current_entity; 269 if (entity) { 270 list_for_each_entry_continue(entity, &rq->entities, list) { 271 if (drm_sched_entity_is_ready(entity)) { 272 /* If we can't queue yet, preserve the current 273 * entity in terms of fairness. 274 */ 275 if (!drm_sched_can_queue(sched, entity)) { 276 spin_unlock(&rq->lock); 277 return ERR_PTR(-ENOSPC); 278 } 279 280 rq->current_entity = entity; 281 reinit_completion(&entity->entity_idle); 282 spin_unlock(&rq->lock); 283 return entity; 284 } 285 } 286 } 287 288 list_for_each_entry(entity, &rq->entities, list) { 289 if (drm_sched_entity_is_ready(entity)) { 290 /* If we can't queue yet, preserve the current entity in 291 * terms of fairness. 292 */ 293 if (!drm_sched_can_queue(sched, entity)) { 294 spin_unlock(&rq->lock); 295 return ERR_PTR(-ENOSPC); 296 } 297 298 rq->current_entity = entity; 299 reinit_completion(&entity->entity_idle); 300 spin_unlock(&rq->lock); 301 return entity; 302 } 303 304 if (entity == rq->current_entity) 305 break; 306 } 307 308 spin_unlock(&rq->lock); 309 310 return NULL; 311 } 312 313 /** 314 * drm_sched_rq_select_entity_fifo - Select an entity which provides a job to run 315 * 316 * @sched: the gpu scheduler 317 * @rq: scheduler run queue to check. 318 * 319 * Find oldest waiting ready entity. 320 * 321 * Return an entity if one is found; return an error-pointer (!NULL) if an 322 * entity was ready, but the scheduler had insufficient credits to accommodate 323 * its job; return NULL, if no ready entity was found. 324 */ 325 static struct drm_sched_entity * 326 drm_sched_rq_select_entity_fifo(struct drm_gpu_scheduler *sched, 327 struct drm_sched_rq *rq) 328 { 329 struct rb_node *rb; 330 331 spin_lock(&rq->lock); 332 for (rb = rb_first_cached(&rq->rb_tree_root); rb; rb = rb_next(rb)) { 333 struct drm_sched_entity *entity; 334 335 entity = rb_entry(rb, struct drm_sched_entity, rb_tree_node); 336 if (drm_sched_entity_is_ready(entity)) { 337 /* If we can't queue yet, preserve the current entity in 338 * terms of fairness. 339 */ 340 if (!drm_sched_can_queue(sched, entity)) { 341 spin_unlock(&rq->lock); 342 return ERR_PTR(-ENOSPC); 343 } 344 345 reinit_completion(&entity->entity_idle); 346 break; 347 } 348 } 349 spin_unlock(&rq->lock); 350 351 return rb ? rb_entry(rb, struct drm_sched_entity, rb_tree_node) : NULL; 352 } 353 354 /** 355 * drm_sched_run_job_queue - enqueue run-job work 356 * @sched: scheduler instance 357 */ 358 static void drm_sched_run_job_queue(struct drm_gpu_scheduler *sched) 359 { 360 if (!READ_ONCE(sched->pause_submit)) 361 queue_work(sched->submit_wq, &sched->work_run_job); 362 } 363 364 /** 365 * __drm_sched_run_free_queue - enqueue free-job work 366 * @sched: scheduler instance 367 */ 368 static void __drm_sched_run_free_queue(struct drm_gpu_scheduler *sched) 369 { 370 if (!READ_ONCE(sched->pause_submit)) 371 queue_work(sched->submit_wq, &sched->work_free_job); 372 } 373 374 /** 375 * drm_sched_run_free_queue - enqueue free-job work if ready 376 * @sched: scheduler instance 377 */ 378 static void drm_sched_run_free_queue(struct drm_gpu_scheduler *sched) 379 { 380 struct drm_sched_job *job; 381 382 spin_lock(&sched->job_list_lock); 383 job = list_first_entry_or_null(&sched->pending_list, 384 struct drm_sched_job, list); 385 if (job && dma_fence_is_signaled(&job->s_fence->finished)) 386 __drm_sched_run_free_queue(sched); 387 spin_unlock(&sched->job_list_lock); 388 } 389 390 /** 391 * drm_sched_job_done - complete a job 392 * @s_job: pointer to the job which is done 393 * 394 * Finish the job's fence and resubmit the work items. 395 */ 396 static void drm_sched_job_done(struct drm_sched_job *s_job, int result) 397 { 398 struct drm_sched_fence *s_fence = s_job->s_fence; 399 struct drm_gpu_scheduler *sched = s_fence->sched; 400 401 atomic_sub(s_job->credits, &sched->credit_count); 402 atomic_dec(sched->score); 403 404 trace_drm_sched_job_done(s_fence); 405 406 dma_fence_get(&s_fence->finished); 407 drm_sched_fence_finished(s_fence, result); 408 dma_fence_put(&s_fence->finished); 409 __drm_sched_run_free_queue(sched); 410 } 411 412 /** 413 * drm_sched_job_done_cb - the callback for a done job 414 * @f: fence 415 * @cb: fence callbacks 416 */ 417 static void drm_sched_job_done_cb(struct dma_fence *f, struct dma_fence_cb *cb) 418 { 419 struct drm_sched_job *s_job = container_of(cb, struct drm_sched_job, cb); 420 421 drm_sched_job_done(s_job, f->error); 422 } 423 424 /** 425 * drm_sched_start_timeout - start timeout for reset worker 426 * 427 * @sched: scheduler instance to start the worker for 428 * 429 * Start the timeout for the given scheduler. 430 */ 431 static void drm_sched_start_timeout(struct drm_gpu_scheduler *sched) 432 { 433 lockdep_assert_held(&sched->job_list_lock); 434 435 if (sched->timeout != MAX_SCHEDULE_TIMEOUT && 436 !list_empty(&sched->pending_list)) 437 mod_delayed_work(sched->timeout_wq, &sched->work_tdr, sched->timeout); 438 } 439 440 static void drm_sched_start_timeout_unlocked(struct drm_gpu_scheduler *sched) 441 { 442 spin_lock(&sched->job_list_lock); 443 drm_sched_start_timeout(sched); 444 spin_unlock(&sched->job_list_lock); 445 } 446 447 /** 448 * drm_sched_tdr_queue_imm: - immediately start job timeout handler 449 * 450 * @sched: scheduler for which the timeout handling should be started. 451 * 452 * Start timeout handling immediately for the named scheduler. 453 */ 454 void drm_sched_tdr_queue_imm(struct drm_gpu_scheduler *sched) 455 { 456 spin_lock(&sched->job_list_lock); 457 sched->timeout = 0; 458 drm_sched_start_timeout(sched); 459 spin_unlock(&sched->job_list_lock); 460 } 461 EXPORT_SYMBOL(drm_sched_tdr_queue_imm); 462 463 /** 464 * drm_sched_fault - immediately start timeout handler 465 * 466 * @sched: scheduler where the timeout handling should be started. 467 * 468 * Start timeout handling immediately when the driver detects a hardware fault. 469 */ 470 void drm_sched_fault(struct drm_gpu_scheduler *sched) 471 { 472 if (sched->timeout_wq) 473 mod_delayed_work(sched->timeout_wq, &sched->work_tdr, 0); 474 } 475 EXPORT_SYMBOL(drm_sched_fault); 476 477 /** 478 * drm_sched_suspend_timeout - Suspend scheduler job timeout 479 * 480 * @sched: scheduler instance for which to suspend the timeout 481 * 482 * Suspend the delayed work timeout for the scheduler. This is done by 483 * modifying the delayed work timeout to an arbitrary large value, 484 * MAX_SCHEDULE_TIMEOUT in this case. 485 * 486 * Returns the timeout remaining 487 * 488 */ 489 unsigned long drm_sched_suspend_timeout(struct drm_gpu_scheduler *sched) 490 { 491 unsigned long sched_timeout, now = jiffies; 492 493 sched_timeout = sched->work_tdr.timer.expires; 494 495 /* 496 * Modify the timeout to an arbitrarily large value. This also prevents 497 * the timeout to be restarted when new submissions arrive 498 */ 499 if (mod_delayed_work(sched->timeout_wq, &sched->work_tdr, MAX_SCHEDULE_TIMEOUT) 500 && time_after(sched_timeout, now)) 501 return sched_timeout - now; 502 else 503 return sched->timeout; 504 } 505 EXPORT_SYMBOL(drm_sched_suspend_timeout); 506 507 /** 508 * drm_sched_resume_timeout - Resume scheduler job timeout 509 * 510 * @sched: scheduler instance for which to resume the timeout 511 * @remaining: remaining timeout 512 * 513 * Resume the delayed work timeout for the scheduler. 514 */ 515 void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched, 516 unsigned long remaining) 517 { 518 spin_lock(&sched->job_list_lock); 519 520 if (list_empty(&sched->pending_list)) 521 cancel_delayed_work(&sched->work_tdr); 522 else 523 mod_delayed_work(sched->timeout_wq, &sched->work_tdr, remaining); 524 525 spin_unlock(&sched->job_list_lock); 526 } 527 EXPORT_SYMBOL(drm_sched_resume_timeout); 528 529 static void drm_sched_job_begin(struct drm_sched_job *s_job) 530 { 531 struct drm_gpu_scheduler *sched = s_job->sched; 532 533 spin_lock(&sched->job_list_lock); 534 list_add_tail(&s_job->list, &sched->pending_list); 535 drm_sched_start_timeout(sched); 536 spin_unlock(&sched->job_list_lock); 537 } 538 539 static void drm_sched_job_timedout(struct work_struct *work) 540 { 541 struct drm_gpu_scheduler *sched; 542 struct drm_sched_job *job; 543 enum drm_gpu_sched_stat status = DRM_GPU_SCHED_STAT_NOMINAL; 544 545 sched = container_of(work, struct drm_gpu_scheduler, work_tdr.work); 546 547 /* Protects against concurrent deletion in drm_sched_get_finished_job */ 548 spin_lock(&sched->job_list_lock); 549 job = list_first_entry_or_null(&sched->pending_list, 550 struct drm_sched_job, list); 551 552 if (job) { 553 /* 554 * Remove the bad job so it cannot be freed by a concurrent 555 * &struct drm_sched_backend_ops.free_job. It will be 556 * reinserted after the scheduler's work items have been 557 * cancelled, at which point it's safe. 558 */ 559 list_del_init(&job->list); 560 spin_unlock(&sched->job_list_lock); 561 562 status = job->sched->ops->timedout_job(job); 563 564 /* 565 * Guilty job did complete and hence needs to be manually removed 566 * See drm_sched_stop doc. 567 */ 568 if (sched->free_guilty) { 569 job->sched->ops->free_job(job); 570 sched->free_guilty = false; 571 } 572 } else { 573 spin_unlock(&sched->job_list_lock); 574 } 575 576 if (status != DRM_GPU_SCHED_STAT_ENODEV) 577 drm_sched_start_timeout_unlocked(sched); 578 } 579 580 /** 581 * drm_sched_stop - stop the scheduler 582 * 583 * @sched: scheduler instance 584 * @bad: job which caused the time out 585 * 586 * Stop the scheduler and also removes and frees all completed jobs. 587 * Note: bad job will not be freed as it might be used later and so it's 588 * callers responsibility to release it manually if it's not part of the 589 * pending list any more. 590 * 591 * This function is typically used for reset recovery (see the docu of 592 * drm_sched_backend_ops.timedout_job() for details). Do not call it for 593 * scheduler teardown, i.e., before calling drm_sched_fini(). 594 */ 595 void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad) 596 { 597 struct drm_sched_job *s_job, *tmp; 598 599 drm_sched_wqueue_stop(sched); 600 601 /* 602 * Reinsert back the bad job here - now it's safe as 603 * drm_sched_get_finished_job() cannot race against us and release the 604 * bad job at this point - we parked (waited for) any in progress 605 * (earlier) cleanups and drm_sched_get_finished_job() will not be 606 * called now until the scheduler's work items are submitted again. 607 */ 608 if (bad && bad->sched == sched) 609 /* 610 * Add at the head of the queue to reflect it was the earliest 611 * job extracted. 612 */ 613 list_add(&bad->list, &sched->pending_list); 614 615 /* 616 * Iterate the job list from later to earlier one and either deactive 617 * their HW callbacks or remove them from pending list if they already 618 * signaled. 619 * This iteration is thread safe as the scheduler's work items have been 620 * cancelled. 621 */ 622 list_for_each_entry_safe_reverse(s_job, tmp, &sched->pending_list, 623 list) { 624 if (s_job->s_fence->parent && 625 dma_fence_remove_callback(s_job->s_fence->parent, 626 &s_job->cb)) { 627 dma_fence_put(s_job->s_fence->parent); 628 s_job->s_fence->parent = NULL; 629 atomic_sub(s_job->credits, &sched->credit_count); 630 } else { 631 /* 632 * remove job from pending_list. 633 * Locking here is for concurrent resume timeout 634 */ 635 spin_lock(&sched->job_list_lock); 636 list_del_init(&s_job->list); 637 spin_unlock(&sched->job_list_lock); 638 639 /* 640 * Wait for job's HW fence callback to finish using s_job 641 * before releasing it. 642 * 643 * Job is still alive so fence refcount at least 1 644 */ 645 dma_fence_wait(&s_job->s_fence->finished, false); 646 647 /* 648 * We must keep bad job alive for later use during 649 * recovery by some of the drivers but leave a hint 650 * that the guilty job must be released. 651 */ 652 if (bad != s_job) 653 sched->ops->free_job(s_job); 654 else 655 sched->free_guilty = true; 656 } 657 } 658 659 /* 660 * Stop pending timer in flight as we rearm it in drm_sched_start. This 661 * avoids the pending timeout work in progress to fire right away after 662 * this TDR finished and before the newly restarted jobs had a 663 * chance to complete. 664 */ 665 cancel_delayed_work(&sched->work_tdr); 666 } 667 EXPORT_SYMBOL(drm_sched_stop); 668 669 /** 670 * drm_sched_start - recover jobs after a reset 671 * 672 * @sched: scheduler instance 673 * @errno: error to set on the pending fences 674 * 675 * This function is typically used for reset recovery (see the docu of 676 * drm_sched_backend_ops.timedout_job() for details). Do not call it for 677 * scheduler startup. The scheduler itself is fully operational after 678 * drm_sched_init() succeeded. 679 */ 680 void drm_sched_start(struct drm_gpu_scheduler *sched, int errno) 681 { 682 struct drm_sched_job *s_job, *tmp; 683 684 /* 685 * Locking the list is not required here as the scheduler's work items 686 * are currently not running, so no new jobs are being inserted or 687 * removed. Also concurrent GPU recovers can't run in parallel. 688 */ 689 list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) { 690 struct dma_fence *fence = s_job->s_fence->parent; 691 692 atomic_add(s_job->credits, &sched->credit_count); 693 694 if (!fence) { 695 drm_sched_job_done(s_job, errno ?: -ECANCELED); 696 continue; 697 } 698 699 if (dma_fence_add_callback(fence, &s_job->cb, 700 drm_sched_job_done_cb)) 701 drm_sched_job_done(s_job, fence->error ?: errno); 702 } 703 704 drm_sched_start_timeout_unlocked(sched); 705 drm_sched_wqueue_start(sched); 706 } 707 EXPORT_SYMBOL(drm_sched_start); 708 709 /** 710 * drm_sched_resubmit_jobs - Deprecated, don't use in new code! 711 * 712 * @sched: scheduler instance 713 * 714 * Re-submitting jobs was a concept AMD came up as cheap way to implement 715 * recovery after a job timeout. 716 * 717 * This turned out to be not working very well. First of all there are many 718 * problem with the dma_fence implementation and requirements. Either the 719 * implementation is risking deadlocks with core memory management or violating 720 * documented implementation details of the dma_fence object. 721 * 722 * Drivers can still save and restore their state for recovery operations, but 723 * we shouldn't make this a general scheduler feature around the dma_fence 724 * interface. 725 */ 726 void drm_sched_resubmit_jobs(struct drm_gpu_scheduler *sched) 727 { 728 struct drm_sched_job *s_job, *tmp; 729 uint64_t guilty_context; 730 bool found_guilty = false; 731 struct dma_fence *fence; 732 733 list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) { 734 struct drm_sched_fence *s_fence = s_job->s_fence; 735 736 if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) { 737 found_guilty = true; 738 guilty_context = s_job->s_fence->scheduled.context; 739 } 740 741 if (found_guilty && s_job->s_fence->scheduled.context == guilty_context) 742 dma_fence_set_error(&s_fence->finished, -ECANCELED); 743 744 fence = sched->ops->run_job(s_job); 745 746 if (IS_ERR_OR_NULL(fence)) { 747 if (IS_ERR(fence)) 748 dma_fence_set_error(&s_fence->finished, PTR_ERR(fence)); 749 750 s_job->s_fence->parent = NULL; 751 } else { 752 753 s_job->s_fence->parent = dma_fence_get(fence); 754 755 /* Drop for orignal kref_init */ 756 dma_fence_put(fence); 757 } 758 } 759 } 760 EXPORT_SYMBOL(drm_sched_resubmit_jobs); 761 762 /** 763 * drm_sched_job_init - init a scheduler job 764 * @job: scheduler job to init 765 * @entity: scheduler entity to use 766 * @credits: the number of credits this job contributes to the schedulers 767 * credit limit 768 * @owner: job owner for debugging 769 * @drm_client_id: &struct drm_file.client_id of the owner (used by trace 770 * events) 771 * 772 * Refer to drm_sched_entity_push_job() documentation 773 * for locking considerations. 774 * 775 * Drivers must make sure drm_sched_job_cleanup() if this function returns 776 * successfully, even when @job is aborted before drm_sched_job_arm() is called. 777 * 778 * Note that this function does not assign a valid value to each struct member 779 * of struct drm_sched_job. Take a look at that struct's documentation to see 780 * who sets which struct member with what lifetime. 781 * 782 * WARNING: amdgpu abuses &drm_sched.ready to signal when the hardware 783 * has died, which can mean that there's no valid runqueue for a @entity. 784 * This function returns -ENOENT in this case (which probably should be -EIO as 785 * a more meanigful return value). 786 * 787 * Returns 0 for success, negative error code otherwise. 788 */ 789 int drm_sched_job_init(struct drm_sched_job *job, 790 struct drm_sched_entity *entity, 791 u32 credits, void *owner, 792 uint64_t drm_client_id) 793 { 794 if (!entity->rq) { 795 /* This will most likely be followed by missing frames 796 * or worse--a blank screen--leave a trail in the 797 * logs, so this can be debugged easier. 798 */ 799 dev_err(job->sched->dev, "%s: entity has no rq!\n", __func__); 800 return -ENOENT; 801 } 802 803 if (unlikely(!credits)) { 804 pr_err("*ERROR* %s: credits cannot be 0!\n", __func__); 805 return -EINVAL; 806 } 807 808 /* 809 * We don't know for sure how the user has allocated. Thus, zero the 810 * struct so that unallowed (i.e., too early) usage of pointers that 811 * this function does not set is guaranteed to lead to a NULL pointer 812 * exception instead of UB. 813 */ 814 memset(job, 0, sizeof(*job)); 815 816 job->entity = entity; 817 job->credits = credits; 818 job->s_fence = drm_sched_fence_alloc(entity, owner, drm_client_id); 819 if (!job->s_fence) 820 return -ENOMEM; 821 822 INIT_LIST_HEAD(&job->list); 823 824 xa_init_flags(&job->dependencies, XA_FLAGS_ALLOC); 825 826 return 0; 827 } 828 EXPORT_SYMBOL(drm_sched_job_init); 829 830 /** 831 * drm_sched_job_arm - arm a scheduler job for execution 832 * @job: scheduler job to arm 833 * 834 * This arms a scheduler job for execution. Specifically it initializes the 835 * &drm_sched_job.s_fence of @job, so that it can be attached to struct dma_resv 836 * or other places that need to track the completion of this job. It also 837 * initializes sequence numbers, which are fundamental for fence ordering. 838 * 839 * Refer to drm_sched_entity_push_job() documentation for locking 840 * considerations. 841 * 842 * Once this function was called, you *must* submit @job with 843 * drm_sched_entity_push_job(). 844 * 845 * This can only be called if drm_sched_job_init() succeeded. 846 */ 847 void drm_sched_job_arm(struct drm_sched_job *job) 848 { 849 struct drm_gpu_scheduler *sched; 850 struct drm_sched_entity *entity = job->entity; 851 852 BUG_ON(!entity); 853 drm_sched_entity_select_rq(entity); 854 sched = entity->rq->sched; 855 856 job->sched = sched; 857 job->s_priority = entity->priority; 858 859 drm_sched_fence_init(job->s_fence, job->entity); 860 } 861 EXPORT_SYMBOL(drm_sched_job_arm); 862 863 /** 864 * drm_sched_job_add_dependency - adds the fence as a job dependency 865 * @job: scheduler job to add the dependencies to 866 * @fence: the dma_fence to add to the list of dependencies. 867 * 868 * Note that @fence is consumed in both the success and error cases. 869 * 870 * Returns: 871 * 0 on success, or an error on failing to expand the array. 872 */ 873 int drm_sched_job_add_dependency(struct drm_sched_job *job, 874 struct dma_fence *fence) 875 { 876 struct dma_fence *entry; 877 unsigned long index; 878 u32 id = 0; 879 int ret; 880 881 if (!fence) 882 return 0; 883 884 /* Deduplicate if we already depend on a fence from the same context. 885 * This lets the size of the array of deps scale with the number of 886 * engines involved, rather than the number of BOs. 887 */ 888 xa_for_each(&job->dependencies, index, entry) { 889 if (entry->context != fence->context) 890 continue; 891 892 if (dma_fence_is_later(fence, entry)) { 893 dma_fence_put(entry); 894 xa_store(&job->dependencies, index, fence, GFP_KERNEL); 895 } else { 896 dma_fence_put(fence); 897 } 898 return 0; 899 } 900 901 ret = xa_alloc(&job->dependencies, &id, fence, xa_limit_32b, GFP_KERNEL); 902 if (ret != 0) 903 dma_fence_put(fence); 904 905 return ret; 906 } 907 EXPORT_SYMBOL(drm_sched_job_add_dependency); 908 909 /** 910 * drm_sched_job_add_syncobj_dependency - adds a syncobj's fence as a job dependency 911 * @job: scheduler job to add the dependencies to 912 * @file: drm file private pointer 913 * @handle: syncobj handle to lookup 914 * @point: timeline point 915 * 916 * This adds the fence matching the given syncobj to @job. 917 * 918 * Returns: 919 * 0 on success, or an error on failing to expand the array. 920 */ 921 int drm_sched_job_add_syncobj_dependency(struct drm_sched_job *job, 922 struct drm_file *file, 923 u32 handle, 924 u32 point) 925 { 926 struct dma_fence *fence; 927 int ret; 928 929 ret = drm_syncobj_find_fence(file, handle, point, 0, &fence); 930 if (ret) 931 return ret; 932 933 return drm_sched_job_add_dependency(job, fence); 934 } 935 EXPORT_SYMBOL(drm_sched_job_add_syncobj_dependency); 936 937 /** 938 * drm_sched_job_add_resv_dependencies - add all fences from the resv to the job 939 * @job: scheduler job to add the dependencies to 940 * @resv: the dma_resv object to get the fences from 941 * @usage: the dma_resv_usage to use to filter the fences 942 * 943 * This adds all fences matching the given usage from @resv to @job. 944 * Must be called with the @resv lock held. 945 * 946 * Returns: 947 * 0 on success, or an error on failing to expand the array. 948 */ 949 int drm_sched_job_add_resv_dependencies(struct drm_sched_job *job, 950 struct dma_resv *resv, 951 enum dma_resv_usage usage) 952 { 953 struct dma_resv_iter cursor; 954 struct dma_fence *fence; 955 int ret; 956 957 dma_resv_assert_held(resv); 958 959 dma_resv_for_each_fence(&cursor, resv, usage, fence) { 960 /* Make sure to grab an additional ref on the added fence */ 961 dma_fence_get(fence); 962 ret = drm_sched_job_add_dependency(job, fence); 963 if (ret) { 964 dma_fence_put(fence); 965 return ret; 966 } 967 } 968 return 0; 969 } 970 EXPORT_SYMBOL(drm_sched_job_add_resv_dependencies); 971 972 /** 973 * drm_sched_job_add_implicit_dependencies - adds implicit dependencies as job 974 * dependencies 975 * @job: scheduler job to add the dependencies to 976 * @obj: the gem object to add new dependencies from. 977 * @write: whether the job might write the object (so we need to depend on 978 * shared fences in the reservation object). 979 * 980 * This should be called after drm_gem_lock_reservations() on your array of 981 * GEM objects used in the job but before updating the reservations with your 982 * own fences. 983 * 984 * Returns: 985 * 0 on success, or an error on failing to expand the array. 986 */ 987 int drm_sched_job_add_implicit_dependencies(struct drm_sched_job *job, 988 struct drm_gem_object *obj, 989 bool write) 990 { 991 return drm_sched_job_add_resv_dependencies(job, obj->resv, 992 dma_resv_usage_rw(write)); 993 } 994 EXPORT_SYMBOL(drm_sched_job_add_implicit_dependencies); 995 996 /** 997 * drm_sched_job_has_dependency - check whether fence is the job's dependency 998 * @job: scheduler job to check 999 * @fence: fence to look for 1000 * 1001 * Returns: 1002 * True if @fence is found within the job's dependencies, or otherwise false. 1003 */ 1004 bool drm_sched_job_has_dependency(struct drm_sched_job *job, 1005 struct dma_fence *fence) 1006 { 1007 struct dma_fence *f; 1008 unsigned long index; 1009 1010 xa_for_each(&job->dependencies, index, f) { 1011 if (f == fence) 1012 return true; 1013 } 1014 1015 return false; 1016 } 1017 EXPORT_SYMBOL(drm_sched_job_has_dependency); 1018 1019 /** 1020 * drm_sched_job_cleanup - clean up scheduler job resources 1021 * @job: scheduler job to clean up 1022 * 1023 * Cleans up the resources allocated with drm_sched_job_init(). 1024 * 1025 * Drivers should call this from their error unwind code if @job is aborted 1026 * before drm_sched_job_arm() is called. 1027 * 1028 * drm_sched_job_arm() is a point of no return since it initializes the fences 1029 * and their sequence number etc. Once that function has been called, you *must* 1030 * submit it with drm_sched_entity_push_job() and cannot simply abort it by 1031 * calling drm_sched_job_cleanup(). 1032 * 1033 * This function should be called in the &drm_sched_backend_ops.free_job callback. 1034 */ 1035 void drm_sched_job_cleanup(struct drm_sched_job *job) 1036 { 1037 struct dma_fence *fence; 1038 unsigned long index; 1039 1040 if (kref_read(&job->s_fence->finished.refcount)) { 1041 /* The job has been processed by the scheduler, i.e., 1042 * drm_sched_job_arm() and drm_sched_entity_push_job() have 1043 * been called. 1044 */ 1045 dma_fence_put(&job->s_fence->finished); 1046 } else { 1047 /* The job was aborted before it has been committed to be run; 1048 * notably, drm_sched_job_arm() has not been called. 1049 */ 1050 drm_sched_fence_free(job->s_fence); 1051 } 1052 1053 job->s_fence = NULL; 1054 1055 xa_for_each(&job->dependencies, index, fence) { 1056 dma_fence_put(fence); 1057 } 1058 xa_destroy(&job->dependencies); 1059 1060 } 1061 EXPORT_SYMBOL(drm_sched_job_cleanup); 1062 1063 /** 1064 * drm_sched_wakeup - Wake up the scheduler if it is ready to queue 1065 * @sched: scheduler instance 1066 * 1067 * Wake up the scheduler if we can queue jobs. 1068 */ 1069 void drm_sched_wakeup(struct drm_gpu_scheduler *sched) 1070 { 1071 drm_sched_run_job_queue(sched); 1072 } 1073 1074 /** 1075 * drm_sched_select_entity - Select next entity to process 1076 * 1077 * @sched: scheduler instance 1078 * 1079 * Return an entity to process or NULL if none are found. 1080 * 1081 * Note, that we break out of the for-loop when "entity" is non-null, which can 1082 * also be an error-pointer--this assures we don't process lower priority 1083 * run-queues. See comments in the respectively called functions. 1084 */ 1085 static struct drm_sched_entity * 1086 drm_sched_select_entity(struct drm_gpu_scheduler *sched) 1087 { 1088 struct drm_sched_entity *entity; 1089 int i; 1090 1091 /* Start with the highest priority. 1092 */ 1093 for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) { 1094 entity = drm_sched_policy == DRM_SCHED_POLICY_FIFO ? 1095 drm_sched_rq_select_entity_fifo(sched, sched->sched_rq[i]) : 1096 drm_sched_rq_select_entity_rr(sched, sched->sched_rq[i]); 1097 if (entity) 1098 break; 1099 } 1100 1101 return IS_ERR(entity) ? NULL : entity; 1102 } 1103 1104 /** 1105 * drm_sched_get_finished_job - fetch the next finished job to be destroyed 1106 * 1107 * @sched: scheduler instance 1108 * 1109 * Returns the next finished job from the pending list (if there is one) 1110 * ready for it to be destroyed. 1111 */ 1112 static struct drm_sched_job * 1113 drm_sched_get_finished_job(struct drm_gpu_scheduler *sched) 1114 { 1115 struct drm_sched_job *job, *next; 1116 1117 spin_lock(&sched->job_list_lock); 1118 1119 job = list_first_entry_or_null(&sched->pending_list, 1120 struct drm_sched_job, list); 1121 1122 if (job && dma_fence_is_signaled(&job->s_fence->finished)) { 1123 /* remove job from pending_list */ 1124 list_del_init(&job->list); 1125 1126 /* cancel this job's TO timer */ 1127 cancel_delayed_work(&sched->work_tdr); 1128 /* make the scheduled timestamp more accurate */ 1129 next = list_first_entry_or_null(&sched->pending_list, 1130 typeof(*next), list); 1131 1132 if (next) { 1133 if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, 1134 &next->s_fence->scheduled.flags)) 1135 next->s_fence->scheduled.timestamp = 1136 dma_fence_timestamp(&job->s_fence->finished); 1137 /* start TO timer for next job */ 1138 drm_sched_start_timeout(sched); 1139 } 1140 } else { 1141 job = NULL; 1142 } 1143 1144 spin_unlock(&sched->job_list_lock); 1145 1146 return job; 1147 } 1148 1149 /** 1150 * drm_sched_pick_best - Get a drm sched from a sched_list with the least load 1151 * @sched_list: list of drm_gpu_schedulers 1152 * @num_sched_list: number of drm_gpu_schedulers in the sched_list 1153 * 1154 * Returns pointer of the sched with the least load or NULL if none of the 1155 * drm_gpu_schedulers are ready 1156 */ 1157 struct drm_gpu_scheduler * 1158 drm_sched_pick_best(struct drm_gpu_scheduler **sched_list, 1159 unsigned int num_sched_list) 1160 { 1161 struct drm_gpu_scheduler *sched, *picked_sched = NULL; 1162 int i; 1163 unsigned int min_score = UINT_MAX, num_score; 1164 1165 for (i = 0; i < num_sched_list; ++i) { 1166 sched = sched_list[i]; 1167 1168 if (!sched->ready) { 1169 DRM_WARN("scheduler %s is not ready, skipping", 1170 sched->name); 1171 continue; 1172 } 1173 1174 num_score = atomic_read(sched->score); 1175 if (num_score < min_score) { 1176 min_score = num_score; 1177 picked_sched = sched; 1178 } 1179 } 1180 1181 return picked_sched; 1182 } 1183 EXPORT_SYMBOL(drm_sched_pick_best); 1184 1185 /** 1186 * drm_sched_free_job_work - worker to call free_job 1187 * 1188 * @w: free job work 1189 */ 1190 static void drm_sched_free_job_work(struct work_struct *w) 1191 { 1192 struct drm_gpu_scheduler *sched = 1193 container_of(w, struct drm_gpu_scheduler, work_free_job); 1194 struct drm_sched_job *job; 1195 1196 job = drm_sched_get_finished_job(sched); 1197 if (job) 1198 sched->ops->free_job(job); 1199 1200 drm_sched_run_free_queue(sched); 1201 drm_sched_run_job_queue(sched); 1202 } 1203 1204 /** 1205 * drm_sched_run_job_work - worker to call run_job 1206 * 1207 * @w: run job work 1208 */ 1209 static void drm_sched_run_job_work(struct work_struct *w) 1210 { 1211 struct drm_gpu_scheduler *sched = 1212 container_of(w, struct drm_gpu_scheduler, work_run_job); 1213 struct drm_sched_entity *entity; 1214 struct dma_fence *fence; 1215 struct drm_sched_fence *s_fence; 1216 struct drm_sched_job *sched_job; 1217 int r; 1218 1219 /* Find entity with a ready job */ 1220 entity = drm_sched_select_entity(sched); 1221 if (!entity) 1222 return; /* No more work */ 1223 1224 sched_job = drm_sched_entity_pop_job(entity); 1225 if (!sched_job) { 1226 complete_all(&entity->entity_idle); 1227 drm_sched_run_job_queue(sched); 1228 return; 1229 } 1230 1231 s_fence = sched_job->s_fence; 1232 1233 atomic_add(sched_job->credits, &sched->credit_count); 1234 drm_sched_job_begin(sched_job); 1235 1236 trace_drm_sched_job_run(sched_job, entity); 1237 /* 1238 * The run_job() callback must by definition return a fence whose 1239 * refcount has been incremented for the scheduler already. 1240 */ 1241 fence = sched->ops->run_job(sched_job); 1242 complete_all(&entity->entity_idle); 1243 drm_sched_fence_scheduled(s_fence, fence); 1244 1245 if (!IS_ERR_OR_NULL(fence)) { 1246 r = dma_fence_add_callback(fence, &sched_job->cb, 1247 drm_sched_job_done_cb); 1248 if (r == -ENOENT) 1249 drm_sched_job_done(sched_job, fence->error); 1250 else if (r) 1251 DRM_DEV_ERROR(sched->dev, "fence add callback failed (%d)\n", r); 1252 1253 dma_fence_put(fence); 1254 } else { 1255 drm_sched_job_done(sched_job, IS_ERR(fence) ? 1256 PTR_ERR(fence) : 0); 1257 } 1258 1259 wake_up(&sched->job_scheduled); 1260 drm_sched_run_job_queue(sched); 1261 } 1262 1263 /** 1264 * drm_sched_init - Init a gpu scheduler instance 1265 * 1266 * @sched: scheduler instance 1267 * @args: scheduler initialization arguments 1268 * 1269 * Return 0 on success, otherwise error code. 1270 */ 1271 int drm_sched_init(struct drm_gpu_scheduler *sched, const struct drm_sched_init_args *args) 1272 { 1273 int i; 1274 1275 sched->ops = args->ops; 1276 sched->credit_limit = args->credit_limit; 1277 sched->name = args->name; 1278 sched->timeout = args->timeout; 1279 sched->hang_limit = args->hang_limit; 1280 sched->timeout_wq = args->timeout_wq ? args->timeout_wq : system_wq; 1281 sched->score = args->score ? args->score : &sched->_score; 1282 sched->dev = args->dev; 1283 1284 if (args->num_rqs > DRM_SCHED_PRIORITY_COUNT) { 1285 /* This is a gross violation--tell drivers what the problem is. 1286 */ 1287 dev_err(sched->dev, "%s: num_rqs cannot be greater than DRM_SCHED_PRIORITY_COUNT\n", 1288 __func__); 1289 return -EINVAL; 1290 } else if (sched->sched_rq) { 1291 /* Not an error, but warn anyway so drivers can 1292 * fine-tune their DRM calling order, and return all 1293 * is good. 1294 */ 1295 dev_warn(sched->dev, "%s: scheduler already initialized!\n", __func__); 1296 return 0; 1297 } 1298 1299 if (args->submit_wq) { 1300 sched->submit_wq = args->submit_wq; 1301 sched->own_submit_wq = false; 1302 } else { 1303 #ifdef CONFIG_LOCKDEP 1304 sched->submit_wq = alloc_ordered_workqueue_lockdep_map(args->name, 1305 WQ_MEM_RECLAIM, 1306 &drm_sched_lockdep_map); 1307 #else 1308 sched->submit_wq = alloc_ordered_workqueue(args->name, WQ_MEM_RECLAIM); 1309 #endif 1310 if (!sched->submit_wq) 1311 return -ENOMEM; 1312 1313 sched->own_submit_wq = true; 1314 } 1315 1316 sched->sched_rq = kmalloc_array(args->num_rqs, sizeof(*sched->sched_rq), 1317 GFP_KERNEL | __GFP_ZERO); 1318 if (!sched->sched_rq) 1319 goto Out_check_own; 1320 sched->num_rqs = args->num_rqs; 1321 for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) { 1322 sched->sched_rq[i] = kzalloc(sizeof(*sched->sched_rq[i]), GFP_KERNEL); 1323 if (!sched->sched_rq[i]) 1324 goto Out_unroll; 1325 drm_sched_rq_init(sched, sched->sched_rq[i]); 1326 } 1327 1328 init_waitqueue_head(&sched->job_scheduled); 1329 INIT_LIST_HEAD(&sched->pending_list); 1330 spin_lock_init(&sched->job_list_lock); 1331 atomic_set(&sched->credit_count, 0); 1332 INIT_DELAYED_WORK(&sched->work_tdr, drm_sched_job_timedout); 1333 INIT_WORK(&sched->work_run_job, drm_sched_run_job_work); 1334 INIT_WORK(&sched->work_free_job, drm_sched_free_job_work); 1335 atomic_set(&sched->_score, 0); 1336 atomic64_set(&sched->job_id_count, 0); 1337 sched->pause_submit = false; 1338 1339 sched->ready = true; 1340 return 0; 1341 Out_unroll: 1342 for (--i ; i >= DRM_SCHED_PRIORITY_KERNEL; i--) 1343 kfree(sched->sched_rq[i]); 1344 1345 kfree(sched->sched_rq); 1346 sched->sched_rq = NULL; 1347 Out_check_own: 1348 if (sched->own_submit_wq) 1349 destroy_workqueue(sched->submit_wq); 1350 dev_err(sched->dev, "%s: Failed to setup GPU scheduler--out of memory\n", __func__); 1351 return -ENOMEM; 1352 } 1353 EXPORT_SYMBOL(drm_sched_init); 1354 1355 /** 1356 * drm_sched_fini - Destroy a gpu scheduler 1357 * 1358 * @sched: scheduler instance 1359 * 1360 * Tears down and cleans up the scheduler. 1361 * 1362 * This stops submission of new jobs to the hardware through 1363 * drm_sched_backend_ops.run_job(). Consequently, drm_sched_backend_ops.free_job() 1364 * will not be called for all jobs still in drm_gpu_scheduler.pending_list. 1365 * There is no solution for this currently. Thus, it is up to the driver to make 1366 * sure that: 1367 * 1368 * a) drm_sched_fini() is only called after for all submitted jobs 1369 * drm_sched_backend_ops.free_job() has been called or that 1370 * b) the jobs for which drm_sched_backend_ops.free_job() has not been called 1371 * after drm_sched_fini() ran are freed manually. 1372 * 1373 * FIXME: Take care of the above problem and prevent this function from leaking 1374 * the jobs in drm_gpu_scheduler.pending_list under any circumstances. 1375 */ 1376 void drm_sched_fini(struct drm_gpu_scheduler *sched) 1377 { 1378 struct drm_sched_entity *s_entity; 1379 int i; 1380 1381 drm_sched_wqueue_stop(sched); 1382 1383 for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) { 1384 struct drm_sched_rq *rq = sched->sched_rq[i]; 1385 1386 spin_lock(&rq->lock); 1387 list_for_each_entry(s_entity, &rq->entities, list) 1388 /* 1389 * Prevents reinsertion and marks job_queue as idle, 1390 * it will be removed from the rq in drm_sched_entity_fini() 1391 * eventually 1392 */ 1393 s_entity->stopped = true; 1394 spin_unlock(&rq->lock); 1395 kfree(sched->sched_rq[i]); 1396 } 1397 1398 /* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */ 1399 wake_up_all(&sched->job_scheduled); 1400 1401 /* Confirm no work left behind accessing device structures */ 1402 cancel_delayed_work_sync(&sched->work_tdr); 1403 1404 if (sched->own_submit_wq) 1405 destroy_workqueue(sched->submit_wq); 1406 sched->ready = false; 1407 kfree(sched->sched_rq); 1408 sched->sched_rq = NULL; 1409 } 1410 EXPORT_SYMBOL(drm_sched_fini); 1411 1412 /** 1413 * drm_sched_increase_karma - Update sched_entity guilty flag 1414 * 1415 * @bad: The job guilty of time out 1416 * 1417 * Increment on every hang caused by the 'bad' job. If this exceeds the hang 1418 * limit of the scheduler then the respective sched entity is marked guilty and 1419 * jobs from it will not be scheduled further 1420 */ 1421 void drm_sched_increase_karma(struct drm_sched_job *bad) 1422 { 1423 int i; 1424 struct drm_sched_entity *tmp; 1425 struct drm_sched_entity *entity; 1426 struct drm_gpu_scheduler *sched = bad->sched; 1427 1428 /* don't change @bad's karma if it's from KERNEL RQ, 1429 * because sometimes GPU hang would cause kernel jobs (like VM updating jobs) 1430 * corrupt but keep in mind that kernel jobs always considered good. 1431 */ 1432 if (bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) { 1433 atomic_inc(&bad->karma); 1434 1435 for (i = DRM_SCHED_PRIORITY_HIGH; i < sched->num_rqs; i++) { 1436 struct drm_sched_rq *rq = sched->sched_rq[i]; 1437 1438 spin_lock(&rq->lock); 1439 list_for_each_entry_safe(entity, tmp, &rq->entities, list) { 1440 if (bad->s_fence->scheduled.context == 1441 entity->fence_context) { 1442 if (entity->guilty) 1443 atomic_set(entity->guilty, 1); 1444 break; 1445 } 1446 } 1447 spin_unlock(&rq->lock); 1448 if (&entity->list != &rq->entities) 1449 break; 1450 } 1451 } 1452 } 1453 EXPORT_SYMBOL(drm_sched_increase_karma); 1454 1455 /** 1456 * drm_sched_wqueue_ready - Is the scheduler ready for submission 1457 * 1458 * @sched: scheduler instance 1459 * 1460 * Returns true if submission is ready 1461 */ 1462 bool drm_sched_wqueue_ready(struct drm_gpu_scheduler *sched) 1463 { 1464 return sched->ready; 1465 } 1466 EXPORT_SYMBOL(drm_sched_wqueue_ready); 1467 1468 /** 1469 * drm_sched_wqueue_stop - stop scheduler submission 1470 * @sched: scheduler instance 1471 * 1472 * Stops the scheduler from pulling new jobs from entities. It also stops 1473 * freeing jobs automatically through drm_sched_backend_ops.free_job(). 1474 */ 1475 void drm_sched_wqueue_stop(struct drm_gpu_scheduler *sched) 1476 { 1477 WRITE_ONCE(sched->pause_submit, true); 1478 cancel_work_sync(&sched->work_run_job); 1479 cancel_work_sync(&sched->work_free_job); 1480 } 1481 EXPORT_SYMBOL(drm_sched_wqueue_stop); 1482 1483 /** 1484 * drm_sched_wqueue_start - start scheduler submission 1485 * @sched: scheduler instance 1486 * 1487 * Restarts the scheduler after drm_sched_wqueue_stop() has stopped it. 1488 * 1489 * This function is not necessary for 'conventional' startup. The scheduler is 1490 * fully operational after drm_sched_init() succeeded. 1491 */ 1492 void drm_sched_wqueue_start(struct drm_gpu_scheduler *sched) 1493 { 1494 WRITE_ONCE(sched->pause_submit, false); 1495 queue_work(sched->submit_wq, &sched->work_run_job); 1496 queue_work(sched->submit_wq, &sched->work_free_job); 1497 } 1498 EXPORT_SYMBOL(drm_sched_wqueue_start); 1499