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 #ifndef _DRM_GPU_SCHEDULER_H_ 25 #define _DRM_GPU_SCHEDULER_H_ 26 27 #include <drm/spsc_queue.h> 28 #include <linux/dma-fence.h> 29 #include <linux/completion.h> 30 #include <linux/xarray.h> 31 #include <linux/workqueue.h> 32 33 #define MAX_WAIT_SCHED_ENTITY_Q_EMPTY msecs_to_jiffies(1000) 34 35 /** 36 * DRM_SCHED_FENCE_DONT_PIPELINE - Prefent dependency pipelining 37 * 38 * Setting this flag on a scheduler fence prevents pipelining of jobs depending 39 * on this fence. In other words we always insert a full CPU round trip before 40 * dependen jobs are pushed to the hw queue. 41 */ 42 #define DRM_SCHED_FENCE_DONT_PIPELINE DMA_FENCE_FLAG_USER_BITS 43 44 /** 45 * DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT - A fence deadline hint has been set 46 * 47 * Because we could have a deadline hint can be set before the backing hw 48 * fence is created, we need to keep track of whether a deadline has already 49 * been set. 50 */ 51 #define DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT (DMA_FENCE_FLAG_USER_BITS + 1) 52 53 enum dma_resv_usage; 54 struct dma_resv; 55 struct drm_gem_object; 56 57 struct drm_gpu_scheduler; 58 struct drm_sched_rq; 59 60 struct drm_file; 61 62 /* These are often used as an (initial) index 63 * to an array, and as such should start at 0. 64 */ 65 enum drm_sched_priority { 66 DRM_SCHED_PRIORITY_MIN, 67 DRM_SCHED_PRIORITY_NORMAL, 68 DRM_SCHED_PRIORITY_HIGH, 69 DRM_SCHED_PRIORITY_KERNEL, 70 71 DRM_SCHED_PRIORITY_COUNT, 72 DRM_SCHED_PRIORITY_UNSET = -2 73 }; 74 75 /* Used to chose between FIFO and RR jobs scheduling */ 76 extern int drm_sched_policy; 77 78 #define DRM_SCHED_POLICY_RR 0 79 #define DRM_SCHED_POLICY_FIFO 1 80 81 /** 82 * struct drm_sched_entity - A wrapper around a job queue (typically 83 * attached to the DRM file_priv). 84 * 85 * Entities will emit jobs in order to their corresponding hardware 86 * ring, and the scheduler will alternate between entities based on 87 * scheduling policy. 88 */ 89 struct drm_sched_entity { 90 /** 91 * @list: 92 * 93 * Used to append this struct to the list of entities in the runqueue 94 * @rq under &drm_sched_rq.entities. 95 * 96 * Protected by &drm_sched_rq.lock of @rq. 97 */ 98 struct list_head list; 99 100 /** 101 * @rq: 102 * 103 * Runqueue on which this entity is currently scheduled. 104 * 105 * FIXME: Locking is very unclear for this. Writers are protected by 106 * @rq_lock, but readers are generally lockless and seem to just race 107 * with not even a READ_ONCE. 108 */ 109 struct drm_sched_rq *rq; 110 111 /** 112 * @sched_list: 113 * 114 * A list of schedulers (struct drm_gpu_scheduler). Jobs from this entity can 115 * be scheduled on any scheduler on this list. 116 * 117 * This can be modified by calling drm_sched_entity_modify_sched(). 118 * Locking is entirely up to the driver, see the above function for more 119 * details. 120 * 121 * This will be set to NULL if &num_sched_list equals 1 and @rq has been 122 * set already. 123 * 124 * FIXME: This means priority changes through 125 * drm_sched_entity_set_priority() will be lost henceforth in this case. 126 */ 127 struct drm_gpu_scheduler **sched_list; 128 129 /** 130 * @num_sched_list: 131 * 132 * Number of drm_gpu_schedulers in the @sched_list. 133 */ 134 unsigned int num_sched_list; 135 136 /** 137 * @priority: 138 * 139 * Priority of the entity. This can be modified by calling 140 * drm_sched_entity_set_priority(). Protected by &rq_lock. 141 */ 142 enum drm_sched_priority priority; 143 144 /** 145 * @rq_lock: 146 * 147 * Lock to modify the runqueue to which this entity belongs. 148 */ 149 spinlock_t rq_lock; 150 151 /** 152 * @job_queue: the list of jobs of this entity. 153 */ 154 struct spsc_queue job_queue; 155 156 /** 157 * @fence_seq: 158 * 159 * A linearly increasing seqno incremented with each new 160 * &drm_sched_fence which is part of the entity. 161 * 162 * FIXME: Callers of drm_sched_job_arm() need to ensure correct locking, 163 * this doesn't need to be atomic. 164 */ 165 atomic_t fence_seq; 166 167 /** 168 * @fence_context: 169 * 170 * A unique context for all the fences which belong to this entity. The 171 * &drm_sched_fence.scheduled uses the fence_context but 172 * &drm_sched_fence.finished uses fence_context + 1. 173 */ 174 uint64_t fence_context; 175 176 /** 177 * @dependency: 178 * 179 * The dependency fence of the job which is on the top of the job queue. 180 */ 181 struct dma_fence *dependency; 182 183 /** 184 * @cb: 185 * 186 * Callback for the dependency fence above. 187 */ 188 struct dma_fence_cb cb; 189 190 /** 191 * @guilty: 192 * 193 * Points to entities' guilty. 194 */ 195 atomic_t *guilty; 196 197 /** 198 * @last_scheduled: 199 * 200 * Points to the finished fence of the last scheduled job. Only written 201 * by the scheduler thread, can be accessed locklessly from 202 * drm_sched_job_arm() iff the queue is empty. 203 */ 204 struct dma_fence __rcu *last_scheduled; 205 206 /** 207 * @last_user: last group leader pushing a job into the entity. 208 */ 209 struct task_struct *last_user; 210 211 /** 212 * @stopped: 213 * 214 * Marks the enity as removed from rq and destined for 215 * termination. This is set by calling drm_sched_entity_flush() and by 216 * drm_sched_fini(). 217 */ 218 bool stopped; 219 220 /** 221 * @entity_idle: 222 * 223 * Signals when entity is not in use, used to sequence entity cleanup in 224 * drm_sched_entity_fini(). 225 */ 226 struct completion entity_idle; 227 228 /** 229 * @oldest_job_waiting: 230 * 231 * Marks earliest job waiting in SW queue 232 */ 233 ktime_t oldest_job_waiting; 234 235 /** 236 * @rb_tree_node: 237 * 238 * The node used to insert this entity into time based priority queue 239 */ 240 struct rb_node rb_tree_node; 241 242 }; 243 244 /** 245 * struct drm_sched_rq - queue of entities to be scheduled. 246 * 247 * @lock: to modify the entities list. 248 * @sched: the scheduler to which this rq belongs to. 249 * @entities: list of the entities to be scheduled. 250 * @current_entity: the entity which is to be scheduled. 251 * @rb_tree_root: root of time based priory queue of entities for FIFO scheduling 252 * 253 * Run queue is a set of entities scheduling command submissions for 254 * one specific ring. It implements the scheduling policy that selects 255 * the next entity to emit commands from. 256 */ 257 struct drm_sched_rq { 258 spinlock_t lock; 259 struct drm_gpu_scheduler *sched; 260 struct list_head entities; 261 struct drm_sched_entity *current_entity; 262 struct rb_root_cached rb_tree_root; 263 }; 264 265 /** 266 * struct drm_sched_fence - fences corresponding to the scheduling of a job. 267 */ 268 struct drm_sched_fence { 269 /** 270 * @scheduled: this fence is what will be signaled by the scheduler 271 * when the job is scheduled. 272 */ 273 struct dma_fence scheduled; 274 275 /** 276 * @finished: this fence is what will be signaled by the scheduler 277 * when the job is completed. 278 * 279 * When setting up an out fence for the job, you should use 280 * this, since it's available immediately upon 281 * drm_sched_job_init(), and the fence returned by the driver 282 * from run_job() won't be created until the dependencies have 283 * resolved. 284 */ 285 struct dma_fence finished; 286 287 /** 288 * @deadline: deadline set on &drm_sched_fence.finished which 289 * potentially needs to be propagated to &drm_sched_fence.parent 290 */ 291 ktime_t deadline; 292 293 /** 294 * @parent: the fence returned by &drm_sched_backend_ops.run_job 295 * when scheduling the job on hardware. We signal the 296 * &drm_sched_fence.finished fence once parent is signalled. 297 */ 298 struct dma_fence *parent; 299 /** 300 * @sched: the scheduler instance to which the job having this struct 301 * belongs to. 302 */ 303 struct drm_gpu_scheduler *sched; 304 /** 305 * @lock: the lock used by the scheduled and the finished fences. 306 */ 307 spinlock_t lock; 308 /** 309 * @owner: job owner for debugging 310 */ 311 void *owner; 312 }; 313 314 struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f); 315 316 /** 317 * struct drm_sched_job - A job to be run by an entity. 318 * 319 * @queue_node: used to append this struct to the queue of jobs in an entity. 320 * @list: a job participates in a "pending" and "done" lists. 321 * @sched: the scheduler instance on which this job is scheduled. 322 * @s_fence: contains the fences for the scheduling of job. 323 * @finish_cb: the callback for the finished fence. 324 * @credits: the number of credits this job contributes to the scheduler 325 * @work: Helper to reschdeule job kill to different context. 326 * @id: a unique id assigned to each job scheduled on the scheduler. 327 * @karma: increment on every hang caused by this job. If this exceeds the hang 328 * limit of the scheduler then the job is marked guilty and will not 329 * be scheduled further. 330 * @s_priority: the priority of the job. 331 * @entity: the entity to which this job belongs. 332 * @cb: the callback for the parent fence in s_fence. 333 * 334 * A job is created by the driver using drm_sched_job_init(), and 335 * should call drm_sched_entity_push_job() once it wants the scheduler 336 * to schedule the job. 337 */ 338 struct drm_sched_job { 339 struct spsc_node queue_node; 340 struct list_head list; 341 struct drm_gpu_scheduler *sched; 342 struct drm_sched_fence *s_fence; 343 344 u32 credits; 345 346 /* 347 * work is used only after finish_cb has been used and will not be 348 * accessed anymore. 349 */ 350 union { 351 struct dma_fence_cb finish_cb; 352 struct work_struct work; 353 }; 354 355 uint64_t id; 356 atomic_t karma; 357 enum drm_sched_priority s_priority; 358 struct drm_sched_entity *entity; 359 struct dma_fence_cb cb; 360 /** 361 * @dependencies: 362 * 363 * Contains the dependencies as struct dma_fence for this job, see 364 * drm_sched_job_add_dependency() and 365 * drm_sched_job_add_implicit_dependencies(). 366 */ 367 struct xarray dependencies; 368 369 /** @last_dependency: tracks @dependencies as they signal */ 370 unsigned long last_dependency; 371 372 /** 373 * @submit_ts: 374 * 375 * When the job was pushed into the entity queue. 376 */ 377 ktime_t submit_ts; 378 }; 379 380 static inline bool drm_sched_invalidate_job(struct drm_sched_job *s_job, 381 int threshold) 382 { 383 return s_job && atomic_inc_return(&s_job->karma) > threshold; 384 } 385 386 enum drm_gpu_sched_stat { 387 DRM_GPU_SCHED_STAT_NONE, /* Reserve 0 */ 388 DRM_GPU_SCHED_STAT_NOMINAL, 389 DRM_GPU_SCHED_STAT_ENODEV, 390 }; 391 392 /** 393 * struct drm_sched_backend_ops - Define the backend operations 394 * called by the scheduler 395 * 396 * These functions should be implemented in the driver side. 397 */ 398 struct drm_sched_backend_ops { 399 /** 400 * @prepare_job: 401 * 402 * Called when the scheduler is considering scheduling this job next, to 403 * get another struct dma_fence for this job to block on. Once it 404 * returns NULL, run_job() may be called. 405 * 406 * Can be NULL if no additional preparation to the dependencies are 407 * necessary. Skipped when jobs are killed instead of run. 408 */ 409 struct dma_fence *(*prepare_job)(struct drm_sched_job *sched_job, 410 struct drm_sched_entity *s_entity); 411 412 /** 413 * @run_job: Called to execute the job once all of the dependencies 414 * have been resolved. This may be called multiple times, if 415 * timedout_job() has happened and drm_sched_job_recovery() 416 * decides to try it again. 417 */ 418 struct dma_fence *(*run_job)(struct drm_sched_job *sched_job); 419 420 /** 421 * @timedout_job: Called when a job has taken too long to execute, 422 * to trigger GPU recovery. 423 * 424 * This method is called in a workqueue context. 425 * 426 * Drivers typically issue a reset to recover from GPU hangs, and this 427 * procedure usually follows the following workflow: 428 * 429 * 1. Stop the scheduler using drm_sched_stop(). This will park the 430 * scheduler thread and cancel the timeout work, guaranteeing that 431 * nothing is queued while we reset the hardware queue 432 * 2. Try to gracefully stop non-faulty jobs (optional) 433 * 3. Issue a GPU reset (driver-specific) 434 * 4. Re-submit jobs using drm_sched_resubmit_jobs() 435 * 5. Restart the scheduler using drm_sched_start(). At that point, new 436 * jobs can be queued, and the scheduler thread is unblocked 437 * 438 * Note that some GPUs have distinct hardware queues but need to reset 439 * the GPU globally, which requires extra synchronization between the 440 * timeout handler of the different &drm_gpu_scheduler. One way to 441 * achieve this synchronization is to create an ordered workqueue 442 * (using alloc_ordered_workqueue()) at the driver level, and pass this 443 * queue to drm_sched_init(), to guarantee that timeout handlers are 444 * executed sequentially. The above workflow needs to be slightly 445 * adjusted in that case: 446 * 447 * 1. Stop all schedulers impacted by the reset using drm_sched_stop() 448 * 2. Try to gracefully stop non-faulty jobs on all queues impacted by 449 * the reset (optional) 450 * 3. Issue a GPU reset on all faulty queues (driver-specific) 451 * 4. Re-submit jobs on all schedulers impacted by the reset using 452 * drm_sched_resubmit_jobs() 453 * 5. Restart all schedulers that were stopped in step #1 using 454 * drm_sched_start() 455 * 456 * Return DRM_GPU_SCHED_STAT_NOMINAL, when all is normal, 457 * and the underlying driver has started or completed recovery. 458 * 459 * Return DRM_GPU_SCHED_STAT_ENODEV, if the device is no longer 460 * available, i.e. has been unplugged. 461 */ 462 enum drm_gpu_sched_stat (*timedout_job)(struct drm_sched_job *sched_job); 463 464 /** 465 * @free_job: Called once the job's finished fence has been signaled 466 * and it's time to clean it up. 467 */ 468 void (*free_job)(struct drm_sched_job *sched_job); 469 470 /** 471 * @update_job_credits: Called when the scheduler is considering this 472 * job for execution. 473 * 474 * This callback returns the number of credits the job would take if 475 * pushed to the hardware. Drivers may use this to dynamically update 476 * the job's credit count. For instance, deduct the number of credits 477 * for already signalled native fences. 478 * 479 * This callback is optional. 480 */ 481 u32 (*update_job_credits)(struct drm_sched_job *sched_job); 482 }; 483 484 /** 485 * struct drm_gpu_scheduler - scheduler instance-specific data 486 * 487 * @ops: backend operations provided by the driver. 488 * @credit_limit: the credit limit of this scheduler 489 * @credit_count: the current credit count of this scheduler 490 * @timeout: the time after which a job is removed from the scheduler. 491 * @name: name of the ring for which this scheduler is being used. 492 * @num_rqs: Number of run-queues. This is at most DRM_SCHED_PRIORITY_COUNT, 493 * as there's usually one run-queue per priority, but could be less. 494 * @sched_rq: An allocated array of run-queues of size @num_rqs; 495 * @job_scheduled: once @drm_sched_entity_do_release is called the scheduler 496 * waits on this wait queue until all the scheduled jobs are 497 * finished. 498 * @job_id_count: used to assign unique id to the each job. 499 * @submit_wq: workqueue used to queue @work_run_job and @work_free_job 500 * @timeout_wq: workqueue used to queue @work_tdr 501 * @work_run_job: work which calls run_job op of each scheduler. 502 * @work_free_job: work which calls free_job op of each scheduler. 503 * @work_tdr: schedules a delayed call to @drm_sched_job_timedout after the 504 * timeout interval is over. 505 * @pending_list: the list of jobs which are currently in the job queue. 506 * @job_list_lock: lock to protect the pending_list. 507 * @hang_limit: once the hangs by a job crosses this limit then it is marked 508 * guilty and it will no longer be considered for scheduling. 509 * @score: score to help loadbalancer pick a idle sched 510 * @_score: score used when the driver doesn't provide one 511 * @ready: marks if the underlying HW is ready to work 512 * @free_guilty: A hit to time out handler to free the guilty job. 513 * @pause_submit: pause queuing of @work_run_job on @submit_wq 514 * @own_submit_wq: scheduler owns allocation of @submit_wq 515 * @dev: system &struct device 516 * 517 * One scheduler is implemented for each hardware ring. 518 */ 519 struct drm_gpu_scheduler { 520 const struct drm_sched_backend_ops *ops; 521 u32 credit_limit; 522 atomic_t credit_count; 523 long timeout; 524 const char *name; 525 u32 num_rqs; 526 struct drm_sched_rq **sched_rq; 527 wait_queue_head_t job_scheduled; 528 atomic64_t job_id_count; 529 struct workqueue_struct *submit_wq; 530 struct workqueue_struct *timeout_wq; 531 struct work_struct work_run_job; 532 struct work_struct work_free_job; 533 struct delayed_work work_tdr; 534 struct list_head pending_list; 535 spinlock_t job_list_lock; 536 int hang_limit; 537 atomic_t *score; 538 atomic_t _score; 539 bool ready; 540 bool free_guilty; 541 bool pause_submit; 542 bool own_submit_wq; 543 struct device *dev; 544 }; 545 546 int drm_sched_init(struct drm_gpu_scheduler *sched, 547 const struct drm_sched_backend_ops *ops, 548 struct workqueue_struct *submit_wq, 549 u32 num_rqs, u32 credit_limit, unsigned int hang_limit, 550 long timeout, struct workqueue_struct *timeout_wq, 551 atomic_t *score, const char *name, struct device *dev); 552 553 void drm_sched_fini(struct drm_gpu_scheduler *sched); 554 int drm_sched_job_init(struct drm_sched_job *job, 555 struct drm_sched_entity *entity, 556 u32 credits, void *owner); 557 void drm_sched_job_arm(struct drm_sched_job *job); 558 int drm_sched_job_add_dependency(struct drm_sched_job *job, 559 struct dma_fence *fence); 560 int drm_sched_job_add_syncobj_dependency(struct drm_sched_job *job, 561 struct drm_file *file, 562 u32 handle, 563 u32 point); 564 int drm_sched_job_add_resv_dependencies(struct drm_sched_job *job, 565 struct dma_resv *resv, 566 enum dma_resv_usage usage); 567 int drm_sched_job_add_implicit_dependencies(struct drm_sched_job *job, 568 struct drm_gem_object *obj, 569 bool write); 570 571 572 void drm_sched_entity_modify_sched(struct drm_sched_entity *entity, 573 struct drm_gpu_scheduler **sched_list, 574 unsigned int num_sched_list); 575 576 void drm_sched_tdr_queue_imm(struct drm_gpu_scheduler *sched); 577 void drm_sched_job_cleanup(struct drm_sched_job *job); 578 void drm_sched_wakeup(struct drm_gpu_scheduler *sched, struct drm_sched_entity *entity); 579 bool drm_sched_wqueue_ready(struct drm_gpu_scheduler *sched); 580 void drm_sched_wqueue_stop(struct drm_gpu_scheduler *sched); 581 void drm_sched_wqueue_start(struct drm_gpu_scheduler *sched); 582 void drm_sched_stop(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad); 583 void drm_sched_start(struct drm_gpu_scheduler *sched, bool full_recovery); 584 void drm_sched_resubmit_jobs(struct drm_gpu_scheduler *sched); 585 void drm_sched_increase_karma(struct drm_sched_job *bad); 586 void drm_sched_reset_karma(struct drm_sched_job *bad); 587 void drm_sched_increase_karma_ext(struct drm_sched_job *bad, int type); 588 bool drm_sched_dependency_optimized(struct dma_fence* fence, 589 struct drm_sched_entity *entity); 590 void drm_sched_fault(struct drm_gpu_scheduler *sched); 591 592 void drm_sched_rq_add_entity(struct drm_sched_rq *rq, 593 struct drm_sched_entity *entity); 594 void drm_sched_rq_remove_entity(struct drm_sched_rq *rq, 595 struct drm_sched_entity *entity); 596 597 void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts); 598 599 int drm_sched_entity_init(struct drm_sched_entity *entity, 600 enum drm_sched_priority priority, 601 struct drm_gpu_scheduler **sched_list, 602 unsigned int num_sched_list, 603 atomic_t *guilty); 604 long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout); 605 void drm_sched_entity_fini(struct drm_sched_entity *entity); 606 void drm_sched_entity_destroy(struct drm_sched_entity *entity); 607 void drm_sched_entity_select_rq(struct drm_sched_entity *entity); 608 struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity); 609 void drm_sched_entity_push_job(struct drm_sched_job *sched_job); 610 void drm_sched_entity_set_priority(struct drm_sched_entity *entity, 611 enum drm_sched_priority priority); 612 bool drm_sched_entity_is_ready(struct drm_sched_entity *entity); 613 int drm_sched_entity_error(struct drm_sched_entity *entity); 614 615 struct drm_sched_fence *drm_sched_fence_alloc( 616 struct drm_sched_entity *s_entity, void *owner); 617 void drm_sched_fence_init(struct drm_sched_fence *fence, 618 struct drm_sched_entity *entity); 619 void drm_sched_fence_free(struct drm_sched_fence *fence); 620 621 void drm_sched_fence_scheduled(struct drm_sched_fence *fence, 622 struct dma_fence *parent); 623 void drm_sched_fence_finished(struct drm_sched_fence *fence, int result); 624 625 unsigned long drm_sched_suspend_timeout(struct drm_gpu_scheduler *sched); 626 void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched, 627 unsigned long remaining); 628 struct drm_gpu_scheduler * 629 drm_sched_pick_best(struct drm_gpu_scheduler **sched_list, 630 unsigned int num_sched_list); 631 632 #endif 633