1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2016 Red Hat 4 * Author: Rob Clark <robdclark@gmail.com> 5 */ 6 7 #include "drm/drm_file.h" 8 #include "drm/msm_drm.h" 9 #include "linux/file.h" 10 #include "linux/sync_file.h" 11 12 #include "msm_drv.h" 13 #include "msm_gem.h" 14 #include "msm_gpu.h" 15 #include "msm_mmu.h" 16 #include "msm_syncobj.h" 17 18 #define vm_dbg(fmt, ...) pr_debug("%s:%d: "fmt"\n", __func__, __LINE__, ##__VA_ARGS__) 19 20 static uint vm_log_shift = 0; 21 MODULE_PARM_DESC(vm_log_shift, "Length of VM op log"); 22 module_param_named(vm_log_shift, vm_log_shift, uint, 0600); 23 24 /** 25 * struct msm_vm_map_op - create new pgtable mapping 26 */ 27 struct msm_vm_map_op { 28 /** @iova: start address for mapping */ 29 uint64_t iova; 30 /** @range: size of the region to map */ 31 uint64_t range; 32 /** @offset: offset into @sgt to map */ 33 uint64_t offset; 34 /** @sgt: pages to map, or NULL for a PRR mapping */ 35 struct sg_table *sgt; 36 /** @prot: the mapping protection flags */ 37 int prot; 38 39 /** 40 * @queue_id: The id of the submitqueue the operation is performed 41 * on, or zero for (in particular) UNMAP ops triggered outside of 42 * a submitqueue (ie. process cleanup) 43 */ 44 int queue_id; 45 }; 46 47 /** 48 * struct msm_vm_unmap_op - unmap a range of pages from pgtable 49 */ 50 struct msm_vm_unmap_op { 51 /** @iova: start address for unmap */ 52 uint64_t iova; 53 /** @range: size of region to unmap */ 54 uint64_t range; 55 56 /** @reason: The reason for the unmap */ 57 const char *reason; 58 59 /** 60 * @queue_id: The id of the submitqueue the operation is performed 61 * on, or zero for (in particular) UNMAP ops triggered outside of 62 * a submitqueue (ie. process cleanup) 63 */ 64 int queue_id; 65 }; 66 67 /** 68 * struct msm_vma_op - A MAP or UNMAP operation 69 */ 70 struct msm_vm_op { 71 /** @op: The operation type */ 72 enum { 73 MSM_VM_OP_MAP = 1, 74 MSM_VM_OP_UNMAP, 75 } op; 76 union { 77 /** @map: Parameters used if op == MSM_VMA_OP_MAP */ 78 struct msm_vm_map_op map; 79 /** @unmap: Parameters used if op == MSM_VMA_OP_UNMAP */ 80 struct msm_vm_unmap_op unmap; 81 }; 82 /** @node: list head in msm_vm_bind_job::vm_ops */ 83 struct list_head node; 84 85 /** 86 * @obj: backing object for pages to be mapped/unmapped 87 * 88 * Async unmap ops, in particular, must hold a reference to the 89 * original GEM object backing the mapping that will be unmapped. 90 * But the same can be required in the map path, for example if 91 * there is not a corresponding unmap op, such as process exit. 92 * 93 * This ensures that the pages backing the mapping are not freed 94 * before the mapping is torn down. 95 */ 96 struct drm_gem_object *obj; 97 }; 98 99 /** 100 * struct msm_vm_bind_job - Tracking for a VM_BIND ioctl 101 * 102 * A table of userspace requested VM updates (MSM_VM_BIND_OP_UNMAP/MAP/MAP_NULL) 103 * gets applied to the vm, generating a list of VM ops (MSM_VM_OP_MAP/UNMAP) 104 * which are applied to the pgtables asynchronously. For example a userspace 105 * requested MSM_VM_BIND_OP_MAP could end up generating both an MSM_VM_OP_UNMAP 106 * to unmap an existing mapping, and a MSM_VM_OP_MAP to apply the new mapping. 107 */ 108 struct msm_vm_bind_job { 109 /** @base: base class for drm_sched jobs */ 110 struct drm_sched_job base; 111 /** @vm: The VM being operated on */ 112 struct drm_gpuvm *vm; 113 /** @fence: The fence that is signaled when job completes */ 114 struct dma_fence *fence; 115 /** @queue: The queue that the job runs on */ 116 struct msm_gpu_submitqueue *queue; 117 /** @prealloc: Tracking for pre-allocated MMU pgtable pages */ 118 struct msm_mmu_prealloc prealloc; 119 /** @vm_ops: a list of struct msm_vm_op */ 120 struct list_head vm_ops; 121 /** @bos_pinned: are the GEM objects being bound pinned? */ 122 bool bos_pinned; 123 /** @nr_ops: the number of userspace requested ops */ 124 unsigned int nr_ops; 125 /** 126 * @ops: the userspace requested ops 127 * 128 * The userspace requested ops are copied/parsed and validated 129 * before we start applying the updates to try to do as much up- 130 * front error checking as possible, to avoid the VM being in an 131 * undefined state due to partially executed VM_BIND. 132 * 133 * This table also serves to hold a reference to the backing GEM 134 * objects. 135 */ 136 struct msm_vm_bind_op { 137 uint32_t op; 138 uint32_t flags; 139 union { 140 struct drm_gem_object *obj; 141 uint32_t handle; 142 }; 143 uint64_t obj_offset; 144 uint64_t iova; 145 uint64_t range; 146 } ops[]; 147 }; 148 149 #define job_foreach_bo(obj, _job) \ 150 for (unsigned i = 0; i < (_job)->nr_ops; i++) \ 151 if ((obj = (_job)->ops[i].obj)) 152 153 static inline struct msm_vm_bind_job *to_msm_vm_bind_job(struct drm_sched_job *job) 154 { 155 return container_of(job, struct msm_vm_bind_job, base); 156 } 157 158 static void 159 msm_gem_vm_free(struct drm_gpuvm *gpuvm) 160 { 161 struct msm_gem_vm *vm = container_of(gpuvm, struct msm_gem_vm, base); 162 163 drm_mm_takedown(&vm->mm); 164 if (vm->mmu) 165 vm->mmu->funcs->destroy(vm->mmu); 166 dma_fence_put(vm->last_fence); 167 put_pid(vm->pid); 168 kfree(vm->log); 169 kfree(vm); 170 } 171 172 /** 173 * msm_gem_vm_unusable() - Mark a VM as unusable 174 * @gpuvm: the VM to mark unusable 175 */ 176 void 177 msm_gem_vm_unusable(struct drm_gpuvm *gpuvm) 178 { 179 struct msm_gem_vm *vm = to_msm_vm(gpuvm); 180 uint32_t vm_log_len = (1 << vm->log_shift); 181 uint32_t vm_log_mask = vm_log_len - 1; 182 uint32_t nr_vm_logs; 183 int first; 184 185 vm->unusable = true; 186 187 /* Bail if no log, or empty log: */ 188 if (!vm->log || !vm->log[0].op) 189 return; 190 191 mutex_lock(&vm->mmu_lock); 192 193 /* 194 * log_idx is the next entry to overwrite, meaning it is the oldest, or 195 * first, entry (other than the special case handled below where the 196 * log hasn't wrapped around yet) 197 */ 198 first = vm->log_idx; 199 200 if (!vm->log[first].op) { 201 /* 202 * If the next log entry has not been written yet, then only 203 * entries 0 to idx-1 are valid (ie. we haven't wrapped around 204 * yet) 205 */ 206 nr_vm_logs = MAX(0, first - 1); 207 first = 0; 208 } else { 209 nr_vm_logs = vm_log_len; 210 } 211 212 pr_err("vm-log:\n"); 213 for (int i = 0; i < nr_vm_logs; i++) { 214 int idx = (i + first) & vm_log_mask; 215 struct msm_gem_vm_log_entry *e = &vm->log[idx]; 216 pr_err(" - %s:%d: 0x%016llx-0x%016llx\n", 217 e->op, e->queue_id, e->iova, 218 e->iova + e->range); 219 } 220 221 mutex_unlock(&vm->mmu_lock); 222 } 223 224 static void 225 vm_log(struct msm_gem_vm *vm, const char *op, uint64_t iova, uint64_t range, int queue_id) 226 { 227 int idx; 228 229 if (!vm->managed) 230 lockdep_assert_held(&vm->mmu_lock); 231 232 vm_dbg("%s:%p:%d: %016llx %016llx", op, vm, queue_id, iova, iova + range); 233 234 if (!vm->log) 235 return; 236 237 idx = vm->log_idx; 238 vm->log[idx].op = op; 239 vm->log[idx].iova = iova; 240 vm->log[idx].range = range; 241 vm->log[idx].queue_id = queue_id; 242 vm->log_idx = (vm->log_idx + 1) & ((1 << vm->log_shift) - 1); 243 } 244 245 static void 246 vm_unmap_op(struct msm_gem_vm *vm, const struct msm_vm_unmap_op *op) 247 { 248 const char *reason = op->reason; 249 250 if (!reason) 251 reason = "unmap"; 252 253 vm_log(vm, reason, op->iova, op->range, op->queue_id); 254 255 vm->mmu->funcs->unmap(vm->mmu, op->iova, op->range); 256 } 257 258 static int 259 vm_map_op(struct msm_gem_vm *vm, const struct msm_vm_map_op *op) 260 { 261 vm_log(vm, "map", op->iova, op->range, op->queue_id); 262 263 return vm->mmu->funcs->map(vm->mmu, op->iova, op->sgt, op->offset, 264 op->range, op->prot); 265 } 266 267 /* Actually unmap memory for the vma */ 268 void msm_gem_vma_unmap(struct drm_gpuva *vma, const char *reason) 269 { 270 struct msm_gem_vm *vm = to_msm_vm(vma->vm); 271 struct msm_gem_vma *msm_vma = to_msm_vma(vma); 272 273 /* Don't do anything if the memory isn't mapped */ 274 if (!msm_vma->mapped) 275 return; 276 277 /* 278 * The mmu_lock is only needed when preallocation is used. But 279 * in that case we don't need to worry about recursion into 280 * shrinker 281 */ 282 if (!vm->managed) 283 mutex_lock(&vm->mmu_lock); 284 285 vm_unmap_op(vm, &(struct msm_vm_unmap_op){ 286 .iova = vma->va.addr, 287 .range = vma->va.range, 288 .reason = reason, 289 }); 290 291 if (!vm->managed) 292 mutex_unlock(&vm->mmu_lock); 293 294 msm_vma->mapped = false; 295 } 296 297 /* Map and pin vma: */ 298 int 299 msm_gem_vma_map(struct drm_gpuva *vma, int prot, struct sg_table *sgt) 300 { 301 struct msm_gem_vm *vm = to_msm_vm(vma->vm); 302 struct msm_gem_vma *msm_vma = to_msm_vma(vma); 303 int ret; 304 305 if (GEM_WARN_ON(!vma->va.addr)) 306 return -EINVAL; 307 308 if (msm_vma->mapped) 309 return 0; 310 311 msm_vma->mapped = true; 312 313 /* 314 * The mmu_lock is only needed when preallocation is used. But 315 * in that case we don't need to worry about recursion into 316 * shrinker 317 */ 318 if (!vm->managed) 319 mutex_lock(&vm->mmu_lock); 320 321 /* 322 * NOTE: if not using pgtable preallocation, we cannot hold 323 * a lock across map/unmap which is also used in the job_run() 324 * path, as this can cause deadlock in job_run() vs shrinker/ 325 * reclaim. 326 */ 327 ret = vm_map_op(vm, &(struct msm_vm_map_op){ 328 .iova = vma->va.addr, 329 .range = vma->va.range, 330 .offset = vma->gem.offset, 331 .sgt = sgt, 332 .prot = prot, 333 }); 334 335 if (!vm->managed) 336 mutex_unlock(&vm->mmu_lock); 337 338 if (ret) 339 msm_vma->mapped = false; 340 341 return ret; 342 } 343 344 /* Close an iova. Warn if it is still in use */ 345 void msm_gem_vma_close(struct drm_gpuva *vma) 346 { 347 struct msm_gem_vm *vm = to_msm_vm(vma->vm); 348 struct msm_gem_vma *msm_vma = to_msm_vma(vma); 349 350 GEM_WARN_ON(msm_vma->mapped); 351 352 drm_gpuvm_resv_assert_held(&vm->base); 353 354 if (vma->gem.obj) 355 msm_gem_assert_locked(vma->gem.obj); 356 357 if (vma->va.addr && vm->managed) 358 drm_mm_remove_node(&msm_vma->node); 359 360 drm_gpuva_remove(vma); 361 drm_gpuva_unlink(vma); 362 363 kfree(vma); 364 } 365 366 /* Create a new vma and allocate an iova for it */ 367 struct drm_gpuva * 368 msm_gem_vma_new(struct drm_gpuvm *gpuvm, struct drm_gem_object *obj, 369 u64 offset, u64 range_start, u64 range_end) 370 { 371 struct msm_gem_vm *vm = to_msm_vm(gpuvm); 372 struct drm_gpuvm_bo *vm_bo; 373 struct msm_gem_vma *vma; 374 int ret; 375 376 drm_gpuvm_resv_assert_held(&vm->base); 377 378 vma = kzalloc(sizeof(*vma), GFP_KERNEL); 379 if (!vma) 380 return ERR_PTR(-ENOMEM); 381 382 if (vm->managed) { 383 BUG_ON(offset != 0); 384 BUG_ON(!obj); /* NULL mappings not valid for kernel managed VM */ 385 ret = drm_mm_insert_node_in_range(&vm->mm, &vma->node, 386 obj->size, PAGE_SIZE, 0, 387 range_start, range_end, 0); 388 389 if (ret) 390 goto err_free_vma; 391 392 range_start = vma->node.start; 393 range_end = range_start + obj->size; 394 } 395 396 if (obj) 397 GEM_WARN_ON((range_end - range_start) > obj->size); 398 399 drm_gpuva_init(&vma->base, range_start, range_end - range_start, obj, offset); 400 vma->mapped = false; 401 402 ret = drm_gpuva_insert(&vm->base, &vma->base); 403 if (ret) 404 goto err_free_range; 405 406 if (!obj) 407 return &vma->base; 408 409 vm_bo = drm_gpuvm_bo_obtain(&vm->base, obj); 410 if (IS_ERR(vm_bo)) { 411 ret = PTR_ERR(vm_bo); 412 goto err_va_remove; 413 } 414 415 drm_gpuvm_bo_extobj_add(vm_bo); 416 drm_gpuva_link(&vma->base, vm_bo); 417 GEM_WARN_ON(drm_gpuvm_bo_put(vm_bo)); 418 419 return &vma->base; 420 421 err_va_remove: 422 drm_gpuva_remove(&vma->base); 423 err_free_range: 424 if (vm->managed) 425 drm_mm_remove_node(&vma->node); 426 err_free_vma: 427 kfree(vma); 428 return ERR_PTR(ret); 429 } 430 431 static int 432 msm_gem_vm_bo_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec) 433 { 434 struct drm_gem_object *obj = vm_bo->obj; 435 struct drm_gpuva *vma; 436 int ret; 437 438 vm_dbg("validate: %p", obj); 439 440 msm_gem_assert_locked(obj); 441 442 drm_gpuvm_bo_for_each_va (vma, vm_bo) { 443 ret = msm_gem_pin_vma_locked(obj, vma); 444 if (ret) 445 return ret; 446 } 447 448 return 0; 449 } 450 451 struct op_arg { 452 unsigned flags; 453 struct msm_vm_bind_job *job; 454 const struct msm_vm_bind_op *op; 455 bool kept; 456 }; 457 458 static void 459 vm_op_enqueue(struct op_arg *arg, struct msm_vm_op _op) 460 { 461 struct msm_vm_op *op = kmalloc(sizeof(*op), GFP_KERNEL); 462 *op = _op; 463 list_add_tail(&op->node, &arg->job->vm_ops); 464 465 if (op->obj) 466 drm_gem_object_get(op->obj); 467 } 468 469 static struct drm_gpuva * 470 vma_from_op(struct op_arg *arg, struct drm_gpuva_op_map *op) 471 { 472 return msm_gem_vma_new(arg->job->vm, op->gem.obj, op->gem.offset, 473 op->va.addr, op->va.addr + op->va.range); 474 } 475 476 static int 477 msm_gem_vm_sm_step_map(struct drm_gpuva_op *op, void *_arg) 478 { 479 struct op_arg *arg = _arg; 480 struct msm_vm_bind_job *job = arg->job; 481 struct drm_gem_object *obj = op->map.gem.obj; 482 struct drm_gpuva *vma; 483 struct sg_table *sgt; 484 unsigned prot; 485 486 if (arg->kept) 487 return 0; 488 489 vma = vma_from_op(arg, &op->map); 490 if (WARN_ON(IS_ERR(vma))) 491 return PTR_ERR(vma); 492 493 vm_dbg("%p:%p:%p: %016llx %016llx", vma->vm, vma, vma->gem.obj, 494 vma->va.addr, vma->va.range); 495 496 vma->flags = ((struct op_arg *)arg)->flags; 497 498 if (obj) { 499 sgt = to_msm_bo(obj)->sgt; 500 prot = msm_gem_prot(obj); 501 } else { 502 sgt = NULL; 503 prot = IOMMU_READ | IOMMU_WRITE; 504 } 505 506 vm_op_enqueue(arg, (struct msm_vm_op){ 507 .op = MSM_VM_OP_MAP, 508 .map = { 509 .sgt = sgt, 510 .iova = vma->va.addr, 511 .range = vma->va.range, 512 .offset = vma->gem.offset, 513 .prot = prot, 514 .queue_id = job->queue->id, 515 }, 516 .obj = vma->gem.obj, 517 }); 518 519 to_msm_vma(vma)->mapped = true; 520 521 return 0; 522 } 523 524 static int 525 msm_gem_vm_sm_step_remap(struct drm_gpuva_op *op, void *arg) 526 { 527 struct msm_vm_bind_job *job = ((struct op_arg *)arg)->job; 528 struct drm_gpuvm *vm = job->vm; 529 struct drm_gpuva *orig_vma = op->remap.unmap->va; 530 struct drm_gpuva *prev_vma = NULL, *next_vma = NULL; 531 struct drm_gpuvm_bo *vm_bo = orig_vma->vm_bo; 532 bool mapped = to_msm_vma(orig_vma)->mapped; 533 unsigned flags; 534 535 vm_dbg("orig_vma: %p:%p:%p: %016llx %016llx", vm, orig_vma, 536 orig_vma->gem.obj, orig_vma->va.addr, orig_vma->va.range); 537 538 if (mapped) { 539 uint64_t unmap_start, unmap_range; 540 541 drm_gpuva_op_remap_to_unmap_range(&op->remap, &unmap_start, &unmap_range); 542 543 vm_op_enqueue(arg, (struct msm_vm_op){ 544 .op = MSM_VM_OP_UNMAP, 545 .unmap = { 546 .iova = unmap_start, 547 .range = unmap_range, 548 .queue_id = job->queue->id, 549 }, 550 .obj = orig_vma->gem.obj, 551 }); 552 553 /* 554 * Part of this GEM obj is still mapped, but we're going to kill the 555 * existing VMA and replace it with one or two new ones (ie. two if 556 * the unmapped range is in the middle of the existing (unmap) VMA). 557 * So just set the state to unmapped: 558 */ 559 to_msm_vma(orig_vma)->mapped = false; 560 } 561 562 /* 563 * Hold a ref to the vm_bo between the msm_gem_vma_close() and the 564 * creation of the new prev/next vma's, in case the vm_bo is tracked 565 * in the VM's evict list: 566 */ 567 if (vm_bo) 568 drm_gpuvm_bo_get(vm_bo); 569 570 /* 571 * The prev_vma and/or next_vma are replacing the unmapped vma, and 572 * therefore should preserve it's flags: 573 */ 574 flags = orig_vma->flags; 575 576 msm_gem_vma_close(orig_vma); 577 578 if (op->remap.prev) { 579 prev_vma = vma_from_op(arg, op->remap.prev); 580 if (WARN_ON(IS_ERR(prev_vma))) 581 return PTR_ERR(prev_vma); 582 583 vm_dbg("prev_vma: %p:%p: %016llx %016llx", vm, prev_vma, prev_vma->va.addr, prev_vma->va.range); 584 to_msm_vma(prev_vma)->mapped = mapped; 585 prev_vma->flags = flags; 586 } 587 588 if (op->remap.next) { 589 next_vma = vma_from_op(arg, op->remap.next); 590 if (WARN_ON(IS_ERR(next_vma))) 591 return PTR_ERR(next_vma); 592 593 vm_dbg("next_vma: %p:%p: %016llx %016llx", vm, next_vma, next_vma->va.addr, next_vma->va.range); 594 to_msm_vma(next_vma)->mapped = mapped; 595 next_vma->flags = flags; 596 } 597 598 if (!mapped) 599 drm_gpuvm_bo_evict(vm_bo, true); 600 601 /* Drop the previous ref: */ 602 drm_gpuvm_bo_put(vm_bo); 603 604 return 0; 605 } 606 607 static int 608 msm_gem_vm_sm_step_unmap(struct drm_gpuva_op *op, void *_arg) 609 { 610 struct op_arg *arg = _arg; 611 struct msm_vm_bind_job *job = arg->job; 612 struct drm_gpuva *vma = op->unmap.va; 613 struct msm_gem_vma *msm_vma = to_msm_vma(vma); 614 615 vm_dbg("%p:%p:%p: %016llx %016llx", vma->vm, vma, vma->gem.obj, 616 vma->va.addr, vma->va.range); 617 618 /* 619 * Detect in-place remap. Turnip does this to change the vma flags, 620 * in particular MSM_VMA_DUMP. In this case we want to avoid actually 621 * touching the page tables, as that would require synchronization 622 * against SUBMIT jobs running on the GPU. 623 */ 624 if (op->unmap.keep && 625 (arg->op->op == MSM_VM_BIND_OP_MAP) && 626 (vma->gem.obj == arg->op->obj) && 627 (vma->gem.offset == arg->op->obj_offset) && 628 (vma->va.addr == arg->op->iova) && 629 (vma->va.range == arg->op->range)) { 630 /* We are only expecting a single in-place unmap+map cb pair: */ 631 WARN_ON(arg->kept); 632 633 /* Leave the existing VMA in place, but signal that to the map cb: */ 634 arg->kept = true; 635 636 /* Only flags are changing, so update that in-place: */ 637 unsigned orig_flags = vma->flags & (DRM_GPUVA_USERBITS - 1); 638 vma->flags = orig_flags | arg->flags; 639 640 return 0; 641 } 642 643 if (!msm_vma->mapped) 644 goto out_close; 645 646 vm_op_enqueue(arg, (struct msm_vm_op){ 647 .op = MSM_VM_OP_UNMAP, 648 .unmap = { 649 .iova = vma->va.addr, 650 .range = vma->va.range, 651 .queue_id = job->queue->id, 652 }, 653 .obj = vma->gem.obj, 654 }); 655 656 msm_vma->mapped = false; 657 658 out_close: 659 msm_gem_vma_close(vma); 660 661 return 0; 662 } 663 664 static const struct drm_gpuvm_ops msm_gpuvm_ops = { 665 .vm_free = msm_gem_vm_free, 666 .vm_bo_validate = msm_gem_vm_bo_validate, 667 .sm_step_map = msm_gem_vm_sm_step_map, 668 .sm_step_remap = msm_gem_vm_sm_step_remap, 669 .sm_step_unmap = msm_gem_vm_sm_step_unmap, 670 }; 671 672 static struct dma_fence * 673 msm_vma_job_run(struct drm_sched_job *_job) 674 { 675 struct msm_vm_bind_job *job = to_msm_vm_bind_job(_job); 676 struct msm_gem_vm *vm = to_msm_vm(job->vm); 677 struct drm_gem_object *obj; 678 int ret = vm->unusable ? -EINVAL : 0; 679 680 vm_dbg(""); 681 682 mutex_lock(&vm->mmu_lock); 683 vm->mmu->prealloc = &job->prealloc; 684 685 while (!list_empty(&job->vm_ops)) { 686 struct msm_vm_op *op = 687 list_first_entry(&job->vm_ops, struct msm_vm_op, node); 688 689 switch (op->op) { 690 case MSM_VM_OP_MAP: 691 /* 692 * On error, stop trying to map new things.. but we 693 * still want to process the unmaps (or in particular, 694 * the drm_gem_object_put()s) 695 */ 696 if (!ret) 697 ret = vm_map_op(vm, &op->map); 698 break; 699 case MSM_VM_OP_UNMAP: 700 vm_unmap_op(vm, &op->unmap); 701 break; 702 } 703 drm_gem_object_put(op->obj); 704 list_del(&op->node); 705 kfree(op); 706 } 707 708 vm->mmu->prealloc = NULL; 709 mutex_unlock(&vm->mmu_lock); 710 711 /* 712 * We failed to perform at least _some_ of the pgtable updates, so 713 * now the VM is in an undefined state. Game over! 714 */ 715 if (ret) 716 msm_gem_vm_unusable(job->vm); 717 718 job_foreach_bo (obj, job) { 719 msm_gem_lock(obj); 720 msm_gem_unpin_locked(obj); 721 msm_gem_unlock(obj); 722 } 723 724 /* VM_BIND ops are synchronous, so no fence to wait on: */ 725 return NULL; 726 } 727 728 static void 729 msm_vma_job_free(struct drm_sched_job *_job) 730 { 731 struct msm_vm_bind_job *job = to_msm_vm_bind_job(_job); 732 struct msm_gem_vm *vm = to_msm_vm(job->vm); 733 struct drm_gem_object *obj; 734 735 vm->mmu->funcs->prealloc_cleanup(vm->mmu, &job->prealloc); 736 737 atomic_sub(job->prealloc.count, &vm->prealloc_throttle.in_flight); 738 739 drm_sched_job_cleanup(_job); 740 741 job_foreach_bo (obj, job) 742 drm_gem_object_put(obj); 743 744 msm_submitqueue_put(job->queue); 745 dma_fence_put(job->fence); 746 747 /* In error paths, we could have unexecuted ops: */ 748 while (!list_empty(&job->vm_ops)) { 749 struct msm_vm_op *op = 750 list_first_entry(&job->vm_ops, struct msm_vm_op, node); 751 list_del(&op->node); 752 kfree(op); 753 } 754 755 wake_up(&vm->prealloc_throttle.wait); 756 757 kfree(job); 758 } 759 760 static const struct drm_sched_backend_ops msm_vm_bind_ops = { 761 .run_job = msm_vma_job_run, 762 .free_job = msm_vma_job_free 763 }; 764 765 /** 766 * msm_gem_vm_create() - Create and initialize a &msm_gem_vm 767 * @drm: the drm device 768 * @mmu: the backing MMU objects handling mapping/unmapping 769 * @name: the name of the VM 770 * @va_start: the start offset of the VA space 771 * @va_size: the size of the VA space 772 * @managed: is it a kernel managed VM? 773 * 774 * In a kernel managed VM, the kernel handles address allocation, and only 775 * synchronous operations are supported. In a user managed VM, userspace 776 * handles virtual address allocation, and both async and sync operations 777 * are supported. 778 */ 779 struct drm_gpuvm * 780 msm_gem_vm_create(struct drm_device *drm, struct msm_mmu *mmu, const char *name, 781 u64 va_start, u64 va_size, bool managed) 782 { 783 /* 784 * We mostly want to use DRM_GPUVM_RESV_PROTECTED, except that 785 * makes drm_gpuvm_bo_evict() a no-op for extobjs (ie. we loose 786 * tracking that an extobj is evicted) :facepalm: 787 */ 788 enum drm_gpuvm_flags flags = 0; 789 struct msm_gem_vm *vm; 790 struct drm_gem_object *dummy_gem; 791 int ret = 0; 792 793 if (IS_ERR(mmu)) 794 return ERR_CAST(mmu); 795 796 vm = kzalloc(sizeof(*vm), GFP_KERNEL); 797 if (!vm) 798 return ERR_PTR(-ENOMEM); 799 800 dummy_gem = drm_gpuvm_resv_object_alloc(drm); 801 if (!dummy_gem) { 802 ret = -ENOMEM; 803 goto err_free_vm; 804 } 805 806 if (!managed) { 807 struct drm_sched_init_args args = { 808 .ops = &msm_vm_bind_ops, 809 .num_rqs = 1, 810 .credit_limit = 1, 811 .timeout = MAX_SCHEDULE_TIMEOUT, 812 .name = "msm-vm-bind", 813 .dev = drm->dev, 814 }; 815 816 ret = drm_sched_init(&vm->sched, &args); 817 if (ret) 818 goto err_free_dummy; 819 820 init_waitqueue_head(&vm->prealloc_throttle.wait); 821 } 822 823 drm_gpuvm_init(&vm->base, name, flags, drm, dummy_gem, 824 va_start, va_size, 0, 0, &msm_gpuvm_ops); 825 drm_gem_object_put(dummy_gem); 826 827 vm->mmu = mmu; 828 mutex_init(&vm->mmu_lock); 829 vm->managed = managed; 830 831 drm_mm_init(&vm->mm, va_start, va_size); 832 833 /* 834 * We don't really need vm log for kernel managed VMs, as the kernel 835 * is responsible for ensuring that GEM objs are mapped if they are 836 * used by a submit. Furthermore we piggyback on mmu_lock to serialize 837 * access to the log. 838 * 839 * Limit the max log_shift to 8 to prevent userspace from asking us 840 * for an unreasonable log size. 841 */ 842 if (!managed) 843 vm->log_shift = MIN(vm_log_shift, 8); 844 845 if (vm->log_shift) { 846 vm->log = kmalloc_array(1 << vm->log_shift, sizeof(vm->log[0]), 847 GFP_KERNEL | __GFP_ZERO); 848 } 849 850 return &vm->base; 851 852 err_free_dummy: 853 drm_gem_object_put(dummy_gem); 854 855 err_free_vm: 856 kfree(vm); 857 return ERR_PTR(ret); 858 } 859 860 /** 861 * msm_gem_vm_close() - Close a VM 862 * @gpuvm: The VM to close 863 * 864 * Called when the drm device file is closed, to tear down VM related resources 865 * (which will drop refcounts to GEM objects that were still mapped into the 866 * VM at the time). 867 */ 868 void 869 msm_gem_vm_close(struct drm_gpuvm *gpuvm) 870 { 871 struct msm_gem_vm *vm = to_msm_vm(gpuvm); 872 struct drm_gpuva *vma, *tmp; 873 struct drm_exec exec; 874 875 /* 876 * For kernel managed VMs, the VMAs are torn down when the handle is 877 * closed, so nothing more to do. 878 */ 879 if (vm->managed) 880 return; 881 882 if (vm->last_fence) 883 dma_fence_wait(vm->last_fence, false); 884 885 /* Kill the scheduler now, so we aren't racing with it for cleanup: */ 886 drm_sched_stop(&vm->sched, NULL); 887 drm_sched_fini(&vm->sched); 888 889 /* Tear down any remaining mappings: */ 890 drm_exec_init(&exec, 0, 2); 891 drm_exec_until_all_locked (&exec) { 892 drm_exec_lock_obj(&exec, drm_gpuvm_resv_obj(gpuvm)); 893 drm_exec_retry_on_contention(&exec); 894 895 drm_gpuvm_for_each_va_safe (vma, tmp, gpuvm) { 896 struct drm_gem_object *obj = vma->gem.obj; 897 898 /* 899 * MSM_BO_NO_SHARE objects share the same resv as the 900 * VM, in which case the obj is already locked: 901 */ 902 if (obj && (obj->resv == drm_gpuvm_resv(gpuvm))) 903 obj = NULL; 904 905 if (obj) { 906 drm_exec_lock_obj(&exec, obj); 907 drm_exec_retry_on_contention(&exec); 908 } 909 910 msm_gem_vma_unmap(vma, "close"); 911 msm_gem_vma_close(vma); 912 913 if (obj) { 914 drm_exec_unlock_obj(&exec, obj); 915 } 916 } 917 } 918 drm_exec_fini(&exec); 919 } 920 921 922 static struct msm_vm_bind_job * 923 vm_bind_job_create(struct drm_device *dev, struct drm_file *file, 924 struct msm_gpu_submitqueue *queue, uint32_t nr_ops) 925 { 926 struct msm_vm_bind_job *job; 927 uint64_t sz; 928 int ret; 929 930 sz = struct_size(job, ops, nr_ops); 931 932 if (sz > SIZE_MAX) 933 return ERR_PTR(-ENOMEM); 934 935 job = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN); 936 if (!job) 937 return ERR_PTR(-ENOMEM); 938 939 ret = drm_sched_job_init(&job->base, queue->entity, 1, queue, 940 file->client_id); 941 if (ret) { 942 kfree(job); 943 return ERR_PTR(ret); 944 } 945 946 job->vm = msm_context_vm(dev, queue->ctx); 947 job->queue = queue; 948 INIT_LIST_HEAD(&job->vm_ops); 949 950 return job; 951 } 952 953 static bool invalid_alignment(uint64_t addr) 954 { 955 /* 956 * Technically this is about GPU alignment, not CPU alignment. But 957 * I've not seen any qcom SoC where the SMMU does not support the 958 * CPU's smallest page size. 959 */ 960 return !PAGE_ALIGNED(addr); 961 } 962 963 static int 964 lookup_op(struct msm_vm_bind_job *job, const struct drm_msm_vm_bind_op *op) 965 { 966 struct drm_device *dev = job->vm->drm; 967 int i = job->nr_ops++; 968 int ret = 0; 969 970 job->ops[i].op = op->op; 971 job->ops[i].handle = op->handle; 972 job->ops[i].obj_offset = op->obj_offset; 973 job->ops[i].iova = op->iova; 974 job->ops[i].range = op->range; 975 job->ops[i].flags = op->flags; 976 977 if (op->flags & ~MSM_VM_BIND_OP_FLAGS) 978 ret = UERR(EINVAL, dev, "invalid flags: %x\n", op->flags); 979 980 if (invalid_alignment(op->iova)) 981 ret = UERR(EINVAL, dev, "invalid address: %016llx\n", op->iova); 982 983 if (invalid_alignment(op->obj_offset)) 984 ret = UERR(EINVAL, dev, "invalid bo_offset: %016llx\n", op->obj_offset); 985 986 if (invalid_alignment(op->range)) 987 ret = UERR(EINVAL, dev, "invalid range: %016llx\n", op->range); 988 989 if (!drm_gpuvm_range_valid(job->vm, op->iova, op->range)) 990 ret = UERR(EINVAL, dev, "invalid range: %016llx, %016llx\n", op->iova, op->range); 991 992 /* 993 * MAP must specify a valid handle. But the handle MBZ for 994 * UNMAP or MAP_NULL. 995 */ 996 if (op->op == MSM_VM_BIND_OP_MAP) { 997 if (!op->handle) 998 ret = UERR(EINVAL, dev, "invalid handle\n"); 999 } else if (op->handle) { 1000 ret = UERR(EINVAL, dev, "handle must be zero\n"); 1001 } 1002 1003 switch (op->op) { 1004 case MSM_VM_BIND_OP_MAP: 1005 case MSM_VM_BIND_OP_MAP_NULL: 1006 case MSM_VM_BIND_OP_UNMAP: 1007 break; 1008 default: 1009 ret = UERR(EINVAL, dev, "invalid op: %u\n", op->op); 1010 break; 1011 } 1012 1013 return ret; 1014 } 1015 1016 /* 1017 * ioctl parsing, parameter validation, and GEM handle lookup 1018 */ 1019 static int 1020 vm_bind_job_lookup_ops(struct msm_vm_bind_job *job, struct drm_msm_vm_bind *args, 1021 struct drm_file *file, int *nr_bos) 1022 { 1023 struct drm_device *dev = job->vm->drm; 1024 int ret = 0; 1025 int cnt = 0; 1026 1027 if (args->nr_ops == 1) { 1028 /* Single op case, the op is inlined: */ 1029 ret = lookup_op(job, &args->op); 1030 } else { 1031 for (unsigned i = 0; i < args->nr_ops; i++) { 1032 struct drm_msm_vm_bind_op op; 1033 void __user *userptr = 1034 u64_to_user_ptr(args->ops + (i * sizeof(op))); 1035 1036 /* make sure we don't have garbage flags, in case we hit 1037 * error path before flags is initialized: 1038 */ 1039 job->ops[i].flags = 0; 1040 1041 if (copy_from_user(&op, userptr, sizeof(op))) { 1042 ret = -EFAULT; 1043 break; 1044 } 1045 1046 ret = lookup_op(job, &op); 1047 if (ret) 1048 break; 1049 } 1050 } 1051 1052 if (ret) { 1053 job->nr_ops = 0; 1054 goto out; 1055 } 1056 1057 spin_lock(&file->table_lock); 1058 1059 for (unsigned i = 0; i < args->nr_ops; i++) { 1060 struct drm_gem_object *obj; 1061 1062 if (!job->ops[i].handle) { 1063 job->ops[i].obj = NULL; 1064 continue; 1065 } 1066 1067 /* 1068 * normally use drm_gem_object_lookup(), but for bulk lookup 1069 * all under single table_lock just hit object_idr directly: 1070 */ 1071 obj = idr_find(&file->object_idr, job->ops[i].handle); 1072 if (!obj) { 1073 ret = UERR(EINVAL, dev, "invalid handle %u at index %u\n", job->ops[i].handle, i); 1074 goto out_unlock; 1075 } 1076 1077 drm_gem_object_get(obj); 1078 1079 job->ops[i].obj = obj; 1080 cnt++; 1081 } 1082 1083 *nr_bos = cnt; 1084 1085 out_unlock: 1086 spin_unlock(&file->table_lock); 1087 1088 out: 1089 return ret; 1090 } 1091 1092 static void 1093 prealloc_count(struct msm_vm_bind_job *job, 1094 struct msm_vm_bind_op *first, 1095 struct msm_vm_bind_op *last) 1096 { 1097 struct msm_mmu *mmu = to_msm_vm(job->vm)->mmu; 1098 1099 if (!first) 1100 return; 1101 1102 uint64_t start_iova = first->iova; 1103 uint64_t end_iova = last->iova + last->range; 1104 1105 mmu->funcs->prealloc_count(mmu, &job->prealloc, start_iova, end_iova - start_iova); 1106 } 1107 1108 static bool 1109 ops_are_same_pte(struct msm_vm_bind_op *first, struct msm_vm_bind_op *next) 1110 { 1111 /* 1112 * Last level pte covers 2MB.. so we should merge two ops, from 1113 * the PoV of figuring out how much pgtable pages to pre-allocate 1114 * if they land in the same 2MB range: 1115 */ 1116 uint64_t pte_mask = ~(SZ_2M - 1); 1117 return ((first->iova + first->range) & pte_mask) == (next->iova & pte_mask); 1118 } 1119 1120 /* 1121 * Determine the amount of memory to prealloc for pgtables. For sparse images, 1122 * in particular, userspace plays some tricks with the order of page mappings 1123 * to get the desired swizzle pattern, resulting in a large # of tiny MAP ops. 1124 * So detect when multiple MAP operations are physically contiguous, and count 1125 * them as a single mapping. Otherwise the prealloc_count() will not realize 1126 * they can share pagetable pages and vastly overcount. 1127 */ 1128 static int 1129 vm_bind_prealloc_count(struct msm_vm_bind_job *job) 1130 { 1131 struct msm_vm_bind_op *first = NULL, *last = NULL; 1132 struct msm_gem_vm *vm = to_msm_vm(job->vm); 1133 int ret; 1134 1135 for (int i = 0; i < job->nr_ops; i++) { 1136 struct msm_vm_bind_op *op = &job->ops[i]; 1137 1138 /* We only care about MAP/MAP_NULL: */ 1139 if (op->op == MSM_VM_BIND_OP_UNMAP) 1140 continue; 1141 1142 /* 1143 * If op is contiguous with last in the current range, then 1144 * it becomes the new last in the range and we continue 1145 * looping: 1146 */ 1147 if (last && ops_are_same_pte(last, op)) { 1148 last = op; 1149 continue; 1150 } 1151 1152 /* 1153 * If op is not contiguous with the current range, flush 1154 * the current range and start anew: 1155 */ 1156 prealloc_count(job, first, last); 1157 first = last = op; 1158 } 1159 1160 /* Flush the remaining range: */ 1161 prealloc_count(job, first, last); 1162 1163 /* 1164 * Now that we know the needed amount to pre-alloc, throttle on pending 1165 * VM_BIND jobs if we already have too much pre-alloc memory in flight 1166 */ 1167 ret = wait_event_interruptible( 1168 vm->prealloc_throttle.wait, 1169 atomic_read(&vm->prealloc_throttle.in_flight) <= 1024); 1170 if (ret) 1171 return ret; 1172 1173 atomic_add(job->prealloc.count, &vm->prealloc_throttle.in_flight); 1174 1175 return 0; 1176 } 1177 1178 /* 1179 * Lock VM and GEM objects 1180 */ 1181 static int 1182 vm_bind_job_lock_objects(struct msm_vm_bind_job *job, struct drm_exec *exec) 1183 { 1184 int ret; 1185 1186 /* Lock VM and objects: */ 1187 drm_exec_until_all_locked (exec) { 1188 ret = drm_exec_lock_obj(exec, drm_gpuvm_resv_obj(job->vm)); 1189 drm_exec_retry_on_contention(exec); 1190 if (ret) 1191 return ret; 1192 1193 for (unsigned i = 0; i < job->nr_ops; i++) { 1194 const struct msm_vm_bind_op *op = &job->ops[i]; 1195 1196 switch (op->op) { 1197 case MSM_VM_BIND_OP_UNMAP: 1198 ret = drm_gpuvm_sm_unmap_exec_lock(job->vm, exec, 1199 op->iova, 1200 op->obj_offset); 1201 break; 1202 case MSM_VM_BIND_OP_MAP: 1203 case MSM_VM_BIND_OP_MAP_NULL: 1204 ret = drm_gpuvm_sm_map_exec_lock(job->vm, exec, 1, 1205 op->iova, op->range, 1206 op->obj, op->obj_offset); 1207 break; 1208 default: 1209 /* 1210 * lookup_op() should have already thrown an error for 1211 * invalid ops 1212 */ 1213 WARN_ON("unreachable"); 1214 } 1215 1216 drm_exec_retry_on_contention(exec); 1217 if (ret) 1218 return ret; 1219 } 1220 } 1221 1222 return 0; 1223 } 1224 1225 /* 1226 * Pin GEM objects, ensuring that we have backing pages. Pinning will move 1227 * the object to the pinned LRU so that the shrinker knows to first consider 1228 * other objects for evicting. 1229 */ 1230 static int 1231 vm_bind_job_pin_objects(struct msm_vm_bind_job *job) 1232 { 1233 struct drm_gem_object *obj; 1234 1235 /* 1236 * First loop, before holding the LRU lock, avoids holding the 1237 * LRU lock while calling msm_gem_pin_vma_locked (which could 1238 * trigger get_pages()) 1239 */ 1240 job_foreach_bo (obj, job) { 1241 struct page **pages; 1242 1243 pages = msm_gem_get_pages_locked(obj, MSM_MADV_WILLNEED); 1244 if (IS_ERR(pages)) 1245 return PTR_ERR(pages); 1246 } 1247 1248 struct msm_drm_private *priv = job->vm->drm->dev_private; 1249 1250 /* 1251 * A second loop while holding the LRU lock (a) avoids acquiring/dropping 1252 * the LRU lock for each individual bo, while (b) avoiding holding the 1253 * LRU lock while calling msm_gem_pin_vma_locked() (which could trigger 1254 * get_pages() which could trigger reclaim.. and if we held the LRU lock 1255 * could trigger deadlock with the shrinker). 1256 */ 1257 mutex_lock(&priv->lru.lock); 1258 job_foreach_bo (obj, job) 1259 msm_gem_pin_obj_locked(obj); 1260 mutex_unlock(&priv->lru.lock); 1261 1262 job->bos_pinned = true; 1263 1264 return 0; 1265 } 1266 1267 /* 1268 * Unpin GEM objects. Normally this is done after the bind job is run. 1269 */ 1270 static void 1271 vm_bind_job_unpin_objects(struct msm_vm_bind_job *job) 1272 { 1273 struct drm_gem_object *obj; 1274 1275 if (!job->bos_pinned) 1276 return; 1277 1278 job_foreach_bo (obj, job) 1279 msm_gem_unpin_locked(obj); 1280 1281 job->bos_pinned = false; 1282 } 1283 1284 /* 1285 * Pre-allocate pgtable memory, and translate the VM bind requests into a 1286 * sequence of pgtable updates to be applied asynchronously. 1287 */ 1288 static int 1289 vm_bind_job_prepare(struct msm_vm_bind_job *job) 1290 { 1291 struct msm_gem_vm *vm = to_msm_vm(job->vm); 1292 struct msm_mmu *mmu = vm->mmu; 1293 int ret; 1294 1295 ret = mmu->funcs->prealloc_allocate(mmu, &job->prealloc); 1296 if (ret) 1297 return ret; 1298 1299 for (unsigned i = 0; i < job->nr_ops; i++) { 1300 const struct msm_vm_bind_op *op = &job->ops[i]; 1301 struct op_arg arg = { 1302 .job = job, 1303 .op = op, 1304 }; 1305 1306 switch (op->op) { 1307 case MSM_VM_BIND_OP_UNMAP: 1308 ret = drm_gpuvm_sm_unmap(job->vm, &arg, op->iova, 1309 op->range); 1310 break; 1311 case MSM_VM_BIND_OP_MAP: 1312 if (op->flags & MSM_VM_BIND_OP_DUMP) 1313 arg.flags |= MSM_VMA_DUMP; 1314 fallthrough; 1315 case MSM_VM_BIND_OP_MAP_NULL: 1316 ret = drm_gpuvm_sm_map(job->vm, &arg, op->iova, 1317 op->range, op->obj, op->obj_offset); 1318 break; 1319 default: 1320 /* 1321 * lookup_op() should have already thrown an error for 1322 * invalid ops 1323 */ 1324 BUG_ON("unreachable"); 1325 } 1326 1327 if (ret) { 1328 /* 1329 * If we've already started modifying the vm, we can't 1330 * adequetly describe to userspace the intermediate 1331 * state the vm is in. So throw up our hands! 1332 */ 1333 if (i > 0) 1334 msm_gem_vm_unusable(job->vm); 1335 return ret; 1336 } 1337 } 1338 1339 return 0; 1340 } 1341 1342 /* 1343 * Attach fences to the GEM objects being bound. This will signify to 1344 * the shrinker that they are busy even after dropping the locks (ie. 1345 * drm_exec_fini()) 1346 */ 1347 static void 1348 vm_bind_job_attach_fences(struct msm_vm_bind_job *job) 1349 { 1350 for (unsigned i = 0; i < job->nr_ops; i++) { 1351 struct drm_gem_object *obj = job->ops[i].obj; 1352 1353 if (!obj) 1354 continue; 1355 1356 dma_resv_add_fence(obj->resv, job->fence, 1357 DMA_RESV_USAGE_KERNEL); 1358 } 1359 } 1360 1361 int 1362 msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file) 1363 { 1364 struct msm_drm_private *priv = dev->dev_private; 1365 struct drm_msm_vm_bind *args = data; 1366 struct msm_context *ctx = file->driver_priv; 1367 struct msm_vm_bind_job *job = NULL; 1368 struct msm_gpu *gpu = priv->gpu; 1369 struct msm_gpu_submitqueue *queue; 1370 struct msm_syncobj_post_dep *post_deps = NULL; 1371 struct drm_syncobj **syncobjs_to_reset = NULL; 1372 struct sync_file *sync_file = NULL; 1373 struct dma_fence *fence; 1374 int out_fence_fd = -1; 1375 int ret, nr_bos = 0; 1376 unsigned i; 1377 1378 if (!gpu) 1379 return -ENXIO; 1380 1381 /* 1382 * Maybe we could allow just UNMAP ops? OTOH userspace should just 1383 * immediately close the device file and all will be torn down. 1384 */ 1385 if (to_msm_vm(ctx->vm)->unusable) 1386 return UERR(EPIPE, dev, "context is unusable"); 1387 1388 /* 1389 * Technically, you cannot create a VM_BIND submitqueue in the first 1390 * place, if you haven't opted in to VM_BIND context. But it is 1391 * cleaner / less confusing, to check this case directly. 1392 */ 1393 if (!msm_context_is_vmbind(ctx)) 1394 return UERR(EINVAL, dev, "context does not support vmbind"); 1395 1396 if (args->flags & ~MSM_VM_BIND_FLAGS) 1397 return UERR(EINVAL, dev, "invalid flags"); 1398 1399 queue = msm_submitqueue_get(ctx, args->queue_id); 1400 if (!queue) 1401 return -ENOENT; 1402 1403 if (!(queue->flags & MSM_SUBMITQUEUE_VM_BIND)) { 1404 ret = UERR(EINVAL, dev, "Invalid queue type"); 1405 goto out_post_unlock; 1406 } 1407 1408 if (args->flags & MSM_VM_BIND_FENCE_FD_OUT) { 1409 out_fence_fd = get_unused_fd_flags(O_CLOEXEC); 1410 if (out_fence_fd < 0) { 1411 ret = out_fence_fd; 1412 goto out_post_unlock; 1413 } 1414 } 1415 1416 job = vm_bind_job_create(dev, file, queue, args->nr_ops); 1417 if (IS_ERR(job)) { 1418 ret = PTR_ERR(job); 1419 goto out_post_unlock; 1420 } 1421 1422 ret = mutex_lock_interruptible(&queue->lock); 1423 if (ret) 1424 goto out_post_unlock; 1425 1426 if (args->flags & MSM_VM_BIND_FENCE_FD_IN) { 1427 struct dma_fence *in_fence; 1428 1429 in_fence = sync_file_get_fence(args->fence_fd); 1430 1431 if (!in_fence) { 1432 ret = UERR(EINVAL, dev, "invalid in-fence"); 1433 goto out_unlock; 1434 } 1435 1436 ret = drm_sched_job_add_dependency(&job->base, in_fence); 1437 if (ret) 1438 goto out_unlock; 1439 } 1440 1441 if (args->in_syncobjs > 0) { 1442 syncobjs_to_reset = msm_syncobj_parse_deps(dev, &job->base, 1443 file, args->in_syncobjs, 1444 args->nr_in_syncobjs, 1445 args->syncobj_stride); 1446 if (IS_ERR(syncobjs_to_reset)) { 1447 ret = PTR_ERR(syncobjs_to_reset); 1448 goto out_unlock; 1449 } 1450 } 1451 1452 if (args->out_syncobjs > 0) { 1453 post_deps = msm_syncobj_parse_post_deps(dev, file, 1454 args->out_syncobjs, 1455 args->nr_out_syncobjs, 1456 args->syncobj_stride); 1457 if (IS_ERR(post_deps)) { 1458 ret = PTR_ERR(post_deps); 1459 goto out_unlock; 1460 } 1461 } 1462 1463 ret = vm_bind_job_lookup_ops(job, args, file, &nr_bos); 1464 if (ret) 1465 goto out_unlock; 1466 1467 ret = vm_bind_prealloc_count(job); 1468 if (ret) 1469 goto out_unlock; 1470 1471 struct drm_exec exec; 1472 unsigned flags = DRM_EXEC_IGNORE_DUPLICATES | DRM_EXEC_INTERRUPTIBLE_WAIT; 1473 drm_exec_init(&exec, flags, nr_bos + 1); 1474 1475 ret = vm_bind_job_lock_objects(job, &exec); 1476 if (ret) 1477 goto out; 1478 1479 ret = vm_bind_job_pin_objects(job); 1480 if (ret) 1481 goto out; 1482 1483 ret = vm_bind_job_prepare(job); 1484 if (ret) 1485 goto out; 1486 1487 drm_sched_job_arm(&job->base); 1488 1489 job->fence = dma_fence_get(&job->base.s_fence->finished); 1490 1491 if (args->flags & MSM_VM_BIND_FENCE_FD_OUT) { 1492 sync_file = sync_file_create(job->fence); 1493 if (!sync_file) 1494 ret = -ENOMEM; 1495 } 1496 1497 if (ret) 1498 goto out; 1499 1500 vm_bind_job_attach_fences(job); 1501 1502 /* 1503 * The job can be free'd (and fence unref'd) at any point after 1504 * drm_sched_entity_push_job(), so we need to hold our own ref 1505 */ 1506 fence = dma_fence_get(job->fence); 1507 1508 drm_sched_entity_push_job(&job->base); 1509 1510 msm_syncobj_reset(syncobjs_to_reset, args->nr_in_syncobjs); 1511 msm_syncobj_process_post_deps(post_deps, args->nr_out_syncobjs, fence); 1512 1513 dma_fence_put(fence); 1514 1515 out: 1516 if (ret) 1517 vm_bind_job_unpin_objects(job); 1518 1519 drm_exec_fini(&exec); 1520 out_unlock: 1521 mutex_unlock(&queue->lock); 1522 out_post_unlock: 1523 if (ret) { 1524 if (out_fence_fd >= 0) 1525 put_unused_fd(out_fence_fd); 1526 if (sync_file) 1527 fput(sync_file->file); 1528 } else if (sync_file) { 1529 fd_install(out_fence_fd, sync_file->file); 1530 args->fence_fd = out_fence_fd; 1531 } 1532 1533 if (!IS_ERR_OR_NULL(job)) { 1534 if (ret) 1535 msm_vma_job_free(&job->base); 1536 } else { 1537 /* 1538 * If the submit hasn't yet taken ownership of the queue 1539 * then we need to drop the reference ourself: 1540 */ 1541 msm_submitqueue_put(queue); 1542 } 1543 1544 if (!IS_ERR_OR_NULL(post_deps)) { 1545 for (i = 0; i < args->nr_out_syncobjs; ++i) { 1546 kfree(post_deps[i].chain); 1547 drm_syncobj_put(post_deps[i].syncobj); 1548 } 1549 kfree(post_deps); 1550 } 1551 1552 if (!IS_ERR_OR_NULL(syncobjs_to_reset)) { 1553 for (i = 0; i < args->nr_in_syncobjs; ++i) { 1554 if (syncobjs_to_reset[i]) 1555 drm_syncobj_put(syncobjs_to_reset[i]); 1556 } 1557 kfree(syncobjs_to_reset); 1558 } 1559 1560 return ret; 1561 } 1562