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: iommu/io-pgtable can allocate pages, so 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 * Revisit this if we can come up with a scheme to pre-alloc pages 328 * for the pgtable in map/unmap ops. 329 */ 330 ret = vm_map_op(vm, &(struct msm_vm_map_op){ 331 .iova = vma->va.addr, 332 .range = vma->va.range, 333 .offset = vma->gem.offset, 334 .sgt = sgt, 335 .prot = prot, 336 }); 337 338 if (!vm->managed) 339 mutex_unlock(&vm->mmu_lock); 340 341 if (ret) 342 msm_vma->mapped = false; 343 344 return ret; 345 } 346 347 /* Close an iova. Warn if it is still in use */ 348 void msm_gem_vma_close(struct drm_gpuva *vma) 349 { 350 struct msm_gem_vm *vm = to_msm_vm(vma->vm); 351 struct msm_gem_vma *msm_vma = to_msm_vma(vma); 352 353 GEM_WARN_ON(msm_vma->mapped); 354 355 drm_gpuvm_resv_assert_held(&vm->base); 356 357 if (vma->gem.obj) 358 msm_gem_assert_locked(vma->gem.obj); 359 360 if (vma->va.addr && vm->managed) 361 drm_mm_remove_node(&msm_vma->node); 362 363 drm_gpuva_remove(vma); 364 drm_gpuva_unlink(vma); 365 366 kfree(vma); 367 } 368 369 /* Create a new vma and allocate an iova for it */ 370 struct drm_gpuva * 371 msm_gem_vma_new(struct drm_gpuvm *gpuvm, struct drm_gem_object *obj, 372 u64 offset, u64 range_start, u64 range_end) 373 { 374 struct drm_gpuva_op_map op_map = { 375 .va.addr = range_start, 376 .va.range = range_end - range_start, 377 .gem.obj = obj, 378 .gem.offset = offset, 379 }; 380 struct msm_gem_vm *vm = to_msm_vm(gpuvm); 381 struct drm_gpuvm_bo *vm_bo; 382 struct msm_gem_vma *vma; 383 int ret; 384 385 drm_gpuvm_resv_assert_held(&vm->base); 386 387 vma = kzalloc(sizeof(*vma), GFP_KERNEL); 388 if (!vma) 389 return ERR_PTR(-ENOMEM); 390 391 if (vm->managed) { 392 BUG_ON(offset != 0); 393 BUG_ON(!obj); /* NULL mappings not valid for kernel managed VM */ 394 ret = drm_mm_insert_node_in_range(&vm->mm, &vma->node, 395 obj->size, PAGE_SIZE, 0, 396 range_start, range_end, 0); 397 398 if (ret) 399 goto err_free_vma; 400 401 range_start = vma->node.start; 402 range_end = range_start + obj->size; 403 } 404 405 if (obj) 406 GEM_WARN_ON((range_end - range_start) > obj->size); 407 408 drm_gpuva_init_from_op(&vma->base, &op_map); 409 vma->mapped = false; 410 411 ret = drm_gpuva_insert(&vm->base, &vma->base); 412 if (ret) 413 goto err_free_range; 414 415 if (!obj) 416 return &vma->base; 417 418 vm_bo = drm_gpuvm_bo_obtain(&vm->base, obj); 419 if (IS_ERR(vm_bo)) { 420 ret = PTR_ERR(vm_bo); 421 goto err_va_remove; 422 } 423 424 drm_gpuvm_bo_extobj_add(vm_bo); 425 drm_gpuva_link(&vma->base, vm_bo); 426 GEM_WARN_ON(drm_gpuvm_bo_put(vm_bo)); 427 428 return &vma->base; 429 430 err_va_remove: 431 drm_gpuva_remove(&vma->base); 432 err_free_range: 433 if (vm->managed) 434 drm_mm_remove_node(&vma->node); 435 err_free_vma: 436 kfree(vma); 437 return ERR_PTR(ret); 438 } 439 440 static int 441 msm_gem_vm_bo_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec) 442 { 443 struct drm_gem_object *obj = vm_bo->obj; 444 struct drm_gpuva *vma; 445 int ret; 446 447 vm_dbg("validate: %p", obj); 448 449 msm_gem_assert_locked(obj); 450 451 drm_gpuvm_bo_for_each_va (vma, vm_bo) { 452 ret = msm_gem_pin_vma_locked(obj, vma); 453 if (ret) 454 return ret; 455 } 456 457 return 0; 458 } 459 460 struct op_arg { 461 unsigned flags; 462 struct msm_vm_bind_job *job; 463 }; 464 465 static void 466 vm_op_enqueue(struct op_arg *arg, struct msm_vm_op _op) 467 { 468 struct msm_vm_op *op = kmalloc(sizeof(*op), GFP_KERNEL); 469 *op = _op; 470 list_add_tail(&op->node, &arg->job->vm_ops); 471 472 if (op->obj) 473 drm_gem_object_get(op->obj); 474 } 475 476 static struct drm_gpuva * 477 vma_from_op(struct op_arg *arg, struct drm_gpuva_op_map *op) 478 { 479 return msm_gem_vma_new(arg->job->vm, op->gem.obj, op->gem.offset, 480 op->va.addr, op->va.addr + op->va.range); 481 } 482 483 static int 484 msm_gem_vm_sm_step_map(struct drm_gpuva_op *op, void *arg) 485 { 486 struct msm_vm_bind_job *job = ((struct op_arg *)arg)->job; 487 struct drm_gem_object *obj = op->map.gem.obj; 488 struct drm_gpuva *vma; 489 struct sg_table *sgt; 490 unsigned prot; 491 492 vma = vma_from_op(arg, &op->map); 493 if (WARN_ON(IS_ERR(vma))) 494 return PTR_ERR(vma); 495 496 vm_dbg("%p:%p:%p: %016llx %016llx", vma->vm, vma, vma->gem.obj, 497 vma->va.addr, vma->va.range); 498 499 vma->flags = ((struct op_arg *)arg)->flags; 500 501 if (obj) { 502 sgt = to_msm_bo(obj)->sgt; 503 prot = msm_gem_prot(obj); 504 } else { 505 sgt = NULL; 506 prot = IOMMU_READ | IOMMU_WRITE; 507 } 508 509 vm_op_enqueue(arg, (struct msm_vm_op){ 510 .op = MSM_VM_OP_MAP, 511 .map = { 512 .sgt = sgt, 513 .iova = vma->va.addr, 514 .range = vma->va.range, 515 .offset = vma->gem.offset, 516 .prot = prot, 517 .queue_id = job->queue->id, 518 }, 519 .obj = vma->gem.obj, 520 }); 521 522 to_msm_vma(vma)->mapped = true; 523 524 return 0; 525 } 526 527 static int 528 msm_gem_vm_sm_step_remap(struct drm_gpuva_op *op, void *arg) 529 { 530 struct msm_vm_bind_job *job = ((struct op_arg *)arg)->job; 531 struct drm_gpuvm *vm = job->vm; 532 struct drm_gpuva *orig_vma = op->remap.unmap->va; 533 struct drm_gpuva *prev_vma = NULL, *next_vma = NULL; 534 struct drm_gpuvm_bo *vm_bo = orig_vma->vm_bo; 535 bool mapped = to_msm_vma(orig_vma)->mapped; 536 unsigned flags; 537 538 vm_dbg("orig_vma: %p:%p:%p: %016llx %016llx", vm, orig_vma, 539 orig_vma->gem.obj, orig_vma->va.addr, orig_vma->va.range); 540 541 if (mapped) { 542 uint64_t unmap_start, unmap_range; 543 544 drm_gpuva_op_remap_to_unmap_range(&op->remap, &unmap_start, &unmap_range); 545 546 vm_op_enqueue(arg, (struct msm_vm_op){ 547 .op = MSM_VM_OP_UNMAP, 548 .unmap = { 549 .iova = unmap_start, 550 .range = unmap_range, 551 .queue_id = job->queue->id, 552 }, 553 .obj = orig_vma->gem.obj, 554 }); 555 556 /* 557 * Part of this GEM obj is still mapped, but we're going to kill the 558 * existing VMA and replace it with one or two new ones (ie. two if 559 * the unmapped range is in the middle of the existing (unmap) VMA). 560 * So just set the state to unmapped: 561 */ 562 to_msm_vma(orig_vma)->mapped = false; 563 } 564 565 /* 566 * Hold a ref to the vm_bo between the msm_gem_vma_close() and the 567 * creation of the new prev/next vma's, in case the vm_bo is tracked 568 * in the VM's evict list: 569 */ 570 if (vm_bo) 571 drm_gpuvm_bo_get(vm_bo); 572 573 /* 574 * The prev_vma and/or next_vma are replacing the unmapped vma, and 575 * therefore should preserve it's flags: 576 */ 577 flags = orig_vma->flags; 578 579 msm_gem_vma_close(orig_vma); 580 581 if (op->remap.prev) { 582 prev_vma = vma_from_op(arg, op->remap.prev); 583 if (WARN_ON(IS_ERR(prev_vma))) 584 return PTR_ERR(prev_vma); 585 586 vm_dbg("prev_vma: %p:%p: %016llx %016llx", vm, prev_vma, prev_vma->va.addr, prev_vma->va.range); 587 to_msm_vma(prev_vma)->mapped = mapped; 588 prev_vma->flags = flags; 589 } 590 591 if (op->remap.next) { 592 next_vma = vma_from_op(arg, op->remap.next); 593 if (WARN_ON(IS_ERR(next_vma))) 594 return PTR_ERR(next_vma); 595 596 vm_dbg("next_vma: %p:%p: %016llx %016llx", vm, next_vma, next_vma->va.addr, next_vma->va.range); 597 to_msm_vma(next_vma)->mapped = mapped; 598 next_vma->flags = flags; 599 } 600 601 if (!mapped) 602 drm_gpuvm_bo_evict(vm_bo, true); 603 604 /* Drop the previous ref: */ 605 drm_gpuvm_bo_put(vm_bo); 606 607 return 0; 608 } 609 610 static int 611 msm_gem_vm_sm_step_unmap(struct drm_gpuva_op *op, void *arg) 612 { 613 struct msm_vm_bind_job *job = ((struct op_arg *)arg)->job; 614 struct drm_gpuva *vma = op->unmap.va; 615 struct msm_gem_vma *msm_vma = to_msm_vma(vma); 616 617 vm_dbg("%p:%p:%p: %016llx %016llx", vma->vm, vma, vma->gem.obj, 618 vma->va.addr, vma->va.range); 619 620 if (!msm_vma->mapped) 621 goto out_close; 622 623 vm_op_enqueue(arg, (struct msm_vm_op){ 624 .op = MSM_VM_OP_UNMAP, 625 .unmap = { 626 .iova = vma->va.addr, 627 .range = vma->va.range, 628 .queue_id = job->queue->id, 629 }, 630 .obj = vma->gem.obj, 631 }); 632 633 msm_vma->mapped = false; 634 635 out_close: 636 msm_gem_vma_close(vma); 637 638 return 0; 639 } 640 641 static const struct drm_gpuvm_ops msm_gpuvm_ops = { 642 .vm_free = msm_gem_vm_free, 643 .vm_bo_validate = msm_gem_vm_bo_validate, 644 .sm_step_map = msm_gem_vm_sm_step_map, 645 .sm_step_remap = msm_gem_vm_sm_step_remap, 646 .sm_step_unmap = msm_gem_vm_sm_step_unmap, 647 }; 648 649 static struct dma_fence * 650 msm_vma_job_run(struct drm_sched_job *_job) 651 { 652 struct msm_vm_bind_job *job = to_msm_vm_bind_job(_job); 653 struct msm_gem_vm *vm = to_msm_vm(job->vm); 654 struct drm_gem_object *obj; 655 int ret = vm->unusable ? -EINVAL : 0; 656 657 vm_dbg(""); 658 659 mutex_lock(&vm->mmu_lock); 660 vm->mmu->prealloc = &job->prealloc; 661 662 while (!list_empty(&job->vm_ops)) { 663 struct msm_vm_op *op = 664 list_first_entry(&job->vm_ops, struct msm_vm_op, node); 665 666 switch (op->op) { 667 case MSM_VM_OP_MAP: 668 /* 669 * On error, stop trying to map new things.. but we 670 * still want to process the unmaps (or in particular, 671 * the drm_gem_object_put()s) 672 */ 673 if (!ret) 674 ret = vm_map_op(vm, &op->map); 675 break; 676 case MSM_VM_OP_UNMAP: 677 vm_unmap_op(vm, &op->unmap); 678 break; 679 } 680 drm_gem_object_put(op->obj); 681 list_del(&op->node); 682 kfree(op); 683 } 684 685 vm->mmu->prealloc = NULL; 686 mutex_unlock(&vm->mmu_lock); 687 688 /* 689 * We failed to perform at least _some_ of the pgtable updates, so 690 * now the VM is in an undefined state. Game over! 691 */ 692 if (ret) 693 msm_gem_vm_unusable(job->vm); 694 695 job_foreach_bo (obj, job) { 696 msm_gem_lock(obj); 697 msm_gem_unpin_locked(obj); 698 msm_gem_unlock(obj); 699 } 700 701 /* VM_BIND ops are synchronous, so no fence to wait on: */ 702 return NULL; 703 } 704 705 static void 706 msm_vma_job_free(struct drm_sched_job *_job) 707 { 708 struct msm_vm_bind_job *job = to_msm_vm_bind_job(_job); 709 struct msm_gem_vm *vm = to_msm_vm(job->vm); 710 struct drm_gem_object *obj; 711 712 vm->mmu->funcs->prealloc_cleanup(vm->mmu, &job->prealloc); 713 714 atomic_sub(job->prealloc.count, &vm->prealloc_throttle.in_flight); 715 716 drm_sched_job_cleanup(_job); 717 718 job_foreach_bo (obj, job) 719 drm_gem_object_put(obj); 720 721 msm_submitqueue_put(job->queue); 722 dma_fence_put(job->fence); 723 724 /* In error paths, we could have unexecuted ops: */ 725 while (!list_empty(&job->vm_ops)) { 726 struct msm_vm_op *op = 727 list_first_entry(&job->vm_ops, struct msm_vm_op, node); 728 list_del(&op->node); 729 kfree(op); 730 } 731 732 wake_up(&vm->prealloc_throttle.wait); 733 734 kfree(job); 735 } 736 737 static const struct drm_sched_backend_ops msm_vm_bind_ops = { 738 .run_job = msm_vma_job_run, 739 .free_job = msm_vma_job_free 740 }; 741 742 /** 743 * msm_gem_vm_create() - Create and initialize a &msm_gem_vm 744 * @drm: the drm device 745 * @mmu: the backing MMU objects handling mapping/unmapping 746 * @name: the name of the VM 747 * @va_start: the start offset of the VA space 748 * @va_size: the size of the VA space 749 * @managed: is it a kernel managed VM? 750 * 751 * In a kernel managed VM, the kernel handles address allocation, and only 752 * synchronous operations are supported. In a user managed VM, userspace 753 * handles virtual address allocation, and both async and sync operations 754 * are supported. 755 */ 756 struct drm_gpuvm * 757 msm_gem_vm_create(struct drm_device *drm, struct msm_mmu *mmu, const char *name, 758 u64 va_start, u64 va_size, bool managed) 759 { 760 /* 761 * We mostly want to use DRM_GPUVM_RESV_PROTECTED, except that 762 * makes drm_gpuvm_bo_evict() a no-op for extobjs (ie. we loose 763 * tracking that an extobj is evicted) :facepalm: 764 */ 765 enum drm_gpuvm_flags flags = 0; 766 struct msm_gem_vm *vm; 767 struct drm_gem_object *dummy_gem; 768 int ret = 0; 769 770 if (IS_ERR(mmu)) 771 return ERR_CAST(mmu); 772 773 vm = kzalloc(sizeof(*vm), GFP_KERNEL); 774 if (!vm) 775 return ERR_PTR(-ENOMEM); 776 777 dummy_gem = drm_gpuvm_resv_object_alloc(drm); 778 if (!dummy_gem) { 779 ret = -ENOMEM; 780 goto err_free_vm; 781 } 782 783 if (!managed) { 784 struct drm_sched_init_args args = { 785 .ops = &msm_vm_bind_ops, 786 .num_rqs = 1, 787 .credit_limit = 1, 788 .timeout = MAX_SCHEDULE_TIMEOUT, 789 .name = "msm-vm-bind", 790 .dev = drm->dev, 791 }; 792 793 ret = drm_sched_init(&vm->sched, &args); 794 if (ret) 795 goto err_free_dummy; 796 797 init_waitqueue_head(&vm->prealloc_throttle.wait); 798 } 799 800 drm_gpuvm_init(&vm->base, name, flags, drm, dummy_gem, 801 va_start, va_size, 0, 0, &msm_gpuvm_ops); 802 drm_gem_object_put(dummy_gem); 803 804 vm->mmu = mmu; 805 mutex_init(&vm->mmu_lock); 806 vm->managed = managed; 807 808 drm_mm_init(&vm->mm, va_start, va_size); 809 810 /* 811 * We don't really need vm log for kernel managed VMs, as the kernel 812 * is responsible for ensuring that GEM objs are mapped if they are 813 * used by a submit. Furthermore we piggyback on mmu_lock to serialize 814 * access to the log. 815 * 816 * Limit the max log_shift to 8 to prevent userspace from asking us 817 * for an unreasonable log size. 818 */ 819 if (!managed) 820 vm->log_shift = MIN(vm_log_shift, 8); 821 822 if (vm->log_shift) { 823 vm->log = kmalloc_array(1 << vm->log_shift, sizeof(vm->log[0]), 824 GFP_KERNEL | __GFP_ZERO); 825 } 826 827 return &vm->base; 828 829 err_free_dummy: 830 drm_gem_object_put(dummy_gem); 831 832 err_free_vm: 833 kfree(vm); 834 return ERR_PTR(ret); 835 } 836 837 /** 838 * msm_gem_vm_close() - Close a VM 839 * @gpuvm: The VM to close 840 * 841 * Called when the drm device file is closed, to tear down VM related resources 842 * (which will drop refcounts to GEM objects that were still mapped into the 843 * VM at the time). 844 */ 845 void 846 msm_gem_vm_close(struct drm_gpuvm *gpuvm) 847 { 848 struct msm_gem_vm *vm = to_msm_vm(gpuvm); 849 struct drm_gpuva *vma, *tmp; 850 struct drm_exec exec; 851 852 /* 853 * For kernel managed VMs, the VMAs are torn down when the handle is 854 * closed, so nothing more to do. 855 */ 856 if (vm->managed) 857 return; 858 859 if (vm->last_fence) 860 dma_fence_wait(vm->last_fence, false); 861 862 /* Kill the scheduler now, so we aren't racing with it for cleanup: */ 863 drm_sched_stop(&vm->sched, NULL); 864 drm_sched_fini(&vm->sched); 865 866 /* Tear down any remaining mappings: */ 867 drm_exec_init(&exec, 0, 2); 868 drm_exec_until_all_locked (&exec) { 869 drm_exec_lock_obj(&exec, drm_gpuvm_resv_obj(gpuvm)); 870 drm_exec_retry_on_contention(&exec); 871 872 drm_gpuvm_for_each_va_safe (vma, tmp, gpuvm) { 873 struct drm_gem_object *obj = vma->gem.obj; 874 875 /* 876 * MSM_BO_NO_SHARE objects share the same resv as the 877 * VM, in which case the obj is already locked: 878 */ 879 if (obj && (obj->resv == drm_gpuvm_resv(gpuvm))) 880 obj = NULL; 881 882 if (obj) { 883 drm_exec_lock_obj(&exec, obj); 884 drm_exec_retry_on_contention(&exec); 885 } 886 887 msm_gem_vma_unmap(vma, "close"); 888 msm_gem_vma_close(vma); 889 890 if (obj) { 891 drm_exec_unlock_obj(&exec, obj); 892 } 893 } 894 } 895 drm_exec_fini(&exec); 896 } 897 898 899 static struct msm_vm_bind_job * 900 vm_bind_job_create(struct drm_device *dev, struct drm_file *file, 901 struct msm_gpu_submitqueue *queue, uint32_t nr_ops) 902 { 903 struct msm_vm_bind_job *job; 904 uint64_t sz; 905 int ret; 906 907 sz = struct_size(job, ops, nr_ops); 908 909 if (sz > SIZE_MAX) 910 return ERR_PTR(-ENOMEM); 911 912 job = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN); 913 if (!job) 914 return ERR_PTR(-ENOMEM); 915 916 ret = drm_sched_job_init(&job->base, queue->entity, 1, queue, 917 file->client_id); 918 if (ret) { 919 kfree(job); 920 return ERR_PTR(ret); 921 } 922 923 job->vm = msm_context_vm(dev, queue->ctx); 924 job->queue = queue; 925 INIT_LIST_HEAD(&job->vm_ops); 926 927 return job; 928 } 929 930 static bool invalid_alignment(uint64_t addr) 931 { 932 /* 933 * Technically this is about GPU alignment, not CPU alignment. But 934 * I've not seen any qcom SoC where the SMMU does not support the 935 * CPU's smallest page size. 936 */ 937 return !PAGE_ALIGNED(addr); 938 } 939 940 static int 941 lookup_op(struct msm_vm_bind_job *job, const struct drm_msm_vm_bind_op *op) 942 { 943 struct drm_device *dev = job->vm->drm; 944 int i = job->nr_ops++; 945 int ret = 0; 946 947 job->ops[i].op = op->op; 948 job->ops[i].handle = op->handle; 949 job->ops[i].obj_offset = op->obj_offset; 950 job->ops[i].iova = op->iova; 951 job->ops[i].range = op->range; 952 job->ops[i].flags = op->flags; 953 954 if (op->flags & ~MSM_VM_BIND_OP_FLAGS) 955 ret = UERR(EINVAL, dev, "invalid flags: %x\n", op->flags); 956 957 if (invalid_alignment(op->iova)) 958 ret = UERR(EINVAL, dev, "invalid address: %016llx\n", op->iova); 959 960 if (invalid_alignment(op->obj_offset)) 961 ret = UERR(EINVAL, dev, "invalid bo_offset: %016llx\n", op->obj_offset); 962 963 if (invalid_alignment(op->range)) 964 ret = UERR(EINVAL, dev, "invalid range: %016llx\n", op->range); 965 966 if (!drm_gpuvm_range_valid(job->vm, op->iova, op->range)) 967 ret = UERR(EINVAL, dev, "invalid range: %016llx, %016llx\n", op->iova, op->range); 968 969 /* 970 * MAP must specify a valid handle. But the handle MBZ for 971 * UNMAP or MAP_NULL. 972 */ 973 if (op->op == MSM_VM_BIND_OP_MAP) { 974 if (!op->handle) 975 ret = UERR(EINVAL, dev, "invalid handle\n"); 976 } else if (op->handle) { 977 ret = UERR(EINVAL, dev, "handle must be zero\n"); 978 } 979 980 switch (op->op) { 981 case MSM_VM_BIND_OP_MAP: 982 case MSM_VM_BIND_OP_MAP_NULL: 983 case MSM_VM_BIND_OP_UNMAP: 984 break; 985 default: 986 ret = UERR(EINVAL, dev, "invalid op: %u\n", op->op); 987 break; 988 } 989 990 return ret; 991 } 992 993 /* 994 * ioctl parsing, parameter validation, and GEM handle lookup 995 */ 996 static int 997 vm_bind_job_lookup_ops(struct msm_vm_bind_job *job, struct drm_msm_vm_bind *args, 998 struct drm_file *file, int *nr_bos) 999 { 1000 struct drm_device *dev = job->vm->drm; 1001 int ret = 0; 1002 int cnt = 0; 1003 1004 if (args->nr_ops == 1) { 1005 /* Single op case, the op is inlined: */ 1006 ret = lookup_op(job, &args->op); 1007 } else { 1008 for (unsigned i = 0; i < args->nr_ops; i++) { 1009 struct drm_msm_vm_bind_op op; 1010 void __user *userptr = 1011 u64_to_user_ptr(args->ops + (i * sizeof(op))); 1012 1013 /* make sure we don't have garbage flags, in case we hit 1014 * error path before flags is initialized: 1015 */ 1016 job->ops[i].flags = 0; 1017 1018 if (copy_from_user(&op, userptr, sizeof(op))) { 1019 ret = -EFAULT; 1020 break; 1021 } 1022 1023 ret = lookup_op(job, &op); 1024 if (ret) 1025 break; 1026 } 1027 } 1028 1029 if (ret) { 1030 job->nr_ops = 0; 1031 goto out; 1032 } 1033 1034 spin_lock(&file->table_lock); 1035 1036 for (unsigned i = 0; i < args->nr_ops; i++) { 1037 struct drm_gem_object *obj; 1038 1039 if (!job->ops[i].handle) { 1040 job->ops[i].obj = NULL; 1041 continue; 1042 } 1043 1044 /* 1045 * normally use drm_gem_object_lookup(), but for bulk lookup 1046 * all under single table_lock just hit object_idr directly: 1047 */ 1048 obj = idr_find(&file->object_idr, job->ops[i].handle); 1049 if (!obj) { 1050 ret = UERR(EINVAL, dev, "invalid handle %u at index %u\n", job->ops[i].handle, i); 1051 goto out_unlock; 1052 } 1053 1054 drm_gem_object_get(obj); 1055 1056 job->ops[i].obj = obj; 1057 cnt++; 1058 } 1059 1060 *nr_bos = cnt; 1061 1062 out_unlock: 1063 spin_unlock(&file->table_lock); 1064 1065 out: 1066 return ret; 1067 } 1068 1069 static void 1070 prealloc_count(struct msm_vm_bind_job *job, 1071 struct msm_vm_bind_op *first, 1072 struct msm_vm_bind_op *last) 1073 { 1074 struct msm_mmu *mmu = to_msm_vm(job->vm)->mmu; 1075 1076 if (!first) 1077 return; 1078 1079 uint64_t start_iova = first->iova; 1080 uint64_t end_iova = last->iova + last->range; 1081 1082 mmu->funcs->prealloc_count(mmu, &job->prealloc, start_iova, end_iova - start_iova); 1083 } 1084 1085 static bool 1086 ops_are_same_pte(struct msm_vm_bind_op *first, struct msm_vm_bind_op *next) 1087 { 1088 /* 1089 * Last level pte covers 2MB.. so we should merge two ops, from 1090 * the PoV of figuring out how much pgtable pages to pre-allocate 1091 * if they land in the same 2MB range: 1092 */ 1093 uint64_t pte_mask = ~(SZ_2M - 1); 1094 return ((first->iova + first->range) & pte_mask) == (next->iova & pte_mask); 1095 } 1096 1097 /* 1098 * Determine the amount of memory to prealloc for pgtables. For sparse images, 1099 * in particular, userspace plays some tricks with the order of page mappings 1100 * to get the desired swizzle pattern, resulting in a large # of tiny MAP ops. 1101 * So detect when multiple MAP operations are physically contiguous, and count 1102 * them as a single mapping. Otherwise the prealloc_count() will not realize 1103 * they can share pagetable pages and vastly overcount. 1104 */ 1105 static int 1106 vm_bind_prealloc_count(struct msm_vm_bind_job *job) 1107 { 1108 struct msm_vm_bind_op *first = NULL, *last = NULL; 1109 struct msm_gem_vm *vm = to_msm_vm(job->vm); 1110 int ret; 1111 1112 for (int i = 0; i < job->nr_ops; i++) { 1113 struct msm_vm_bind_op *op = &job->ops[i]; 1114 1115 /* We only care about MAP/MAP_NULL: */ 1116 if (op->op == MSM_VM_BIND_OP_UNMAP) 1117 continue; 1118 1119 /* 1120 * If op is contiguous with last in the current range, then 1121 * it becomes the new last in the range and we continue 1122 * looping: 1123 */ 1124 if (last && ops_are_same_pte(last, op)) { 1125 last = op; 1126 continue; 1127 } 1128 1129 /* 1130 * If op is not contiguous with the current range, flush 1131 * the current range and start anew: 1132 */ 1133 prealloc_count(job, first, last); 1134 first = last = op; 1135 } 1136 1137 /* Flush the remaining range: */ 1138 prealloc_count(job, first, last); 1139 1140 /* 1141 * Now that we know the needed amount to pre-alloc, throttle on pending 1142 * VM_BIND jobs if we already have too much pre-alloc memory in flight 1143 */ 1144 ret = wait_event_interruptible( 1145 vm->prealloc_throttle.wait, 1146 atomic_read(&vm->prealloc_throttle.in_flight) <= 1024); 1147 if (ret) 1148 return ret; 1149 1150 atomic_add(job->prealloc.count, &vm->prealloc_throttle.in_flight); 1151 1152 return 0; 1153 } 1154 1155 /* 1156 * Lock VM and GEM objects 1157 */ 1158 static int 1159 vm_bind_job_lock_objects(struct msm_vm_bind_job *job, struct drm_exec *exec) 1160 { 1161 int ret; 1162 1163 /* Lock VM and objects: */ 1164 drm_exec_until_all_locked (exec) { 1165 ret = drm_exec_lock_obj(exec, drm_gpuvm_resv_obj(job->vm)); 1166 drm_exec_retry_on_contention(exec); 1167 if (ret) 1168 return ret; 1169 1170 for (unsigned i = 0; i < job->nr_ops; i++) { 1171 const struct msm_vm_bind_op *op = &job->ops[i]; 1172 1173 switch (op->op) { 1174 case MSM_VM_BIND_OP_UNMAP: 1175 ret = drm_gpuvm_sm_unmap_exec_lock(job->vm, exec, 1176 op->iova, 1177 op->obj_offset); 1178 break; 1179 case MSM_VM_BIND_OP_MAP: 1180 case MSM_VM_BIND_OP_MAP_NULL: { 1181 struct drm_gpuvm_map_req map_req = { 1182 .map.va.addr = op->iova, 1183 .map.va.range = op->range, 1184 .map.gem.obj = op->obj, 1185 .map.gem.offset = op->obj_offset, 1186 }; 1187 1188 ret = drm_gpuvm_sm_map_exec_lock(job->vm, exec, 1, &map_req); 1189 break; 1190 } 1191 default: 1192 /* 1193 * lookup_op() should have already thrown an error for 1194 * invalid ops 1195 */ 1196 WARN_ON("unreachable"); 1197 } 1198 1199 drm_exec_retry_on_contention(exec); 1200 if (ret) 1201 return ret; 1202 } 1203 } 1204 1205 return 0; 1206 } 1207 1208 /* 1209 * Pin GEM objects, ensuring that we have backing pages. Pinning will move 1210 * the object to the pinned LRU so that the shrinker knows to first consider 1211 * other objects for evicting. 1212 */ 1213 static int 1214 vm_bind_job_pin_objects(struct msm_vm_bind_job *job) 1215 { 1216 struct drm_gem_object *obj; 1217 1218 /* 1219 * First loop, before holding the LRU lock, avoids holding the 1220 * LRU lock while calling msm_gem_pin_vma_locked (which could 1221 * trigger get_pages()) 1222 */ 1223 job_foreach_bo (obj, job) { 1224 struct page **pages; 1225 1226 pages = msm_gem_get_pages_locked(obj, MSM_MADV_WILLNEED); 1227 if (IS_ERR(pages)) 1228 return PTR_ERR(pages); 1229 } 1230 1231 struct msm_drm_private *priv = job->vm->drm->dev_private; 1232 1233 /* 1234 * A second loop while holding the LRU lock (a) avoids acquiring/dropping 1235 * the LRU lock for each individual bo, while (b) avoiding holding the 1236 * LRU lock while calling msm_gem_pin_vma_locked() (which could trigger 1237 * get_pages() which could trigger reclaim.. and if we held the LRU lock 1238 * could trigger deadlock with the shrinker). 1239 */ 1240 mutex_lock(&priv->lru.lock); 1241 job_foreach_bo (obj, job) 1242 msm_gem_pin_obj_locked(obj); 1243 mutex_unlock(&priv->lru.lock); 1244 1245 job->bos_pinned = true; 1246 1247 return 0; 1248 } 1249 1250 /* 1251 * Unpin GEM objects. Normally this is done after the bind job is run. 1252 */ 1253 static void 1254 vm_bind_job_unpin_objects(struct msm_vm_bind_job *job) 1255 { 1256 struct drm_gem_object *obj; 1257 1258 if (!job->bos_pinned) 1259 return; 1260 1261 job_foreach_bo (obj, job) 1262 msm_gem_unpin_locked(obj); 1263 1264 job->bos_pinned = false; 1265 } 1266 1267 /* 1268 * Pre-allocate pgtable memory, and translate the VM bind requests into a 1269 * sequence of pgtable updates to be applied asynchronously. 1270 */ 1271 static int 1272 vm_bind_job_prepare(struct msm_vm_bind_job *job) 1273 { 1274 struct msm_gem_vm *vm = to_msm_vm(job->vm); 1275 struct msm_mmu *mmu = vm->mmu; 1276 int ret; 1277 1278 ret = mmu->funcs->prealloc_allocate(mmu, &job->prealloc); 1279 if (ret) 1280 return ret; 1281 1282 for (unsigned i = 0; i < job->nr_ops; i++) { 1283 const struct msm_vm_bind_op *op = &job->ops[i]; 1284 struct op_arg arg = { 1285 .job = job, 1286 }; 1287 1288 switch (op->op) { 1289 case MSM_VM_BIND_OP_UNMAP: 1290 ret = drm_gpuvm_sm_unmap(job->vm, &arg, op->iova, 1291 op->range); 1292 break; 1293 case MSM_VM_BIND_OP_MAP: 1294 if (op->flags & MSM_VM_BIND_OP_DUMP) 1295 arg.flags |= MSM_VMA_DUMP; 1296 fallthrough; 1297 case MSM_VM_BIND_OP_MAP_NULL: { 1298 struct drm_gpuvm_map_req map_req = { 1299 .map.va.addr = op->iova, 1300 .map.va.range = op->range, 1301 .map.gem.obj = op->obj, 1302 .map.gem.offset = op->obj_offset, 1303 }; 1304 1305 ret = drm_gpuvm_sm_map(job->vm, &arg, &map_req); 1306 break; 1307 } 1308 default: 1309 /* 1310 * lookup_op() should have already thrown an error for 1311 * invalid ops 1312 */ 1313 BUG_ON("unreachable"); 1314 } 1315 1316 if (ret) { 1317 /* 1318 * If we've already started modifying the vm, we can't 1319 * adequetly describe to userspace the intermediate 1320 * state the vm is in. So throw up our hands! 1321 */ 1322 if (i > 0) 1323 msm_gem_vm_unusable(job->vm); 1324 return ret; 1325 } 1326 } 1327 1328 return 0; 1329 } 1330 1331 /* 1332 * Attach fences to the GEM objects being bound. This will signify to 1333 * the shrinker that they are busy even after dropping the locks (ie. 1334 * drm_exec_fini()) 1335 */ 1336 static void 1337 vm_bind_job_attach_fences(struct msm_vm_bind_job *job) 1338 { 1339 for (unsigned i = 0; i < job->nr_ops; i++) { 1340 struct drm_gem_object *obj = job->ops[i].obj; 1341 1342 if (!obj) 1343 continue; 1344 1345 dma_resv_add_fence(obj->resv, job->fence, 1346 DMA_RESV_USAGE_KERNEL); 1347 } 1348 } 1349 1350 int 1351 msm_ioctl_vm_bind(struct drm_device *dev, void *data, struct drm_file *file) 1352 { 1353 struct msm_drm_private *priv = dev->dev_private; 1354 struct drm_msm_vm_bind *args = data; 1355 struct msm_context *ctx = file->driver_priv; 1356 struct msm_vm_bind_job *job = NULL; 1357 struct msm_gpu *gpu = priv->gpu; 1358 struct msm_gpu_submitqueue *queue; 1359 struct msm_syncobj_post_dep *post_deps = NULL; 1360 struct drm_syncobj **syncobjs_to_reset = NULL; 1361 struct sync_file *sync_file = NULL; 1362 struct dma_fence *fence; 1363 int out_fence_fd = -1; 1364 int ret, nr_bos = 0; 1365 unsigned i; 1366 1367 if (!gpu) 1368 return -ENXIO; 1369 1370 /* 1371 * Maybe we could allow just UNMAP ops? OTOH userspace should just 1372 * immediately close the device file and all will be torn down. 1373 */ 1374 if (to_msm_vm(ctx->vm)->unusable) 1375 return UERR(EPIPE, dev, "context is unusable"); 1376 1377 /* 1378 * Technically, you cannot create a VM_BIND submitqueue in the first 1379 * place, if you haven't opted in to VM_BIND context. But it is 1380 * cleaner / less confusing, to check this case directly. 1381 */ 1382 if (!msm_context_is_vmbind(ctx)) 1383 return UERR(EINVAL, dev, "context does not support vmbind"); 1384 1385 if (args->flags & ~MSM_VM_BIND_FLAGS) 1386 return UERR(EINVAL, dev, "invalid flags"); 1387 1388 queue = msm_submitqueue_get(ctx, args->queue_id); 1389 if (!queue) 1390 return -ENOENT; 1391 1392 if (!(queue->flags & MSM_SUBMITQUEUE_VM_BIND)) { 1393 ret = UERR(EINVAL, dev, "Invalid queue type"); 1394 goto out_post_unlock; 1395 } 1396 1397 if (args->flags & MSM_VM_BIND_FENCE_FD_OUT) { 1398 out_fence_fd = get_unused_fd_flags(O_CLOEXEC); 1399 if (out_fence_fd < 0) { 1400 ret = out_fence_fd; 1401 goto out_post_unlock; 1402 } 1403 } 1404 1405 job = vm_bind_job_create(dev, file, queue, args->nr_ops); 1406 if (IS_ERR(job)) { 1407 ret = PTR_ERR(job); 1408 goto out_post_unlock; 1409 } 1410 1411 ret = mutex_lock_interruptible(&queue->lock); 1412 if (ret) 1413 goto out_post_unlock; 1414 1415 if (args->flags & MSM_VM_BIND_FENCE_FD_IN) { 1416 struct dma_fence *in_fence; 1417 1418 in_fence = sync_file_get_fence(args->fence_fd); 1419 1420 if (!in_fence) { 1421 ret = UERR(EINVAL, dev, "invalid in-fence"); 1422 goto out_unlock; 1423 } 1424 1425 ret = drm_sched_job_add_dependency(&job->base, in_fence); 1426 if (ret) 1427 goto out_unlock; 1428 } 1429 1430 if (args->in_syncobjs > 0) { 1431 syncobjs_to_reset = msm_syncobj_parse_deps(dev, &job->base, 1432 file, args->in_syncobjs, 1433 args->nr_in_syncobjs, 1434 args->syncobj_stride); 1435 if (IS_ERR(syncobjs_to_reset)) { 1436 ret = PTR_ERR(syncobjs_to_reset); 1437 goto out_unlock; 1438 } 1439 } 1440 1441 if (args->out_syncobjs > 0) { 1442 post_deps = msm_syncobj_parse_post_deps(dev, file, 1443 args->out_syncobjs, 1444 args->nr_out_syncobjs, 1445 args->syncobj_stride); 1446 if (IS_ERR(post_deps)) { 1447 ret = PTR_ERR(post_deps); 1448 goto out_unlock; 1449 } 1450 } 1451 1452 ret = vm_bind_job_lookup_ops(job, args, file, &nr_bos); 1453 if (ret) 1454 goto out_unlock; 1455 1456 ret = vm_bind_prealloc_count(job); 1457 if (ret) 1458 goto out_unlock; 1459 1460 struct drm_exec exec; 1461 unsigned flags = DRM_EXEC_IGNORE_DUPLICATES | DRM_EXEC_INTERRUPTIBLE_WAIT; 1462 drm_exec_init(&exec, flags, nr_bos + 1); 1463 1464 ret = vm_bind_job_lock_objects(job, &exec); 1465 if (ret) 1466 goto out; 1467 1468 ret = vm_bind_job_pin_objects(job); 1469 if (ret) 1470 goto out; 1471 1472 ret = vm_bind_job_prepare(job); 1473 if (ret) 1474 goto out; 1475 1476 drm_sched_job_arm(&job->base); 1477 1478 job->fence = dma_fence_get(&job->base.s_fence->finished); 1479 1480 if (args->flags & MSM_VM_BIND_FENCE_FD_OUT) { 1481 sync_file = sync_file_create(job->fence); 1482 if (!sync_file) { 1483 ret = -ENOMEM; 1484 } else { 1485 fd_install(out_fence_fd, sync_file->file); 1486 args->fence_fd = out_fence_fd; 1487 } 1488 } 1489 1490 if (ret) 1491 goto out; 1492 1493 vm_bind_job_attach_fences(job); 1494 1495 /* 1496 * The job can be free'd (and fence unref'd) at any point after 1497 * drm_sched_entity_push_job(), so we need to hold our own ref 1498 */ 1499 fence = dma_fence_get(job->fence); 1500 1501 drm_sched_entity_push_job(&job->base); 1502 1503 msm_syncobj_reset(syncobjs_to_reset, args->nr_in_syncobjs); 1504 msm_syncobj_process_post_deps(post_deps, args->nr_out_syncobjs, fence); 1505 1506 dma_fence_put(fence); 1507 1508 out: 1509 if (ret) 1510 vm_bind_job_unpin_objects(job); 1511 1512 drm_exec_fini(&exec); 1513 out_unlock: 1514 mutex_unlock(&queue->lock); 1515 out_post_unlock: 1516 if (ret && (out_fence_fd >= 0)) { 1517 put_unused_fd(out_fence_fd); 1518 if (sync_file) 1519 fput(sync_file->file); 1520 } 1521 1522 if (!IS_ERR_OR_NULL(job)) { 1523 if (ret) 1524 msm_vma_job_free(&job->base); 1525 } else { 1526 /* 1527 * If the submit hasn't yet taken ownership of the queue 1528 * then we need to drop the reference ourself: 1529 */ 1530 msm_submitqueue_put(queue); 1531 } 1532 1533 if (!IS_ERR_OR_NULL(post_deps)) { 1534 for (i = 0; i < args->nr_out_syncobjs; ++i) { 1535 kfree(post_deps[i].chain); 1536 drm_syncobj_put(post_deps[i].syncobj); 1537 } 1538 kfree(post_deps); 1539 } 1540 1541 if (!IS_ERR_OR_NULL(syncobjs_to_reset)) { 1542 for (i = 0; i < args->nr_in_syncobjs; ++i) { 1543 if (syncobjs_to_reset[i]) 1544 drm_syncobj_put(syncobjs_to_reset[i]); 1545 } 1546 kfree(syncobjs_to_reset); 1547 } 1548 1549 return ret; 1550 } 1551