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