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