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->guilty = guilty; 133 entity->priority = priority; 134 entity->last_user = current->group_leader; 135 entity->rq_priority = drm_sched_policy == DRM_SCHED_POLICY_FAIR ? 136 DRM_SCHED_PRIORITY_KERNEL : priority; 137 entity->num_sched_list = num_sched_list; 138 entity->sched_list = num_sched_list > 1 ? sched_list : NULL; 139 RCU_INIT_POINTER(entity->last_scheduled, NULL); 140 RB_CLEAR_NODE(&entity->rb_tree_node); 141 142 if (!sched_list[0]->sched_rq) { 143 /* Since every entry covered by num_sched_list 144 * should be non-NULL and therefore we warn drivers 145 * not to do this and to fix their DRM calling order. 146 */ 147 pr_warn("%s: called with uninitialized scheduler\n", __func__); 148 } else { 149 enum drm_sched_priority p = entity->priority; 150 151 /* 152 * The "priority" of an entity cannot exceed the number of 153 * run-queues of a scheduler. Protect against num_rqs being 0, 154 * by converting to signed. Choose the lowest priority 155 * available. 156 */ 157 if (p >= sched_list[0]->num_user_rqs) { 158 dev_err(sched_list[0]->dev, "entity with out-of-bounds priority:%u num_user_rqs:%u\n", 159 p, sched_list[0]->num_user_rqs); 160 p = max_t(s32, 161 (s32)sched_list[0]->num_user_rqs - 1, 162 (s32)DRM_SCHED_PRIORITY_KERNEL); 163 entity->priority = p; 164 } 165 entity->rq = sched_list[0]->sched_rq[entity->rq_priority]; 166 } 167 168 init_completion(&entity->entity_idle); 169 170 /* We start in an idle state. */ 171 complete_all(&entity->entity_idle); 172 173 spin_lock_init(&entity->lock); 174 spsc_queue_init(&entity->job_queue); 175 176 atomic_set(&entity->fence_seq, 0); 177 entity->fence_context = dma_fence_context_alloc(2); 178 179 return 0; 180 } 181 EXPORT_SYMBOL(drm_sched_entity_init); 182 183 /** 184 * drm_sched_entity_modify_sched - Modify sched of an entity 185 * @entity: scheduler entity to init 186 * @sched_list: the list of new drm scheds which will replace 187 * existing entity->sched_list 188 * @num_sched_list: number of drm sched in sched_list 189 * 190 * Note that this must be called under the same common lock for @entity as 191 * drm_sched_job_arm() and drm_sched_entity_push_job(), or the driver needs to 192 * guarantee through some other means that this is never called while new jobs 193 * can be pushed to @entity. 194 */ 195 void drm_sched_entity_modify_sched(struct drm_sched_entity *entity, 196 struct drm_gpu_scheduler **sched_list, 197 unsigned int num_sched_list) 198 { 199 WARN_ON(!num_sched_list || !sched_list); 200 201 spin_lock(&entity->lock); 202 entity->sched_list = sched_list; 203 entity->num_sched_list = num_sched_list; 204 spin_unlock(&entity->lock); 205 } 206 EXPORT_SYMBOL(drm_sched_entity_modify_sched); 207 208 static bool drm_sched_entity_is_idle(struct drm_sched_entity *entity) 209 { 210 bool idle = false; 211 212 spin_lock(&entity->lock); 213 214 if (list_empty(&entity->list) || 215 spsc_queue_count(&entity->job_queue) == 0 || 216 entity->stopped) 217 idle = true; 218 219 spin_unlock(&entity->lock); 220 221 return idle; 222 } 223 224 /** 225 * drm_sched_entity_error - return error of last scheduled job 226 * @entity: scheduler entity to check 227 * 228 * Opportunistically return the error of the last scheduled job. Result can 229 * change any time when new jobs are pushed to the hw. 230 */ 231 int drm_sched_entity_error(struct drm_sched_entity *entity) 232 { 233 struct dma_fence *fence; 234 int r; 235 236 rcu_read_lock(); 237 fence = rcu_dereference(entity->last_scheduled); 238 r = fence ? fence->error : 0; 239 rcu_read_unlock(); 240 241 return r; 242 } 243 EXPORT_SYMBOL(drm_sched_entity_error); 244 245 static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f, 246 struct dma_fence_cb *cb); 247 248 static void drm_sched_entity_kill_jobs_work(struct work_struct *wrk) 249 { 250 struct drm_sched_job *job = container_of(wrk, typeof(*job), work); 251 struct dma_fence *f; 252 unsigned long index; 253 254 /* Wait for all dependencies to avoid data corruptions */ 255 xa_for_each(&job->dependencies, index, f) { 256 struct drm_sched_fence *s_fence = to_drm_sched_fence(f); 257 258 if (s_fence && f == &s_fence->scheduled) { 259 /* The dependencies array had a reference on the scheduled 260 * fence, and the finished fence refcount might have 261 * dropped to zero. Use dma_fence_get_rcu() so we get 262 * a NULL fence in that case. 263 */ 264 f = dma_fence_get_rcu(&s_fence->finished); 265 266 /* Now that we have a reference on the finished fence, 267 * we can release the reference the dependencies array 268 * had on the scheduled fence. 269 */ 270 dma_fence_put(&s_fence->scheduled); 271 } 272 273 xa_erase(&job->dependencies, index); 274 if (f && !dma_fence_add_callback(f, &job->finish_cb, 275 drm_sched_entity_kill_jobs_cb)) 276 return; 277 278 dma_fence_put(f); 279 } 280 281 drm_sched_fence_scheduled(job->s_fence, NULL); 282 drm_sched_fence_finished(job->s_fence, -ESRCH); 283 WARN_ON(job->s_fence->parent); 284 job->sched->ops->free_job(job); 285 } 286 287 /* Signal the scheduler finished fence when the entity in question is killed. */ 288 static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f, 289 struct dma_fence_cb *cb) 290 { 291 struct drm_sched_job *job = container_of(cb, struct drm_sched_job, 292 finish_cb); 293 294 dma_fence_put(f); 295 296 INIT_WORK(&job->work, drm_sched_entity_kill_jobs_work); 297 schedule_work(&job->work); 298 } 299 300 /** 301 * drm_sched_entity_kill - kill an entity's pending jobs and remove it 302 * @entity: the entity to remove 303 * 304 * Removes the entity from the scheduler's run queue and kills all pending jobs. 305 * 306 * This function should be used over drm_sched_entity_flush() if it is not 307 * desired to actually wait for all pending jobs to finish. 308 */ 309 void drm_sched_entity_kill(struct drm_sched_entity *entity) 310 { 311 struct drm_sched_job *job; 312 struct dma_fence *prev; 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 sched = entity->rq->sched; 365 /* 366 * The client will not queue more jobs during this fini - consume 367 * existing queued ones, or discard them on SIGKILL. 368 */ 369 if (current->flags & PF_EXITING) { 370 if (timeout) 371 ret = wait_event_timeout( 372 sched->job_scheduled, 373 drm_sched_entity_is_idle(entity), 374 timeout); 375 } else { 376 wait_event_killable(sched->job_scheduled, 377 drm_sched_entity_is_idle(entity)); 378 } 379 380 /* For a killed process disallow further enqueueing of jobs. */ 381 last_user = cmpxchg(&entity->last_user, current->group_leader, NULL); 382 if (last_user == current->group_leader && 383 (current->flags & PF_EXITING) && (current->exit_code == SIGKILL)) 384 drm_sched_entity_kill(entity); 385 386 return ret; 387 } 388 EXPORT_SYMBOL(drm_sched_entity_flush); 389 390 /** 391 * drm_sched_entity_fini - Destroy a context entity 392 * 393 * @entity: scheduler entity 394 * 395 * Cleanups up @entity which has been initialized by drm_sched_entity_init(). 396 * 397 * If there are potentially job still in flight or getting newly queued 398 * drm_sched_entity_flush() must be called first. This function then goes over 399 * the entity and signals all jobs with an error code if the process was killed. 400 */ 401 void drm_sched_entity_fini(struct drm_sched_entity *entity) 402 { 403 /* 404 * If consumption of existing jobs wasn't completed forcefully remove 405 * them. Also makes sure that the scheduler won't touch this entity any 406 * more. 407 */ 408 drm_sched_entity_kill(entity); 409 410 if (entity->dependency) { 411 dma_fence_remove_callback(entity->dependency, &entity->cb); 412 dma_fence_put(entity->dependency); 413 entity->dependency = NULL; 414 } 415 416 dma_fence_put(rcu_dereference_check(entity->last_scheduled, true)); 417 RCU_INIT_POINTER(entity->last_scheduled, NULL); 418 drm_sched_entity_stats_put(entity->stats); 419 } 420 EXPORT_SYMBOL(drm_sched_entity_fini); 421 422 /** 423 * drm_sched_entity_destroy - Destroy a context entity 424 * @entity: scheduler entity 425 * 426 * Calls drm_sched_entity_flush() and drm_sched_entity_fini() as a 427 * convenience wrapper. 428 */ 429 void drm_sched_entity_destroy(struct drm_sched_entity *entity) 430 { 431 drm_sched_entity_flush(entity, MAX_WAIT_SCHED_ENTITY_Q_EMPTY); 432 drm_sched_entity_fini(entity); 433 } 434 EXPORT_SYMBOL(drm_sched_entity_destroy); 435 436 /* 437 * drm_sched_entity_wakeup - callback to clear the entity's dependency and 438 * wake up the scheduler 439 */ 440 static void drm_sched_entity_wakeup(struct dma_fence *f, 441 struct dma_fence_cb *cb) 442 { 443 struct drm_sched_entity *entity = 444 container_of(cb, struct drm_sched_entity, cb); 445 446 entity->dependency = NULL; 447 dma_fence_put(f); 448 drm_sched_wakeup(entity->rq->sched); 449 } 450 451 /** 452 * drm_sched_entity_set_priority - Sets priority of the entity 453 * 454 * @entity: scheduler entity 455 * @priority: scheduler priority 456 * 457 * Update the priority of runqueues used for the entity. 458 */ 459 void drm_sched_entity_set_priority(struct drm_sched_entity *entity, 460 enum drm_sched_priority priority) 461 { 462 spin_lock(&entity->lock); 463 entity->priority = priority; 464 spin_unlock(&entity->lock); 465 } 466 EXPORT_SYMBOL(drm_sched_entity_set_priority); 467 468 /* 469 * Add a callback to the current dependency of the entity to wake up the 470 * scheduler when the entity becomes available. 471 */ 472 static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity, 473 struct drm_sched_job *sched_job) 474 { 475 struct drm_gpu_scheduler *sched = entity->rq->sched; 476 struct dma_fence *fence = entity->dependency; 477 struct drm_sched_fence *s_fence; 478 479 if (fence->context == entity->fence_context || 480 fence->context == entity->fence_context + 1) { 481 /* 482 * Fence is a scheduled/finished fence from a job 483 * which belongs to the same entity, we can ignore 484 * fences from ourself 485 */ 486 dma_fence_put(entity->dependency); 487 return false; 488 } 489 490 s_fence = to_drm_sched_fence(fence); 491 if (!fence->error && s_fence && s_fence->sched == sched && 492 !test_bit(DRM_SCHED_FENCE_DONT_PIPELINE, &fence->flags)) { 493 494 /* 495 * Fence is from the same scheduler, only need to wait for 496 * it to be scheduled 497 */ 498 fence = dma_fence_get(&s_fence->scheduled); 499 dma_fence_put(entity->dependency); 500 entity->dependency = fence; 501 } 502 503 if (trace_drm_sched_job_unschedulable_enabled() && 504 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &entity->dependency->flags)) 505 trace_drm_sched_job_unschedulable(sched_job, entity->dependency); 506 507 if (!dma_fence_add_callback(entity->dependency, &entity->cb, 508 drm_sched_entity_wakeup)) 509 return true; 510 511 dma_fence_put(entity->dependency); 512 return false; 513 } 514 515 static struct dma_fence * 516 drm_sched_job_dependency(struct drm_sched_job *job, 517 struct drm_sched_entity *entity) 518 { 519 struct dma_fence *f; 520 521 /* We keep the fence around, so we can iterate over all dependencies 522 * in drm_sched_entity_kill_jobs_cb() to ensure all deps are signaled 523 * before killing the job. 524 */ 525 f = xa_load(&job->dependencies, job->last_dependency); 526 if (f) { 527 job->last_dependency++; 528 return dma_fence_get(f); 529 } 530 531 if (job->sched->ops->prepare_job) 532 return job->sched->ops->prepare_job(job, entity); 533 534 return NULL; 535 } 536 537 struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity) 538 { 539 struct drm_sched_job *sched_job; 540 541 sched_job = drm_sched_entity_queue_peek(entity); 542 if (!sched_job) 543 return NULL; 544 545 while ((entity->dependency = 546 drm_sched_job_dependency(sched_job, entity))) { 547 if (drm_sched_entity_add_dependency_cb(entity, sched_job)) 548 return NULL; 549 } 550 551 /* skip jobs from entity that marked guilty */ 552 if (entity->guilty && atomic_read(entity->guilty)) 553 dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED); 554 555 dma_fence_put(rcu_dereference_check(entity->last_scheduled, true)); 556 rcu_assign_pointer(entity->last_scheduled, 557 dma_fence_get(&sched_job->s_fence->finished)); 558 559 /* 560 * If the queue is empty we allow drm_sched_entity_select_rq() to 561 * locklessly access ->last_scheduled. This only works if we set the 562 * pointer before we dequeue and if we a write barrier here. 563 */ 564 smp_wmb(); 565 566 spin_lock(&entity->lock); 567 spsc_queue_pop(&entity->job_queue); 568 drm_sched_rq_pop_entity(entity); 569 spin_unlock(&entity->lock); 570 571 /* Jobs and entities might have different lifecycles. Since we're 572 * removing the job from the entities queue, set the jobs entity pointer 573 * to NULL to prevent any future access of the entity through this job. 574 */ 575 sched_job->entity = NULL; 576 577 return sched_job; 578 } 579 580 void drm_sched_entity_select_rq(struct drm_sched_entity *entity) 581 { 582 struct dma_fence *fence; 583 struct drm_gpu_scheduler *sched; 584 struct drm_sched_rq *rq; 585 586 /* single possible engine and already selected */ 587 if (!entity->sched_list) 588 return; 589 590 /* queue non-empty, stay on the same engine */ 591 if (spsc_queue_count(&entity->job_queue)) 592 return; 593 594 /* 595 * Only when the queue is empty are we guaranteed that 596 * drm_sched_run_job_work() cannot change entity->last_scheduled. To 597 * enforce ordering we need a read barrier here. See 598 * drm_sched_entity_pop_job() for the other side. 599 */ 600 smp_rmb(); 601 602 fence = rcu_dereference_check(entity->last_scheduled, true); 603 604 /* stay on the same engine if the previous job hasn't finished */ 605 if (fence && !dma_fence_is_signaled(fence)) 606 return; 607 608 spin_lock(&entity->lock); 609 sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list); 610 rq = sched ? sched->sched_rq[entity->rq_priority] : NULL; 611 if (rq != entity->rq) { 612 drm_sched_rq_remove_entity(entity->rq, entity); 613 entity->rq = rq; 614 } 615 616 if (entity->num_sched_list == 1) 617 entity->sched_list = NULL; 618 619 spin_unlock(&entity->lock); 620 } 621 622 /** 623 * drm_sched_entity_push_job - Submit a job to the entity's job queue 624 * @sched_job: job to submit 625 * 626 * Note: To guarantee that the order of insertion to queue matches the job's 627 * fence sequence number this function should be called with drm_sched_job_arm() 628 * under common lock for the struct drm_sched_entity that was set up for 629 * @sched_job in drm_sched_job_init(). 630 */ 631 void drm_sched_entity_push_job(struct drm_sched_job *sched_job) 632 { 633 struct drm_sched_entity *entity = sched_job->entity; 634 bool first; 635 ktime_t submit_ts; 636 637 trace_drm_sched_job_queue(sched_job, entity); 638 639 if (trace_drm_sched_job_add_dep_enabled()) { 640 struct dma_fence *entry; 641 unsigned long index; 642 643 xa_for_each(&sched_job->dependencies, index, entry) 644 trace_drm_sched_job_add_dep(sched_job, entry); 645 } 646 atomic_inc(entity->rq->sched->score); 647 WRITE_ONCE(entity->last_user, current->group_leader); 648 649 /* 650 * After the sched_job is pushed into the entity queue, it may be 651 * completed and freed up at any time. We can no longer access it. 652 * Make sure to set the submit_ts first, to avoid a race. 653 */ 654 sched_job->submit_ts = submit_ts = ktime_get(); 655 656 spin_lock(&entity->lock); 657 658 first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node); 659 660 /* first job wakes up scheduler */ 661 if (first) { 662 struct drm_gpu_scheduler *sched; 663 664 sched = drm_sched_rq_add_entity(entity, submit_ts); 665 if (sched) 666 drm_sched_wakeup(sched); 667 } 668 669 spin_unlock(&entity->lock); 670 } 671 EXPORT_SYMBOL(drm_sched_entity_push_job); 672