at commit c03c1a14
| 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 | rmb(); /* for list_empty to work without lock */ |
| 211 | |
| 212 | if (list_empty(&entity->list) || |
| 213 | spsc_queue_count(&entity->job_queue) == 0 || |
| 214 | entity->stopped) |
| 215 | return true; |
| 216 | |
| 217 | return false; |
| 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 | rcu_read_lock(); |
| 233 | fence = rcu_dereference(entity->last_scheduled); |
| 234 | r = fence ? fence->error : 0; |
| 235 | rcu_read_unlock(); |
| 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 | /* The entity is guaranteed to not be used by the scheduler */ |
| 319 | prev = rcu_dereference_check(entity->last_scheduled, true); |
| 320 | dma_fence_get(prev); |
| 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(rcu_dereference_check(entity->last_scheduled, true)); |
| 413 | RCU_INIT_POINTER(entity->last_scheduled, NULL); |
| 414 | drm_sched_entity_stats_put(entity->stats); |
| 415 | } |
| 416 | EXPORT_SYMBOL(drm_sched_entity_fini); |
| 417 | |
| 418 | /** |
| 419 | * drm_sched_entity_destroy - Destroy a context entity |
| 420 | * @entity: scheduler entity |
| 421 | * |
| 422 | * Calls drm_sched_entity_flush() and drm_sched_entity_fini() as a |
| 423 | * convenience wrapper. |
| 424 | */ |
| 425 | void drm_sched_entity_destroy(struct drm_sched_entity *entity) |
| 426 | { |
| 427 | drm_sched_entity_flush(entity, MAX_WAIT_SCHED_ENTITY_Q_EMPTY); |
| 428 | drm_sched_entity_fini(entity); |
| 429 | } |
| 430 | EXPORT_SYMBOL(drm_sched_entity_destroy); |
| 431 | |
| 432 | /* |
| 433 | * drm_sched_entity_wakeup - callback to clear the entity's dependency and |
| 434 | * wake up the scheduler |
| 435 | */ |
| 436 | static void drm_sched_entity_wakeup(struct dma_fence *f, |
| 437 | struct dma_fence_cb *cb) |
| 438 | { |
| 439 | struct drm_sched_entity *entity = |
| 440 | container_of(cb, struct drm_sched_entity, cb); |
| 441 | |
| 442 | entity->dependency = NULL; |
| 443 | dma_fence_put(f); |
| 444 | drm_sched_wakeup(entity->rq->sched); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * drm_sched_entity_set_priority - Sets priority of the entity |
| 449 | * |
| 450 | * @entity: scheduler entity |
| 451 | * @priority: scheduler priority |
| 452 | * |
| 453 | * Update the priority of runqueues used for the entity. |
| 454 | */ |
| 455 | void drm_sched_entity_set_priority(struct drm_sched_entity *entity, |
| 456 | enum drm_sched_priority priority) |
| 457 | { |
| 458 | spin_lock(&entity->lock); |
| 459 | entity->priority = priority; |
| 460 | spin_unlock(&entity->lock); |
| 461 | } |
| 462 | EXPORT_SYMBOL(drm_sched_entity_set_priority); |
| 463 | |
| 464 | /* |
| 465 | * Add a callback to the current dependency of the entity to wake up the |
| 466 | * scheduler when the entity becomes available. |
| 467 | */ |
| 468 | static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity, |
| 469 | struct drm_sched_job *sched_job) |
| 470 | { |
| 471 | struct drm_gpu_scheduler *sched = entity->rq->sched; |
| 472 | struct dma_fence *fence = entity->dependency; |
| 473 | struct drm_sched_fence *s_fence; |
| 474 | |
| 475 | if (fence->context == entity->fence_context || |
| 476 | fence->context == entity->fence_context + 1) { |
| 477 | /* |
| 478 | * Fence is a scheduled/finished fence from a job |
| 479 | * which belongs to the same entity, we can ignore |
| 480 | * fences from ourself |
| 481 | */ |
| 482 | dma_fence_put(entity->dependency); |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | s_fence = to_drm_sched_fence(fence); |
| 487 | if (!fence->error && s_fence && s_fence->sched == sched && |
| 488 | !test_bit(DRM_SCHED_FENCE_DONT_PIPELINE, &fence->flags)) { |
| 489 | |
| 490 | /* |
| 491 | * Fence is from the same scheduler, only need to wait for |
| 492 | * it to be scheduled |
| 493 | */ |
| 494 | fence = dma_fence_get(&s_fence->scheduled); |
| 495 | dma_fence_put(entity->dependency); |
| 496 | entity->dependency = fence; |
| 497 | } |
| 498 | |
| 499 | if (trace_drm_sched_job_unschedulable_enabled() && |
| 500 | !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &entity->dependency->flags)) |
| 501 | trace_drm_sched_job_unschedulable(sched_job, entity->dependency); |
| 502 | |
| 503 | if (!dma_fence_add_callback(entity->dependency, &entity->cb, |
| 504 | drm_sched_entity_wakeup)) |
| 505 | return true; |
| 506 | |
| 507 | dma_fence_put(entity->dependency); |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | static struct dma_fence * |
| 512 | drm_sched_job_dependency(struct drm_sched_job *job, |
| 513 | struct drm_sched_entity *entity) |
| 514 | { |
| 515 | struct dma_fence *f; |
| 516 | |
| 517 | /* We keep the fence around, so we can iterate over all dependencies |
| 518 | * in drm_sched_entity_kill_jobs_cb() to ensure all deps are signaled |
| 519 | * before killing the job. |
| 520 | */ |
| 521 | f = xa_load(&job->dependencies, job->last_dependency); |
| 522 | if (f) { |
| 523 | job->last_dependency++; |
| 524 | return dma_fence_get(f); |
| 525 | } |
| 526 | |
| 527 | if (job->sched->ops->prepare_job) |
| 528 | return job->sched->ops->prepare_job(job, entity); |
| 529 | |
| 530 | return NULL; |
| 531 | } |
| 532 | |
| 533 | struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity) |
| 534 | { |
| 535 | struct drm_sched_job *sched_job; |
| 536 | |
| 537 | sched_job = drm_sched_entity_queue_peek(entity); |
| 538 | if (!sched_job) |
| 539 | return NULL; |
| 540 | |
| 541 | while ((entity->dependency = |
| 542 | drm_sched_job_dependency(sched_job, entity))) { |
| 543 | if (drm_sched_entity_add_dependency_cb(entity, sched_job)) |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | /* skip jobs from entity that marked guilty */ |
| 548 | if (entity->guilty && atomic_read(entity->guilty)) |
| 549 | dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED); |
| 550 | |
| 551 | dma_fence_put(rcu_dereference_check(entity->last_scheduled, true)); |
| 552 | rcu_assign_pointer(entity->last_scheduled, |
| 553 | dma_fence_get(&sched_job->s_fence->finished)); |
| 554 | |
| 555 | /* |
| 556 | * If the queue is empty we allow drm_sched_entity_select_rq() to |
| 557 | * locklessly access ->last_scheduled. This only works if we set the |
| 558 | * pointer before we dequeue and if we a write barrier here. |
| 559 | */ |
| 560 | smp_wmb(); |
| 561 | |
| 562 | spin_lock(&entity->lock); |
| 563 | spsc_queue_pop(&entity->job_queue); |
| 564 | drm_sched_rq_pop_entity(entity); |
| 565 | spin_unlock(&entity->lock); |
| 566 | |
| 567 | /* Jobs and entities might have different lifecycles. Since we're |
| 568 | * removing the job from the entities queue, set the jobs entity pointer |
| 569 | * to NULL to prevent any future access of the entity through this job. |
| 570 | */ |
| 571 | sched_job->entity = NULL; |
| 572 | |
| 573 | return sched_job; |
| 574 | } |
| 575 | |
| 576 | void drm_sched_entity_select_rq(struct drm_sched_entity *entity) |
| 577 | { |
| 578 | struct dma_fence *fence; |
| 579 | struct drm_gpu_scheduler *sched; |
| 580 | struct drm_sched_rq *rq; |
| 581 | |
| 582 | /* single possible engine and already selected */ |
| 583 | if (!entity->sched_list) |
| 584 | return; |
| 585 | |
| 586 | /* queue non-empty, stay on the same engine */ |
| 587 | if (spsc_queue_count(&entity->job_queue)) |
| 588 | return; |
| 589 | |
| 590 | /* |
| 591 | * Only when the queue is empty are we guaranteed that |
| 592 | * drm_sched_run_job_work() cannot change entity->last_scheduled. To |
| 593 | * enforce ordering we need a read barrier here. See |
| 594 | * drm_sched_entity_pop_job() for the other side. |
| 595 | */ |
| 596 | smp_rmb(); |
| 597 | |
| 598 | fence = rcu_dereference_check(entity->last_scheduled, true); |
| 599 | |
| 600 | /* stay on the same engine if the previous job hasn't finished */ |
| 601 | if (fence && !dma_fence_is_signaled(fence)) |
| 602 | return; |
| 603 | |
| 604 | spin_lock(&entity->lock); |
| 605 | sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list); |
| 606 | rq = sched ? sched->sched_rq[entity->rq_priority] : NULL; |
| 607 | if (rq != entity->rq) { |
| 608 | drm_sched_rq_remove_entity(entity->rq, entity); |
| 609 | entity->rq = rq; |
| 610 | } |
| 611 | |
| 612 | if (entity->num_sched_list == 1) |
| 613 | entity->sched_list = NULL; |
| 614 | |
| 615 | spin_unlock(&entity->lock); |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * drm_sched_entity_push_job - Submit a job to the entity's job queue |
| 620 | * @sched_job: job to submit |
| 621 | * |
| 622 | * Note: To guarantee that the order of insertion to queue matches the job's |
| 623 | * fence sequence number this function should be called with drm_sched_job_arm() |
| 624 | * under common lock for the struct drm_sched_entity that was set up for |
| 625 | * @sched_job in drm_sched_job_init(). |
| 626 | */ |
| 627 | void drm_sched_entity_push_job(struct drm_sched_job *sched_job) |
| 628 | { |
| 629 | struct drm_sched_entity *entity = sched_job->entity; |
| 630 | bool first; |
| 631 | ktime_t submit_ts; |
| 632 | |
| 633 | trace_drm_sched_job_queue(sched_job, entity); |
| 634 | |
| 635 | if (trace_drm_sched_job_add_dep_enabled()) { |
| 636 | struct dma_fence *entry; |
| 637 | unsigned long index; |
| 638 | |
| 639 | xa_for_each(&sched_job->dependencies, index, entry) |
| 640 | trace_drm_sched_job_add_dep(sched_job, entry); |
| 641 | } |
| 642 | atomic_inc(entity->rq->sched->score); |
| 643 | WRITE_ONCE(entity->last_user, current->group_leader); |
| 644 | |
| 645 | /* |
| 646 | * After the sched_job is pushed into the entity queue, it may be |
| 647 | * completed and freed up at any time. We can no longer access it. |
| 648 | * Make sure to set the submit_ts first, to avoid a race. |
| 649 | */ |
| 650 | sched_job->submit_ts = submit_ts = ktime_get(); |
| 651 | |
| 652 | spin_lock(&entity->lock); |
| 653 | |
| 654 | first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node); |
| 655 | |
| 656 | /* first job wakes up scheduler */ |
| 657 | if (first) { |
| 658 | struct drm_gpu_scheduler *sched; |
| 659 | |
| 660 | sched = drm_sched_rq_add_entity(entity, submit_ts); |
| 661 | if (sched) |
| 662 | drm_sched_wakeup(sched); |
| 663 | } |
| 664 | |
| 665 | spin_unlock(&entity->lock); |
| 666 | } |
| 667 | EXPORT_SYMBOL(drm_sched_entity_push_job); |