1 // SPDX-License-Identifier: GPL-2.0+ 2 /* Copyright (C) 2014-2018 Broadcom */ 3 4 #include <linux/device.h> 5 #include <linux/dma-mapping.h> 6 #include <linux/io.h> 7 #include <linux/module.h> 8 #include <linux/platform_device.h> 9 #include <linux/reset.h> 10 #include <linux/sched/signal.h> 11 #include <linux/uaccess.h> 12 13 #include <drm/drm_managed.h> 14 #include <drm/drm_syncobj.h> 15 #include <uapi/drm/v3d_drm.h> 16 17 #include "v3d_drv.h" 18 #include "v3d_regs.h" 19 #include "v3d_trace.h" 20 21 static void 22 v3d_init_core(struct v3d_dev *v3d, int core) 23 { 24 /* Set OVRTMUOUT, which means that the texture sampler uniform 25 * configuration's tmu output type field is used, instead of 26 * using the hardware default behavior based on the texture 27 * type. If you want the default behavior, you can still put 28 * "2" in the indirect texture state's output_type field. 29 */ 30 if (v3d->ver < 40) 31 V3D_CORE_WRITE(core, V3D_CTL_MISCCFG, V3D_MISCCFG_OVRTMUOUT); 32 33 /* Whenever we flush the L2T cache, we always want to flush 34 * the whole thing. 35 */ 36 V3D_CORE_WRITE(core, V3D_CTL_L2TFLSTA, 0); 37 V3D_CORE_WRITE(core, V3D_CTL_L2TFLEND, ~0); 38 } 39 40 /* Sets invariant state for the HW. */ 41 static void 42 v3d_init_hw_state(struct v3d_dev *v3d) 43 { 44 v3d_init_core(v3d, 0); 45 } 46 47 static void 48 v3d_idle_axi(struct v3d_dev *v3d, int core) 49 { 50 V3D_CORE_WRITE(core, V3D_GMP_CFG(v3d->ver), V3D_GMP_CFG_STOP_REQ); 51 52 if (wait_for((V3D_CORE_READ(core, V3D_GMP_STATUS(v3d->ver)) & 53 (V3D_GMP_STATUS_RD_COUNT_MASK | 54 V3D_GMP_STATUS_WR_COUNT_MASK | 55 V3D_GMP_STATUS_CFG_BUSY)) == 0, 100)) { 56 DRM_ERROR("Failed to wait for safe GMP shutdown\n"); 57 } 58 } 59 60 static void 61 v3d_idle_gca(struct v3d_dev *v3d) 62 { 63 if (v3d->ver >= 41) 64 return; 65 66 V3D_GCA_WRITE(V3D_GCA_SAFE_SHUTDOWN, V3D_GCA_SAFE_SHUTDOWN_EN); 67 68 if (wait_for((V3D_GCA_READ(V3D_GCA_SAFE_SHUTDOWN_ACK) & 69 V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED) == 70 V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED, 100)) { 71 DRM_ERROR("Failed to wait for safe GCA shutdown\n"); 72 } 73 } 74 75 static void 76 v3d_reset_by_bridge(struct v3d_dev *v3d) 77 { 78 int version = V3D_BRIDGE_READ(V3D_TOP_GR_BRIDGE_REVISION); 79 80 if (V3D_GET_FIELD(version, V3D_TOP_GR_BRIDGE_MAJOR) == 2) { 81 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 82 V3D_TOP_GR_BRIDGE_SW_INIT_0_V3D_CLK_108_SW_INIT); 83 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 0); 84 85 /* GFXH-1383: The SW_INIT may cause a stray write to address 0 86 * of the unit, so reset it to its power-on value here. 87 */ 88 V3D_WRITE(V3D_HUB_AXICFG, V3D_HUB_AXICFG_MAX_LEN_MASK); 89 } else { 90 WARN_ON_ONCE(V3D_GET_FIELD(version, 91 V3D_TOP_GR_BRIDGE_MAJOR) != 7); 92 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 93 V3D_TOP_GR_BRIDGE_SW_INIT_1_V3D_CLK_108_SW_INIT); 94 V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 0); 95 } 96 } 97 98 static void 99 v3d_reset_v3d(struct v3d_dev *v3d) 100 { 101 if (v3d->reset) 102 reset_control_reset(v3d->reset); 103 else 104 v3d_reset_by_bridge(v3d); 105 106 v3d_init_hw_state(v3d); 107 } 108 109 void 110 v3d_reset(struct v3d_dev *v3d) 111 { 112 struct drm_device *dev = &v3d->drm; 113 114 DRM_DEV_ERROR(dev->dev, "Resetting GPU for hang.\n"); 115 DRM_DEV_ERROR(dev->dev, "V3D_ERR_STAT: 0x%08x\n", 116 V3D_CORE_READ(0, V3D_ERR_STAT)); 117 trace_v3d_reset_begin(dev); 118 119 /* XXX: only needed for safe powerdown, not reset. */ 120 if (false) 121 v3d_idle_axi(v3d, 0); 122 123 v3d_idle_gca(v3d); 124 v3d_reset_v3d(v3d); 125 126 v3d_mmu_set_page_table(v3d); 127 v3d_irq_reset(v3d); 128 129 v3d_perfmon_stop(v3d, v3d->active_perfmon, false); 130 131 trace_v3d_reset_end(dev); 132 } 133 134 static void 135 v3d_flush_l3(struct v3d_dev *v3d) 136 { 137 if (v3d->ver < 41) { 138 u32 gca_ctrl = V3D_GCA_READ(V3D_GCA_CACHE_CTRL); 139 140 V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL, 141 gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH); 142 143 if (v3d->ver < 33) { 144 V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL, 145 gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH); 146 } 147 } 148 } 149 150 /* Invalidates the (read-only) L2C cache. This was the L2 cache for 151 * uniforms and instructions on V3D 3.2. 152 */ 153 static void 154 v3d_invalidate_l2c(struct v3d_dev *v3d, int core) 155 { 156 if (v3d->ver > 32) 157 return; 158 159 V3D_CORE_WRITE(core, V3D_CTL_L2CACTL, 160 V3D_L2CACTL_L2CCLR | 161 V3D_L2CACTL_L2CENA); 162 } 163 164 /* Invalidates texture L2 cachelines */ 165 static void 166 v3d_flush_l2t(struct v3d_dev *v3d, int core) 167 { 168 /* While there is a busy bit (V3D_L2TCACTL_L2TFLS), we don't 169 * need to wait for completion before dispatching the job -- 170 * L2T accesses will be stalled until the flush has completed. 171 * However, we do need to make sure we don't try to trigger a 172 * new flush while the L2_CLEAN queue is trying to 173 * synchronously clean after a job. 174 */ 175 mutex_lock(&v3d->cache_clean_lock); 176 V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, 177 V3D_L2TCACTL_L2TFLS | 178 V3D_SET_FIELD(V3D_L2TCACTL_FLM_FLUSH, V3D_L2TCACTL_FLM)); 179 mutex_unlock(&v3d->cache_clean_lock); 180 } 181 182 /* Cleans texture L1 and L2 cachelines (writing back dirty data). 183 * 184 * For cleaning, which happens from the CACHE_CLEAN queue after CSD has 185 * executed, we need to make sure that the clean is done before 186 * signaling job completion. So, we synchronously wait before 187 * returning, and we make sure that L2 invalidates don't happen in the 188 * meantime to confuse our are-we-done checks. 189 */ 190 void 191 v3d_clean_caches(struct v3d_dev *v3d) 192 { 193 struct drm_device *dev = &v3d->drm; 194 int core = 0; 195 196 trace_v3d_cache_clean_begin(dev); 197 198 V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, V3D_L2TCACTL_TMUWCF); 199 if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) & 200 V3D_L2TCACTL_TMUWCF), 100)) { 201 DRM_ERROR("Timeout waiting for TMU write combiner flush\n"); 202 } 203 204 mutex_lock(&v3d->cache_clean_lock); 205 V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, 206 V3D_L2TCACTL_L2TFLS | 207 V3D_SET_FIELD(V3D_L2TCACTL_FLM_CLEAN, V3D_L2TCACTL_FLM)); 208 209 if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) & 210 V3D_L2TCACTL_L2TFLS), 100)) { 211 DRM_ERROR("Timeout waiting for L2T clean\n"); 212 } 213 214 mutex_unlock(&v3d->cache_clean_lock); 215 216 trace_v3d_cache_clean_end(dev); 217 } 218 219 /* Invalidates the slice caches. These are read-only caches. */ 220 static void 221 v3d_invalidate_slices(struct v3d_dev *v3d, int core) 222 { 223 V3D_CORE_WRITE(core, V3D_CTL_SLCACTL, 224 V3D_SET_FIELD(0xf, V3D_SLCACTL_TVCCS) | 225 V3D_SET_FIELD(0xf, V3D_SLCACTL_TDCCS) | 226 V3D_SET_FIELD(0xf, V3D_SLCACTL_UCC) | 227 V3D_SET_FIELD(0xf, V3D_SLCACTL_ICC)); 228 } 229 230 void 231 v3d_invalidate_caches(struct v3d_dev *v3d) 232 { 233 /* Invalidate the caches from the outside in. That way if 234 * another CL's concurrent use of nearby memory were to pull 235 * an invalidated cacheline back in, we wouldn't leave stale 236 * data in the inner cache. 237 */ 238 v3d_flush_l3(v3d); 239 v3d_invalidate_l2c(v3d, 0); 240 v3d_flush_l2t(v3d, 0); 241 v3d_invalidate_slices(v3d, 0); 242 } 243 244 /* Takes the reservation lock on all the BOs being referenced, so that 245 * at queue submit time we can update the reservations. 246 * 247 * We don't lock the RCL the tile alloc/state BOs, or overflow memory 248 * (all of which are on exec->unref_list). They're entirely private 249 * to v3d, so we don't attach dma-buf fences to them. 250 */ 251 static int 252 v3d_lock_bo_reservations(struct v3d_job *job, 253 struct ww_acquire_ctx *acquire_ctx) 254 { 255 int i, ret; 256 257 ret = drm_gem_lock_reservations(job->bo, job->bo_count, acquire_ctx); 258 if (ret) 259 return ret; 260 261 for (i = 0; i < job->bo_count; i++) { 262 ret = dma_resv_reserve_fences(job->bo[i]->resv, 1); 263 if (ret) 264 goto fail; 265 266 ret = drm_sched_job_add_implicit_dependencies(&job->base, 267 job->bo[i], true); 268 if (ret) 269 goto fail; 270 } 271 272 return 0; 273 274 fail: 275 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx); 276 return ret; 277 } 278 279 /** 280 * v3d_lookup_bos() - Sets up job->bo[] with the GEM objects 281 * referenced by the job. 282 * @dev: DRM device 283 * @file_priv: DRM file for this fd 284 * @job: V3D job being set up 285 * @bo_handles: GEM handles 286 * @bo_count: Number of GEM handles passed in 287 * 288 * The command validator needs to reference BOs by their index within 289 * the submitted job's BO list. This does the validation of the job's 290 * BO list and reference counting for the lifetime of the job. 291 * 292 * Note that this function doesn't need to unreference the BOs on 293 * failure, because that will happen at v3d_exec_cleanup() time. 294 */ 295 static int 296 v3d_lookup_bos(struct drm_device *dev, 297 struct drm_file *file_priv, 298 struct v3d_job *job, 299 u64 bo_handles, 300 u32 bo_count) 301 { 302 job->bo_count = bo_count; 303 304 if (!job->bo_count) { 305 /* See comment on bo_index for why we have to check 306 * this. 307 */ 308 DRM_DEBUG("Rendering requires BOs\n"); 309 return -EINVAL; 310 } 311 312 return drm_gem_objects_lookup(file_priv, 313 (void __user *)(uintptr_t)bo_handles, 314 job->bo_count, &job->bo); 315 } 316 317 static void 318 v3d_job_free(struct kref *ref) 319 { 320 struct v3d_job *job = container_of(ref, struct v3d_job, refcount); 321 int i; 322 323 if (job->bo) { 324 for (i = 0; i < job->bo_count; i++) 325 drm_gem_object_put(job->bo[i]); 326 kvfree(job->bo); 327 } 328 329 dma_fence_put(job->irq_fence); 330 dma_fence_put(job->done_fence); 331 332 if (job->perfmon) 333 v3d_perfmon_put(job->perfmon); 334 335 kfree(job); 336 } 337 338 static void 339 v3d_render_job_free(struct kref *ref) 340 { 341 struct v3d_render_job *job = container_of(ref, struct v3d_render_job, 342 base.refcount); 343 struct v3d_bo *bo, *save; 344 345 list_for_each_entry_safe(bo, save, &job->unref_list, unref_head) { 346 drm_gem_object_put(&bo->base.base); 347 } 348 349 v3d_job_free(ref); 350 } 351 352 void v3d_job_cleanup(struct v3d_job *job) 353 { 354 if (!job) 355 return; 356 357 drm_sched_job_cleanup(&job->base); 358 v3d_job_put(job); 359 } 360 361 void v3d_job_put(struct v3d_job *job) 362 { 363 kref_put(&job->refcount, job->free); 364 } 365 366 int 367 v3d_wait_bo_ioctl(struct drm_device *dev, void *data, 368 struct drm_file *file_priv) 369 { 370 int ret; 371 struct drm_v3d_wait_bo *args = data; 372 ktime_t start = ktime_get(); 373 u64 delta_ns; 374 unsigned long timeout_jiffies = 375 nsecs_to_jiffies_timeout(args->timeout_ns); 376 377 if (args->pad != 0) 378 return -EINVAL; 379 380 ret = drm_gem_dma_resv_wait(file_priv, args->handle, 381 true, timeout_jiffies); 382 383 /* Decrement the user's timeout, in case we got interrupted 384 * such that the ioctl will be restarted. 385 */ 386 delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start)); 387 if (delta_ns < args->timeout_ns) 388 args->timeout_ns -= delta_ns; 389 else 390 args->timeout_ns = 0; 391 392 /* Asked to wait beyond the jiffie/scheduler precision? */ 393 if (ret == -ETIME && args->timeout_ns) 394 ret = -EAGAIN; 395 396 return ret; 397 } 398 399 static int 400 v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv, 401 void **container, size_t size, void (*free)(struct kref *ref), 402 u32 in_sync, struct v3d_submit_ext *se, enum v3d_queue queue) 403 { 404 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 405 struct v3d_job *job; 406 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC); 407 int ret, i; 408 409 *container = kcalloc(1, size, GFP_KERNEL); 410 if (!*container) { 411 DRM_ERROR("Cannot allocate memory for v3d job."); 412 return -ENOMEM; 413 } 414 415 job = *container; 416 job->v3d = v3d; 417 job->free = free; 418 job->file = file_priv; 419 420 ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue], 421 1, v3d_priv); 422 if (ret) 423 goto fail; 424 425 if (has_multisync) { 426 if (se->in_sync_count && se->wait_stage == queue) { 427 struct drm_v3d_sem __user *handle = u64_to_user_ptr(se->in_syncs); 428 429 for (i = 0; i < se->in_sync_count; i++) { 430 struct drm_v3d_sem in; 431 432 if (copy_from_user(&in, handle++, sizeof(in))) { 433 ret = -EFAULT; 434 DRM_DEBUG("Failed to copy wait dep handle.\n"); 435 goto fail_deps; 436 } 437 ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in.handle, 0); 438 439 // TODO: Investigate why this was filtered out for the IOCTL. 440 if (ret && ret != -ENOENT) 441 goto fail_deps; 442 } 443 } 444 } else { 445 ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, in_sync, 0); 446 447 // TODO: Investigate why this was filtered out for the IOCTL. 448 if (ret && ret != -ENOENT) 449 goto fail_deps; 450 } 451 452 kref_init(&job->refcount); 453 454 return 0; 455 456 fail_deps: 457 drm_sched_job_cleanup(&job->base); 458 fail: 459 kfree(*container); 460 *container = NULL; 461 462 return ret; 463 } 464 465 static void 466 v3d_push_job(struct v3d_job *job) 467 { 468 drm_sched_job_arm(&job->base); 469 470 job->done_fence = dma_fence_get(&job->base.s_fence->finished); 471 472 /* put by scheduler job completion */ 473 kref_get(&job->refcount); 474 475 drm_sched_entity_push_job(&job->base); 476 } 477 478 static void 479 v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv, 480 struct v3d_job *job, 481 struct ww_acquire_ctx *acquire_ctx, 482 u32 out_sync, 483 struct v3d_submit_ext *se, 484 struct dma_fence *done_fence) 485 { 486 struct drm_syncobj *sync_out; 487 bool has_multisync = se && (se->flags & DRM_V3D_EXT_ID_MULTI_SYNC); 488 int i; 489 490 for (i = 0; i < job->bo_count; i++) { 491 /* XXX: Use shared fences for read-only objects. */ 492 dma_resv_add_fence(job->bo[i]->resv, job->done_fence, 493 DMA_RESV_USAGE_WRITE); 494 } 495 496 drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx); 497 498 /* Update the return sync object for the job */ 499 /* If it only supports a single signal semaphore*/ 500 if (!has_multisync) { 501 sync_out = drm_syncobj_find(file_priv, out_sync); 502 if (sync_out) { 503 drm_syncobj_replace_fence(sync_out, done_fence); 504 drm_syncobj_put(sync_out); 505 } 506 return; 507 } 508 509 /* If multiple semaphores extension is supported */ 510 if (se->out_sync_count) { 511 for (i = 0; i < se->out_sync_count; i++) { 512 drm_syncobj_replace_fence(se->out_syncs[i].syncobj, 513 done_fence); 514 drm_syncobj_put(se->out_syncs[i].syncobj); 515 } 516 kvfree(se->out_syncs); 517 } 518 } 519 520 static void 521 v3d_put_multisync_post_deps(struct v3d_submit_ext *se) 522 { 523 unsigned int i; 524 525 if (!(se && se->out_sync_count)) 526 return; 527 528 for (i = 0; i < se->out_sync_count; i++) 529 drm_syncobj_put(se->out_syncs[i].syncobj); 530 kvfree(se->out_syncs); 531 } 532 533 static int 534 v3d_get_multisync_post_deps(struct drm_file *file_priv, 535 struct v3d_submit_ext *se, 536 u32 count, u64 handles) 537 { 538 struct drm_v3d_sem __user *post_deps; 539 int i, ret; 540 541 if (!count) 542 return 0; 543 544 se->out_syncs = (struct v3d_submit_outsync *) 545 kvmalloc_array(count, 546 sizeof(struct v3d_submit_outsync), 547 GFP_KERNEL); 548 if (!se->out_syncs) 549 return -ENOMEM; 550 551 post_deps = u64_to_user_ptr(handles); 552 553 for (i = 0; i < count; i++) { 554 struct drm_v3d_sem out; 555 556 if (copy_from_user(&out, post_deps++, sizeof(out))) { 557 ret = -EFAULT; 558 DRM_DEBUG("Failed to copy post dep handles\n"); 559 goto fail; 560 } 561 562 se->out_syncs[i].syncobj = drm_syncobj_find(file_priv, 563 out.handle); 564 if (!se->out_syncs[i].syncobj) { 565 ret = -EINVAL; 566 goto fail; 567 } 568 } 569 se->out_sync_count = count; 570 571 return 0; 572 573 fail: 574 for (i--; i >= 0; i--) 575 drm_syncobj_put(se->out_syncs[i].syncobj); 576 kvfree(se->out_syncs); 577 578 return ret; 579 } 580 581 /* Get data for multiple binary semaphores synchronization. Parse syncobj 582 * to be signaled when job completes (out_sync). 583 */ 584 static int 585 v3d_get_multisync_submit_deps(struct drm_file *file_priv, 586 struct drm_v3d_extension __user *ext, 587 void *data) 588 { 589 struct drm_v3d_multi_sync multisync; 590 struct v3d_submit_ext *se = data; 591 int ret; 592 593 if (copy_from_user(&multisync, ext, sizeof(multisync))) 594 return -EFAULT; 595 596 if (multisync.pad) 597 return -EINVAL; 598 599 ret = v3d_get_multisync_post_deps(file_priv, data, multisync.out_sync_count, 600 multisync.out_syncs); 601 if (ret) 602 return ret; 603 604 se->in_sync_count = multisync.in_sync_count; 605 se->in_syncs = multisync.in_syncs; 606 se->flags |= DRM_V3D_EXT_ID_MULTI_SYNC; 607 se->wait_stage = multisync.wait_stage; 608 609 return 0; 610 } 611 612 /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data 613 * according to the extension id (name). 614 */ 615 static int 616 v3d_get_extensions(struct drm_file *file_priv, 617 u64 ext_handles, 618 void *data) 619 { 620 struct drm_v3d_extension __user *user_ext; 621 int ret; 622 623 user_ext = u64_to_user_ptr(ext_handles); 624 while (user_ext) { 625 struct drm_v3d_extension ext; 626 627 if (copy_from_user(&ext, user_ext, sizeof(ext))) { 628 DRM_DEBUG("Failed to copy submit extension\n"); 629 return -EFAULT; 630 } 631 632 switch (ext.id) { 633 case DRM_V3D_EXT_ID_MULTI_SYNC: 634 ret = v3d_get_multisync_submit_deps(file_priv, user_ext, data); 635 if (ret) 636 return ret; 637 break; 638 default: 639 DRM_DEBUG_DRIVER("Unknown extension id: %d\n", ext.id); 640 return -EINVAL; 641 } 642 643 user_ext = u64_to_user_ptr(ext.next); 644 } 645 646 return 0; 647 } 648 649 /** 650 * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D. 651 * @dev: DRM device 652 * @data: ioctl argument 653 * @file_priv: DRM file for this fd 654 * 655 * This is the main entrypoint for userspace to submit a 3D frame to 656 * the GPU. Userspace provides the binner command list (if 657 * applicable), and the kernel sets up the render command list to draw 658 * to the framebuffer described in the ioctl, using the command lists 659 * that the 3D engine's binner will produce. 660 */ 661 int 662 v3d_submit_cl_ioctl(struct drm_device *dev, void *data, 663 struct drm_file *file_priv) 664 { 665 struct v3d_dev *v3d = to_v3d_dev(dev); 666 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 667 struct drm_v3d_submit_cl *args = data; 668 struct v3d_submit_ext se = {0}; 669 struct v3d_bin_job *bin = NULL; 670 struct v3d_render_job *render = NULL; 671 struct v3d_job *clean_job = NULL; 672 struct v3d_job *last_job; 673 struct ww_acquire_ctx acquire_ctx; 674 int ret = 0; 675 676 trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end); 677 678 if (args->pad) 679 return -EINVAL; 680 681 if (args->flags && 682 args->flags & ~(DRM_V3D_SUBMIT_CL_FLUSH_CACHE | 683 DRM_V3D_SUBMIT_EXTENSION)) { 684 DRM_INFO("invalid flags: %d\n", args->flags); 685 return -EINVAL; 686 } 687 688 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) { 689 ret = v3d_get_extensions(file_priv, args->extensions, &se); 690 if (ret) { 691 DRM_DEBUG("Failed to get extensions.\n"); 692 return ret; 693 } 694 } 695 696 ret = v3d_job_init(v3d, file_priv, (void *)&render, sizeof(*render), 697 v3d_render_job_free, args->in_sync_rcl, &se, V3D_RENDER); 698 if (ret) 699 goto fail; 700 701 render->start = args->rcl_start; 702 render->end = args->rcl_end; 703 INIT_LIST_HEAD(&render->unref_list); 704 705 if (args->bcl_start != args->bcl_end) { 706 ret = v3d_job_init(v3d, file_priv, (void *)&bin, sizeof(*bin), 707 v3d_job_free, args->in_sync_bcl, &se, V3D_BIN); 708 if (ret) 709 goto fail; 710 711 bin->start = args->bcl_start; 712 bin->end = args->bcl_end; 713 bin->qma = args->qma; 714 bin->qms = args->qms; 715 bin->qts = args->qts; 716 bin->render = render; 717 } 718 719 if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) { 720 ret = v3d_job_init(v3d, file_priv, (void *)&clean_job, sizeof(*clean_job), 721 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN); 722 if (ret) 723 goto fail; 724 725 last_job = clean_job; 726 } else { 727 last_job = &render->base; 728 } 729 730 ret = v3d_lookup_bos(dev, file_priv, last_job, 731 args->bo_handles, args->bo_handle_count); 732 if (ret) 733 goto fail; 734 735 ret = v3d_lock_bo_reservations(last_job, &acquire_ctx); 736 if (ret) 737 goto fail; 738 739 if (args->perfmon_id) { 740 render->base.perfmon = v3d_perfmon_find(v3d_priv, 741 args->perfmon_id); 742 743 if (!render->base.perfmon) { 744 ret = -ENOENT; 745 goto fail_perfmon; 746 } 747 } 748 749 mutex_lock(&v3d->sched_lock); 750 if (bin) { 751 bin->base.perfmon = render->base.perfmon; 752 v3d_perfmon_get(bin->base.perfmon); 753 v3d_push_job(&bin->base); 754 755 ret = drm_sched_job_add_dependency(&render->base.base, 756 dma_fence_get(bin->base.done_fence)); 757 if (ret) 758 goto fail_unreserve; 759 } 760 761 v3d_push_job(&render->base); 762 763 if (clean_job) { 764 struct dma_fence *render_fence = 765 dma_fence_get(render->base.done_fence); 766 ret = drm_sched_job_add_dependency(&clean_job->base, 767 render_fence); 768 if (ret) 769 goto fail_unreserve; 770 clean_job->perfmon = render->base.perfmon; 771 v3d_perfmon_get(clean_job->perfmon); 772 v3d_push_job(clean_job); 773 } 774 775 mutex_unlock(&v3d->sched_lock); 776 777 v3d_attach_fences_and_unlock_reservation(file_priv, 778 last_job, 779 &acquire_ctx, 780 args->out_sync, 781 &se, 782 last_job->done_fence); 783 784 if (bin) 785 v3d_job_put(&bin->base); 786 v3d_job_put(&render->base); 787 if (clean_job) 788 v3d_job_put(clean_job); 789 790 return 0; 791 792 fail_unreserve: 793 mutex_unlock(&v3d->sched_lock); 794 fail_perfmon: 795 drm_gem_unlock_reservations(last_job->bo, 796 last_job->bo_count, &acquire_ctx); 797 fail: 798 v3d_job_cleanup((void *)bin); 799 v3d_job_cleanup((void *)render); 800 v3d_job_cleanup(clean_job); 801 v3d_put_multisync_post_deps(&se); 802 803 return ret; 804 } 805 806 /** 807 * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D. 808 * @dev: DRM device 809 * @data: ioctl argument 810 * @file_priv: DRM file for this fd 811 * 812 * Userspace provides the register setup for the TFU, which we don't 813 * need to validate since the TFU is behind the MMU. 814 */ 815 int 816 v3d_submit_tfu_ioctl(struct drm_device *dev, void *data, 817 struct drm_file *file_priv) 818 { 819 struct v3d_dev *v3d = to_v3d_dev(dev); 820 struct drm_v3d_submit_tfu *args = data; 821 struct v3d_submit_ext se = {0}; 822 struct v3d_tfu_job *job = NULL; 823 struct ww_acquire_ctx acquire_ctx; 824 int ret = 0; 825 826 trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia); 827 828 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) { 829 DRM_DEBUG("invalid flags: %d\n", args->flags); 830 return -EINVAL; 831 } 832 833 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) { 834 ret = v3d_get_extensions(file_priv, args->extensions, &se); 835 if (ret) { 836 DRM_DEBUG("Failed to get extensions.\n"); 837 return ret; 838 } 839 } 840 841 ret = v3d_job_init(v3d, file_priv, (void *)&job, sizeof(*job), 842 v3d_job_free, args->in_sync, &se, V3D_TFU); 843 if (ret) 844 goto fail; 845 846 job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles), 847 sizeof(*job->base.bo), GFP_KERNEL); 848 if (!job->base.bo) { 849 ret = -ENOMEM; 850 goto fail; 851 } 852 853 job->args = *args; 854 855 for (job->base.bo_count = 0; 856 job->base.bo_count < ARRAY_SIZE(args->bo_handles); 857 job->base.bo_count++) { 858 struct drm_gem_object *bo; 859 860 if (!args->bo_handles[job->base.bo_count]) 861 break; 862 863 bo = drm_gem_object_lookup(file_priv, args->bo_handles[job->base.bo_count]); 864 if (!bo) { 865 DRM_DEBUG("Failed to look up GEM BO %d: %d\n", 866 job->base.bo_count, 867 args->bo_handles[job->base.bo_count]); 868 ret = -ENOENT; 869 goto fail; 870 } 871 job->base.bo[job->base.bo_count] = bo; 872 } 873 874 ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx); 875 if (ret) 876 goto fail; 877 878 mutex_lock(&v3d->sched_lock); 879 v3d_push_job(&job->base); 880 mutex_unlock(&v3d->sched_lock); 881 882 v3d_attach_fences_and_unlock_reservation(file_priv, 883 &job->base, &acquire_ctx, 884 args->out_sync, 885 &se, 886 job->base.done_fence); 887 888 v3d_job_put(&job->base); 889 890 return 0; 891 892 fail: 893 v3d_job_cleanup((void *)job); 894 v3d_put_multisync_post_deps(&se); 895 896 return ret; 897 } 898 899 /** 900 * v3d_submit_csd_ioctl() - Submits a CSD (texture formatting) job to the V3D. 901 * @dev: DRM device 902 * @data: ioctl argument 903 * @file_priv: DRM file for this fd 904 * 905 * Userspace provides the register setup for the CSD, which we don't 906 * need to validate since the CSD is behind the MMU. 907 */ 908 int 909 v3d_submit_csd_ioctl(struct drm_device *dev, void *data, 910 struct drm_file *file_priv) 911 { 912 struct v3d_dev *v3d = to_v3d_dev(dev); 913 struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 914 struct drm_v3d_submit_csd *args = data; 915 struct v3d_submit_ext se = {0}; 916 struct v3d_csd_job *job = NULL; 917 struct v3d_job *clean_job = NULL; 918 struct ww_acquire_ctx acquire_ctx; 919 int ret; 920 921 trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]); 922 923 if (args->pad) 924 return -EINVAL; 925 926 if (!v3d_has_csd(v3d)) { 927 DRM_DEBUG("Attempting CSD submit on non-CSD hardware\n"); 928 return -EINVAL; 929 } 930 931 if (args->flags && !(args->flags & DRM_V3D_SUBMIT_EXTENSION)) { 932 DRM_INFO("invalid flags: %d\n", args->flags); 933 return -EINVAL; 934 } 935 936 if (args->flags & DRM_V3D_SUBMIT_EXTENSION) { 937 ret = v3d_get_extensions(file_priv, args->extensions, &se); 938 if (ret) { 939 DRM_DEBUG("Failed to get extensions.\n"); 940 return ret; 941 } 942 } 943 944 ret = v3d_job_init(v3d, file_priv, (void *)&job, sizeof(*job), 945 v3d_job_free, args->in_sync, &se, V3D_CSD); 946 if (ret) 947 goto fail; 948 949 ret = v3d_job_init(v3d, file_priv, (void *)&clean_job, sizeof(*clean_job), 950 v3d_job_free, 0, NULL, V3D_CACHE_CLEAN); 951 if (ret) 952 goto fail; 953 954 job->args = *args; 955 956 ret = v3d_lookup_bos(dev, file_priv, clean_job, 957 args->bo_handles, args->bo_handle_count); 958 if (ret) 959 goto fail; 960 961 ret = v3d_lock_bo_reservations(clean_job, &acquire_ctx); 962 if (ret) 963 goto fail; 964 965 if (args->perfmon_id) { 966 job->base.perfmon = v3d_perfmon_find(v3d_priv, 967 args->perfmon_id); 968 if (!job->base.perfmon) { 969 ret = -ENOENT; 970 goto fail_perfmon; 971 } 972 } 973 974 mutex_lock(&v3d->sched_lock); 975 v3d_push_job(&job->base); 976 977 ret = drm_sched_job_add_dependency(&clean_job->base, 978 dma_fence_get(job->base.done_fence)); 979 if (ret) 980 goto fail_unreserve; 981 982 v3d_push_job(clean_job); 983 mutex_unlock(&v3d->sched_lock); 984 985 v3d_attach_fences_and_unlock_reservation(file_priv, 986 clean_job, 987 &acquire_ctx, 988 args->out_sync, 989 &se, 990 clean_job->done_fence); 991 992 v3d_job_put(&job->base); 993 v3d_job_put(clean_job); 994 995 return 0; 996 997 fail_unreserve: 998 mutex_unlock(&v3d->sched_lock); 999 fail_perfmon: 1000 drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count, 1001 &acquire_ctx); 1002 fail: 1003 v3d_job_cleanup((void *)job); 1004 v3d_job_cleanup(clean_job); 1005 v3d_put_multisync_post_deps(&se); 1006 1007 return ret; 1008 } 1009 1010 int 1011 v3d_gem_init(struct drm_device *dev) 1012 { 1013 struct v3d_dev *v3d = to_v3d_dev(dev); 1014 u32 pt_size = 4096 * 1024; 1015 int ret, i; 1016 1017 for (i = 0; i < V3D_MAX_QUEUES; i++) { 1018 v3d->queue[i].fence_context = dma_fence_context_alloc(1); 1019 v3d->queue[i].start_ns = 0; 1020 v3d->queue[i].enabled_ns = 0; 1021 v3d->queue[i].jobs_sent = 0; 1022 } 1023 1024 spin_lock_init(&v3d->mm_lock); 1025 spin_lock_init(&v3d->job_lock); 1026 ret = drmm_mutex_init(dev, &v3d->bo_lock); 1027 if (ret) 1028 return ret; 1029 ret = drmm_mutex_init(dev, &v3d->reset_lock); 1030 if (ret) 1031 return ret; 1032 ret = drmm_mutex_init(dev, &v3d->sched_lock); 1033 if (ret) 1034 return ret; 1035 ret = drmm_mutex_init(dev, &v3d->cache_clean_lock); 1036 if (ret) 1037 return ret; 1038 1039 /* Note: We don't allocate address 0. Various bits of HW 1040 * treat 0 as special, such as the occlusion query counters 1041 * where 0 means "disabled". 1042 */ 1043 drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1); 1044 1045 v3d->pt = dma_alloc_wc(v3d->drm.dev, pt_size, 1046 &v3d->pt_paddr, 1047 GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO); 1048 if (!v3d->pt) { 1049 drm_mm_takedown(&v3d->mm); 1050 dev_err(v3d->drm.dev, 1051 "Failed to allocate page tables. Please ensure you have DMA enabled.\n"); 1052 return -ENOMEM; 1053 } 1054 1055 v3d_init_hw_state(v3d); 1056 v3d_mmu_set_page_table(v3d); 1057 1058 ret = v3d_sched_init(v3d); 1059 if (ret) { 1060 drm_mm_takedown(&v3d->mm); 1061 dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt, 1062 v3d->pt_paddr); 1063 } 1064 1065 return 0; 1066 } 1067 1068 void 1069 v3d_gem_destroy(struct drm_device *dev) 1070 { 1071 struct v3d_dev *v3d = to_v3d_dev(dev); 1072 1073 v3d_sched_fini(v3d); 1074 1075 /* Waiting for jobs to finish would need to be done before 1076 * unregistering V3D. 1077 */ 1078 WARN_ON(v3d->bin_job); 1079 WARN_ON(v3d->render_job); 1080 WARN_ON(v3d->tfu_job); 1081 WARN_ON(v3d->csd_job); 1082 1083 drm_mm_takedown(&v3d->mm); 1084 1085 dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt, 1086 v3d->pt_paddr); 1087 } 1088