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