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 #include <linux/export.h> 25 #include <linux/slab.h> 26 #include <linux/completion.h> 27 28 #include <drm/drm_print.h> 29 #include <drm/gpu_scheduler.h> 30 31 #include "sched_internal.h" 32 33 #include "gpu_scheduler_trace.h" 34 35 /** 36 * drm_sched_entity_stats_release - Entity stats kref release function 37 * @kref: Entity stats embedded kref pointer 38 */ 39 void drm_sched_entity_stats_release(struct kref *kref) 40 { 41 struct drm_sched_entity_stats *stats = 42 container_of(kref, typeof(*stats), kref); 43 44 kfree(stats); 45 } 46 47 /** 48 * drm_sched_entity_stats_new - Allocate a new struct drm_sched_entity_stats object 49 * 50 * Return: Pointer to newly allocated struct drm_sched_entity_stats object. 51 */ 52 static struct drm_sched_entity_stats *drm_sched_entity_stats_new(void) 53 { 54 struct drm_sched_entity_stats *stats; 55 56 stats = kzalloc_obj(*stats); 57 if (!stats) 58 return NULL; 59 60 kref_init(&stats->kref); 61 spin_lock_init(&stats->lock); 62 ewma_drm_sched_avgtime_init(&stats->avg_job_us); 63 64 return stats; 65 } 66 67 /** 68 * drm_sched_entity_stats_job_add_gpu_time - Account job execution time to entity 69 * @job: Scheduler job to account. 70 * 71 * Accounts the execution time of @job to its respective entity stats object. 72 * 73 * Return: Job's real duration in micro seconds. 74 */ 75 ktime_t drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job) 76 { 77 struct drm_sched_entity_stats *stats = job->entity_stats; 78 struct drm_sched_fence *s_fence = job->s_fence; 79 ktime_t start, end, duration; 80 81 start = dma_fence_timestamp(&s_fence->scheduled); 82 end = dma_fence_timestamp(&s_fence->finished); 83 duration = ktime_sub(end, start); 84 85 spin_lock(&stats->lock); 86 stats->runtime = ktime_add(stats->runtime, duration); 87 ewma_drm_sched_avgtime_add(&stats->avg_job_us, ktime_to_us(duration)); 88 spin_unlock(&stats->lock); 89 90 return duration; 91 } 92 93 /** 94 * drm_sched_entity_init - Init a context entity used by scheduler when 95 * submit to HW ring. 96 * 97 * @entity: scheduler entity to init 98 * @priority: priority of the entity 99 * @sched_list: the list of drm scheds on which jobs from this 100 * entity can be submitted 101 * @num_sched_list: number of drm sched in sched_list 102 * @guilty: atomic_t set to 1 when a job on this queue 103 * is found to be guilty causing a timeout 104 * 105 * Note that the &sched_list must have at least one element to schedule the entity. 106 * 107 * For changing @priority later on at runtime see 108 * drm_sched_entity_set_priority(). For changing the set of schedulers 109 * @sched_list at runtime see drm_sched_entity_modify_sched(). 110 * 111 * An entity is cleaned up by calling drm_sched_entity_fini(). See also 112 * drm_sched_entity_destroy(). 113 * 114 * Returns 0 on success or a negative error code on failure. 115 */ 116 int drm_sched_entity_init(struct drm_sched_entity *entity, 117 enum drm_sched_priority priority, 118 struct drm_gpu_scheduler **sched_list, 119 unsigned int num_sched_list, 120 atomic_t *guilty) 121 { 122 if (!entity || !sched_list || !num_sched_list || !sched_list[0]) 123 return -EINVAL; 124 125 memset(entity, 0, sizeof(struct drm_sched_entity)); 126 127 entity->stats = drm_sched_entity_stats_new(); 128 if (!entity->stats) 129 return -ENOMEM; 130 131 INIT_LIST_HEAD(&entity->list); 132 entity->rq = NULL; 133 entity->guilty = guilty; 134 entity->priority = priority; 135 entity->last_user = current->group_leader; 136 entity->rq_priority = drm_sched_policy == DRM_SCHED_POLICY_FAIR ? 137 DRM_SCHED_PRIORITY_KERNEL : priority; 138 entity->num_sched_list = num_sched_list; 139 entity->sched_list = num_sched_list > 1 ? sched_list : NULL; 140 RCU_INIT_POINTER(entity->last_scheduled, NULL); 141 RB_CLEAR_NODE(&entity->rb_tree_node); 142 143 if (!sched_list[0]->sched_rq) { 144 /* Since every entry covered by num_sched_list 145 * should be non-NULL and therefore we warn drivers 146 * not to do this and to fix their DRM calling order. 147 */ 148 pr_warn("%s: called with uninitialized scheduler\n", __func__); 149 } else { 150 enum drm_sched_priority p = entity->priority; 151 152 /* 153 * The "priority" of an entity cannot exceed the number of 154 * run-queues of a scheduler. Protect against num_rqs being 0, 155 * by converting to signed. Choose the lowest priority 156 * available. 157 */ 158 if (p >= sched_list[0]->num_user_rqs) { 159 dev_err(sched_list[0]->dev, "entity with out-of-bounds priority:%u num_user_rqs:%u\n", 160 p, sched_list[0]->num_user_rqs); 161 p = max_t(s32, 162 (s32)sched_list[0]->num_user_rqs - 1, 163 (s32)DRM_SCHED_PRIORITY_KERNEL); 164 entity->priority = p; 165 } 166 entity->rq = sched_list[0]->sched_rq[entity->rq_priority]; 167 } 168 169 init_completion(&entity->entity_idle); 170 171 /* We start in an idle state. */ 172 complete_all(&entity->entity_idle); 173 174 spin_lock_init(&entity->lock); 175 spsc_queue_init(&entity->job_queue); 176 177 atomic_set(&entity->fence_seq, 0); 178 entity->fence_context = dma_fence_context_alloc(2); 179 180 return 0; 181 } 182 EXPORT_SYMBOL(drm_sched_entity_init); 183 184 /** 185 * drm_sched_entity_modify_sched - Modify sched of an entity 186 * @entity: scheduler entity to init 187 * @sched_list: the list of new drm scheds which will replace 188 * existing entity->sched_list 189 * @num_sched_list: number of drm sched in sched_list 190 * 191 * Note that this must be called under the same common lock for @entity as 192 * drm_sched_job_arm() and drm_sched_entity_push_job(), or the driver needs to 193 * guarantee through some other means that this is never called while new jobs 194 * can be pushed to @entity. 195 */ 196 void drm_sched_entity_modify_sched(struct drm_sched_entity *entity, 197 struct drm_gpu_scheduler **sched_list, 198 unsigned int num_sched_list) 199 { 200 WARN_ON(!num_sched_list || !sched_list); 201 202 spin_lock(&entity->lock); 203 entity->sched_list = sched_list; 204 entity->num_sched_list = num_sched_list; 205 spin_unlock(&entity->lock); 206 } 207 EXPORT_SYMBOL(drm_sched_entity_modify_sched); 208 209 static bool drm_sched_entity_is_idle(struct drm_sched_entity *entity) 210 { 211 rmb(); /* for list_empty to work without lock */ 212 213 if (list_empty(&entity->list) || 214 spsc_queue_count(&entity->job_queue) == 0 || 215 entity->stopped) 216 return true; 217 218 return false; 219 } 220 221 /** 222 * drm_sched_entity_error - return error of last scheduled job 223 * @entity: scheduler entity to check 224 * 225 * Opportunistically return the error of the last scheduled job. Result can 226 * change any time when new jobs are pushed to the hw. 227 */ 228 int drm_sched_entity_error(struct drm_sched_entity *entity) 229 { 230 struct dma_fence *fence; 231 int r; 232 233 rcu_read_lock(); 234 fence = rcu_dereference(entity->last_scheduled); 235 r = fence ? fence->error : 0; 236 rcu_read_unlock(); 237 238 return r; 239 } 240 EXPORT_SYMBOL(drm_sched_entity_error); 241 242 static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f, 243 struct dma_fence_cb *cb); 244 245 static void drm_sched_entity_kill_jobs_work(struct work_struct *wrk) 246 { 247 struct drm_sched_job *job = container_of(wrk, typeof(*job), work); 248 struct dma_fence *f; 249 unsigned long index; 250 251 /* Wait for all dependencies to avoid data corruptions */ 252 xa_for_each(&job->dependencies, index, f) { 253 struct drm_sched_fence *s_fence = to_drm_sched_fence(f); 254 255 if (s_fence && f == &s_fence->scheduled) { 256 /* The dependencies array had a reference on the scheduled 257 * fence, and the finished fence refcount might have 258 * dropped to zero. Use dma_fence_get_rcu() so we get 259 * a NULL fence in that case. 260 */ 261 f = dma_fence_get_rcu(&s_fence->finished); 262 263 /* Now that we have a reference on the finished fence, 264 * we can release the reference the dependencies array 265 * had on the scheduled fence. 266 */ 267 dma_fence_put(&s_fence->scheduled); 268 } 269 270 xa_erase(&job->dependencies, index); 271 if (f && !dma_fence_add_callback(f, &job->finish_cb, 272 drm_sched_entity_kill_jobs_cb)) 273 return; 274 275 dma_fence_put(f); 276 } 277 278 drm_sched_fence_scheduled(job->s_fence, NULL); 279 drm_sched_fence_finished(job->s_fence, -ESRCH); 280 WARN_ON(job->s_fence->parent); 281 job->sched->ops->free_job(job); 282 } 283 284 /* Signal the scheduler finished fence when the entity in question is killed. */ 285 static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f, 286 struct dma_fence_cb *cb) 287 { 288 struct drm_sched_job *job = container_of(cb, struct drm_sched_job, 289 finish_cb); 290 291 dma_fence_put(f); 292 293 INIT_WORK(&job->work, drm_sched_entity_kill_jobs_work); 294 schedule_work(&job->work); 295 } 296 297 /** 298 * drm_sched_entity_kill - kill an entity's pending jobs and remove it 299 * @entity: the entity to remove 300 * 301 * Removes the entity from the scheduler's run queue and kills all pending jobs. 302 * 303 * This function should be used over drm_sched_entity_flush() if it is not 304 * desired to actually wait for all pending jobs to finish. 305 */ 306 void drm_sched_entity_kill(struct drm_sched_entity *entity) 307 { 308 struct drm_sched_job *job; 309 struct dma_fence *prev; 310 311 if (!entity->rq) 312 return; 313 314 spin_lock(&entity->lock); 315 entity->stopped = true; 316 drm_sched_rq_remove_entity(entity->rq, entity); 317 spin_unlock(&entity->lock); 318 319 /* Make sure this entity is not used by the scheduler at the moment */ 320 wait_for_completion(&entity->entity_idle); 321 322 /* The entity is guaranteed to not be used by the scheduler */ 323 prev = rcu_dereference_check(entity->last_scheduled, true); 324 dma_fence_get(prev); 325 while ((job = drm_sched_entity_queue_pop(entity))) { 326 struct drm_sched_fence *s_fence = job->s_fence; 327 328 dma_fence_get(&s_fence->finished); 329 if (!prev || 330 dma_fence_add_callback(prev, &job->finish_cb, 331 drm_sched_entity_kill_jobs_cb)) { 332 /* 333 * Adding callback above failed. 334 * dma_fence_put() checks for NULL. 335 */ 336 dma_fence_put(prev); 337 drm_sched_entity_kill_jobs_cb(NULL, &job->finish_cb); 338 } 339 340 prev = &s_fence->finished; 341 } 342 dma_fence_put(prev); 343 } 344 EXPORT_SYMBOL(drm_sched_entity_kill); 345 346 /** 347 * drm_sched_entity_flush - Flush a context entity 348 * 349 * @entity: scheduler entity 350 * @timeout: time to wait in for Q to become empty in jiffies. 351 * 352 * Splitting drm_sched_entity_fini() into two functions, The first one does the 353 * waiting, removes the entity from the runqueue and returns an error when the 354 * process was killed. 355 * 356 * Returns the remaining time in jiffies left from the input timeout 357 */ 358 long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout) 359 { 360 struct drm_gpu_scheduler *sched; 361 struct task_struct *last_user; 362 long ret = timeout; 363 364 if (!entity->rq) 365 return 0; 366 367 sched = entity->rq->sched; 368 /* 369 * The client will not queue more jobs during this fini - consume 370 * existing queued ones, or discard them on SIGKILL. 371 */ 372 if (current->flags & PF_EXITING) { 373 if (timeout) 374 ret = wait_event_timeout( 375 sched->job_scheduled, 376 drm_sched_entity_is_idle(entity), 377 timeout); 378 } else { 379 wait_event_killable(sched->job_scheduled, 380 drm_sched_entity_is_idle(entity)); 381 } 382 383 /* For a killed process disallow further enqueueing of jobs. */ 384 last_user = cmpxchg(&entity->last_user, current->group_leader, NULL); 385 if (last_user == current->group_leader && 386 (current->flags & PF_EXITING) && (current->exit_code == SIGKILL)) 387 drm_sched_entity_kill(entity); 388 389 return ret; 390 } 391 EXPORT_SYMBOL(drm_sched_entity_flush); 392 393 /** 394 * drm_sched_entity_fini - Destroy a context entity 395 * 396 * @entity: scheduler entity 397 * 398 * Cleanups up @entity which has been initialized by drm_sched_entity_init(). 399 * 400 * If there are potentially job still in flight or getting newly queued 401 * drm_sched_entity_flush() must be called first. This function then goes over 402 * the entity and signals all jobs with an error code if the process was killed. 403 */ 404 void drm_sched_entity_fini(struct drm_sched_entity *entity) 405 { 406 /* 407 * If consumption of existing jobs wasn't completed forcefully remove 408 * them. Also makes sure that the scheduler won't touch this entity any 409 * more. 410 */ 411 drm_sched_entity_kill(entity); 412 413 if (entity->dependency) { 414 dma_fence_remove_callback(entity->dependency, &entity->cb); 415 dma_fence_put(entity->dependency); 416 entity->dependency = NULL; 417 } 418 419 dma_fence_put(rcu_dereference_check(entity->last_scheduled, true)); 420 RCU_INIT_POINTER(entity->last_scheduled, NULL); 421 drm_sched_entity_stats_put(entity->stats); 422 } 423 EXPORT_SYMBOL(drm_sched_entity_fini); 424 425 /** 426 * drm_sched_entity_destroy - Destroy a context entity 427 * @entity: scheduler entity 428 * 429 * Calls drm_sched_entity_flush() and drm_sched_entity_fini() as a 430 * convenience wrapper. 431 */ 432 void drm_sched_entity_destroy(struct drm_sched_entity *entity) 433 { 434 drm_sched_entity_flush(entity, MAX_WAIT_SCHED_ENTITY_Q_EMPTY); 435 drm_sched_entity_fini(entity); 436 } 437 EXPORT_SYMBOL(drm_sched_entity_destroy); 438 439 /* 440 * drm_sched_entity_wakeup - callback to clear the entity's dependency and 441 * wake up the scheduler 442 */ 443 static void drm_sched_entity_wakeup(struct dma_fence *f, 444 struct dma_fence_cb *cb) 445 { 446 struct drm_sched_entity *entity = 447 container_of(cb, struct drm_sched_entity, cb); 448 449 entity->dependency = NULL; 450 dma_fence_put(f); 451 drm_sched_wakeup(entity->rq->sched); 452 } 453 454 /** 455 * drm_sched_entity_set_priority - Sets priority of the entity 456 * 457 * @entity: scheduler entity 458 * @priority: scheduler priority 459 * 460 * Update the priority of runqueues used for the entity. 461 */ 462 void drm_sched_entity_set_priority(struct drm_sched_entity *entity, 463 enum drm_sched_priority priority) 464 { 465 spin_lock(&entity->lock); 466 entity->priority = priority; 467 spin_unlock(&entity->lock); 468 } 469 EXPORT_SYMBOL(drm_sched_entity_set_priority); 470 471 /* 472 * Add a callback to the current dependency of the entity to wake up the 473 * scheduler when the entity becomes available. 474 */ 475 static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity, 476 struct drm_sched_job *sched_job) 477 { 478 struct drm_gpu_scheduler *sched = entity->rq->sched; 479 struct dma_fence *fence = entity->dependency; 480 struct drm_sched_fence *s_fence; 481 482 if (fence->context == entity->fence_context || 483 fence->context == entity->fence_context + 1) { 484 /* 485 * Fence is a scheduled/finished fence from a job 486 * which belongs to the same entity, we can ignore 487 * fences from ourself 488 */ 489 dma_fence_put(entity->dependency); 490 return false; 491 } 492 493 s_fence = to_drm_sched_fence(fence); 494 if (!fence->error && s_fence && s_fence->sched == sched && 495 !test_bit(DRM_SCHED_FENCE_DONT_PIPELINE, &fence->flags)) { 496 497 /* 498 * Fence is from the same scheduler, only need to wait for 499 * it to be scheduled 500 */ 501 fence = dma_fence_get(&s_fence->scheduled); 502 dma_fence_put(entity->dependency); 503 entity->dependency = fence; 504 } 505 506 if (trace_drm_sched_job_unschedulable_enabled() && 507 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &entity->dependency->flags)) 508 trace_drm_sched_job_unschedulable(sched_job, entity->dependency); 509 510 if (!dma_fence_add_callback(entity->dependency, &entity->cb, 511 drm_sched_entity_wakeup)) 512 return true; 513 514 dma_fence_put(entity->dependency); 515 return false; 516 } 517 518 static struct dma_fence * 519 drm_sched_job_dependency(struct drm_sched_job *job, 520 struct drm_sched_entity *entity) 521 { 522 struct dma_fence *f; 523 524 /* We keep the fence around, so we can iterate over all dependencies 525 * in drm_sched_entity_kill_jobs_cb() to ensure all deps are signaled 526 * before killing the job. 527 */ 528 f = xa_load(&job->dependencies, job->last_dependency); 529 if (f) { 530 job->last_dependency++; 531 return dma_fence_get(f); 532 } 533 534 if (job->sched->ops->prepare_job) 535 return job->sched->ops->prepare_job(job, entity); 536 537 return NULL; 538 } 539 540 struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity) 541 { 542 struct drm_sched_job *sched_job; 543 544 sched_job = drm_sched_entity_queue_peek(entity); 545 if (!sched_job) 546 return NULL; 547 548 while ((entity->dependency = 549 drm_sched_job_dependency(sched_job, entity))) { 550 if (drm_sched_entity_add_dependency_cb(entity, sched_job)) 551 return NULL; 552 } 553 554 /* skip jobs from entity that marked guilty */ 555 if (entity->guilty && atomic_read(entity->guilty)) 556 dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED); 557 558 dma_fence_put(rcu_dereference_check(entity->last_scheduled, true)); 559 rcu_assign_pointer(entity->last_scheduled, 560 dma_fence_get(&sched_job->s_fence->finished)); 561 562 /* 563 * If the queue is empty we allow drm_sched_entity_select_rq() to 564 * locklessly access ->last_scheduled. This only works if we set the 565 * pointer before we dequeue and if we a write barrier here. 566 */ 567 smp_wmb(); 568 569 spsc_queue_pop(&entity->job_queue); 570 571 drm_sched_rq_pop_entity(entity); 572 573 /* Jobs and entities might have different lifecycles. Since we're 574 * removing the job from the entities queue, set the jobs entity pointer 575 * to NULL to prevent any future access of the entity through this job. 576 */ 577 sched_job->entity = NULL; 578 579 return sched_job; 580 } 581 582 void drm_sched_entity_select_rq(struct drm_sched_entity *entity) 583 { 584 struct dma_fence *fence; 585 struct drm_gpu_scheduler *sched; 586 struct drm_sched_rq *rq; 587 588 /* single possible engine and already selected */ 589 if (!entity->sched_list) 590 return; 591 592 /* queue non-empty, stay on the same engine */ 593 if (spsc_queue_count(&entity->job_queue)) 594 return; 595 596 /* 597 * Only when the queue is empty are we guaranteed that 598 * drm_sched_run_job_work() cannot change entity->last_scheduled. To 599 * enforce ordering we need a read barrier here. See 600 * drm_sched_entity_pop_job() for the other side. 601 */ 602 smp_rmb(); 603 604 fence = rcu_dereference_check(entity->last_scheduled, true); 605 606 /* stay on the same engine if the previous job hasn't finished */ 607 if (fence && !dma_fence_is_signaled(fence)) 608 return; 609 610 spin_lock(&entity->lock); 611 sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list); 612 rq = sched ? sched->sched_rq[entity->rq_priority] : NULL; 613 if (rq != entity->rq) { 614 drm_sched_rq_remove_entity(entity->rq, entity); 615 entity->rq = rq; 616 } 617 618 if (entity->num_sched_list == 1) 619 entity->sched_list = NULL; 620 621 spin_unlock(&entity->lock); 622 } 623 624 /** 625 * drm_sched_entity_push_job - Submit a job to the entity's job queue 626 * @sched_job: job to submit 627 * 628 * Note: To guarantee that the order of insertion to queue matches the job's 629 * fence sequence number this function should be called with drm_sched_job_arm() 630 * under common lock for the struct drm_sched_entity that was set up for 631 * @sched_job in drm_sched_job_init(). 632 */ 633 void drm_sched_entity_push_job(struct drm_sched_job *sched_job) 634 { 635 struct drm_sched_entity *entity = sched_job->entity; 636 bool first; 637 ktime_t submit_ts; 638 639 trace_drm_sched_job_queue(sched_job, entity); 640 641 if (trace_drm_sched_job_add_dep_enabled()) { 642 struct dma_fence *entry; 643 unsigned long index; 644 645 xa_for_each(&sched_job->dependencies, index, entry) 646 trace_drm_sched_job_add_dep(sched_job, entry); 647 } 648 atomic_inc(entity->rq->sched->score); 649 WRITE_ONCE(entity->last_user, current->group_leader); 650 651 /* 652 * After the sched_job is pushed into the entity queue, it may be 653 * completed and freed up at any time. We can no longer access it. 654 * Make sure to set the submit_ts first, to avoid a race. 655 */ 656 sched_job->submit_ts = submit_ts = ktime_get(); 657 first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node); 658 659 /* first job wakes up scheduler */ 660 if (first) { 661 struct drm_gpu_scheduler *sched; 662 663 sched = drm_sched_rq_add_entity(entity, submit_ts); 664 if (sched) 665 drm_sched_wakeup(sched); 666 } 667 } 668 EXPORT_SYMBOL(drm_sched_entity_push_job); 669