1 // SPDX-License-Identifier: GPL-2.0-only OR MIT 2 /* Copyright (c) 2023 Imagination Technologies Ltd. */ 3 4 #include <drm/drm_managed.h> 5 #include <drm/gpu_scheduler.h> 6 #include <linux/overflow.h> 7 8 #include "pvr_cccb.h" 9 #include "pvr_context.h" 10 #include "pvr_device.h" 11 #include "pvr_drv.h" 12 #include "pvr_job.h" 13 #include "pvr_queue.h" 14 #include "pvr_trace.h" 15 #include "pvr_vm.h" 16 17 #include "pvr_rogue_fwif_client.h" 18 19 #define MAX_DEADLINE_MS 30000 20 21 #define CTX_COMPUTE_CCCB_SIZE_LOG2 15 22 #define CTX_FRAG_CCCB_SIZE_LOG2 15 23 #define CTX_GEOM_CCCB_SIZE_LOG2 15 24 #define CTX_TRANSFER_CCCB_SIZE_LOG2 15 25 26 static int get_xfer_ctx_state_size(struct pvr_device *pvr_dev) 27 { 28 u32 num_isp_store_registers; 29 30 if (PVR_HAS_FEATURE(pvr_dev, xe_memory_hierarchy)) { 31 num_isp_store_registers = 1; 32 } else { 33 int err; 34 35 err = PVR_FEATURE_VALUE(pvr_dev, num_isp_ipp_pipes, &num_isp_store_registers); 36 if (WARN_ON(err)) 37 return err; 38 } 39 40 return struct_size_t(struct rogue_fwif_frag_ctx_state, 41 frag_reg_isp_store, num_isp_store_registers); 42 } 43 44 static int get_frag_ctx_state_size(struct pvr_device *pvr_dev) 45 { 46 u32 num_isp_store_registers; 47 int err; 48 49 if (PVR_HAS_FEATURE(pvr_dev, xe_memory_hierarchy)) { 50 err = PVR_FEATURE_VALUE(pvr_dev, num_raster_pipes, &num_isp_store_registers); 51 if (WARN_ON(err)) 52 return err; 53 54 if (PVR_HAS_FEATURE(pvr_dev, gpu_multicore_support)) { 55 u32 xpu_max_slaves; 56 57 err = PVR_FEATURE_VALUE(pvr_dev, xpu_max_slaves, &xpu_max_slaves); 58 if (WARN_ON(err)) 59 return err; 60 61 num_isp_store_registers *= (1 + xpu_max_slaves); 62 } 63 } else { 64 err = PVR_FEATURE_VALUE(pvr_dev, num_isp_ipp_pipes, &num_isp_store_registers); 65 if (WARN_ON(err)) 66 return err; 67 } 68 69 return struct_size_t(struct rogue_fwif_frag_ctx_state, 70 frag_reg_isp_store, num_isp_store_registers); 71 } 72 73 static int get_ctx_state_size(struct pvr_device *pvr_dev, enum drm_pvr_job_type type) 74 { 75 switch (type) { 76 case DRM_PVR_JOB_TYPE_GEOMETRY: 77 return sizeof(struct rogue_fwif_geom_ctx_state); 78 case DRM_PVR_JOB_TYPE_FRAGMENT: 79 return get_frag_ctx_state_size(pvr_dev); 80 case DRM_PVR_JOB_TYPE_COMPUTE: 81 return sizeof(struct rogue_fwif_compute_ctx_state); 82 case DRM_PVR_JOB_TYPE_TRANSFER_FRAG: 83 return get_xfer_ctx_state_size(pvr_dev); 84 } 85 86 WARN(1, "Invalid queue type"); 87 return -EINVAL; 88 } 89 90 static u32 get_ctx_offset(enum drm_pvr_job_type type) 91 { 92 switch (type) { 93 case DRM_PVR_JOB_TYPE_GEOMETRY: 94 return offsetof(struct rogue_fwif_fwrendercontext, geom_context); 95 case DRM_PVR_JOB_TYPE_FRAGMENT: 96 return offsetof(struct rogue_fwif_fwrendercontext, frag_context); 97 case DRM_PVR_JOB_TYPE_COMPUTE: 98 return offsetof(struct rogue_fwif_fwcomputecontext, cdm_context); 99 case DRM_PVR_JOB_TYPE_TRANSFER_FRAG: 100 return offsetof(struct rogue_fwif_fwtransfercontext, tq_context); 101 } 102 103 return 0; 104 } 105 106 static const char * 107 pvr_queue_fence_get_driver_name(struct dma_fence *f) 108 { 109 return PVR_DRIVER_NAME; 110 } 111 112 static void pvr_queue_fence_release_work(struct work_struct *w) 113 { 114 struct pvr_queue_fence *fence = container_of(w, struct pvr_queue_fence, release_work); 115 116 pvr_context_put(fence->queue->ctx); 117 dma_fence_free(&fence->base); 118 } 119 120 static void pvr_queue_fence_release(struct dma_fence *f) 121 { 122 struct pvr_queue_fence *fence = container_of(f, struct pvr_queue_fence, base); 123 struct pvr_device *pvr_dev = fence->queue->ctx->pvr_dev; 124 125 queue_work(pvr_dev->sched_wq, &fence->release_work); 126 } 127 128 static const char * 129 pvr_queue_job_fence_get_timeline_name(struct dma_fence *f) 130 { 131 struct pvr_queue_fence *fence = container_of(f, struct pvr_queue_fence, base); 132 133 switch (fence->queue->type) { 134 case DRM_PVR_JOB_TYPE_GEOMETRY: 135 return "geometry"; 136 137 case DRM_PVR_JOB_TYPE_FRAGMENT: 138 return "fragment"; 139 140 case DRM_PVR_JOB_TYPE_COMPUTE: 141 return "compute"; 142 143 case DRM_PVR_JOB_TYPE_TRANSFER_FRAG: 144 return "transfer"; 145 } 146 147 WARN(1, "Invalid queue type"); 148 return "invalid"; 149 } 150 151 static const char * 152 pvr_queue_cccb_fence_get_timeline_name(struct dma_fence *f) 153 { 154 struct pvr_queue_fence *fence = container_of(f, struct pvr_queue_fence, base); 155 156 switch (fence->queue->type) { 157 case DRM_PVR_JOB_TYPE_GEOMETRY: 158 return "geometry-cccb"; 159 160 case DRM_PVR_JOB_TYPE_FRAGMENT: 161 return "fragment-cccb"; 162 163 case DRM_PVR_JOB_TYPE_COMPUTE: 164 return "compute-cccb"; 165 166 case DRM_PVR_JOB_TYPE_TRANSFER_FRAG: 167 return "transfer-cccb"; 168 } 169 170 WARN(1, "Invalid queue type"); 171 return "invalid"; 172 } 173 174 static const struct dma_fence_ops pvr_queue_job_fence_ops = { 175 .get_driver_name = pvr_queue_fence_get_driver_name, 176 .get_timeline_name = pvr_queue_job_fence_get_timeline_name, 177 .release = pvr_queue_fence_release, 178 }; 179 180 /** 181 * pvr_queue_fence_is_ufo_backed() - Check if a dma_fence is backed by a UFO. 182 * @f: The dma_fence to check. 183 * 184 * Return: 185 * * true if the dma_fence is backed by a UFO, or 186 * * false otherwise. 187 */ 188 static inline bool 189 pvr_queue_fence_is_ufo_backed(struct dma_fence *f) 190 { 191 /* 192 * Currently the only dma_fence backed by a UFO object is the job fence, 193 * e.g. pvr_job::done_fence, wrapped by a pvr_queue_fence object. 194 */ 195 return f && f->ops == &pvr_queue_job_fence_ops; 196 } 197 198 /** 199 * to_pvr_queue_job_fence() - Return a pvr_queue_fence object if the fence is 200 * already backed by a UFO. 201 * @f: The dma_fence to turn into a pvr_queue_fence. 202 * 203 * This could be called on: 204 * - a job fence directly, in which case it simply returns the containing pvr_queue_fence; 205 * - a drm_sched_fence's scheduled or finished fence, in which case it will first try to follow 206 * the parent pointer to find the job fence (note that the parent pointer is initialized 207 * only after the run_job() callback is called on the drm_sched_fence's owning job); 208 * - any other dma_fence, in which case it will return NULL. 209 * 210 * Return: 211 * * A non-NULL pvr_queue_fence object if the dma_fence is backed by a UFO, or 212 * * NULL otherwise. 213 */ 214 static struct pvr_queue_fence * 215 to_pvr_queue_job_fence(struct dma_fence *f) 216 { 217 struct drm_sched_fence *sched_fence = to_drm_sched_fence(f); 218 219 if (sched_fence) 220 f = sched_fence->parent; 221 222 if (pvr_queue_fence_is_ufo_backed(f)) 223 return container_of(f, struct pvr_queue_fence, base); 224 225 return NULL; 226 } 227 228 static const struct dma_fence_ops pvr_queue_cccb_fence_ops = { 229 .get_driver_name = pvr_queue_fence_get_driver_name, 230 .get_timeline_name = pvr_queue_cccb_fence_get_timeline_name, 231 .release = pvr_queue_fence_release, 232 }; 233 234 /** 235 * pvr_queue_fence_put() - Put wrapper for pvr_queue_fence objects. 236 * @f: The dma_fence object to put. 237 * 238 * If the pvr_queue_fence has been initialized, we call dma_fence_put(), 239 * otherwise we free the object with dma_fence_free(). This allows us 240 * to do the right thing before and after pvr_queue_fence_init() had been 241 * called. 242 */ 243 static void pvr_queue_fence_put(struct dma_fence *f) 244 { 245 if (!f) 246 return; 247 248 if (WARN_ON(f->ops && 249 f->ops != &pvr_queue_cccb_fence_ops && 250 f->ops != &pvr_queue_job_fence_ops)) 251 return; 252 253 /* If the fence hasn't been initialized yet, free the object directly. */ 254 if (f->ops) 255 dma_fence_put(f); 256 else 257 dma_fence_free(f); 258 } 259 260 /** 261 * pvr_queue_fence_alloc() - Allocate a pvr_queue_fence fence object 262 * 263 * Call this function to allocate job CCCB and done fences. This only 264 * allocates the objects. Initialization happens when the underlying 265 * dma_fence object is to be returned to drm_sched (in prepare_job() or 266 * run_job()). 267 * 268 * Return: 269 * * A valid pointer if the allocation succeeds, or 270 * * NULL if the allocation fails. 271 */ 272 static struct dma_fence * 273 pvr_queue_fence_alloc(void) 274 { 275 struct pvr_queue_fence *fence; 276 277 fence = kzalloc_obj(*fence); 278 if (!fence) 279 return NULL; 280 281 return &fence->base; 282 } 283 284 /** 285 * pvr_queue_fence_init() - Initializes a pvr_queue_fence object. 286 * @f: The fence to initialize 287 * @queue: The queue this fence belongs to. 288 * @fence_ops: The fence operations. 289 * @fence_ctx: The fence context. 290 * 291 * Wrapper around dma_fence_init() that takes care of initializing the 292 * pvr_queue_fence::queue field too. 293 */ 294 static void 295 pvr_queue_fence_init(struct dma_fence *f, 296 struct pvr_queue *queue, 297 const struct dma_fence_ops *fence_ops, 298 struct pvr_queue_fence_ctx *fence_ctx) 299 { 300 struct pvr_queue_fence *fence = container_of(f, struct pvr_queue_fence, base); 301 302 pvr_context_get(queue->ctx); 303 fence->queue = queue; 304 INIT_WORK(&fence->release_work, pvr_queue_fence_release_work); 305 dma_fence_init(&fence->base, fence_ops, 306 &fence_ctx->lock, fence_ctx->id, 307 atomic_inc_return(&fence_ctx->seqno)); 308 } 309 310 /** 311 * pvr_queue_cccb_fence_init() - Initializes a CCCB fence object. 312 * @fence: The fence to initialize. 313 * @queue: The queue this fence belongs to. 314 * 315 * Initializes a fence that can be used to wait for CCCB space. 316 * 317 * Should be called in the ::prepare_job() path, so the fence returned to 318 * drm_sched is valid. 319 */ 320 static void 321 pvr_queue_cccb_fence_init(struct dma_fence *fence, struct pvr_queue *queue) 322 { 323 pvr_queue_fence_init(fence, queue, &pvr_queue_cccb_fence_ops, 324 &queue->cccb_fence_ctx.base); 325 } 326 327 /** 328 * pvr_queue_job_fence_init() - Initializes a job done fence object. 329 * @fence: The fence to initialize. 330 * @queue: The queue this fence belongs to. 331 * 332 * Initializes a fence that will be signaled when the GPU is done executing 333 * a job. 334 * 335 * Should be called *before* the ::run_job() path, so the fence is initialised 336 * before being placed in the pending_list. 337 */ 338 static void 339 pvr_queue_job_fence_init(struct dma_fence *fence, struct pvr_queue *queue) 340 { 341 if (!fence->ops) 342 pvr_queue_fence_init(fence, queue, &pvr_queue_job_fence_ops, 343 &queue->job_fence_ctx); 344 } 345 346 /** 347 * pvr_queue_fence_ctx_init() - Queue fence context initialization. 348 * @fence_ctx: The context to initialize 349 */ 350 static void 351 pvr_queue_fence_ctx_init(struct pvr_queue_fence_ctx *fence_ctx) 352 { 353 spin_lock_init(&fence_ctx->lock); 354 fence_ctx->id = dma_fence_context_alloc(1); 355 atomic_set(&fence_ctx->seqno, 0); 356 } 357 358 static u32 ufo_cmds_size(u32 elem_count) 359 { 360 /* We can pass at most ROGUE_FWIF_CCB_CMD_MAX_UFOS per UFO-related command. */ 361 u32 full_cmd_count = elem_count / ROGUE_FWIF_CCB_CMD_MAX_UFOS; 362 u32 remaining_elems = elem_count % ROGUE_FWIF_CCB_CMD_MAX_UFOS; 363 u32 size = full_cmd_count * 364 pvr_cccb_get_size_of_cmd_with_hdr(ROGUE_FWIF_CCB_CMD_MAX_UFOS * 365 sizeof(struct rogue_fwif_ufo)); 366 367 if (remaining_elems) { 368 size += pvr_cccb_get_size_of_cmd_with_hdr(remaining_elems * 369 sizeof(struct rogue_fwif_ufo)); 370 } 371 372 return size; 373 } 374 375 static u32 job_cmds_size(struct pvr_job *job, u32 ufo_wait_count) 376 { 377 /* 378 * One UFO command per native fence this job will be waiting on (unless any are 379 * signaled by the time the job is submitted), plus a command for the job itself, 380 * plus one UFO command for the fence signaling. 381 */ 382 return ufo_cmds_size(ufo_wait_count) + 383 pvr_cccb_get_size_of_cmd_with_hdr(job->cmd_len) + 384 ufo_cmds_size(1); 385 } 386 387 static bool 388 is_paired_job_fence(struct dma_fence *fence, struct pvr_job *job) 389 { 390 /* This assumes "fence" is one of "job"'s drm_sched_job::dependencies */ 391 return job->type == DRM_PVR_JOB_TYPE_FRAGMENT && 392 job->paired_job && 393 &job->paired_job->base.s_fence->scheduled == fence; 394 } 395 396 /** 397 * job_count_remaining_native_deps() - Count the number of non-signaled native dependencies. 398 * @job: Job to operate on. 399 * 400 * Returns: Number of non-signaled native deps remaining. 401 */ 402 static unsigned long job_count_remaining_native_deps(struct pvr_job *job) 403 { 404 unsigned long remaining_count = 0; 405 struct dma_fence *fence = NULL; 406 unsigned long index; 407 408 xa_for_each(&job->base.dependencies, index, fence) { 409 struct pvr_queue_fence *jfence; 410 411 if (is_paired_job_fence(fence, job)) { 412 /* 413 * A fence between paired jobs won't resolve to a pvr_queue_fence (i.e. 414 * be backed by a UFO) until the jobs have been submitted, together. 415 * The submitting code will insert a partial render fence command for this. 416 */ 417 WARN_ON(dma_fence_is_signaled(fence)); 418 remaining_count++; 419 continue; 420 } 421 422 jfence = to_pvr_queue_job_fence(fence); 423 if (!jfence) 424 continue; 425 426 if (!dma_fence_is_signaled(&jfence->base)) 427 remaining_count++; 428 } 429 430 return remaining_count; 431 } 432 433 /** 434 * pvr_queue_get_job_cccb_fence() - Get the CCCB fence attached to a job. 435 * @queue: The queue this job will be submitted to. 436 * @job: The job to get the CCCB fence on. 437 * 438 * The CCCB fence is a synchronization primitive allowing us to delay job 439 * submission until there's enough space in the CCCB to submit the job. 440 * 441 * Return: 442 * * NULL if there's enough space in the CCCB to submit this job, or 443 * * A valid dma_fence object otherwise. 444 */ 445 static struct dma_fence * 446 pvr_queue_get_job_cccb_fence(struct pvr_queue *queue, struct pvr_job *job) 447 { 448 struct pvr_queue_fence *cccb_fence; 449 unsigned int native_deps_remaining; 450 451 /* If the fence is NULL, that means we already checked that we had 452 * enough space in the cccb for our job. 453 */ 454 if (!job->cccb_fence) 455 return NULL; 456 457 mutex_lock(&queue->cccb_fence_ctx.job_lock); 458 459 /* Count remaining native dependencies and check if the job fits in the CCCB. */ 460 native_deps_remaining = job_count_remaining_native_deps(job); 461 if (pvr_cccb_cmdseq_fits(&queue->cccb, job_cmds_size(job, native_deps_remaining))) { 462 pvr_queue_fence_put(job->cccb_fence); 463 job->cccb_fence = NULL; 464 goto out_unlock; 465 } 466 467 /* There should be no job attached to the CCCB fence context: 468 * drm_sched_entity guarantees that jobs are submitted one at a time. 469 */ 470 if (WARN_ON(queue->cccb_fence_ctx.job)) 471 pvr_job_put(queue->cccb_fence_ctx.job); 472 473 queue->cccb_fence_ctx.job = pvr_job_get(job); 474 475 /* Initialize the fence before returning it. */ 476 cccb_fence = container_of(job->cccb_fence, struct pvr_queue_fence, base); 477 if (!WARN_ON(cccb_fence->queue)) 478 pvr_queue_cccb_fence_init(job->cccb_fence, queue); 479 480 out_unlock: 481 mutex_unlock(&queue->cccb_fence_ctx.job_lock); 482 483 return dma_fence_get(job->cccb_fence); 484 } 485 486 /** 487 * pvr_queue_get_job_kccb_fence() - Get the KCCB fence attached to a job. 488 * @queue: The queue this job will be submitted to. 489 * @job: The job to get the KCCB fence on. 490 * 491 * The KCCB fence is a synchronization primitive allowing us to delay job 492 * submission until there's enough space in the KCCB to submit the job. 493 * 494 * Return: 495 * * NULL if there's enough space in the KCCB to submit this job, or 496 * * A valid dma_fence object otherwise. 497 */ 498 static struct dma_fence * 499 pvr_queue_get_job_kccb_fence(struct pvr_queue *queue, struct pvr_job *job) 500 { 501 struct pvr_device *pvr_dev = queue->ctx->pvr_dev; 502 struct dma_fence *kccb_fence = NULL; 503 504 /* If the fence is NULL, that means we already checked that we had 505 * enough space in the KCCB for our job. 506 */ 507 if (!job->kccb_fence) 508 return NULL; 509 510 if (!WARN_ON(job->kccb_fence->ops)) { 511 kccb_fence = pvr_kccb_reserve_slot(pvr_dev, job->kccb_fence); 512 job->kccb_fence = NULL; 513 } 514 515 return kccb_fence; 516 } 517 518 static struct dma_fence * 519 pvr_queue_get_paired_frag_job_dep(struct pvr_job *job) 520 { 521 struct pvr_job *frag_job = job->type == DRM_PVR_JOB_TYPE_GEOMETRY ? 522 job->paired_job : NULL; 523 struct pvr_queue *frag_queue = frag_job ? frag_job->ctx->queues.fragment : NULL; 524 struct dma_fence *f; 525 unsigned long index; 526 527 if (!frag_job) 528 return NULL; 529 530 /* Have the geometry job wait on the paired fragment job's dependencies as well. */ 531 xa_for_each(&frag_job->base.dependencies, index, f) { 532 /* Skip already signaled fences. */ 533 if (dma_fence_is_signaled(f)) 534 continue; 535 536 /* 537 * The paired job fence won't be signaled until both jobs have 538 * been submitted, so we can't wait on it to schedule them. 539 */ 540 if (f == &job->base.s_fence->scheduled) 541 continue; 542 543 return dma_fence_get(f); 544 } 545 546 /* Initialize the paired fragment job's done_fence, so we can signal it. */ 547 pvr_queue_job_fence_init(frag_job->done_fence, frag_queue); 548 549 return pvr_queue_get_job_cccb_fence(frag_queue, frag_job); 550 } 551 552 /** 553 * pvr_queue_prepare_job() - Return the next internal dependencies expressed as a dma_fence. 554 * @sched_job: The job to query the next internal dependency on 555 * @s_entity: The entity this job is queue on. 556 * 557 * After iterating over drm_sched_job::dependencies, drm_sched let the driver return 558 * its own internal dependencies. We use this function to return our internal dependencies. 559 */ 560 static struct dma_fence * 561 pvr_queue_prepare_job(struct drm_sched_job *sched_job, 562 struct drm_sched_entity *s_entity) 563 { 564 struct pvr_job *job = container_of(sched_job, struct pvr_job, base); 565 struct pvr_queue *queue = container_of(s_entity, struct pvr_queue, entity); 566 struct dma_fence *internal_dep = NULL; 567 568 if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT && job->paired_job) { 569 /* 570 * This will be called on a paired fragment job after being submitted 571 * to the firmware as part of the paired geometry job's submission. 572 * We can tell if this is the case and bail early from whether run_job() 573 * has been called on the geometry job, which would issue a pm ref on 574 * this job as well. 575 */ 576 if (job->has_pm_ref) 577 return NULL; 578 } 579 580 /* 581 * Initialize the done_fence, so we can signal it. This must be done 582 * here because otherwise by the time of run_job() the job will end up 583 * in the pending list without a valid fence. 584 */ 585 pvr_queue_job_fence_init(job->done_fence, queue); 586 587 /* CCCB fence is used to make sure we have enough space in the CCCB to 588 * submit our commands. 589 */ 590 internal_dep = pvr_queue_get_job_cccb_fence(queue, job); 591 592 /* KCCB fence is used to make sure we have a KCCB slot to queue our 593 * CMD_KICK. 594 */ 595 if (!internal_dep) 596 internal_dep = pvr_queue_get_job_kccb_fence(queue, job); 597 598 /* Any extra internal dependency should be added here, using the following 599 * pattern: 600 * 601 * if (!internal_dep) 602 * internal_dep = pvr_queue_get_job_xxxx_fence(queue, job); 603 */ 604 605 /* The paired job fence should come last, when everything else is ready. */ 606 if (!internal_dep) 607 internal_dep = pvr_queue_get_paired_frag_job_dep(job); 608 609 return internal_dep; 610 } 611 612 /** 613 * pvr_queue_update_active_state_locked() - Update the queue active state. 614 * @queue: Queue to update the state on. 615 * 616 * Locked version of pvr_queue_update_active_state(). Must be called with 617 * pvr_device::queue::lock held. 618 */ 619 static void pvr_queue_update_active_state_locked(struct pvr_queue *queue) 620 { 621 struct pvr_device *pvr_dev = queue->ctx->pvr_dev; 622 623 lockdep_assert_held(&pvr_dev->queues.lock); 624 625 /* The queue is temporary out of any list when it's being reset, 626 * we don't want a call to pvr_queue_update_active_state_locked() 627 * to re-insert it behind our back. 628 */ 629 if (list_empty(&queue->node)) 630 return; 631 632 if (!atomic_read(&queue->in_flight_job_count)) 633 list_move_tail(&queue->node, &pvr_dev->queues.idle); 634 else 635 list_move_tail(&queue->node, &pvr_dev->queues.active); 636 } 637 638 /** 639 * pvr_queue_update_active_state() - Update the queue active state. 640 * @queue: Queue to update the state on. 641 * 642 * Active state is based on the in_flight_job_count value. 643 * 644 * Updating the active state implies moving the queue in or out of the 645 * active queue list, which also defines whether the queue is checked 646 * or not when a FW event is received. 647 * 648 * This function should be called any time a job is submitted or it done 649 * fence is signaled. 650 */ 651 static void pvr_queue_update_active_state(struct pvr_queue *queue) 652 { 653 struct pvr_device *pvr_dev = queue->ctx->pvr_dev; 654 655 mutex_lock(&pvr_dev->queues.lock); 656 pvr_queue_update_active_state_locked(queue); 657 mutex_unlock(&pvr_dev->queues.lock); 658 } 659 660 static void pvr_queue_submit_job_to_cccb(struct pvr_job *job) 661 { 662 struct pvr_queue *queue = container_of(job->base.sched, struct pvr_queue, scheduler); 663 struct rogue_fwif_ufo ufos[ROGUE_FWIF_CCB_CMD_MAX_UFOS]; 664 struct pvr_cccb *cccb = &queue->cccb; 665 struct pvr_queue_fence *jfence; 666 struct dma_fence *fence; 667 unsigned long index; 668 u32 ufo_count = 0; 669 670 /* We need to add the queue to the active list before updating the CCCB, 671 * otherwise we might miss the FW event informing us that something 672 * happened on this queue. 673 */ 674 atomic_inc(&queue->in_flight_job_count); 675 pvr_queue_update_active_state(queue); 676 677 xa_for_each(&job->base.dependencies, index, fence) { 678 jfence = to_pvr_queue_job_fence(fence); 679 if (!jfence) 680 continue; 681 682 /* Some dependencies might have been signaled since prepare_job() */ 683 if (dma_fence_is_signaled(&jfence->base)) 684 continue; 685 686 pvr_fw_object_get_fw_addr(jfence->queue->timeline_ufo.fw_obj, 687 &ufos[ufo_count].addr); 688 ufos[ufo_count++].value = jfence->base.seqno; 689 690 if (ufo_count == ARRAY_SIZE(ufos)) { 691 pvr_cccb_write_command_with_header(cccb, ROGUE_FWIF_CCB_CMD_TYPE_FENCE_PR, 692 sizeof(ufos), ufos, 0, 0); 693 ufo_count = 0; 694 } 695 } 696 697 if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT && job->paired_job) { 698 /* 699 * The loop above will only process dependencies backed by a UFO i.e. with 700 * a valid parent fence assigned, but the paired job dependency won't have 701 * one until both jobs have been submitted. Access the parent fence directly 702 * here instead, submitting it last as partial render fence. 703 */ 704 jfence = to_pvr_queue_job_fence(job->paired_job->done_fence); 705 if (!WARN_ON(!jfence)) { 706 pvr_fw_object_get_fw_addr(jfence->queue->timeline_ufo.fw_obj, 707 &ufos[ufo_count].addr); 708 ufos[ufo_count++].value = job->paired_job->done_fence->seqno; 709 } 710 } 711 712 if (ufo_count) { 713 pvr_cccb_write_command_with_header(cccb, ROGUE_FWIF_CCB_CMD_TYPE_FENCE_PR, 714 sizeof(ufos[0]) * ufo_count, ufos, 0, 0); 715 } 716 717 if (job->type == DRM_PVR_JOB_TYPE_GEOMETRY && job->paired_job) { 718 struct rogue_fwif_cmd_geom *cmd = job->cmd; 719 720 /* Reference value for the partial render test is the current queue fence 721 * seqno minus one. 722 */ 723 pvr_fw_object_get_fw_addr(queue->timeline_ufo.fw_obj, 724 &cmd->partial_render_geom_frag_fence.addr); 725 cmd->partial_render_geom_frag_fence.value = job->done_fence->seqno - 1; 726 } 727 728 trace_pvr_job_submit_fw(job); 729 730 /* Submit job to FW */ 731 pvr_cccb_write_command_with_header(cccb, job->fw_ccb_cmd_type, job->cmd_len, job->cmd, 732 job->id, job->id); 733 734 /* Update command to signal the job fence. */ 735 pvr_fw_object_get_fw_addr(queue->timeline_ufo.fw_obj, &ufos[0].addr); 736 ufos[0].value = job->done_fence->seqno; 737 pvr_cccb_write_command_with_header(cccb, ROGUE_FWIF_CCB_CMD_TYPE_UPDATE, 738 sizeof(ufos[0]), ufos, 0, 0); 739 } 740 741 /** 742 * pvr_queue_run_job() - Submit a job to the FW. 743 * @sched_job: The job to submit. 744 * 745 * This function is called when all non-native dependencies have been met and 746 * when the commands resulting from this job are guaranteed to fit in the CCCB. 747 */ 748 static struct dma_fence *pvr_queue_run_job(struct drm_sched_job *sched_job) 749 { 750 struct pvr_job *job = container_of(sched_job, struct pvr_job, base); 751 struct pvr_device *pvr_dev = job->pvr_dev; 752 int err; 753 754 /* The fragment job is issued along the geometry job when we use combined 755 * geom+frag kicks. When we get there, we should simply return the 756 * done_fence that's been initialized earlier. 757 */ 758 if (job->paired_job && job->type == DRM_PVR_JOB_TYPE_FRAGMENT && 759 job->done_fence->ops) { 760 return dma_fence_get(job->done_fence); 761 } 762 763 /* The only kind of jobs that can be paired are geometry and fragment, and 764 * we bail out early if we see a fragment job that's paired with a geometry job. 765 * Paired jobs must also target the same context and point to the same HWRT. 766 */ 767 if (WARN_ON(job->paired_job && 768 (job->type != DRM_PVR_JOB_TYPE_GEOMETRY || 769 job->paired_job->type != DRM_PVR_JOB_TYPE_FRAGMENT || 770 job->hwrt != job->paired_job->hwrt || 771 job->ctx != job->paired_job->ctx))) 772 return ERR_PTR(-EINVAL); 773 774 err = pvr_job_get_pm_ref(job); 775 if (WARN_ON(err)) 776 return ERR_PTR(err); 777 778 if (job->paired_job) { 779 err = pvr_job_get_pm_ref(job->paired_job); 780 if (WARN_ON(err)) 781 return ERR_PTR(err); 782 } 783 784 /* Submit our job to the CCCB */ 785 pvr_queue_submit_job_to_cccb(job); 786 787 if (job->paired_job) { 788 struct pvr_job *geom_job = job; 789 struct pvr_job *frag_job = job->paired_job; 790 struct pvr_queue *geom_queue = job->ctx->queues.geometry; 791 struct pvr_queue *frag_queue = job->ctx->queues.fragment; 792 793 /* Submit the fragment job along the geometry job and send a combined kick. */ 794 pvr_queue_submit_job_to_cccb(frag_job); 795 pvr_cccb_send_kccb_combined_kick(pvr_dev, 796 &geom_queue->cccb, &frag_queue->cccb, 797 pvr_context_get_fw_addr(geom_job->ctx) + 798 geom_queue->ctx_offset, 799 pvr_context_get_fw_addr(frag_job->ctx) + 800 frag_queue->ctx_offset, 801 job->hwrt, 802 frag_job->fw_ccb_cmd_type == 803 ROGUE_FWIF_CCB_CMD_TYPE_FRAG_PR); 804 } else { 805 struct pvr_queue *queue = container_of(job->base.sched, 806 struct pvr_queue, scheduler); 807 808 pvr_cccb_send_kccb_kick(pvr_dev, &queue->cccb, 809 pvr_context_get_fw_addr(job->ctx) + queue->ctx_offset, 810 job->hwrt); 811 } 812 813 return dma_fence_get(job->done_fence); 814 } 815 816 static void pvr_queue_stop(struct pvr_queue *queue, struct pvr_job *bad_job) 817 { 818 drm_sched_stop(&queue->scheduler, bad_job ? &bad_job->base : NULL); 819 } 820 821 static void pvr_queue_start(struct pvr_queue *queue) 822 { 823 struct pvr_job *job; 824 825 /* Make sure we CPU-signal the UFO object, so other queues don't get 826 * blocked waiting on it. 827 */ 828 *queue->timeline_ufo.value = atomic_read(&queue->job_fence_ctx.seqno); 829 830 list_for_each_entry(job, &queue->scheduler.pending_list, base.list) { 831 if (dma_fence_is_signaled(job->done_fence)) { 832 /* Jobs might have completed after drm_sched_stop() was called. 833 * In that case, re-assign the parent field to the done_fence. 834 */ 835 WARN_ON(job->base.s_fence->parent); 836 job->base.s_fence->parent = dma_fence_get(job->done_fence); 837 } else { 838 /* If we had unfinished jobs, flag the entity as guilty so no 839 * new job can be submitted. 840 */ 841 atomic_set(&queue->ctx->faulty, 1); 842 } 843 } 844 845 drm_sched_start(&queue->scheduler, 0); 846 } 847 848 /** 849 * pvr_queue_timedout_job() - Handle a job timeout event. 850 * @s_job: The job this timeout occurred on. 851 * 852 * FIXME: We don't do anything here to unblock the situation, we just stop+start 853 * the scheduler, and re-assign parent fences in the middle. 854 * 855 * Return: 856 * *%DRM_GPU_SCHED_STAT_NO_HANG if the job fence has already been 857 * signaled, or 858 * *%DRM_GPU_SCHED_STAT_RESET otherwise. 859 */ 860 static enum drm_gpu_sched_stat 861 pvr_queue_timedout_job(struct drm_sched_job *s_job) 862 { 863 struct drm_gpu_scheduler *sched = s_job->sched; 864 struct pvr_queue *queue = container_of(sched, struct pvr_queue, scheduler); 865 struct pvr_device *pvr_dev = queue->ctx->pvr_dev; 866 struct pvr_job *job; 867 u32 job_count = 0; 868 869 if (dma_fence_is_signaled(s_job->s_fence->parent)) 870 return DRM_GPU_SCHED_STAT_NO_HANG; 871 872 dev_err(sched->dev, "Job timeout\n"); 873 874 /* Before we stop the scheduler, make sure the queue is out of any list, so 875 * any call to pvr_queue_update_active_state_locked() that might happen 876 * until the scheduler is really stopped doesn't end up re-inserting the 877 * queue in the active list. This would cause 878 * pvr_queue_signal_done_fences() and drm_sched_stop() to race with each 879 * other when accessing the pending_list, since drm_sched_stop() doesn't 880 * grab the job_list_lock when modifying the list (it's assuming the 881 * only other accessor is the scheduler, and it's safe to not grab the 882 * lock since it's stopped). 883 */ 884 mutex_lock(&pvr_dev->queues.lock); 885 list_del_init(&queue->node); 886 mutex_unlock(&pvr_dev->queues.lock); 887 888 drm_sched_stop(sched, s_job); 889 890 /* Re-assign job parent fences. */ 891 list_for_each_entry(job, &sched->pending_list, base.list) { 892 job->base.s_fence->parent = dma_fence_get(job->done_fence); 893 job_count++; 894 } 895 WARN_ON(atomic_read(&queue->in_flight_job_count) != job_count); 896 897 /* Re-insert the queue in the proper list, and kick a queue processing 898 * operation if there were jobs pending. 899 */ 900 mutex_lock(&pvr_dev->queues.lock); 901 if (!job_count) { 902 list_move_tail(&queue->node, &pvr_dev->queues.idle); 903 } else { 904 atomic_set(&queue->in_flight_job_count, job_count); 905 list_move_tail(&queue->node, &pvr_dev->queues.active); 906 pvr_queue_process(queue); 907 } 908 mutex_unlock(&pvr_dev->queues.lock); 909 910 drm_sched_start(sched, 0); 911 912 return DRM_GPU_SCHED_STAT_RESET; 913 } 914 915 /** 916 * pvr_queue_free_job() - Release the reference the scheduler had on a job object. 917 * @sched_job: Job object to free. 918 */ 919 static void pvr_queue_free_job(struct drm_sched_job *sched_job) 920 { 921 struct pvr_job *job = container_of(sched_job, struct pvr_job, base); 922 923 drm_sched_job_cleanup(sched_job); 924 925 if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT && job->paired_job) 926 pvr_job_put(job->paired_job); 927 928 job->paired_job = NULL; 929 pvr_job_put(job); 930 } 931 932 static const struct drm_sched_backend_ops pvr_queue_sched_ops = { 933 .prepare_job = pvr_queue_prepare_job, 934 .run_job = pvr_queue_run_job, 935 .timedout_job = pvr_queue_timedout_job, 936 .free_job = pvr_queue_free_job, 937 }; 938 939 /** 940 * pvr_queue_fence_is_native() - Check if a dma_fence is native to this driver. 941 * @f: Fence to test. 942 * 943 * Check if the fence we're being passed is a drm_sched_fence that is coming from this driver. 944 * 945 * It may be a UFO-backed fence i.e. a fence that can be signaled or waited upon FW-side, 946 * such as pvr_job::done_fence objects that are backed by the timeline UFO attached to the queue 947 * they are pushed to. 948 */ 949 bool pvr_queue_fence_is_native(struct dma_fence *f) 950 { 951 struct drm_sched_fence *sched_fence = f ? to_drm_sched_fence(f) : NULL; 952 953 if (sched_fence && 954 sched_fence->sched->ops == &pvr_queue_sched_ops) 955 return true; 956 957 return pvr_queue_fence_is_ufo_backed(f); 958 } 959 960 /** 961 * pvr_queue_signal_done_fences() - Signal done fences. 962 * @queue: Queue to check. 963 * 964 * Signal done fences of jobs whose seqno is less than the current value of 965 * the UFO object attached to the queue. 966 */ 967 static void 968 pvr_queue_signal_done_fences(struct pvr_queue *queue) 969 { 970 struct pvr_job *job, *tmp_job; 971 u32 cur_seqno; 972 973 spin_lock(&queue->scheduler.job_list_lock); 974 cur_seqno = *queue->timeline_ufo.value; 975 list_for_each_entry_safe(job, tmp_job, &queue->scheduler.pending_list, base.list) { 976 if ((int)(cur_seqno - lower_32_bits(job->done_fence->seqno)) < 0) 977 break; 978 979 if (!dma_fence_is_signaled(job->done_fence)) { 980 dma_fence_signal(job->done_fence); 981 pvr_job_release_pm_ref(job); 982 atomic_dec(&queue->in_flight_job_count); 983 } 984 } 985 spin_unlock(&queue->scheduler.job_list_lock); 986 } 987 988 /** 989 * pvr_queue_check_job_waiting_for_cccb_space() - Check if a job waiting for CCCB space 990 * can be unblocked and pushed to the CCCB. 991 * @queue: Queue to check 992 * 993 * If we have a job waiting for CCCB, and this job now fits in the CCCB, we signal 994 * its CCCB fence, which should kick drm_sched. 995 */ 996 static void 997 pvr_queue_check_job_waiting_for_cccb_space(struct pvr_queue *queue) 998 { 999 struct pvr_queue_fence *cccb_fence; 1000 u32 native_deps_remaining; 1001 struct pvr_job *job; 1002 1003 mutex_lock(&queue->cccb_fence_ctx.job_lock); 1004 job = queue->cccb_fence_ctx.job; 1005 if (!job) 1006 goto out_unlock; 1007 1008 /* If we have a job attached to the CCCB fence context, its CCCB fence 1009 * shouldn't be NULL. 1010 */ 1011 if (WARN_ON(!job->cccb_fence)) { 1012 job = NULL; 1013 goto out_unlock; 1014 } 1015 1016 /* If we get there, CCCB fence has to be initialized. */ 1017 cccb_fence = container_of(job->cccb_fence, struct pvr_queue_fence, base); 1018 if (WARN_ON(!cccb_fence->queue)) { 1019 job = NULL; 1020 goto out_unlock; 1021 } 1022 1023 /* Evict signaled dependencies before checking for CCCB space. 1024 * If the job fits, signal the CCCB fence, this should unblock 1025 * the drm_sched_entity. 1026 */ 1027 native_deps_remaining = job_count_remaining_native_deps(job); 1028 if (!pvr_cccb_cmdseq_fits(&queue->cccb, job_cmds_size(job, native_deps_remaining))) { 1029 job = NULL; 1030 goto out_unlock; 1031 } 1032 1033 dma_fence_signal(job->cccb_fence); 1034 pvr_queue_fence_put(job->cccb_fence); 1035 job->cccb_fence = NULL; 1036 queue->cccb_fence_ctx.job = NULL; 1037 1038 out_unlock: 1039 mutex_unlock(&queue->cccb_fence_ctx.job_lock); 1040 1041 pvr_job_put(job); 1042 } 1043 1044 /** 1045 * pvr_queue_process() - Process events that happened on a queue. 1046 * @queue: Queue to check 1047 * 1048 * Signal job fences and check if jobs waiting for CCCB space can be unblocked. 1049 */ 1050 void pvr_queue_process(struct pvr_queue *queue) 1051 { 1052 lockdep_assert_held(&queue->ctx->pvr_dev->queues.lock); 1053 1054 pvr_queue_check_job_waiting_for_cccb_space(queue); 1055 pvr_queue_signal_done_fences(queue); 1056 pvr_queue_update_active_state_locked(queue); 1057 } 1058 1059 static u32 get_dm_type(struct pvr_queue *queue) 1060 { 1061 switch (queue->type) { 1062 case DRM_PVR_JOB_TYPE_GEOMETRY: 1063 return PVR_FWIF_DM_GEOM; 1064 case DRM_PVR_JOB_TYPE_TRANSFER_FRAG: 1065 case DRM_PVR_JOB_TYPE_FRAGMENT: 1066 return PVR_FWIF_DM_FRAG; 1067 case DRM_PVR_JOB_TYPE_COMPUTE: 1068 return PVR_FWIF_DM_CDM; 1069 } 1070 1071 return ~0; 1072 } 1073 1074 /** 1075 * init_fw_context() - Initializes the queue part of a FW context. 1076 * @queue: Queue object to initialize the FW context for. 1077 * @fw_ctx_map: The FW context CPU mapping. 1078 * 1079 * FW contexts are containing various states, one of them being a per-queue state 1080 * that needs to be initialized for each queue being exposed by a context. This 1081 * function takes care of that. 1082 */ 1083 static void init_fw_context(struct pvr_queue *queue, void *fw_ctx_map) 1084 { 1085 struct pvr_context *ctx = queue->ctx; 1086 struct pvr_fw_object *fw_mem_ctx_obj = pvr_vm_get_fw_mem_context(ctx->vm_ctx); 1087 struct rogue_fwif_fwcommoncontext *cctx_fw; 1088 struct pvr_cccb *cccb = &queue->cccb; 1089 1090 cctx_fw = fw_ctx_map + queue->ctx_offset; 1091 cctx_fw->ccbctl_fw_addr = cccb->ctrl_fw_addr; 1092 cctx_fw->ccb_fw_addr = cccb->cccb_fw_addr; 1093 1094 cctx_fw->dm = get_dm_type(queue); 1095 cctx_fw->priority = ctx->priority; 1096 cctx_fw->priority_seq_num = 0; 1097 cctx_fw->max_deadline_ms = MAX_DEADLINE_MS; 1098 cctx_fw->pid = task_tgid_nr(current); 1099 cctx_fw->server_common_context_id = ctx->ctx_id; 1100 1101 pvr_fw_object_get_fw_addr(fw_mem_ctx_obj, &cctx_fw->fw_mem_context_fw_addr); 1102 1103 pvr_fw_object_get_fw_addr(queue->reg_state_obj, &cctx_fw->context_state_addr); 1104 } 1105 1106 /** 1107 * pvr_queue_cleanup_fw_context() - Wait for the FW context to be idle and clean it up. 1108 * @queue: Queue on FW context to clean up. 1109 * 1110 * Return: 1111 * * 0 on success, 1112 * * Any error returned by pvr_fw_structure_cleanup() otherwise. 1113 */ 1114 static int pvr_queue_cleanup_fw_context(struct pvr_queue *queue) 1115 { 1116 if (!queue->ctx->fw_obj) 1117 return 0; 1118 1119 return pvr_fw_structure_cleanup(queue->ctx->pvr_dev, 1120 ROGUE_FWIF_CLEANUP_FWCOMMONCONTEXT, 1121 queue->ctx->fw_obj, queue->ctx_offset); 1122 } 1123 1124 /** 1125 * pvr_queue_job_init() - Initialize queue related fields in a pvr_job object. 1126 * @job: The job to initialize. 1127 * @drm_client_id: drm_file.client_id submitting the job 1128 * 1129 * Bind the job to a queue and allocate memory to guarantee pvr_queue_job_arm() 1130 * and pvr_queue_job_push() can't fail. We also make sure the context type is 1131 * valid and the job can fit in the CCCB. 1132 * 1133 * Return: 1134 * * 0 on success, or 1135 * * An error code if something failed. 1136 */ 1137 int pvr_queue_job_init(struct pvr_job *job, u64 drm_client_id) 1138 { 1139 /* Fragment jobs need at least one native fence wait on the geometry job fence. */ 1140 u32 min_native_dep_count = job->type == DRM_PVR_JOB_TYPE_FRAGMENT ? 1 : 0; 1141 struct pvr_queue *queue; 1142 int err; 1143 1144 if (atomic_read(&job->ctx->faulty)) 1145 return -EIO; 1146 1147 queue = pvr_context_get_queue_for_job(job->ctx, job->type); 1148 if (!queue) 1149 return -EINVAL; 1150 1151 if (!pvr_cccb_cmdseq_can_fit(&queue->cccb, job_cmds_size(job, min_native_dep_count))) 1152 return -E2BIG; 1153 1154 err = drm_sched_job_init(&job->base, &queue->entity, 1, THIS_MODULE, drm_client_id); 1155 if (err) 1156 return err; 1157 1158 job->cccb_fence = pvr_queue_fence_alloc(); 1159 job->kccb_fence = pvr_kccb_fence_alloc(); 1160 job->done_fence = pvr_queue_fence_alloc(); 1161 if (!job->cccb_fence || !job->kccb_fence || !job->done_fence) 1162 return -ENOMEM; 1163 1164 return 0; 1165 } 1166 1167 /** 1168 * pvr_queue_job_arm() - Arm a job object. 1169 * @job: The job to arm. 1170 * 1171 * Initializes fences and return the drm_sched finished fence so it can 1172 * be exposed to the outside world. Once this function is called, you should 1173 * make sure the job is pushed using pvr_queue_job_push(), or guarantee that 1174 * no one grabbed a reference to the returned fence. The latter can happen if 1175 * we do multi-job submission, and something failed when creating/initializing 1176 * a job. In that case, we know the fence didn't leave the driver, and we 1177 * can thus guarantee nobody will wait on an dead fence object. 1178 * 1179 * Return: 1180 * * A dma_fence object. 1181 */ 1182 struct dma_fence *pvr_queue_job_arm(struct pvr_job *job) 1183 { 1184 drm_sched_job_arm(&job->base); 1185 1186 return &job->base.s_fence->finished; 1187 } 1188 1189 /** 1190 * pvr_queue_job_cleanup() - Cleanup fence/scheduler related fields in the job object. 1191 * @job: The job to cleanup. 1192 * 1193 * Should be called in the job release path. 1194 */ 1195 void pvr_queue_job_cleanup(struct pvr_job *job) 1196 { 1197 pvr_queue_fence_put(job->done_fence); 1198 pvr_queue_fence_put(job->cccb_fence); 1199 pvr_kccb_fence_put(job->kccb_fence); 1200 1201 if (job->base.s_fence) 1202 drm_sched_job_cleanup(&job->base); 1203 1204 trace_pvr_job_done(job); 1205 } 1206 1207 /** 1208 * pvr_queue_job_push() - Push a job to its queue. 1209 * @job: The job to push. 1210 * 1211 * Must be called after pvr_queue_job_init() and after all dependencies 1212 * have been added to the job. This will effectively queue the job to 1213 * the drm_sched_entity attached to the queue. We grab a reference on 1214 * the job object, so the caller is free to drop its reference when it's 1215 * done accessing the job object. 1216 */ 1217 void pvr_queue_job_push(struct pvr_job *job) 1218 { 1219 struct pvr_queue *queue = container_of(job->base.sched, struct pvr_queue, scheduler); 1220 1221 /* Keep track of the last queued job scheduled fence for combined submit. */ 1222 dma_fence_put(queue->last_queued_job_scheduled_fence); 1223 queue->last_queued_job_scheduled_fence = dma_fence_get(&job->base.s_fence->scheduled); 1224 1225 pvr_job_get(job); 1226 drm_sched_entity_push_job(&job->base); 1227 } 1228 1229 static void reg_state_init(void *cpu_ptr, void *priv) 1230 { 1231 struct pvr_queue *queue = priv; 1232 1233 if (queue->type == DRM_PVR_JOB_TYPE_GEOMETRY) { 1234 struct rogue_fwif_geom_ctx_state *geom_ctx_state_fw = cpu_ptr; 1235 1236 geom_ctx_state_fw->geom_core[0].geom_reg_vdm_call_stack_pointer_init = 1237 queue->callstack_addr; 1238 } 1239 } 1240 1241 /** 1242 * pvr_queue_create() - Create a queue object. 1243 * @ctx: The context this queue will be attached to. 1244 * @type: The type of jobs being pushed to this queue. 1245 * @args: The arguments passed to the context creation function. 1246 * @fw_ctx_map: CPU mapping of the FW context object. 1247 * 1248 * Create a queue object that will be used to queue and track jobs. 1249 * 1250 * Return: 1251 * * A valid pointer to a pvr_queue object, or 1252 * * An error pointer if the creation/initialization failed. 1253 */ 1254 struct pvr_queue *pvr_queue_create(struct pvr_context *ctx, 1255 enum drm_pvr_job_type type, 1256 struct drm_pvr_ioctl_create_context_args *args, 1257 void *fw_ctx_map) 1258 { 1259 static const struct { 1260 u32 cccb_size; 1261 const char *name; 1262 } props[] = { 1263 [DRM_PVR_JOB_TYPE_GEOMETRY] = { 1264 .cccb_size = CTX_GEOM_CCCB_SIZE_LOG2, 1265 .name = "geometry", 1266 }, 1267 [DRM_PVR_JOB_TYPE_FRAGMENT] = { 1268 .cccb_size = CTX_FRAG_CCCB_SIZE_LOG2, 1269 .name = "fragment" 1270 }, 1271 [DRM_PVR_JOB_TYPE_COMPUTE] = { 1272 .cccb_size = CTX_COMPUTE_CCCB_SIZE_LOG2, 1273 .name = "compute" 1274 }, 1275 [DRM_PVR_JOB_TYPE_TRANSFER_FRAG] = { 1276 .cccb_size = CTX_TRANSFER_CCCB_SIZE_LOG2, 1277 .name = "transfer_frag" 1278 }, 1279 }; 1280 struct pvr_device *pvr_dev = ctx->pvr_dev; 1281 const struct drm_sched_init_args sched_args = { 1282 .ops = &pvr_queue_sched_ops, 1283 .submit_wq = pvr_dev->sched_wq, 1284 .credit_limit = 64 * 1024, 1285 .hang_limit = 1, 1286 .timeout = msecs_to_jiffies(500), 1287 .timeout_wq = pvr_dev->sched_wq, 1288 .name = "pvr-queue", 1289 .dev = pvr_dev->base.dev, 1290 }; 1291 struct drm_gpu_scheduler *sched; 1292 struct pvr_queue *queue; 1293 int ctx_state_size, err; 1294 void *cpu_map; 1295 1296 if (WARN_ON(type >= sizeof(props))) 1297 return ERR_PTR(-EINVAL); 1298 1299 switch (ctx->type) { 1300 case DRM_PVR_CTX_TYPE_RENDER: 1301 if (type != DRM_PVR_JOB_TYPE_GEOMETRY && 1302 type != DRM_PVR_JOB_TYPE_FRAGMENT) 1303 return ERR_PTR(-EINVAL); 1304 break; 1305 case DRM_PVR_CTX_TYPE_COMPUTE: 1306 if (type != DRM_PVR_JOB_TYPE_COMPUTE) 1307 return ERR_PTR(-EINVAL); 1308 break; 1309 case DRM_PVR_CTX_TYPE_TRANSFER_FRAG: 1310 if (type != DRM_PVR_JOB_TYPE_TRANSFER_FRAG) 1311 return ERR_PTR(-EINVAL); 1312 break; 1313 default: 1314 return ERR_PTR(-EINVAL); 1315 } 1316 1317 ctx_state_size = get_ctx_state_size(pvr_dev, type); 1318 if (ctx_state_size < 0) 1319 return ERR_PTR(ctx_state_size); 1320 1321 queue = kzalloc_obj(*queue); 1322 if (!queue) 1323 return ERR_PTR(-ENOMEM); 1324 1325 queue->type = type; 1326 queue->ctx_offset = get_ctx_offset(type); 1327 queue->ctx = ctx; 1328 queue->callstack_addr = args->callstack_addr; 1329 sched = &queue->scheduler; 1330 INIT_LIST_HEAD(&queue->node); 1331 mutex_init(&queue->cccb_fence_ctx.job_lock); 1332 pvr_queue_fence_ctx_init(&queue->cccb_fence_ctx.base); 1333 pvr_queue_fence_ctx_init(&queue->job_fence_ctx); 1334 1335 err = pvr_cccb_init(pvr_dev, &queue->cccb, props[type].cccb_size, props[type].name); 1336 if (err) 1337 goto err_free_queue; 1338 1339 err = pvr_fw_object_create(pvr_dev, ctx_state_size, 1340 PVR_BO_FW_FLAGS_DEVICE_UNCACHED, 1341 reg_state_init, queue, &queue->reg_state_obj); 1342 if (err) 1343 goto err_cccb_fini; 1344 1345 init_fw_context(queue, fw_ctx_map); 1346 1347 if (type != DRM_PVR_JOB_TYPE_GEOMETRY && type != DRM_PVR_JOB_TYPE_FRAGMENT && 1348 args->callstack_addr) { 1349 err = -EINVAL; 1350 goto err_release_reg_state; 1351 } 1352 1353 cpu_map = pvr_fw_object_create_and_map(pvr_dev, sizeof(*queue->timeline_ufo.value), 1354 PVR_BO_FW_FLAGS_DEVICE_UNCACHED, 1355 NULL, NULL, &queue->timeline_ufo.fw_obj); 1356 if (IS_ERR(cpu_map)) { 1357 err = PTR_ERR(cpu_map); 1358 goto err_release_reg_state; 1359 } 1360 1361 queue->timeline_ufo.value = cpu_map; 1362 1363 err = drm_sched_init(&queue->scheduler, &sched_args); 1364 if (err) 1365 goto err_release_ufo; 1366 1367 err = drm_sched_entity_init(&queue->entity, 1368 DRM_SCHED_PRIORITY_KERNEL, 1369 &sched, 1, &ctx->faulty); 1370 if (err) 1371 goto err_sched_fini; 1372 1373 mutex_lock(&pvr_dev->queues.lock); 1374 list_add_tail(&queue->node, &pvr_dev->queues.idle); 1375 mutex_unlock(&pvr_dev->queues.lock); 1376 1377 return queue; 1378 1379 err_sched_fini: 1380 drm_sched_fini(&queue->scheduler); 1381 1382 err_release_ufo: 1383 pvr_fw_object_unmap_and_destroy(queue->timeline_ufo.fw_obj); 1384 1385 err_release_reg_state: 1386 pvr_fw_object_destroy(queue->reg_state_obj); 1387 1388 err_cccb_fini: 1389 pvr_cccb_fini(&queue->cccb); 1390 1391 err_free_queue: 1392 mutex_destroy(&queue->cccb_fence_ctx.job_lock); 1393 kfree(queue); 1394 1395 return ERR_PTR(err); 1396 } 1397 1398 void pvr_queue_device_pre_reset(struct pvr_device *pvr_dev) 1399 { 1400 struct pvr_queue *queue; 1401 1402 mutex_lock(&pvr_dev->queues.lock); 1403 list_for_each_entry(queue, &pvr_dev->queues.idle, node) 1404 pvr_queue_stop(queue, NULL); 1405 list_for_each_entry(queue, &pvr_dev->queues.active, node) 1406 pvr_queue_stop(queue, NULL); 1407 mutex_unlock(&pvr_dev->queues.lock); 1408 } 1409 1410 void pvr_queue_device_post_reset(struct pvr_device *pvr_dev) 1411 { 1412 struct pvr_queue *queue; 1413 1414 mutex_lock(&pvr_dev->queues.lock); 1415 list_for_each_entry(queue, &pvr_dev->queues.active, node) 1416 pvr_queue_start(queue); 1417 list_for_each_entry(queue, &pvr_dev->queues.idle, node) 1418 pvr_queue_start(queue); 1419 mutex_unlock(&pvr_dev->queues.lock); 1420 } 1421 1422 /** 1423 * pvr_queue_kill() - Kill a queue. 1424 * @queue: The queue to kill. 1425 * 1426 * Kill the queue so no new jobs can be pushed. Should be called when the 1427 * context handle is destroyed. The queue object might last longer if jobs 1428 * are still in flight and holding a reference to the context this queue 1429 * belongs to. 1430 */ 1431 void pvr_queue_kill(struct pvr_queue *queue) 1432 { 1433 drm_sched_entity_destroy(&queue->entity); 1434 dma_fence_put(queue->last_queued_job_scheduled_fence); 1435 queue->last_queued_job_scheduled_fence = NULL; 1436 } 1437 1438 /** 1439 * pvr_queue_destroy() - Destroy a queue. 1440 * @queue: The queue to destroy. 1441 * @cleanup_queue_entity: Whether to cleanup the queue entity. 1442 * 1443 * Cleanup the queue and free the resources attached to it. Should be 1444 * called from the context release function. 1445 */ 1446 void pvr_queue_destroy(struct pvr_queue *queue, bool cleanup_queue_entity) 1447 { 1448 if (!queue) 1449 return; 1450 1451 mutex_lock(&queue->ctx->pvr_dev->queues.lock); 1452 list_del_init(&queue->node); 1453 mutex_unlock(&queue->ctx->pvr_dev->queues.lock); 1454 1455 drm_sched_fini(&queue->scheduler); 1456 if (cleanup_queue_entity) 1457 drm_sched_entity_fini(&queue->entity); 1458 1459 if (WARN_ON(queue->last_queued_job_scheduled_fence)) 1460 dma_fence_put(queue->last_queued_job_scheduled_fence); 1461 1462 pvr_queue_cleanup_fw_context(queue); 1463 1464 pvr_fw_object_unmap_and_destroy(queue->timeline_ufo.fw_obj); 1465 pvr_fw_object_destroy(queue->reg_state_obj); 1466 pvr_cccb_fini(&queue->cccb); 1467 mutex_destroy(&queue->cccb_fence_ctx.job_lock); 1468 kfree(queue); 1469 } 1470 1471 /** 1472 * pvr_queue_device_init() - Device-level initialization of queue related fields. 1473 * @pvr_dev: The device to initialize. 1474 * 1475 * Initializes all fields related to queue management in pvr_device. 1476 * 1477 * Return: 1478 * * 0 on success, or 1479 * * An error code on failure. 1480 */ 1481 int pvr_queue_device_init(struct pvr_device *pvr_dev) 1482 { 1483 int err; 1484 1485 INIT_LIST_HEAD(&pvr_dev->queues.active); 1486 INIT_LIST_HEAD(&pvr_dev->queues.idle); 1487 err = drmm_mutex_init(from_pvr_device(pvr_dev), &pvr_dev->queues.lock); 1488 if (err) 1489 return err; 1490 1491 pvr_dev->sched_wq = alloc_workqueue("powervr-sched", WQ_UNBOUND, 0); 1492 if (!pvr_dev->sched_wq) 1493 return -ENOMEM; 1494 1495 return 0; 1496 } 1497 1498 /** 1499 * pvr_queue_device_fini() - Device-level cleanup of queue related fields. 1500 * @pvr_dev: The device to cleanup. 1501 * 1502 * Cleanup/free all queue-related resources attached to a pvr_device object. 1503 */ 1504 void pvr_queue_device_fini(struct pvr_device *pvr_dev) 1505 { 1506 destroy_workqueue(pvr_dev->sched_wq); 1507 } 1508