1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2014-2018 Broadcom 4 * Copyright (C) 2023 Raspberry Pi 5 */ 6 7 #include <linux/dma-fence-unwrap.h> 8 #include <linux/overflow.h> 9 10 #include <drm/drm_print.h> 11 #include <drm/drm_syncobj.h> 12 13 #include "v3d_drv.h" 14 #include "v3d_regs.h" 15 #include "v3d_trace.h" 16 17 /* Takes the reservation lock on all the BOs being referenced, so that 18 * we can attach fences and update the reservations after pushing the job 19 * to the queue. 20 * 21 * We don't lock the RCL the tile alloc/state BOs, or overflow memory 22 * (all of which are on render->unref_list). They're entirely private 23 * to v3d, so we don't attach dma-buf fences to them. 24 */ 25 static int 26 v3d_submit_lock_reservations(struct v3d_submit *submit) 27 { 28 int i, j, ret; 29 30 drm_exec_init(&submit->exec, 31 DRM_EXEC_INTERRUPTIBLE_WAIT | DRM_EXEC_IGNORE_DUPLICATES, 0); 32 drm_exec_until_all_locked(&submit->exec) { 33 for (i = 0; i < submit->job_count; i++) { 34 struct v3d_job *job = submit->jobs[i]; 35 36 ret = drm_exec_prepare_array(&submit->exec, job->bo, 37 job->bo_count, 1); 38 if (ret) 39 break; 40 } 41 drm_exec_retry_on_contention(&submit->exec); 42 if (ret) 43 goto fail; 44 } 45 46 for (i = 0; i < submit->job_count; i++) { 47 struct v3d_job *job = submit->jobs[i]; 48 49 if (!job->has_implicit_dep) 50 continue; 51 52 for (j = 0; j < job->bo_count; j++) { 53 ret = drm_sched_job_add_implicit_dependencies(&job->base, 54 job->bo[j], 55 true); 56 if (ret) 57 goto fail; 58 } 59 } 60 61 return 0; 62 63 fail: 64 drm_exec_fini(&submit->exec); 65 return ret; 66 } 67 68 static void 69 v3d_submit_unlock_reservations(struct v3d_submit *submit) 70 { 71 drm_exec_fini(&submit->exec); 72 } 73 74 /** 75 * v3d_lookup_bos() - Sets up job->bo[] with the GEM objects 76 * referenced by the job. 77 * @file_priv: DRM file for this fd 78 * @job: V3D job being set up 79 * @bo_handles: GEM handles 80 * @bo_count: Number of GEM handles passed in 81 * 82 * The command validator needs to reference BOs by their index within 83 * the submitted job's BO list. This does the validation of the job's 84 * BO list and reference counting for the lifetime of the job. 85 * 86 * Note that this function doesn't need to unreference the BOs on 87 * failure, because that will happen at `v3d_job_free()`. 88 */ 89 static int 90 v3d_lookup_bos(struct drm_file *file_priv, struct v3d_job *job, 91 u64 bo_handles, u32 bo_count) 92 { 93 if (!bo_count) { 94 drm_warn(&job->v3d->drm, "Rendering requires BOs\n"); 95 return -EINVAL; 96 } 97 98 job->bo_count = bo_count; 99 100 return drm_gem_objects_lookup(file_priv, 101 (void __user *)(uintptr_t)bo_handles, 102 job->bo_count, &job->bo); 103 } 104 105 /** 106 * v3d_job_reference_bos() - Share another job's BOs with @dst 107 * @dst: job that acquires references to the BOs 108 * @src: job whose already-resolved BO list is shared 109 * 110 * For submissions with multiple jobs that use the same BOs, a trailing job 111 * shouldn't look the handles up again, as it could cause inconsistencies. 112 * Instead, it should reference the previous job's BOs. 113 */ 114 static int 115 v3d_job_reference_bos(struct v3d_job *dst, struct v3d_job *src) 116 { 117 dst->bo = kvmalloc_objs(*dst->bo, src->bo_count); 118 if (!dst->bo) 119 return -ENOMEM; 120 121 dst->bo_count = src->bo_count; 122 for (int i = 0; i < dst->bo_count; i++) { 123 dst->bo[i] = src->bo[i]; 124 drm_gem_object_get(dst->bo[i]); 125 } 126 127 return 0; 128 } 129 130 static void 131 v3d_job_free(struct kref *ref) 132 { 133 struct v3d_job *job = container_of(ref, struct v3d_job, refcount); 134 int i; 135 136 if (job->bo) { 137 for (i = 0; i < job->bo_count; i++) 138 drm_gem_object_put(job->bo[i]); 139 kvfree(job->bo); 140 } 141 142 dma_fence_put(job->irq_fence); 143 dma_fence_put(job->done_fence); 144 145 if (job->perfmon) 146 v3d_perfmon_put(job->perfmon); 147 148 v3d_stats_put(job->client_stats); 149 v3d_stats_put(job->global_stats); 150 151 if (job->has_pm_ref) 152 v3d_pm_runtime_put(job->v3d); 153 154 kfree(job); 155 } 156 157 static void 158 v3d_render_job_free(struct kref *ref) 159 { 160 struct v3d_render_job *job = container_of(ref, struct v3d_render_job, 161 base.refcount); 162 struct v3d_bo *bo, *save; 163 164 list_for_each_entry_safe(bo, save, &job->unref_list, unref_head) { 165 drm_gem_object_put(&bo->base.base); 166 } 167 168 v3d_job_free(ref); 169 } 170 171 static void 172 v3d_cpu_job_free(struct kref *ref) 173 { 174 struct v3d_cpu_job *job = container_of(ref, struct v3d_cpu_job, 175 base.refcount); 176 177 v3d_timestamp_query_info_free(&job->timestamp_query, 178 job->timestamp_query.count); 179 180 v3d_performance_query_info_free(&job->performance_query, 181 job->performance_query.count); 182 183 if (job->indirect_csd.indirect) 184 drm_gem_object_put(job->indirect_csd.indirect); 185 186 v3d_job_free(ref); 187 } 188 189 void v3d_job_cleanup(struct v3d_job *job) 190 { 191 if (!job) 192 return; 193 194 drm_sched_job_cleanup(&job->base); 195 v3d_job_put(job); 196 } 197 198 void v3d_job_put(struct v3d_job *job) 199 { 200 if (!job) 201 return; 202 203 kref_put(&job->refcount, job->free); 204 } 205 206 static int 207 v3d_job_add_syncobjs(struct v3d_job *job, struct drm_file *file_priv, 208 u32 in_sync, struct v3d_submit_ext *se) 209 { 210 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC); 211 struct v3d_dev *v3d = job->v3d; 212 int ret = 0; 213 214 if (!has_multisync) { 215 /* Ignore syncobj if its handle is zero */ 216 if (in_sync) 217 ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, 218 in_sync, 0); 219 return ret; 220 } 221 222 if (se->in_sync_count && se->wait_stage == job->queue) { 223 struct drm_v3d_sem __user *handle = u64_to_user_ptr(se->in_syncs); 224 225 for (int i = 0; i < se->in_sync_count; i++) { 226 struct drm_v3d_sem in; 227 228 if (copy_from_user(&in, handle++, sizeof(in))) { 229 drm_dbg(&v3d->drm, "Failed to copy wait dep handle.\n"); 230 return -EFAULT; 231 } 232 233 /* Ignore syncobj if its handle is zero */ 234 if (in.handle) { 235 ret = drm_sched_job_add_syncobj_dependency(&job->base, 236 file_priv, in.handle, 0); 237 if (ret) 238 return ret; 239 } 240 } 241 } 242 243 return 0; 244 } 245 246 static const struct { 247 size_t size; 248 void (*free)(struct kref *ref); 249 bool has_implicit_dep; 250 } v3d_job_types[] = { 251 [V3D_BIN] = { sizeof(struct v3d_bin_job), v3d_job_free, false }, 252 [V3D_RENDER] = { sizeof(struct v3d_render_job), v3d_render_job_free, true }, 253 [V3D_TFU] = { sizeof(struct v3d_tfu_job), v3d_job_free, true }, 254 [V3D_CSD] = { sizeof(struct v3d_csd_job), v3d_job_free, true }, 255 [V3D_CACHE_CLEAN] = { sizeof(struct v3d_job), v3d_job_free, false }, 256 [V3D_CPU] = { sizeof(struct v3d_cpu_job), v3d_cpu_job_free, true }, 257 }; 258 259 static struct v3d_job * 260 v3d_submit_add_job(struct v3d_submit *submit, enum v3d_queue queue) 261 { 262 struct v3d_file_priv *v3d_priv = submit->file_priv->driver_priv; 263 struct v3d_dev *v3d = submit->v3d; 264 struct v3d_job *job; 265 int ret; 266 267 if (queue >= V3D_MAX_QUEUES) 268 return ERR_PTR(-EINVAL); 269 270 job = kzalloc(v3d_job_types[queue].size, GFP_KERNEL); 271 if (!job) 272 return ERR_PTR(-ENOMEM); 273 274 job->v3d = v3d; 275 job->queue = queue; 276 job->file_priv = v3d_priv; 277 job->free = v3d_job_types[queue].free; 278 job->has_implicit_dep = v3d_job_types[queue].has_implicit_dep; 279 280 ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue], 281 1, v3d_priv, submit->file_priv->client_id); 282 if (ret) 283 goto fail_free; 284 285 /* CPU jobs don't require hardware resources */ 286 if (queue != V3D_CPU) { 287 ret = v3d_pm_runtime_get(v3d); 288 if (ret) 289 goto fail_sched_job; 290 job->has_pm_ref = true; 291 } 292 293 kref_init(&job->refcount); 294 295 job->client_stats = v3d_stats_get(v3d_priv->stats[queue]); 296 job->global_stats = v3d_stats_get(v3d->queue[queue].stats); 297 298 submit->jobs[submit->job_count++] = job; 299 300 return job; 301 302 fail_sched_job: 303 drm_sched_job_cleanup(&job->base); 304 fail_free: 305 kfree(job); 306 return ERR_PTR(ret); 307 } 308 309 static void 310 v3d_submit_put_jobs(struct v3d_submit *submit) 311 { 312 for (int i = 0; i < submit->job_count; i++) 313 v3d_job_put(submit->jobs[i]); 314 } 315 316 static void 317 v3d_submit_cleanup_jobs(struct v3d_submit *submit) 318 { 319 for (int i = 0; i < submit->job_count; i++) 320 v3d_job_cleanup(submit->jobs[i]); 321 } 322 323 static int 324 v3d_attach_perfmon_to_jobs(struct v3d_submit *submit, u32 perfmon_id) 325 { 326 struct v3d_file_priv *v3d_priv = submit->file_priv->driver_priv; 327 struct v3d_dev *v3d = submit->v3d; 328 struct v3d_perfmon *perfmon; 329 330 if (!perfmon_id) 331 return 0; 332 333 scoped_guard(spinlock_irqsave, &v3d->perfmon_state.lock) { 334 if (v3d->global_perfmon) 335 return -EAGAIN; 336 } 337 338 perfmon = v3d_perfmon_find(v3d_priv, perfmon_id); 339 if (!perfmon) 340 return -ENOENT; 341 342 for (int i = 0; i < submit->job_count; i++) { 343 submit->jobs[i]->perfmon = perfmon; 344 if (i != 0) 345 v3d_perfmon_get(perfmon); 346 } 347 348 return 0; 349 } 350 351 /* 352 * Prepare fences to enforce job serialization when a perfmon is active. A job 353 * that carries a non-global perfmon must wait for every job currently in-flight 354 * across all HW queues to finish, otherwise concurrent unrelated work on the 355 * same core would pollute the performance counters. Symmetrically, while such a 356 * job is still in-flight, all subsequently submitted jobs must wait for it. 357 * 358 * We don't serialize the jobs when using a global perfmon as it's expected to 359 * track concurrent activity from all jobs. 360 */ 361 static int 362 v3d_serialize_for_perfmon(struct v3d_job *job) 363 { 364 struct v3d_dev *v3d = job->v3d; 365 struct dma_fence *merged; 366 bool is_global_perfmon; 367 int ret; 368 369 lockdep_assert_held(&v3d->sched_lock); 370 371 scoped_guard(spinlock_irqsave, &v3d->perfmon_state.lock) 372 is_global_perfmon = !!v3d->global_perfmon; 373 374 if (is_global_perfmon) 375 goto publish; 376 377 if (job->perfmon) { 378 for (enum v3d_queue q = 0; q < V3D_MAX_QUEUES; q++) { 379 struct dma_fence *f = v3d->perfmon_state.last_hw_fence[q]; 380 381 if (!f || dma_fence_is_signaled(f)) 382 continue; 383 384 ret = drm_sched_job_add_dependency(&job->base, dma_fence_get(f)); 385 if (ret) 386 return ret; 387 } 388 } else if (v3d->perfmon_state.fence && 389 !dma_fence_is_signaled(v3d->perfmon_state.fence)) { 390 ret = drm_sched_job_add_dependency(&job->base, 391 dma_fence_get(v3d->perfmon_state.fence)); 392 if (ret) 393 return ret; 394 } 395 396 publish: 397 /* 398 * Accumulate every in-flight job on this queue into one merged fence. 399 * A HW queue is fed by several scheduler entities (one per-fd), so jobs 400 * on it can complete out of order. 401 */ 402 merged = dma_fence_unwrap_merge(v3d->perfmon_state.last_hw_fence[job->queue], 403 job->done_fence); 404 if (!merged) 405 return -ENOMEM; 406 407 dma_fence_put(v3d->perfmon_state.last_hw_fence[job->queue]); 408 v3d->perfmon_state.last_hw_fence[job->queue] = merged; 409 410 if (job->perfmon && !is_global_perfmon) { 411 dma_fence_put(v3d->perfmon_state.fence); 412 v3d->perfmon_state.fence = dma_fence_get(job->done_fence); 413 } 414 415 return 0; 416 } 417 418 static void 419 v3d_submit_attach_object_fences(struct v3d_submit *submit) 420 { 421 struct v3d_job *last_job = submit->jobs[submit->job_count - 1]; 422 423 /* The submission's last fence covers the entire submission. Attach it 424 * to every BO touched by any job in the submission. 425 */ 426 for (int i = 0; i < submit->job_count; i++) { 427 struct v3d_job *job = submit->jobs[i]; 428 429 for (int j = 0; j < job->bo_count; j++) { 430 /* XXX: Use shared fences for read-only objects. */ 431 dma_resv_add_fence(job->bo[j]->resv, last_job->done_fence, 432 DMA_RESV_USAGE_WRITE); 433 } 434 } 435 } 436 437 static void 438 v3d_submit_process_post_deps(struct v3d_submit *submit, struct drm_syncobj *sync_out, 439 struct v3d_submit_ext *se) 440 { 441 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC); 442 struct v3d_job *last_job = submit->jobs[submit->job_count - 1]; 443 444 /* Make sure single syncobj and multisync are mutually exclusive */ 445 WARN_ON_ONCE(sync_out && has_multisync); 446 447 /* Update the return sync object for the job */ 448 /* If it only supports a single signal semaphore*/ 449 if (!has_multisync) { 450 if (sync_out) { 451 drm_syncobj_replace_fence(sync_out, last_job->done_fence); 452 drm_syncobj_put(sync_out); 453 } 454 return; 455 } 456 457 /* If multiple semaphores extension is supported */ 458 if (se->out_sync_count) { 459 for (int i = 0; i < se->out_sync_count; i++) { 460 drm_syncobj_replace_fence(se->out_syncs[i].syncobj, 461 last_job->done_fence); 462 drm_syncobj_put(se->out_syncs[i].syncobj); 463 } 464 kvfree(se->out_syncs); 465 } 466 } 467 468 static int 469 v3d_submit_jobs(struct v3d_submit *submit, struct drm_syncobj *sync_out, 470 struct v3d_submit_ext *se) 471 { 472 struct v3d_dev *v3d = submit->v3d; 473 int ret = 0; 474 475 mutex_lock(&v3d->sched_lock); 476 477 for (int i = 0; i < submit->job_count; i++) { 478 struct v3d_job *job = submit->jobs[i]; 479 480 drm_sched_job_arm(&job->base); 481 job->done_fence = dma_fence_get(&job->base.s_fence->finished); 482 483 /* put by scheduler job completion */ 484 kref_get(&job->refcount); 485 } 486 487 for (int i = 1; i < submit->job_count; i++) { 488 ret = drm_sched_job_add_dependency(&submit->jobs[i]->base, 489 dma_fence_get(submit->jobs[i - 1]->done_fence)); 490 if (ret) 491 goto err; 492 } 493 494 for (int i = 0; i < submit->job_count; i++) { 495 ret = v3d_serialize_for_perfmon(submit->jobs[i]); 496 if (ret) 497 goto err; 498 } 499 500 for (int i = 0; i < submit->job_count; i++) 501 drm_sched_entity_push_job(&submit->jobs[i]->base); 502 503 mutex_unlock(&v3d->sched_lock); 504 505 v3d_submit_attach_object_fences(submit); 506 v3d_submit_unlock_reservations(submit); 507 v3d_submit_process_post_deps(submit, sync_out, se); 508 509 v3d_submit_put_jobs(submit); 510 511 return 0; 512 513 err: 514 /* Mark every armed job as failed so run_job() skips execution */ 515 for (int i = 0; i < submit->job_count; i++) 516 dma_fence_set_error(&submit->jobs[i]->base.s_fence->finished, ret); 517 518 for (int i = 0; i < submit->job_count; i++) 519 drm_sched_entity_push_job(&submit->jobs[i]->base); 520 521 mutex_unlock(&v3d->sched_lock); 522 523 v3d_submit_unlock_reservations(submit); 524 v3d_submit_put_jobs(submit); 525 526 return ret; 527 } 528 529 static int 530 v3d_setup_csd_jobs_and_bos(struct v3d_submit *submit, 531 struct drm_v3d_submit_csd *args, 532 struct v3d_submit_ext *se) 533 { 534 struct v3d_csd_job *job; 535 struct v3d_job *clean_job; 536 int ret; 537 538 job = (struct v3d_csd_job *)v3d_submit_add_job(submit, V3D_CSD); 539 if (IS_ERR(job)) 540 return PTR_ERR(job); 541 542 ret = v3d_job_add_syncobjs(&job->base, submit->file_priv, args->in_sync, se); 543 if (ret) 544 return ret; 545 546 ret = v3d_lookup_bos(submit->file_priv, &job->base, args->bo_handles, 547 args->bo_handle_count); 548 if (ret) 549 return ret; 550 551 job->args = *args; 552 553 clean_job = v3d_submit_add_job(submit, V3D_CACHE_CLEAN); 554 if (IS_ERR(clean_job)) 555 return PTR_ERR(clean_job); 556 557 return v3d_job_reference_bos(clean_job, &job->base); 558 } 559 560 static void 561 v3d_submit_put_post_deps(struct drm_syncobj *sync_out, struct v3d_submit_ext *se) 562 { 563 unsigned int i; 564 565 if (sync_out) 566 drm_syncobj_put(sync_out); 567 568 if (!(se && se->out_sync_count)) 569 return; 570 571 for (i = 0; i < se->out_sync_count; i++) 572 drm_syncobj_put(se->out_syncs[i].syncobj); 573 kvfree(se->out_syncs); 574 } 575 576 static int 577 v3d_get_multisync_post_deps(struct drm_file *file_priv, 578 struct v3d_submit_ext *se, 579 u32 count, u64 handles) 580 { 581 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 582 struct v3d_dev *v3d = v3d_priv->v3d; 583 struct drm_v3d_sem __user *post_deps; 584 int i, ret; 585 586 if (!count) 587 return 0; 588 589 se->out_syncs = (struct v3d_submit_outsync *) 590 kvmalloc_objs(struct v3d_submit_outsync, count); 591 if (!se->out_syncs) 592 return -ENOMEM; 593 594 post_deps = u64_to_user_ptr(handles); 595 596 for (i = 0; i < count; i++) { 597 struct drm_v3d_sem out; 598 599 if (copy_from_user(&out, post_deps++, sizeof(out))) { 600 ret = -EFAULT; 601 drm_dbg(&v3d->drm, "Failed to copy post dep handles\n"); 602 goto fail; 603 } 604 605 se->out_syncs[i].syncobj = drm_syncobj_find(file_priv, 606 out.handle); 607 if (!se->out_syncs[i].syncobj) { 608 ret = -EINVAL; 609 goto fail; 610 } 611 } 612 se->out_sync_count = count; 613 614 return 0; 615 616 fail: 617 for (i--; i >= 0; i--) 618 drm_syncobj_put(se->out_syncs[i].syncobj); 619 kvfree(se->out_syncs); 620 621 return ret; 622 } 623 624 /* Get data for multiple binary semaphores synchronization. Parse syncobj 625 * to be signaled when job completes (out_sync). 626 */ 627 static int 628 v3d_get_multisync_submit_deps(struct drm_file *file_priv, 629 struct drm_v3d_extension __user *ext, 630 struct v3d_submit_ext *se) 631 { 632 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 633 struct v3d_dev *v3d = v3d_priv->v3d; 634 struct drm_v3d_multi_sync multisync; 635 int ret; 636 637 if (se->in_sync_count || se->out_sync_count) { 638 drm_dbg(&v3d->drm, "Two multisync extensions were added to the same job."); 639 return -EINVAL; 640 } 641 642 if (copy_from_user(&multisync, ext, sizeof(multisync))) 643 return -EFAULT; 644 645 if (multisync.pad) 646 return -EINVAL; 647 648 if (!multisync.in_sync_count && !multisync.out_sync_count) { 649 drm_dbg(&v3d->drm, "Empty multisync extension\n"); 650 return -EINVAL; 651 } 652 653 ret = v3d_get_multisync_post_deps(file_priv, se, multisync.out_sync_count, 654 multisync.out_syncs); 655 if (ret) 656 return ret; 657 658 se->in_sync_count = multisync.in_sync_count; 659 se->in_syncs = multisync.in_syncs; 660 se->flags |= DRM_V3D_EXT_ID_MULTI_SYNC; 661 se->wait_stage = multisync.wait_stage; 662 663 return 0; 664 } 665 666 /* Returns false if the CPU job has an invalid configuration. */ 667 static bool 668 v3d_validate_cpu_job(struct drm_file *file_priv, struct v3d_cpu_job *job) 669 { 670 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 671 struct v3d_dev *v3d = v3d_priv->v3d; 672 673 if (!job) { 674 drm_dbg(&v3d->drm, "CPU job extension was attached to a GPU job.\n"); 675 return false; 676 } 677 678 if (job->job_type) { 679 drm_dbg(&v3d->drm, "Two CPU job extensions were added to the same CPU job.\n"); 680 return false; 681 } 682 683 return true; 684 } 685 686 /* Get data for the indirect CSD job submission. */ 687 static int 688 v3d_get_cpu_indirect_csd_params(struct drm_file *file_priv, 689 struct drm_v3d_extension __user *ext, 690 struct v3d_cpu_job *job) 691 { 692 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 693 struct v3d_dev *v3d = v3d_priv->v3d; 694 struct drm_v3d_indirect_csd indirect_csd; 695 struct v3d_indirect_csd_info *info = &job->indirect_csd; 696 697 if (!v3d_validate_cpu_job(file_priv, job)) 698 return -EINVAL; 699 700 if (copy_from_user(&indirect_csd, ext, sizeof(indirect_csd))) 701 return -EFAULT; 702 703 if (!v3d_has_csd(v3d)) { 704 drm_warn(&v3d->drm, "Attempting CSD submit on non-CSD hardware.\n"); 705 return -EINVAL; 706 } 707 708 job->job_type = V3D_CPU_JOB_TYPE_INDIRECT_CSD; 709 info->args = indirect_csd.submit; 710 info->offset = indirect_csd.offset; 711 info->wg_size = indirect_csd.wg_size; 712 memcpy(&info->wg_uniform_offsets, &indirect_csd.wg_uniform_offsets, 713 sizeof(indirect_csd.wg_uniform_offsets)); 714 715 info->indirect = drm_gem_object_lookup(file_priv, indirect_csd.indirect); 716 717 return 0; 718 } 719 720 /* Get data for the query timestamp job submission. */ 721 static int 722 v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv, 723 struct drm_v3d_extension __user *ext, 724 struct v3d_cpu_job *job) 725 { 726 u32 __user *offsets, *syncs; 727 struct drm_v3d_timestamp_query timestamp; 728 struct v3d_timestamp_query_info *query_info = &job->timestamp_query; 729 unsigned int i; 730 int err; 731 732 if (!v3d_validate_cpu_job(file_priv, job)) 733 return -EINVAL; 734 735 if (copy_from_user(×tamp, ext, sizeof(timestamp))) 736 return -EFAULT; 737 738 if (timestamp.pad) 739 return -EINVAL; 740 741 job->job_type = V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY; 742 743 query_info->queries = kvmalloc_objs(struct v3d_timestamp_query, 744 timestamp.count); 745 if (!query_info->queries) 746 return -ENOMEM; 747 748 offsets = u64_to_user_ptr(timestamp.offsets); 749 syncs = u64_to_user_ptr(timestamp.syncs); 750 751 for (i = 0; i < timestamp.count; i++) { 752 u32 offset, sync; 753 754 if (get_user(offset, offsets++)) { 755 err = -EFAULT; 756 goto error; 757 } 758 759 query_info->queries[i].offset = offset; 760 761 if (get_user(sync, syncs++)) { 762 err = -EFAULT; 763 goto error; 764 } 765 766 query_info->queries[i].syncobj = drm_syncobj_find(file_priv, 767 sync); 768 if (!query_info->queries[i].syncobj) { 769 err = -ENOENT; 770 goto error; 771 } 772 } 773 query_info->count = timestamp.count; 774 775 return 0; 776 777 error: 778 v3d_timestamp_query_info_free(&job->timestamp_query, i); 779 return err; 780 } 781 782 static int 783 v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv, 784 struct drm_v3d_extension __user *ext, 785 struct v3d_cpu_job *job) 786 { 787 u32 __user *syncs; 788 struct drm_v3d_reset_timestamp_query reset; 789 struct v3d_timestamp_query_info *query_info = &job->timestamp_query; 790 unsigned int i; 791 int err; 792 793 if (!v3d_validate_cpu_job(file_priv, job)) 794 return -EINVAL; 795 796 if (copy_from_user(&reset, ext, sizeof(reset))) 797 return -EFAULT; 798 799 job->job_type = V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY; 800 801 query_info->queries = kvmalloc_objs(struct v3d_timestamp_query, 802 reset.count); 803 if (!query_info->queries) 804 return -ENOMEM; 805 806 syncs = u64_to_user_ptr(reset.syncs); 807 808 for (i = 0; i < reset.count; i++) { 809 u32 sync; 810 811 query_info->queries[i].offset = reset.offset + 8 * i; 812 813 if (get_user(sync, syncs++)) { 814 err = -EFAULT; 815 goto error; 816 } 817 818 query_info->queries[i].syncobj = drm_syncobj_find(file_priv, 819 sync); 820 if (!query_info->queries[i].syncobj) { 821 err = -ENOENT; 822 goto error; 823 } 824 } 825 query_info->count = reset.count; 826 827 return 0; 828 829 error: 830 v3d_timestamp_query_info_free(&job->timestamp_query, i); 831 return err; 832 } 833 834 /* Get data for the copy timestamp query results job submission. */ 835 static int 836 v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv, 837 struct drm_v3d_extension __user *ext, 838 struct v3d_cpu_job *job) 839 { 840 u32 __user *offsets, *syncs; 841 struct drm_v3d_copy_timestamp_query copy; 842 struct v3d_timestamp_query_info *query_info = &job->timestamp_query; 843 unsigned int i; 844 int err; 845 846 if (!v3d_validate_cpu_job(file_priv, job)) 847 return -EINVAL; 848 849 if (copy_from_user(©, ext, sizeof(copy))) 850 return -EFAULT; 851 852 if (copy.pad) 853 return -EINVAL; 854 855 job->job_type = V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY; 856 857 query_info->queries = kvmalloc_objs(struct v3d_timestamp_query, 858 copy.count); 859 if (!query_info->queries) 860 return -ENOMEM; 861 862 offsets = u64_to_user_ptr(copy.offsets); 863 syncs = u64_to_user_ptr(copy.syncs); 864 865 for (i = 0; i < copy.count; i++) { 866 u32 offset, sync; 867 868 if (get_user(offset, offsets++)) { 869 err = -EFAULT; 870 goto error; 871 } 872 873 query_info->queries[i].offset = offset; 874 875 if (get_user(sync, syncs++)) { 876 err = -EFAULT; 877 goto error; 878 } 879 880 query_info->queries[i].syncobj = drm_syncobj_find(file_priv, 881 sync); 882 if (!query_info->queries[i].syncobj) { 883 err = -ENOENT; 884 goto error; 885 } 886 } 887 query_info->count = copy.count; 888 889 job->copy.do_64bit = copy.do_64bit; 890 job->copy.do_partial = copy.do_partial; 891 job->copy.availability_bit = copy.availability_bit; 892 job->copy.offset = copy.offset; 893 job->copy.stride = copy.stride; 894 895 return 0; 896 897 error: 898 v3d_timestamp_query_info_free(&job->timestamp_query, i); 899 return err; 900 } 901 902 static int 903 v3d_copy_query_info(struct v3d_performance_query_info *query_info, 904 unsigned int count, 905 unsigned int nperfmons, 906 u32 __user *syncs, 907 u64 __user *kperfmon_ids, 908 struct drm_file *file_priv) 909 { 910 unsigned int i, j; 911 int err; 912 913 for (i = 0; i < count; i++) { 914 struct v3d_performance_query *query = &query_info->queries[i]; 915 u32 __user *ids_pointer; 916 u32 sync, id; 917 u64 ids; 918 919 if (get_user(sync, syncs++)) { 920 err = -EFAULT; 921 goto error; 922 } 923 924 if (get_user(ids, kperfmon_ids++)) { 925 err = -EFAULT; 926 goto error; 927 } 928 929 query->kperfmon_ids = 930 kvmalloc_array(nperfmons, 931 sizeof(struct v3d_performance_query *), 932 GFP_KERNEL); 933 if (!query->kperfmon_ids) { 934 err = -ENOMEM; 935 goto error; 936 } 937 938 ids_pointer = u64_to_user_ptr(ids); 939 940 for (j = 0; j < nperfmons; j++) { 941 if (get_user(id, ids_pointer++)) { 942 kvfree(query->kperfmon_ids); 943 err = -EFAULT; 944 goto error; 945 } 946 947 query->kperfmon_ids[j] = id; 948 } 949 950 query->syncobj = drm_syncobj_find(file_priv, sync); 951 if (!query->syncobj) { 952 kvfree(query->kperfmon_ids); 953 err = -ENOENT; 954 goto error; 955 } 956 } 957 958 return 0; 959 960 error: 961 v3d_performance_query_info_free(query_info, i); 962 return err; 963 } 964 965 static int 966 v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, 967 struct drm_v3d_extension __user *ext, 968 struct v3d_cpu_job *job) 969 { 970 struct v3d_performance_query_info *query_info = &job->performance_query; 971 struct drm_v3d_reset_performance_query reset; 972 int err; 973 974 if (!v3d_validate_cpu_job(file_priv, job)) 975 return -EINVAL; 976 977 if (copy_from_user(&reset, ext, sizeof(reset))) 978 return -EFAULT; 979 980 job->job_type = V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY; 981 982 query_info->queries = 983 kvmalloc_objs(struct v3d_performance_query, reset.count); 984 if (!query_info->queries) 985 return -ENOMEM; 986 987 err = v3d_copy_query_info(query_info, 988 reset.count, 989 reset.nperfmons, 990 u64_to_user_ptr(reset.syncs), 991 u64_to_user_ptr(reset.kperfmon_ids), 992 file_priv); 993 if (err) 994 return err; 995 996 query_info->count = reset.count; 997 query_info->nperfmons = reset.nperfmons; 998 999 return 0; 1000 } 1001 1002 static int 1003 v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, 1004 struct drm_v3d_extension __user *ext, 1005 struct v3d_cpu_job *job) 1006 { 1007 struct v3d_performance_query_info *query_info = &job->performance_query; 1008 struct drm_v3d_copy_performance_query copy; 1009 int err; 1010 1011 if (!v3d_validate_cpu_job(file_priv, job)) 1012 return -EINVAL; 1013 1014 if (copy_from_user(©, ext, sizeof(copy))) 1015 return -EFAULT; 1016 1017 if (copy.pad) 1018 return -EINVAL; 1019 1020 job->job_type = V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY; 1021 1022 query_info->queries = 1023 kvmalloc_objs(struct v3d_performance_query, copy.count); 1024 if (!query_info->queries) 1025 return -ENOMEM; 1026 1027 err = v3d_copy_query_info(query_info, 1028 copy.count, 1029 copy.nperfmons, 1030 u64_to_user_ptr(copy.syncs), 1031 u64_to_user_ptr(copy.kperfmon_ids), 1032 file_priv); 1033 if (err) 1034 return err; 1035 1036 query_info->count = copy.count; 1037 query_info->nperfmons = copy.nperfmons; 1038 query_info->ncounters = copy.ncounters; 1039 1040 job->copy.do_64bit = copy.do_64bit; 1041 job->copy.do_partial = copy.do_partial; 1042 job->copy.availability_bit = copy.availability_bit; 1043 job->copy.offset = copy.offset; 1044 job->copy.stride = copy.stride; 1045 1046 return 0; 1047 } 1048 1049 /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data 1050 * according to the extension id (name). 1051 */ 1052 static int 1053 v3d_get_extensions(struct drm_file *file_priv, 1054 u64 ext_handles, 1055 struct v3d_submit_ext *se, 1056 struct v3d_cpu_job *job) 1057 { 1058 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 1059 struct v3d_dev *v3d = v3d_priv->v3d; 1060 struct drm_v3d_extension __user *user_ext; 1061 int ret; 1062 1063 user_ext = u64_to_user_ptr(ext_handles); 1064 while (user_ext) { 1065 struct drm_v3d_extension ext; 1066 1067 if (copy_from_user(&ext, user_ext, sizeof(ext))) { 1068 drm_dbg(&v3d->drm, "Failed to copy submit extension\n"); 1069 return -EFAULT; 1070 } 1071 1072 switch (ext.id) { 1073 case DRM_V3D_EXT_ID_MULTI_SYNC: 1074 ret = v3d_get_multisync_submit_deps(file_priv, user_ext, se); 1075 break; 1076 case DRM_V3D_EXT_ID_CPU_INDIRECT_CSD: 1077 ret = v3d_get_cpu_indirect_csd_params(file_priv, user_ext, job); 1078 break; 1079 case DRM_V3D_EXT_ID_CPU_TIMESTAMP_QUERY: 1080 ret = v3d_get_cpu_timestamp_query_params(file_priv, user_ext, job); 1081 break; 1082 case DRM_V3D_EXT_ID_CPU_RESET_TIMESTAMP_QUERY: 1083 ret = v3d_get_cpu_reset_timestamp_params(file_priv, user_ext, job); 1084 break; 1085 case DRM_V3D_EXT_ID_CPU_COPY_TIMESTAMP_QUERY: 1086 ret = v3d_get_cpu_copy_query_results_params(file_priv, user_ext, job); 1087 break; 1088 case DRM_V3D_EXT_ID_CPU_RESET_PERFORMANCE_QUERY: 1089 ret = v3d_get_cpu_reset_performance_params(file_priv, user_ext, job); 1090 break; 1091 case DRM_V3D_EXT_ID_CPU_COPY_PERFORMANCE_QUERY: 1092 ret = v3d_get_cpu_copy_performance_query_params(file_priv, user_ext, job); 1093 break; 1094 default: 1095 drm_dbg(&v3d->drm, "Unknown V3D extension ID: %d\n", ext.id); 1096 return -EINVAL; 1097 } 1098 1099 if (ret) 1100 return ret; 1101 1102 user_ext = u64_to_user_ptr(ext.next); 1103 } 1104 1105 return 0; 1106 } 1107 1108 /** 1109 * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D. 1110 * @dev: DRM device 1111 * @data: ioctl argument 1112 * @file_priv: DRM file for this fd 1113 * 1114 * This is the main entrypoint for userspace to submit a 3D frame to 1115 * the GPU. Userspace provides the binner command list (if 1116 * applicable), and the kernel sets up the render command list to draw 1117 * to the framebuffer described in the ioctl, using the command lists 1118 * that the 3D engine's binner will produce. 1119 */ 1120 int 1121 v3d_submit_cl_ioctl(struct drm_device *dev, void *data, 1122 struct drm_file *file_priv) 1123 { 1124 struct v3d_submit submit = { .v3d = to_v3d_dev(dev), .file_priv = file_priv }; 1125 struct drm_v3d_submit_cl *args = data; 1126 struct drm_syncobj *sync_out = NULL; 1127 struct v3d_submit_ext se = {0}; 1128 struct v3d_bin_job *bin = NULL; 1129 struct v3d_render_job *render; 1130 struct v3d_job *clean_job; 1131 int ret; 1132 1133 trace_v3d_submit_cl_ioctl(dev, args->rcl_start, args->rcl_end); 1134 1135 if (args->pad) 1136 return -EINVAL; 1137 1138 if (args->flags && 1139 args->flags & ~(DRM_V3D_SUBMIT_CL_FLUSH_CACHE | 1140 DRM_V3D_SUBMIT_EXTENSION)) { 1141 drm_dbg(dev, "invalid flags: %d\n", args->flags); 1142 return -EINVAL; 1143 } 1144 1145 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) { 1146 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL); 1147 if (ret) { 1148 drm_dbg(dev, "Failed to get extensions.\n"); 1149 return ret; 1150 } 1151 } 1152 1153 /* If multisync is configured, give priority to it and ignore out_sync. */ 1154 if (args->out_sync && !(se.flags & DRM_V3D_EXT_ID_MULTI_SYNC)) { 1155 sync_out = drm_syncobj_find(file_priv, args->out_sync); 1156 if (!sync_out) 1157 return -ENOENT; 1158 } 1159 1160 if (args->bcl_start != args->bcl_end) { 1161 bin = (struct v3d_bin_job *)v3d_submit_add_job(&submit, V3D_BIN); 1162 if (IS_ERR(bin)) { 1163 ret = PTR_ERR(bin); 1164 goto fail; 1165 } 1166 1167 bin->start = args->bcl_start; 1168 bin->end = args->bcl_end; 1169 bin->qma = args->qma; 1170 bin->qms = args->qms; 1171 bin->qts = args->qts; 1172 1173 ret = v3d_job_add_syncobjs(&bin->base, file_priv, args->in_sync_bcl, 1174 &se); 1175 if (ret) 1176 goto fail; 1177 } 1178 1179 render = (struct v3d_render_job *)v3d_submit_add_job(&submit, V3D_RENDER); 1180 if (IS_ERR(render)) { 1181 ret = PTR_ERR(render); 1182 goto fail; 1183 } 1184 1185 INIT_LIST_HEAD(&render->unref_list); 1186 render->start = args->rcl_start; 1187 render->end = args->rcl_end; 1188 1189 if (bin) 1190 bin->render = render; 1191 1192 ret = v3d_job_add_syncobjs(&render->base, file_priv, args->in_sync_rcl, &se); 1193 if (ret) 1194 goto fail; 1195 1196 /* 1197 * We don't associate the BOs with the BIN job. Fences are only 1198 * attached to the last job in the submission chain, and BIN jobs 1199 * don't need implicit dependencies since depending on results from 1200 * another context is not a realistic scenario for binning. 1201 */ 1202 ret = v3d_lookup_bos(submit.file_priv, &render->base, 1203 args->bo_handles, args->bo_handle_count); 1204 if (ret) 1205 goto fail; 1206 1207 if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) { 1208 clean_job = v3d_submit_add_job(&submit, V3D_CACHE_CLEAN); 1209 if (IS_ERR(clean_job)) { 1210 ret = PTR_ERR(clean_job); 1211 goto fail; 1212 } 1213 1214 ret = v3d_job_reference_bos(clean_job, &render->base); 1215 if (ret) 1216 goto fail; 1217 } 1218 1219 ret = v3d_attach_perfmon_to_jobs(&submit, args->perfmon_id); 1220 if (ret) 1221 goto fail; 1222 1223 ret = v3d_submit_lock_reservations(&submit); 1224 if (ret) 1225 goto fail; 1226 1227 ret = v3d_submit_jobs(&submit, sync_out, &se); 1228 if (ret) 1229 goto fail_submit; 1230 1231 return 0; 1232 1233 fail: 1234 v3d_submit_cleanup_jobs(&submit); 1235 fail_submit: 1236 v3d_submit_put_post_deps(sync_out, &se); 1237 1238 return ret; 1239 } 1240 1241 /** 1242 * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D. 1243 * @dev: DRM device 1244 * @data: ioctl argument 1245 * @file_priv: DRM file for this fd 1246 * 1247 * Userspace provides the register setup for the TFU, which we don't 1248 * need to validate since the TFU is behind the MMU. 1249 */ 1250 int 1251 v3d_submit_tfu_ioctl(struct drm_device *dev, void *data, 1252 struct drm_file *file_priv) 1253 { 1254 struct v3d_submit submit = { .v3d = to_v3d_dev(dev), .file_priv = file_priv }; 1255 struct drm_v3d_submit_tfu *args = data; 1256 struct drm_syncobj *sync_out = NULL; 1257 struct v3d_submit_ext se = {0}; 1258 struct v3d_tfu_job *job; 1259 int ret = 0; 1260 1261 trace_v3d_submit_tfu_ioctl(dev, args->iia); 1262 1263 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) { 1264 drm_dbg(dev, "invalid flags: %d\n", args->flags); 1265 return -EINVAL; 1266 } 1267 1268 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) { 1269 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL); 1270 if (ret) { 1271 drm_dbg(dev, "Failed to get extensions.\n"); 1272 return ret; 1273 } 1274 } 1275 1276 /* If multisync is configured, give priority to it and ignore out_sync. */ 1277 if (args->out_sync && !(se.flags & DRM_V3D_EXT_ID_MULTI_SYNC)) { 1278 sync_out = drm_syncobj_find(file_priv, args->out_sync); 1279 if (!sync_out) 1280 return -ENOENT; 1281 } 1282 1283 job = (struct v3d_tfu_job *)v3d_submit_add_job(&submit, V3D_TFU); 1284 if (IS_ERR(job)) { 1285 ret = PTR_ERR(job); 1286 goto fail; 1287 } 1288 1289 ret = v3d_job_add_syncobjs(&job->base, file_priv, args->in_sync, &se); 1290 if (ret) 1291 goto fail; 1292 1293 job->base.bo = kzalloc_objs(*job->base.bo, ARRAY_SIZE(args->bo_handles)); 1294 if (!job->base.bo) { 1295 ret = -ENOMEM; 1296 goto fail; 1297 } 1298 1299 job->args = *args; 1300 1301 for (job->base.bo_count = 0; 1302 job->base.bo_count < ARRAY_SIZE(args->bo_handles); 1303 job->base.bo_count++) { 1304 struct drm_gem_object *bo; 1305 1306 if (!args->bo_handles[job->base.bo_count]) 1307 break; 1308 1309 bo = drm_gem_object_lookup(file_priv, args->bo_handles[job->base.bo_count]); 1310 if (!bo) { 1311 drm_dbg(dev, "Failed to look up GEM BO %d: %d\n", 1312 job->base.bo_count, 1313 args->bo_handles[job->base.bo_count]); 1314 ret = -ENOENT; 1315 goto fail; 1316 } 1317 job->base.bo[job->base.bo_count] = bo; 1318 } 1319 1320 ret = v3d_submit_lock_reservations(&submit); 1321 if (ret) 1322 goto fail; 1323 1324 ret = v3d_submit_jobs(&submit, sync_out, &se); 1325 if (ret) 1326 goto fail_submit; 1327 1328 return 0; 1329 1330 fail: 1331 v3d_submit_cleanup_jobs(&submit); 1332 fail_submit: 1333 v3d_submit_put_post_deps(sync_out, &se); 1334 1335 return ret; 1336 } 1337 1338 /** 1339 * v3d_submit_csd_ioctl() - Submits a CSD (compute shader) job to the V3D. 1340 * @dev: DRM device 1341 * @data: ioctl argument 1342 * @file_priv: DRM file for this fd 1343 * 1344 * Userspace provides the register setup for the CSD, which we don't 1345 * need to validate since the CSD is behind the MMU. 1346 */ 1347 int 1348 v3d_submit_csd_ioctl(struct drm_device *dev, void *data, 1349 struct drm_file *file_priv) 1350 { 1351 struct v3d_submit submit = { .v3d = to_v3d_dev(dev), .file_priv = file_priv }; 1352 struct drm_v3d_submit_csd *args = data; 1353 struct drm_syncobj *sync_out = NULL; 1354 struct v3d_submit_ext se = {0}; 1355 int ret; 1356 1357 trace_v3d_submit_csd_ioctl(dev, args->cfg[5], args->cfg[6]); 1358 1359 if (args->pad) 1360 return -EINVAL; 1361 1362 if (!v3d_has_csd(submit.v3d)) { 1363 drm_warn(dev, "Attempting CSD submit on non-CSD hardware\n"); 1364 return -EINVAL; 1365 } 1366 1367 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) { 1368 drm_dbg(dev, "invalid flags: %d\n", args->flags); 1369 return -EINVAL; 1370 } 1371 1372 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) { 1373 ret = v3d_get_extensions(file_priv, args->extensions, &se, NULL); 1374 if (ret) { 1375 drm_dbg(dev, "Failed to get extensions.\n"); 1376 return ret; 1377 } 1378 } 1379 1380 /* If multisync is configured, give priority to it and ignore out_sync. */ 1381 if (args->out_sync && !(se.flags & DRM_V3D_EXT_ID_MULTI_SYNC)) { 1382 sync_out = drm_syncobj_find(file_priv, args->out_sync); 1383 if (!sync_out) 1384 return -ENOENT; 1385 } 1386 1387 ret = v3d_setup_csd_jobs_and_bos(&submit, args, &se); 1388 if (ret) 1389 goto fail; 1390 1391 ret = v3d_attach_perfmon_to_jobs(&submit, args->perfmon_id); 1392 if (ret) 1393 goto fail; 1394 1395 ret = v3d_submit_lock_reservations(&submit); 1396 if (ret) 1397 goto fail; 1398 1399 ret = v3d_submit_jobs(&submit, sync_out, &se); 1400 if (ret) 1401 goto fail_submit; 1402 1403 return 0; 1404 1405 fail: 1406 v3d_submit_cleanup_jobs(&submit); 1407 fail_submit: 1408 v3d_submit_put_post_deps(sync_out, &se); 1409 1410 return ret; 1411 } 1412 1413 static const unsigned int cpu_job_bo_handle_count[] = { 1414 [V3D_CPU_JOB_TYPE_INDIRECT_CSD] = 1, 1415 [V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY] = 1, 1416 [V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY] = 1, 1417 [V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY] = 2, 1418 [V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY] = 0, 1419 [V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY] = 1, 1420 }; 1421 1422 /* Reject offset + (count - 1) * stride + write_size if it leaves the BO. */ 1423 static int 1424 v3d_check_copy_extent(struct drm_device *dev, size_t bo_size, 1425 u32 offset, u32 stride, u32 count, u64 write_size) 1426 { 1427 u64 last; 1428 1429 if (!count) 1430 return 0; 1431 1432 /* 1433 * The executors walk a u8 * cursor, so the furthest written byte is 1434 * offset + (count - 1) * stride + write_size, matching the pointer 1435 * arithmetic in v3d_copy_query_results()/v3d_copy_performance_query(). 1436 * (count - 1) * stride is a u32 * u32 product that is exact in u64, 1437 * and offset + write_size stays far below the u64 range, so a single 1438 * overflow check guards the total. 1439 */ 1440 last = write_size + offset; 1441 if (check_add_overflow((u64)(count - 1) * stride, last, &last) || 1442 last > bo_size) { 1443 drm_dbg(dev, "CPU job copy buffer exceeds the destination BO.\n"); 1444 return -EINVAL; 1445 } 1446 1447 return 0; 1448 } 1449 1450 /* Reject a query CPU job whose writes would land outside their BO. */ 1451 static int 1452 v3d_cpu_job_bounds_check(struct v3d_cpu_job *job) 1453 { 1454 struct drm_device *dev = &job->base.v3d->drm; 1455 struct v3d_timestamp_query_info *tquery = &job->timestamp_query; 1456 struct v3d_copy_query_results_info *copy = &job->copy; 1457 u32 elem = copy->do_64bit ? sizeof(u64) : sizeof(u32); 1458 struct v3d_bo *dst, *src; 1459 u64 slots, write_size; 1460 u32 i; 1461 1462 switch (job->job_type) { 1463 case V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY: 1464 case V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY: 1465 /* Each query writes one u64 timestamp slot into bo[0]. */ 1466 dst = to_v3d_bo(job->base.bo[0]); 1467 1468 for (i = 0; i < tquery->count; i++) { 1469 if ((u64)tquery->queries[i].offset + sizeof(u64) > 1470 dst->base.base.size) 1471 goto err_range; 1472 } 1473 return 0; 1474 case V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY: 1475 /* Copies one u64 per query from bo[1] into bo[0]. */ 1476 dst = to_v3d_bo(job->base.bo[0]); 1477 src = to_v3d_bo(job->base.bo[1]); 1478 1479 for (i = 0; i < tquery->count; i++) { 1480 if ((u64)tquery->queries[i].offset + sizeof(u64) > 1481 src->base.base.size) 1482 goto err_range; 1483 } 1484 1485 write_size = (copy->availability_bit ? 2 : 1) * elem; 1486 return v3d_check_copy_extent(dev, dst->base.base.size, 1487 copy->offset, copy->stride, 1488 tquery->count, write_size); 1489 case V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY: 1490 /* 1491 * Each query writes nperfmons * DRM_V3D_MAX_PERF_COUNTERS 1492 * counter slots into bo[0], plus an availability slot at 1493 * index ncounters. nperfmons and ncounters are user values, 1494 * so the slot count is computed overflow-safe. 1495 */ 1496 dst = to_v3d_bo(job->base.bo[0]); 1497 1498 slots = (u64)job->performance_query.nperfmons * 1499 DRM_V3D_MAX_PERF_COUNTERS; 1500 if (copy->availability_bit) 1501 slots = max(slots, 1502 (u64)job->performance_query.ncounters + 1); 1503 1504 write_size = slots * elem; 1505 return v3d_check_copy_extent(dev, dst->base.base.size, 1506 copy->offset, copy->stride, 1507 job->performance_query.count, 1508 write_size); 1509 case V3D_CPU_JOB_TYPE_INDIRECT_CSD: { 1510 struct v3d_indirect_csd_info *indirect_csd = &job->indirect_csd; 1511 1512 /* 3 is the three dimensions (x, y, z) of the workgroup counts. */ 1513 src = to_v3d_bo(job->base.bo[0]); 1514 if ((u64)indirect_csd->offset + 3 * sizeof(u32) > 1515 src->base.base.size) 1516 goto err_range; 1517 1518 dst = to_v3d_bo(indirect_csd->indirect); 1519 for (i = 0; i < 3; i++) { 1520 u32 uidx = indirect_csd->wg_uniform_offsets[i]; 1521 1522 /* 1523 * 0xffffffff means "skip this rewrite", so the exec 1524 * path never writes that index and it needs no check. 1525 */ 1526 if (uidx != 0xffffffff && 1527 (u64)uidx * sizeof(u32) + sizeof(u32) > 1528 dst->base.base.size) 1529 goto err_range; 1530 } 1531 return 0; 1532 } 1533 default: 1534 return 0; 1535 } 1536 1537 err_range: 1538 drm_dbg(dev, "CPU job query offset exceeds the BO.\n"); 1539 return -EINVAL; 1540 } 1541 1542 /** 1543 * v3d_submit_cpu_ioctl() - Submits a CPU job to the V3D. 1544 * @dev: DRM device 1545 * @data: ioctl argument 1546 * @file_priv: DRM file for this fd 1547 * 1548 * Userspace specifies the CPU job type and data required to perform its 1549 * operations through the drm_v3d_extension struct. 1550 */ 1551 int 1552 v3d_submit_cpu_ioctl(struct drm_device *dev, void *data, 1553 struct drm_file *file_priv) 1554 { 1555 struct v3d_submit submit = { .v3d = to_v3d_dev(dev), .file_priv = file_priv }; 1556 struct drm_v3d_submit_cpu *args = data; 1557 struct v3d_submit_ext se = {0}; 1558 struct v3d_cpu_job *cpu_job = NULL; 1559 int ret; 1560 1561 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) { 1562 drm_dbg(dev, "Invalid flags: %d\n", args->flags); 1563 return -EINVAL; 1564 } 1565 1566 cpu_job = (struct v3d_cpu_job *)v3d_submit_add_job(&submit, V3D_CPU); 1567 if (IS_ERR(cpu_job)) 1568 return PTR_ERR(cpu_job); 1569 1570 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) { 1571 ret = v3d_get_extensions(file_priv, args->extensions, &se, cpu_job); 1572 if (ret) { 1573 drm_dbg(dev, "Failed to get extensions.\n"); 1574 goto fail; 1575 } 1576 } 1577 1578 /* Every CPU job must have a CPU job user extension */ 1579 if (!cpu_job->job_type) { 1580 drm_dbg(dev, "CPU job must have a CPU job user extension.\n"); 1581 ret = -EINVAL; 1582 goto fail; 1583 } 1584 1585 if (args->bo_handle_count != cpu_job_bo_handle_count[cpu_job->job_type]) { 1586 drm_dbg(dev, "This CPU job was not submitted with the proper number of BOs.\n"); 1587 ret = -EINVAL; 1588 goto fail; 1589 } 1590 1591 trace_v3d_submit_cpu_ioctl(dev, cpu_job->job_type); 1592 1593 ret = v3d_job_add_syncobjs(&cpu_job->base, file_priv, 0, &se); 1594 if (ret) 1595 goto fail; 1596 1597 /* Look up the CPU jobs' BOs before v3d_setup_csd_jobs_and_bos() appends 1598 * the CSD and clean jobs in the case of indirect CSD job. 1599 */ 1600 if (args->bo_handle_count) { 1601 ret = v3d_lookup_bos(submit.file_priv, &cpu_job->base, 1602 args->bo_handles, args->bo_handle_count); 1603 if (ret) 1604 goto fail; 1605 1606 ret = v3d_cpu_job_bounds_check(cpu_job); 1607 if (ret) 1608 goto fail; 1609 } 1610 1611 if (cpu_job->job_type == V3D_CPU_JOB_TYPE_INDIRECT_CSD) { 1612 ret = v3d_setup_csd_jobs_and_bos(&submit, &cpu_job->indirect_csd.args, 1613 NULL); 1614 if (ret) 1615 goto fail; 1616 1617 /* The CSD job was appended at jobs[1] */ 1618 if (WARN_ON(submit.jobs[1]->queue != V3D_CSD)) { 1619 ret = -EINVAL; 1620 goto fail; 1621 } 1622 1623 cpu_job->indirect_csd.job = container_of(submit.jobs[1], struct v3d_csd_job, 1624 base); 1625 } 1626 1627 ret = v3d_submit_lock_reservations(&submit); 1628 if (ret) 1629 goto fail; 1630 1631 ret = v3d_submit_jobs(&submit, NULL, &se); 1632 if (ret) 1633 goto fail_submit; 1634 1635 return 0; 1636 1637 fail: 1638 v3d_submit_cleanup_jobs(&submit); 1639 fail_submit: 1640 v3d_submit_put_post_deps(NULL, &se); 1641 1642 return ret; 1643 } 1644