1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2013 Red Hat 4 * Author: Rob Clark <robdclark@gmail.com> 5 */ 6 7 #include <linux/file.h> 8 #include <linux/sync_file.h> 9 #include <linux/uaccess.h> 10 11 #include <drm/drm_drv.h> 12 #include <drm/drm_file.h> 13 #include <drm/drm_syncobj.h> 14 15 #include "msm_drv.h" 16 #include "msm_gpu.h" 17 #include "msm_gem.h" 18 #include "msm_gpu_trace.h" 19 20 /* For userspace errors, use DRM_UT_DRIVER.. so that userspace can enable 21 * error msgs for debugging, but we don't spam dmesg by default 22 */ 23 #define SUBMIT_ERROR(err, submit, fmt, ...) \ 24 UERR(err, (submit)->dev, fmt, ##__VA_ARGS__) 25 26 /* 27 * Cmdstream submission: 28 */ 29 30 static struct msm_gem_submit *submit_create(struct drm_device *dev, 31 struct msm_gpu *gpu, 32 struct msm_gpu_submitqueue *queue, uint32_t nr_bos, 33 uint32_t nr_cmds) 34 { 35 static atomic_t ident = ATOMIC_INIT(0); 36 struct msm_gem_submit *submit; 37 uint64_t sz; 38 int ret; 39 40 sz = struct_size(submit, bos, nr_bos) + 41 ((u64)nr_cmds * sizeof(submit->cmd[0])); 42 43 if (sz > SIZE_MAX) 44 return ERR_PTR(-ENOMEM); 45 46 submit = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN); 47 if (!submit) 48 return ERR_PTR(-ENOMEM); 49 50 submit->hw_fence = msm_fence_alloc(); 51 if (IS_ERR(submit->hw_fence)) { 52 ret = PTR_ERR(submit->hw_fence); 53 kfree(submit); 54 return ERR_PTR(ret); 55 } 56 57 ret = drm_sched_job_init(&submit->base, queue->entity, 1, queue); 58 if (ret) { 59 kfree(submit->hw_fence); 60 kfree(submit); 61 return ERR_PTR(ret); 62 } 63 64 kref_init(&submit->ref); 65 submit->dev = dev; 66 submit->aspace = queue->ctx->aspace; 67 submit->gpu = gpu; 68 submit->cmd = (void *)&submit->bos[nr_bos]; 69 submit->queue = queue; 70 submit->pid = get_pid(task_pid(current)); 71 submit->ring = gpu->rb[queue->ring_nr]; 72 submit->fault_dumped = false; 73 74 /* Get a unique identifier for the submission for logging purposes */ 75 submit->ident = atomic_inc_return(&ident) - 1; 76 77 INIT_LIST_HEAD(&submit->node); 78 79 return submit; 80 } 81 82 void __msm_gem_submit_destroy(struct kref *kref) 83 { 84 struct msm_gem_submit *submit = 85 container_of(kref, struct msm_gem_submit, ref); 86 unsigned i; 87 88 if (submit->fence_id) { 89 spin_lock(&submit->queue->idr_lock); 90 idr_remove(&submit->queue->fence_idr, submit->fence_id); 91 spin_unlock(&submit->queue->idr_lock); 92 } 93 94 dma_fence_put(submit->user_fence); 95 96 /* 97 * If the submit is freed before msm_job_run(), then hw_fence is 98 * just some pre-allocated memory, not a reference counted fence. 99 * Once the job runs and the hw_fence is initialized, it will 100 * have a refcount of at least one, since the submit holds a ref 101 * to the hw_fence. 102 */ 103 if (kref_read(&submit->hw_fence->refcount) == 0) { 104 kfree(submit->hw_fence); 105 } else { 106 dma_fence_put(submit->hw_fence); 107 } 108 109 put_pid(submit->pid); 110 msm_submitqueue_put(submit->queue); 111 112 for (i = 0; i < submit->nr_cmds; i++) 113 kfree(submit->cmd[i].relocs); 114 115 kfree(submit); 116 } 117 118 static int submit_lookup_objects(struct msm_gem_submit *submit, 119 struct drm_msm_gem_submit *args, struct drm_file *file) 120 { 121 unsigned i; 122 int ret = 0; 123 124 for (i = 0; i < args->nr_bos; i++) { 125 struct drm_msm_gem_submit_bo submit_bo; 126 void __user *userptr = 127 u64_to_user_ptr(args->bos + (i * sizeof(submit_bo))); 128 129 /* make sure we don't have garbage flags, in case we hit 130 * error path before flags is initialized: 131 */ 132 submit->bos[i].flags = 0; 133 134 if (copy_from_user(&submit_bo, userptr, sizeof(submit_bo))) { 135 ret = -EFAULT; 136 i = 0; 137 goto out; 138 } 139 140 /* at least one of READ and/or WRITE flags should be set: */ 141 #define MANDATORY_FLAGS (MSM_SUBMIT_BO_READ | MSM_SUBMIT_BO_WRITE) 142 143 if ((submit_bo.flags & ~MSM_SUBMIT_BO_FLAGS) || 144 !(submit_bo.flags & MANDATORY_FLAGS)) { 145 ret = SUBMIT_ERROR(EINVAL, submit, "invalid flags: %x\n", submit_bo.flags); 146 i = 0; 147 goto out; 148 } 149 150 submit->bos[i].handle = submit_bo.handle; 151 submit->bos[i].flags = submit_bo.flags; 152 } 153 154 spin_lock(&file->table_lock); 155 156 for (i = 0; i < args->nr_bos; i++) { 157 struct drm_gem_object *obj; 158 159 /* normally use drm_gem_object_lookup(), but for bulk lookup 160 * all under single table_lock just hit object_idr directly: 161 */ 162 obj = idr_find(&file->object_idr, submit->bos[i].handle); 163 if (!obj) { 164 ret = SUBMIT_ERROR(EINVAL, submit, "invalid handle %u at index %u\n", submit->bos[i].handle, i); 165 goto out_unlock; 166 } 167 168 drm_gem_object_get(obj); 169 170 submit->bos[i].obj = obj; 171 } 172 173 out_unlock: 174 spin_unlock(&file->table_lock); 175 176 out: 177 submit->nr_bos = i; 178 179 return ret; 180 } 181 182 static int submit_lookup_cmds(struct msm_gem_submit *submit, 183 struct drm_msm_gem_submit *args, struct drm_file *file) 184 { 185 unsigned i; 186 size_t sz; 187 int ret = 0; 188 189 for (i = 0; i < args->nr_cmds; i++) { 190 struct drm_msm_gem_submit_cmd submit_cmd; 191 void __user *userptr = 192 u64_to_user_ptr(args->cmds + (i * sizeof(submit_cmd))); 193 194 ret = copy_from_user(&submit_cmd, userptr, sizeof(submit_cmd)); 195 if (ret) { 196 ret = -EFAULT; 197 goto out; 198 } 199 200 /* validate input from userspace: */ 201 switch (submit_cmd.type) { 202 case MSM_SUBMIT_CMD_BUF: 203 case MSM_SUBMIT_CMD_IB_TARGET_BUF: 204 case MSM_SUBMIT_CMD_CTX_RESTORE_BUF: 205 break; 206 default: 207 return SUBMIT_ERROR(EINVAL, submit, "invalid type: %08x\n", submit_cmd.type); 208 } 209 210 if (submit_cmd.size % 4) { 211 ret = SUBMIT_ERROR(EINVAL, submit, "non-aligned cmdstream buffer size: %u\n", 212 submit_cmd.size); 213 goto out; 214 } 215 216 submit->cmd[i].type = submit_cmd.type; 217 submit->cmd[i].size = submit_cmd.size / 4; 218 submit->cmd[i].offset = submit_cmd.submit_offset / 4; 219 submit->cmd[i].idx = submit_cmd.submit_idx; 220 submit->cmd[i].nr_relocs = submit_cmd.nr_relocs; 221 222 userptr = u64_to_user_ptr(submit_cmd.relocs); 223 224 sz = array_size(submit_cmd.nr_relocs, 225 sizeof(struct drm_msm_gem_submit_reloc)); 226 /* check for overflow: */ 227 if (sz == SIZE_MAX) { 228 ret = -ENOMEM; 229 goto out; 230 } 231 submit->cmd[i].relocs = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN); 232 if (!submit->cmd[i].relocs) { 233 ret = -ENOMEM; 234 goto out; 235 } 236 ret = copy_from_user(submit->cmd[i].relocs, userptr, sz); 237 if (ret) { 238 ret = -EFAULT; 239 goto out; 240 } 241 } 242 243 out: 244 return ret; 245 } 246 247 /* This is where we make sure all the bo's are reserved and pin'd: */ 248 static int submit_lock_objects(struct msm_gem_submit *submit) 249 { 250 int ret; 251 252 drm_exec_init(&submit->exec, DRM_EXEC_INTERRUPTIBLE_WAIT, submit->nr_bos); 253 254 drm_exec_until_all_locked (&submit->exec) { 255 for (unsigned i = 0; i < submit->nr_bos; i++) { 256 struct drm_gem_object *obj = submit->bos[i].obj; 257 ret = drm_exec_prepare_obj(&submit->exec, obj, 1); 258 drm_exec_retry_on_contention(&submit->exec); 259 if (ret) 260 goto error; 261 } 262 } 263 264 return 0; 265 266 error: 267 return ret; 268 } 269 270 static int submit_fence_sync(struct msm_gem_submit *submit) 271 { 272 int i, ret = 0; 273 274 for (i = 0; i < submit->nr_bos; i++) { 275 struct drm_gem_object *obj = submit->bos[i].obj; 276 bool write = submit->bos[i].flags & MSM_SUBMIT_BO_WRITE; 277 278 /* Otherwise userspace can ask for implicit sync to be 279 * disabled on specific buffers. This is useful for internal 280 * usermode driver managed buffers, suballocation, etc. 281 */ 282 if (submit->bos[i].flags & MSM_SUBMIT_BO_NO_IMPLICIT) 283 continue; 284 285 ret = drm_sched_job_add_implicit_dependencies(&submit->base, 286 obj, 287 write); 288 if (ret) 289 break; 290 } 291 292 return ret; 293 } 294 295 static int submit_pin_objects(struct msm_gem_submit *submit) 296 { 297 struct msm_drm_private *priv = submit->dev->dev_private; 298 int i, ret = 0; 299 300 for (i = 0; i < submit->nr_bos; i++) { 301 struct drm_gem_object *obj = submit->bos[i].obj; 302 struct msm_gem_vma *vma; 303 304 /* if locking succeeded, pin bo: */ 305 vma = msm_gem_get_vma_locked(obj, submit->aspace); 306 if (IS_ERR(vma)) { 307 ret = PTR_ERR(vma); 308 break; 309 } 310 311 ret = msm_gem_pin_vma_locked(obj, vma); 312 if (ret) 313 break; 314 315 submit->bos[i].iova = vma->iova; 316 } 317 318 /* 319 * A second loop while holding the LRU lock (a) avoids acquiring/dropping 320 * the LRU lock for each individual bo, while (b) avoiding holding the 321 * LRU lock while calling msm_gem_pin_vma_locked() (which could trigger 322 * get_pages() which could trigger reclaim.. and if we held the LRU lock 323 * could trigger deadlock with the shrinker). 324 */ 325 mutex_lock(&priv->lru.lock); 326 for (i = 0; i < submit->nr_bos; i++) { 327 msm_gem_pin_obj_locked(submit->bos[i].obj); 328 } 329 mutex_unlock(&priv->lru.lock); 330 331 submit->bos_pinned = true; 332 333 return ret; 334 } 335 336 static void submit_unpin_objects(struct msm_gem_submit *submit) 337 { 338 if (!submit->bos_pinned) 339 return; 340 341 for (int i = 0; i < submit->nr_bos; i++) { 342 struct drm_gem_object *obj = submit->bos[i].obj; 343 344 msm_gem_unpin_locked(obj); 345 } 346 347 submit->bos_pinned = false; 348 } 349 350 static void submit_attach_object_fences(struct msm_gem_submit *submit) 351 { 352 int i; 353 354 for (i = 0; i < submit->nr_bos; i++) { 355 struct drm_gem_object *obj = submit->bos[i].obj; 356 357 if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE) 358 dma_resv_add_fence(obj->resv, submit->user_fence, 359 DMA_RESV_USAGE_WRITE); 360 else if (submit->bos[i].flags & MSM_SUBMIT_BO_READ) 361 dma_resv_add_fence(obj->resv, submit->user_fence, 362 DMA_RESV_USAGE_READ); 363 } 364 } 365 366 static int submit_bo(struct msm_gem_submit *submit, uint32_t idx, 367 struct drm_gem_object **obj, uint64_t *iova) 368 { 369 if (idx >= submit->nr_bos) { 370 return SUBMIT_ERROR(EINVAL, submit, "invalid buffer index: %u (out of %u)\n", 371 idx, submit->nr_bos); 372 } 373 374 if (obj) 375 *obj = submit->bos[idx].obj; 376 if (iova) 377 *iova = submit->bos[idx].iova; 378 379 return 0; 380 } 381 382 /* process the reloc's and patch up the cmdstream as needed: */ 383 static int submit_reloc(struct msm_gem_submit *submit, struct drm_gem_object *obj, 384 uint32_t offset, uint32_t nr_relocs, struct drm_msm_gem_submit_reloc *relocs) 385 { 386 uint32_t i, last_offset = 0; 387 uint32_t *ptr; 388 int ret = 0; 389 390 if (offset % 4) 391 return SUBMIT_ERROR(EINVAL, submit, "non-aligned cmdstream buffer: %u\n", offset); 392 393 /* For now, just map the entire thing. Eventually we probably 394 * to do it page-by-page, w/ kmap() if not vmap()d.. 395 */ 396 ptr = msm_gem_get_vaddr_locked(obj); 397 398 if (IS_ERR(ptr)) { 399 ret = PTR_ERR(ptr); 400 DBG("failed to map: %d", ret); 401 return ret; 402 } 403 404 for (i = 0; i < nr_relocs; i++) { 405 struct drm_msm_gem_submit_reloc submit_reloc = relocs[i]; 406 uint32_t off; 407 uint64_t iova; 408 409 if (submit_reloc.submit_offset % 4) { 410 ret = SUBMIT_ERROR(EINVAL, submit, "non-aligned reloc offset: %u\n", 411 submit_reloc.submit_offset); 412 goto out; 413 } 414 415 /* offset in dwords: */ 416 off = submit_reloc.submit_offset / 4; 417 418 if ((off >= (obj->size / 4)) || 419 (off < last_offset)) { 420 ret = SUBMIT_ERROR(EINVAL, submit, "invalid offset %u at reloc %u\n", off, i); 421 goto out; 422 } 423 424 ret = submit_bo(submit, submit_reloc.reloc_idx, NULL, &iova); 425 if (ret) 426 goto out; 427 428 iova += submit_reloc.reloc_offset; 429 430 if (submit_reloc.shift < 0) 431 iova >>= -submit_reloc.shift; 432 else 433 iova <<= submit_reloc.shift; 434 435 ptr[off] = iova | submit_reloc.or; 436 437 last_offset = off; 438 } 439 440 out: 441 msm_gem_put_vaddr_locked(obj); 442 443 return ret; 444 } 445 446 /* Cleanup submit at end of ioctl. In the error case, this also drops 447 * references, unpins, and drops active refcnt. In the non-error case, 448 * this is done when the submit is retired. 449 */ 450 static void submit_cleanup(struct msm_gem_submit *submit, bool error) 451 { 452 if (error) { 453 submit_unpin_objects(submit); 454 /* job wasn't enqueued to scheduler, so early retirement: */ 455 msm_submit_retire(submit); 456 } 457 458 if (submit->exec.objects) 459 drm_exec_fini(&submit->exec); 460 } 461 462 void msm_submit_retire(struct msm_gem_submit *submit) 463 { 464 int i; 465 466 for (i = 0; i < submit->nr_bos; i++) { 467 struct drm_gem_object *obj = submit->bos[i].obj; 468 469 drm_gem_object_put(obj); 470 } 471 } 472 473 struct msm_submit_post_dep { 474 struct drm_syncobj *syncobj; 475 uint64_t point; 476 struct dma_fence_chain *chain; 477 }; 478 479 static struct drm_syncobj **msm_parse_deps(struct msm_gem_submit *submit, 480 struct drm_file *file, 481 uint64_t in_syncobjs_addr, 482 uint32_t nr_in_syncobjs, 483 size_t syncobj_stride) 484 { 485 struct drm_syncobj **syncobjs = NULL; 486 struct drm_msm_gem_submit_syncobj syncobj_desc = {0}; 487 int ret = 0; 488 uint32_t i, j; 489 490 syncobjs = kcalloc(nr_in_syncobjs, sizeof(*syncobjs), 491 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); 492 if (!syncobjs) 493 return ERR_PTR(-ENOMEM); 494 495 for (i = 0; i < nr_in_syncobjs; ++i) { 496 uint64_t address = in_syncobjs_addr + i * syncobj_stride; 497 498 if (copy_from_user(&syncobj_desc, 499 u64_to_user_ptr(address), 500 min(syncobj_stride, sizeof(syncobj_desc)))) { 501 ret = -EFAULT; 502 break; 503 } 504 505 if (syncobj_desc.point && 506 !drm_core_check_feature(submit->dev, DRIVER_SYNCOBJ_TIMELINE)) { 507 ret = SUBMIT_ERROR(EOPNOTSUPP, submit, "syncobj timeline unsupported"); 508 break; 509 } 510 511 if (syncobj_desc.flags & ~MSM_SUBMIT_SYNCOBJ_FLAGS) { 512 ret = SUBMIT_ERROR(EINVAL, submit, "invalid syncobj flags: %x", syncobj_desc.flags); 513 break; 514 } 515 516 ret = drm_sched_job_add_syncobj_dependency(&submit->base, file, 517 syncobj_desc.handle, syncobj_desc.point); 518 if (ret) 519 break; 520 521 if (syncobj_desc.flags & MSM_SUBMIT_SYNCOBJ_RESET) { 522 syncobjs[i] = 523 drm_syncobj_find(file, syncobj_desc.handle); 524 if (!syncobjs[i]) { 525 ret = SUBMIT_ERROR(EINVAL, submit, "invalid syncobj handle: %u", i); 526 break; 527 } 528 } 529 } 530 531 if (ret) { 532 for (j = 0; j <= i; ++j) { 533 if (syncobjs[j]) 534 drm_syncobj_put(syncobjs[j]); 535 } 536 kfree(syncobjs); 537 return ERR_PTR(ret); 538 } 539 return syncobjs; 540 } 541 542 static void msm_reset_syncobjs(struct drm_syncobj **syncobjs, 543 uint32_t nr_syncobjs) 544 { 545 uint32_t i; 546 547 for (i = 0; syncobjs && i < nr_syncobjs; ++i) { 548 if (syncobjs[i]) 549 drm_syncobj_replace_fence(syncobjs[i], NULL); 550 } 551 } 552 553 static struct msm_submit_post_dep *msm_parse_post_deps(struct drm_device *dev, 554 struct drm_file *file, 555 uint64_t syncobjs_addr, 556 uint32_t nr_syncobjs, 557 size_t syncobj_stride) 558 { 559 struct msm_submit_post_dep *post_deps; 560 struct drm_msm_gem_submit_syncobj syncobj_desc = {0}; 561 int ret = 0; 562 uint32_t i, j; 563 564 post_deps = kcalloc(nr_syncobjs, sizeof(*post_deps), 565 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); 566 if (!post_deps) 567 return ERR_PTR(-ENOMEM); 568 569 for (i = 0; i < nr_syncobjs; ++i) { 570 uint64_t address = syncobjs_addr + i * syncobj_stride; 571 572 if (copy_from_user(&syncobj_desc, 573 u64_to_user_ptr(address), 574 min(syncobj_stride, sizeof(syncobj_desc)))) { 575 ret = -EFAULT; 576 break; 577 } 578 579 post_deps[i].point = syncobj_desc.point; 580 581 if (syncobj_desc.flags) { 582 ret = UERR(EINVAL, dev, "invalid syncobj flags"); 583 break; 584 } 585 586 if (syncobj_desc.point) { 587 if (!drm_core_check_feature(dev, 588 DRIVER_SYNCOBJ_TIMELINE)) { 589 ret = UERR(EOPNOTSUPP, dev, "syncobj timeline unsupported"); 590 break; 591 } 592 593 post_deps[i].chain = dma_fence_chain_alloc(); 594 if (!post_deps[i].chain) { 595 ret = -ENOMEM; 596 break; 597 } 598 } 599 600 post_deps[i].syncobj = 601 drm_syncobj_find(file, syncobj_desc.handle); 602 if (!post_deps[i].syncobj) { 603 ret = UERR(EINVAL, dev, "invalid syncobj handle"); 604 break; 605 } 606 } 607 608 if (ret) { 609 for (j = 0; j <= i; ++j) { 610 dma_fence_chain_free(post_deps[j].chain); 611 if (post_deps[j].syncobj) 612 drm_syncobj_put(post_deps[j].syncobj); 613 } 614 615 kfree(post_deps); 616 return ERR_PTR(ret); 617 } 618 619 return post_deps; 620 } 621 622 static void msm_process_post_deps(struct msm_submit_post_dep *post_deps, 623 uint32_t count, struct dma_fence *fence) 624 { 625 uint32_t i; 626 627 for (i = 0; post_deps && i < count; ++i) { 628 if (post_deps[i].chain) { 629 drm_syncobj_add_point(post_deps[i].syncobj, 630 post_deps[i].chain, 631 fence, post_deps[i].point); 632 post_deps[i].chain = NULL; 633 } else { 634 drm_syncobj_replace_fence(post_deps[i].syncobj, 635 fence); 636 } 637 } 638 } 639 640 int msm_ioctl_gem_submit(struct drm_device *dev, void *data, 641 struct drm_file *file) 642 { 643 struct msm_drm_private *priv = dev->dev_private; 644 struct drm_msm_gem_submit *args = data; 645 struct msm_file_private *ctx = file->driver_priv; 646 struct msm_gem_submit *submit = NULL; 647 struct msm_gpu *gpu = priv->gpu; 648 struct msm_gpu_submitqueue *queue; 649 struct msm_ringbuffer *ring; 650 struct msm_submit_post_dep *post_deps = NULL; 651 struct drm_syncobj **syncobjs_to_reset = NULL; 652 int out_fence_fd = -1; 653 unsigned i; 654 int ret; 655 656 if (!gpu) 657 return -ENXIO; 658 659 if (args->pad) 660 return -EINVAL; 661 662 if (unlikely(!ctx->aspace) && !capable(CAP_SYS_RAWIO)) { 663 DRM_ERROR_RATELIMITED("IOMMU support or CAP_SYS_RAWIO required!\n"); 664 return -EPERM; 665 } 666 667 /* for now, we just have 3d pipe.. eventually this would need to 668 * be more clever to dispatch to appropriate gpu module: 669 */ 670 if (MSM_PIPE_ID(args->flags) != MSM_PIPE_3D0) 671 return UERR(EINVAL, dev, "invalid pipe"); 672 673 if (MSM_PIPE_FLAGS(args->flags) & ~MSM_SUBMIT_FLAGS) 674 return UERR(EINVAL, dev, "invalid flags"); 675 676 if (args->flags & MSM_SUBMIT_SUDO) { 677 if (!IS_ENABLED(CONFIG_DRM_MSM_GPU_SUDO) || 678 !capable(CAP_SYS_RAWIO)) 679 return -EINVAL; 680 } 681 682 queue = msm_submitqueue_get(ctx, args->queueid); 683 if (!queue) 684 return -ENOENT; 685 686 ring = gpu->rb[queue->ring_nr]; 687 688 if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) { 689 out_fence_fd = get_unused_fd_flags(O_CLOEXEC); 690 if (out_fence_fd < 0) { 691 ret = out_fence_fd; 692 goto out_post_unlock; 693 } 694 } 695 696 submit = submit_create(dev, gpu, queue, args->nr_bos, args->nr_cmds); 697 if (IS_ERR(submit)) { 698 ret = PTR_ERR(submit); 699 goto out_post_unlock; 700 } 701 702 trace_msm_gpu_submit(pid_nr(submit->pid), ring->id, submit->ident, 703 args->nr_bos, args->nr_cmds); 704 705 ret = mutex_lock_interruptible(&queue->lock); 706 if (ret) 707 goto out_post_unlock; 708 709 if (args->flags & MSM_SUBMIT_SUDO) 710 submit->in_rb = true; 711 712 if (args->flags & MSM_SUBMIT_FENCE_FD_IN) { 713 struct dma_fence *in_fence; 714 715 in_fence = sync_file_get_fence(args->fence_fd); 716 717 if (!in_fence) { 718 ret = UERR(EINVAL, dev, "invalid in-fence"); 719 goto out_unlock; 720 } 721 722 ret = drm_sched_job_add_dependency(&submit->base, in_fence); 723 if (ret) 724 goto out_unlock; 725 } 726 727 if (args->flags & MSM_SUBMIT_SYNCOBJ_IN) { 728 syncobjs_to_reset = msm_parse_deps(submit, file, 729 args->in_syncobjs, 730 args->nr_in_syncobjs, 731 args->syncobj_stride); 732 if (IS_ERR(syncobjs_to_reset)) { 733 ret = PTR_ERR(syncobjs_to_reset); 734 goto out_unlock; 735 } 736 } 737 738 if (args->flags & MSM_SUBMIT_SYNCOBJ_OUT) { 739 post_deps = msm_parse_post_deps(dev, file, 740 args->out_syncobjs, 741 args->nr_out_syncobjs, 742 args->syncobj_stride); 743 if (IS_ERR(post_deps)) { 744 ret = PTR_ERR(post_deps); 745 goto out_unlock; 746 } 747 } 748 749 ret = submit_lookup_objects(submit, args, file); 750 if (ret) 751 goto out; 752 753 ret = submit_lookup_cmds(submit, args, file); 754 if (ret) 755 goto out; 756 757 /* copy_*_user while holding a ww ticket upsets lockdep */ 758 ret = submit_lock_objects(submit); 759 if (ret) 760 goto out; 761 762 if (!(args->flags & MSM_SUBMIT_NO_IMPLICIT)) { 763 ret = submit_fence_sync(submit); 764 if (ret) 765 goto out; 766 } 767 768 ret = submit_pin_objects(submit); 769 if (ret) 770 goto out; 771 772 for (i = 0; i < args->nr_cmds; i++) { 773 struct drm_gem_object *obj; 774 uint64_t iova; 775 776 ret = submit_bo(submit, submit->cmd[i].idx, &obj, &iova); 777 if (ret) 778 goto out; 779 780 if (!submit->cmd[i].size || 781 (size_add(submit->cmd[i].size, submit->cmd[i].offset) > obj->size / 4)) { 782 ret = UERR(EINVAL, dev, "invalid cmdstream size: %u\n", 783 submit->cmd[i].size * 4); 784 goto out; 785 } 786 787 submit->cmd[i].iova = iova + (submit->cmd[i].offset * 4); 788 789 if (likely(!submit->cmd[i].nr_relocs)) 790 continue; 791 792 if (!gpu->allow_relocs) { 793 ret = UERR(EINVAL, dev, "relocs not allowed\n"); 794 goto out; 795 } 796 797 ret = submit_reloc(submit, obj, submit->cmd[i].offset * 4, 798 submit->cmd[i].nr_relocs, submit->cmd[i].relocs); 799 if (ret) 800 goto out; 801 } 802 803 submit->nr_cmds = i; 804 805 idr_preload(GFP_KERNEL); 806 807 spin_lock(&queue->idr_lock); 808 809 /* 810 * If using userspace provided seqno fence, validate that the id 811 * is available before arming sched job. Since access to fence_idr 812 * is serialized on the queue lock, the slot should be still avail 813 * after the job is armed 814 */ 815 if ((args->flags & MSM_SUBMIT_FENCE_SN_IN) && 816 (!args->fence || idr_find(&queue->fence_idr, args->fence))) { 817 spin_unlock(&queue->idr_lock); 818 idr_preload_end(); 819 ret = UERR(EINVAL, dev, "invalid in-fence-sn"); 820 goto out; 821 } 822 823 drm_sched_job_arm(&submit->base); 824 825 submit->user_fence = dma_fence_get(&submit->base.s_fence->finished); 826 827 if (args->flags & MSM_SUBMIT_FENCE_SN_IN) { 828 /* 829 * Userspace has assigned the seqno fence that it wants 830 * us to use. It is an error to pick a fence sequence 831 * number that is not available. 832 */ 833 submit->fence_id = args->fence; 834 ret = idr_alloc_u32(&queue->fence_idr, submit->user_fence, 835 &submit->fence_id, submit->fence_id, 836 GFP_NOWAIT); 837 /* 838 * We've already validated that the fence_id slot is valid, 839 * so if idr_alloc_u32 failed, it is a kernel bug 840 */ 841 WARN_ON(ret); 842 } else { 843 /* 844 * Allocate an id which can be used by WAIT_FENCE ioctl to map 845 * back to the underlying fence. 846 */ 847 submit->fence_id = idr_alloc_cyclic(&queue->fence_idr, 848 submit->user_fence, 1, 849 INT_MAX, GFP_NOWAIT); 850 } 851 852 spin_unlock(&queue->idr_lock); 853 idr_preload_end(); 854 855 if (submit->fence_id < 0) { 856 ret = submit->fence_id; 857 submit->fence_id = 0; 858 } 859 860 if (ret == 0 && args->flags & MSM_SUBMIT_FENCE_FD_OUT) { 861 struct sync_file *sync_file = sync_file_create(submit->user_fence); 862 if (!sync_file) { 863 ret = -ENOMEM; 864 } else { 865 fd_install(out_fence_fd, sync_file->file); 866 args->fence_fd = out_fence_fd; 867 } 868 } 869 870 if (ret) 871 goto out; 872 873 submit_attach_object_fences(submit); 874 875 /* The scheduler owns a ref now: */ 876 msm_gem_submit_get(submit); 877 878 msm_rd_dump_submit(priv->rd, submit, NULL); 879 880 drm_sched_entity_push_job(&submit->base); 881 882 args->fence = submit->fence_id; 883 queue->last_fence = submit->fence_id; 884 885 msm_reset_syncobjs(syncobjs_to_reset, args->nr_in_syncobjs); 886 msm_process_post_deps(post_deps, args->nr_out_syncobjs, 887 submit->user_fence); 888 889 890 out: 891 submit_cleanup(submit, !!ret); 892 out_unlock: 893 mutex_unlock(&queue->lock); 894 out_post_unlock: 895 if (ret && (out_fence_fd >= 0)) 896 put_unused_fd(out_fence_fd); 897 898 if (!IS_ERR_OR_NULL(submit)) { 899 msm_gem_submit_put(submit); 900 } else { 901 /* 902 * If the submit hasn't yet taken ownership of the queue 903 * then we need to drop the reference ourself: 904 */ 905 msm_submitqueue_put(queue); 906 } 907 if (!IS_ERR_OR_NULL(post_deps)) { 908 for (i = 0; i < args->nr_out_syncobjs; ++i) { 909 kfree(post_deps[i].chain); 910 drm_syncobj_put(post_deps[i].syncobj); 911 } 912 kfree(post_deps); 913 } 914 915 if (!IS_ERR_OR_NULL(syncobjs_to_reset)) { 916 for (i = 0; i < args->nr_in_syncobjs; ++i) { 917 if (syncobjs_to_reset[i]) 918 drm_syncobj_put(syncobjs_to_reset[i]); 919 } 920 kfree(syncobjs_to_reset); 921 } 922 923 return ret; 924 } 925