1 /* 2 * Copyright © 2016 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 */ 24 25 #include <linux/sched/mm.h> 26 #include <drm/drm_gem.h> 27 28 #include "display/intel_frontbuffer.h" 29 #include "gem/i915_gem_lmem.h" 30 #include "gem/i915_gem_tiling.h" 31 #include "gt/intel_engine.h" 32 #include "gt/intel_engine_heartbeat.h" 33 #include "gt/intel_gt.h" 34 #include "gt/intel_gt_requests.h" 35 36 #include "i915_drv.h" 37 #include "i915_gem_evict.h" 38 #include "i915_sw_fence_work.h" 39 #include "i915_trace.h" 40 #include "i915_vma.h" 41 #include "i915_vma_resource.h" 42 43 static inline void assert_vma_held_evict(const struct i915_vma *vma) 44 { 45 /* 46 * We may be forced to unbind when the vm is dead, to clean it up. 47 * This is the only exception to the requirement of the object lock 48 * being held. 49 */ 50 if (atomic_read(&vma->vm->open)) 51 assert_object_held_shared(vma->obj); 52 } 53 54 static struct kmem_cache *slab_vmas; 55 56 static struct i915_vma *i915_vma_alloc(void) 57 { 58 return kmem_cache_zalloc(slab_vmas, GFP_KERNEL); 59 } 60 61 static void i915_vma_free(struct i915_vma *vma) 62 { 63 return kmem_cache_free(slab_vmas, vma); 64 } 65 66 #if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM) 67 68 #include <linux/stackdepot.h> 69 70 static void vma_print_allocator(struct i915_vma *vma, const char *reason) 71 { 72 char buf[512]; 73 74 if (!vma->node.stack) { 75 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n", 76 vma->node.start, vma->node.size, reason); 77 return; 78 } 79 80 stack_depot_snprint(vma->node.stack, buf, sizeof(buf), 0); 81 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n", 82 vma->node.start, vma->node.size, reason, buf); 83 } 84 85 #else 86 87 static void vma_print_allocator(struct i915_vma *vma, const char *reason) 88 { 89 } 90 91 #endif 92 93 static inline struct i915_vma *active_to_vma(struct i915_active *ref) 94 { 95 return container_of(ref, typeof(struct i915_vma), active); 96 } 97 98 static int __i915_vma_active(struct i915_active *ref) 99 { 100 return i915_vma_tryget(active_to_vma(ref)) ? 0 : -ENOENT; 101 } 102 103 static void __i915_vma_retire(struct i915_active *ref) 104 { 105 i915_vma_put(active_to_vma(ref)); 106 } 107 108 static struct i915_vma * 109 vma_create(struct drm_i915_gem_object *obj, 110 struct i915_address_space *vm, 111 const struct i915_ggtt_view *view) 112 { 113 struct i915_vma *pos = ERR_PTR(-E2BIG); 114 struct i915_vma *vma; 115 struct rb_node *rb, **p; 116 117 /* The aliasing_ppgtt should never be used directly! */ 118 GEM_BUG_ON(vm == &vm->gt->ggtt->alias->vm); 119 120 vma = i915_vma_alloc(); 121 if (vma == NULL) 122 return ERR_PTR(-ENOMEM); 123 124 kref_init(&vma->ref); 125 vma->vm = i915_vm_get(vm); 126 vma->ops = &vm->vma_ops; 127 vma->obj = obj; 128 vma->size = obj->base.size; 129 vma->display_alignment = I915_GTT_MIN_ALIGNMENT; 130 131 i915_active_init(&vma->active, __i915_vma_active, __i915_vma_retire, 0); 132 133 /* Declare ourselves safe for use inside shrinkers */ 134 if (IS_ENABLED(CONFIG_LOCKDEP)) { 135 fs_reclaim_acquire(GFP_KERNEL); 136 might_lock(&vma->active.mutex); 137 fs_reclaim_release(GFP_KERNEL); 138 } 139 140 INIT_LIST_HEAD(&vma->closed_link); 141 142 if (view && view->type != I915_GGTT_VIEW_NORMAL) { 143 vma->ggtt_view = *view; 144 if (view->type == I915_GGTT_VIEW_PARTIAL) { 145 GEM_BUG_ON(range_overflows_t(u64, 146 view->partial.offset, 147 view->partial.size, 148 obj->base.size >> PAGE_SHIFT)); 149 vma->size = view->partial.size; 150 vma->size <<= PAGE_SHIFT; 151 GEM_BUG_ON(vma->size > obj->base.size); 152 } else if (view->type == I915_GGTT_VIEW_ROTATED) { 153 vma->size = intel_rotation_info_size(&view->rotated); 154 vma->size <<= PAGE_SHIFT; 155 } else if (view->type == I915_GGTT_VIEW_REMAPPED) { 156 vma->size = intel_remapped_info_size(&view->remapped); 157 vma->size <<= PAGE_SHIFT; 158 } 159 } 160 161 if (unlikely(vma->size > vm->total)) 162 goto err_vma; 163 164 GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE)); 165 166 spin_lock(&obj->vma.lock); 167 168 if (i915_is_ggtt(vm)) { 169 if (unlikely(overflows_type(vma->size, u32))) 170 goto err_unlock; 171 172 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size, 173 i915_gem_object_get_tiling(obj), 174 i915_gem_object_get_stride(obj)); 175 if (unlikely(vma->fence_size < vma->size || /* overflow */ 176 vma->fence_size > vm->total)) 177 goto err_unlock; 178 179 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT)); 180 181 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size, 182 i915_gem_object_get_tiling(obj), 183 i915_gem_object_get_stride(obj)); 184 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment)); 185 186 __set_bit(I915_VMA_GGTT_BIT, __i915_vma_flags(vma)); 187 } 188 189 rb = NULL; 190 p = &obj->vma.tree.rb_node; 191 while (*p) { 192 long cmp; 193 194 rb = *p; 195 pos = rb_entry(rb, struct i915_vma, obj_node); 196 197 /* 198 * If the view already exists in the tree, another thread 199 * already created a matching vma, so return the older instance 200 * and dispose of ours. 201 */ 202 cmp = i915_vma_compare(pos, vm, view); 203 if (cmp < 0) 204 p = &rb->rb_right; 205 else if (cmp > 0) 206 p = &rb->rb_left; 207 else 208 goto err_unlock; 209 } 210 rb_link_node(&vma->obj_node, rb, p); 211 rb_insert_color(&vma->obj_node, &obj->vma.tree); 212 213 if (i915_vma_is_ggtt(vma)) 214 /* 215 * We put the GGTT vma at the start of the vma-list, followed 216 * by the ppGGTT vma. This allows us to break early when 217 * iterating over only the GGTT vma for an object, see 218 * for_each_ggtt_vma() 219 */ 220 list_add(&vma->obj_link, &obj->vma.list); 221 else 222 list_add_tail(&vma->obj_link, &obj->vma.list); 223 224 spin_unlock(&obj->vma.lock); 225 226 return vma; 227 228 err_unlock: 229 spin_unlock(&obj->vma.lock); 230 err_vma: 231 i915_vm_put(vm); 232 i915_vma_free(vma); 233 return pos; 234 } 235 236 static struct i915_vma * 237 i915_vma_lookup(struct drm_i915_gem_object *obj, 238 struct i915_address_space *vm, 239 const struct i915_ggtt_view *view) 240 { 241 struct rb_node *rb; 242 243 rb = obj->vma.tree.rb_node; 244 while (rb) { 245 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node); 246 long cmp; 247 248 cmp = i915_vma_compare(vma, vm, view); 249 if (cmp == 0) 250 return vma; 251 252 if (cmp < 0) 253 rb = rb->rb_right; 254 else 255 rb = rb->rb_left; 256 } 257 258 return NULL; 259 } 260 261 /** 262 * i915_vma_instance - return the singleton instance of the VMA 263 * @obj: parent &struct drm_i915_gem_object to be mapped 264 * @vm: address space in which the mapping is located 265 * @view: additional mapping requirements 266 * 267 * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with 268 * the same @view characteristics. If a match is not found, one is created. 269 * Once created, the VMA is kept until either the object is freed, or the 270 * address space is closed. 271 * 272 * Returns the vma, or an error pointer. 273 */ 274 struct i915_vma * 275 i915_vma_instance(struct drm_i915_gem_object *obj, 276 struct i915_address_space *vm, 277 const struct i915_ggtt_view *view) 278 { 279 struct i915_vma *vma; 280 281 GEM_BUG_ON(view && !i915_is_ggtt_or_dpt(vm)); 282 GEM_BUG_ON(!atomic_read(&vm->open)); 283 284 spin_lock(&obj->vma.lock); 285 vma = i915_vma_lookup(obj, vm, view); 286 spin_unlock(&obj->vma.lock); 287 288 /* vma_create() will resolve the race if another creates the vma */ 289 if (unlikely(!vma)) 290 vma = vma_create(obj, vm, view); 291 292 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view)); 293 return vma; 294 } 295 296 struct i915_vma_work { 297 struct dma_fence_work base; 298 struct i915_address_space *vm; 299 struct i915_vm_pt_stash stash; 300 struct i915_vma_resource *vma_res; 301 struct drm_i915_gem_object *pinned; 302 struct i915_sw_dma_fence_cb cb; 303 enum i915_cache_level cache_level; 304 unsigned int flags; 305 }; 306 307 static void __vma_bind(struct dma_fence_work *work) 308 { 309 struct i915_vma_work *vw = container_of(work, typeof(*vw), base); 310 struct i915_vma_resource *vma_res = vw->vma_res; 311 312 vma_res->ops->bind_vma(vma_res->vm, &vw->stash, 313 vma_res, vw->cache_level, vw->flags); 314 315 } 316 317 static void __vma_release(struct dma_fence_work *work) 318 { 319 struct i915_vma_work *vw = container_of(work, typeof(*vw), base); 320 321 if (vw->pinned) 322 i915_gem_object_put(vw->pinned); 323 324 i915_vm_free_pt_stash(vw->vm, &vw->stash); 325 i915_vm_put(vw->vm); 326 if (vw->vma_res) 327 i915_vma_resource_put(vw->vma_res); 328 } 329 330 static const struct dma_fence_work_ops bind_ops = { 331 .name = "bind", 332 .work = __vma_bind, 333 .release = __vma_release, 334 }; 335 336 struct i915_vma_work *i915_vma_work(void) 337 { 338 struct i915_vma_work *vw; 339 340 vw = kzalloc(sizeof(*vw), GFP_KERNEL); 341 if (!vw) 342 return NULL; 343 344 dma_fence_work_init(&vw->base, &bind_ops); 345 vw->base.dma.error = -EAGAIN; /* disable the worker by default */ 346 347 return vw; 348 } 349 350 int i915_vma_wait_for_bind(struct i915_vma *vma) 351 { 352 int err = 0; 353 354 if (rcu_access_pointer(vma->active.excl.fence)) { 355 struct dma_fence *fence; 356 357 rcu_read_lock(); 358 fence = dma_fence_get_rcu_safe(&vma->active.excl.fence); 359 rcu_read_unlock(); 360 if (fence) { 361 err = dma_fence_wait(fence, true); 362 dma_fence_put(fence); 363 } 364 } 365 366 return err; 367 } 368 369 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM) 370 static int i915_vma_verify_bind_complete(struct i915_vma *vma) 371 { 372 struct dma_fence *fence = i915_active_fence_get(&vma->active.excl); 373 int err; 374 375 if (!fence) 376 return 0; 377 378 if (dma_fence_is_signaled(fence)) 379 err = fence->error; 380 else 381 err = -EBUSY; 382 383 dma_fence_put(fence); 384 385 return err; 386 } 387 #else 388 #define i915_vma_verify_bind_complete(_vma) 0 389 #endif 390 391 I915_SELFTEST_EXPORT void 392 i915_vma_resource_init_from_vma(struct i915_vma_resource *vma_res, 393 struct i915_vma *vma) 394 { 395 struct drm_i915_gem_object *obj = vma->obj; 396 397 i915_vma_resource_init(vma_res, vma->vm, vma->pages, &vma->page_sizes, 398 obj->mm.rsgt, i915_gem_object_is_readonly(obj), 399 i915_gem_object_is_lmem(obj), obj->mm.region, 400 vma->ops, vma->private, vma->node.start, 401 vma->node.size, vma->size); 402 } 403 404 /** 405 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space. 406 * @vma: VMA to map 407 * @cache_level: mapping cache level 408 * @flags: flags like global or local mapping 409 * @work: preallocated worker for allocating and binding the PTE 410 * @vma_res: pointer to a preallocated vma resource. The resource is either 411 * consumed or freed. 412 * 413 * DMA addresses are taken from the scatter-gather table of this object (or of 414 * this VMA in case of non-default GGTT views) and PTE entries set up. 415 * Note that DMA addresses are also the only part of the SG table we care about. 416 */ 417 int i915_vma_bind(struct i915_vma *vma, 418 enum i915_cache_level cache_level, 419 u32 flags, 420 struct i915_vma_work *work, 421 struct i915_vma_resource *vma_res) 422 { 423 u32 bind_flags; 424 u32 vma_flags; 425 int ret; 426 427 lockdep_assert_held(&vma->vm->mutex); 428 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); 429 GEM_BUG_ON(vma->size > vma->node.size); 430 431 if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start, 432 vma->node.size, 433 vma->vm->total))) { 434 i915_vma_resource_free(vma_res); 435 return -ENODEV; 436 } 437 438 if (GEM_DEBUG_WARN_ON(!flags)) { 439 i915_vma_resource_free(vma_res); 440 return -EINVAL; 441 } 442 443 bind_flags = flags; 444 bind_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND; 445 446 vma_flags = atomic_read(&vma->flags); 447 vma_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND; 448 449 bind_flags &= ~vma_flags; 450 if (bind_flags == 0) { 451 i915_vma_resource_free(vma_res); 452 return 0; 453 } 454 455 GEM_BUG_ON(!atomic_read(&vma->pages_count)); 456 457 /* Wait for or await async unbinds touching our range */ 458 if (work && bind_flags & vma->vm->bind_async_flags) 459 ret = i915_vma_resource_bind_dep_await(vma->vm, 460 &work->base.chain, 461 vma->node.start, 462 vma->node.size, 463 true, 464 GFP_NOWAIT | 465 __GFP_RETRY_MAYFAIL | 466 __GFP_NOWARN); 467 else 468 ret = i915_vma_resource_bind_dep_sync(vma->vm, vma->node.start, 469 vma->node.size, true); 470 if (ret) { 471 i915_vma_resource_free(vma_res); 472 return ret; 473 } 474 475 if (vma->resource || !vma_res) { 476 /* Rebinding with an additional I915_VMA_*_BIND */ 477 GEM_WARN_ON(!vma_flags); 478 i915_vma_resource_free(vma_res); 479 } else { 480 i915_vma_resource_init_from_vma(vma_res, vma); 481 vma->resource = vma_res; 482 } 483 trace_i915_vma_bind(vma, bind_flags); 484 if (work && bind_flags & vma->vm->bind_async_flags) { 485 struct dma_fence *prev; 486 487 work->vma_res = i915_vma_resource_get(vma->resource); 488 work->cache_level = cache_level; 489 work->flags = bind_flags; 490 491 /* 492 * Note we only want to chain up to the migration fence on 493 * the pages (not the object itself). As we don't track that, 494 * yet, we have to use the exclusive fence instead. 495 * 496 * Also note that we do not want to track the async vma as 497 * part of the obj->resv->excl_fence as it only affects 498 * execution and not content or object's backing store lifetime. 499 */ 500 prev = i915_active_set_exclusive(&vma->active, &work->base.dma); 501 if (prev) { 502 __i915_sw_fence_await_dma_fence(&work->base.chain, 503 prev, 504 &work->cb); 505 dma_fence_put(prev); 506 } 507 508 work->base.dma.error = 0; /* enable the queue_work() */ 509 510 /* 511 * If we don't have the refcounted pages list, keep a reference 512 * on the object to avoid waiting for the async bind to 513 * complete in the object destruction path. 514 */ 515 if (!work->vma_res->bi.pages_rsgt) 516 work->pinned = i915_gem_object_get(vma->obj); 517 } else { 518 if (vma->obj) { 519 ret = i915_gem_object_wait_moving_fence(vma->obj, true); 520 if (ret) { 521 i915_vma_resource_free(vma->resource); 522 vma->resource = NULL; 523 524 return ret; 525 } 526 } 527 vma->ops->bind_vma(vma->vm, NULL, vma->resource, cache_level, 528 bind_flags); 529 } 530 531 if (vma->obj) 532 set_bit(I915_BO_WAS_BOUND_BIT, &vma->obj->flags); 533 534 atomic_or(bind_flags, &vma->flags); 535 return 0; 536 } 537 538 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma) 539 { 540 void __iomem *ptr; 541 int err; 542 543 if (WARN_ON_ONCE(vma->obj->flags & I915_BO_ALLOC_GPU_ONLY)) 544 return IO_ERR_PTR(-EINVAL); 545 546 if (!i915_gem_object_is_lmem(vma->obj)) { 547 if (GEM_WARN_ON(!i915_vma_is_map_and_fenceable(vma))) { 548 err = -ENODEV; 549 goto err; 550 } 551 } 552 553 GEM_BUG_ON(!i915_vma_is_ggtt(vma)); 554 GEM_BUG_ON(!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND)); 555 GEM_BUG_ON(i915_vma_verify_bind_complete(vma)); 556 557 ptr = READ_ONCE(vma->iomap); 558 if (ptr == NULL) { 559 /* 560 * TODO: consider just using i915_gem_object_pin_map() for lmem 561 * instead, which already supports mapping non-contiguous chunks 562 * of pages, that way we can also drop the 563 * I915_BO_ALLOC_CONTIGUOUS when allocating the object. 564 */ 565 if (i915_gem_object_is_lmem(vma->obj)) 566 ptr = i915_gem_object_lmem_io_map(vma->obj, 0, 567 vma->obj->base.size); 568 else 569 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap, 570 vma->node.start, 571 vma->node.size); 572 if (ptr == NULL) { 573 err = -ENOMEM; 574 goto err; 575 } 576 577 if (unlikely(cmpxchg(&vma->iomap, NULL, ptr))) { 578 io_mapping_unmap(ptr); 579 ptr = vma->iomap; 580 } 581 } 582 583 __i915_vma_pin(vma); 584 585 err = i915_vma_pin_fence(vma); 586 if (err) 587 goto err_unpin; 588 589 i915_vma_set_ggtt_write(vma); 590 591 /* NB Access through the GTT requires the device to be awake. */ 592 return ptr; 593 594 err_unpin: 595 __i915_vma_unpin(vma); 596 err: 597 return IO_ERR_PTR(err); 598 } 599 600 void i915_vma_flush_writes(struct i915_vma *vma) 601 { 602 if (i915_vma_unset_ggtt_write(vma)) 603 intel_gt_flush_ggtt_writes(vma->vm->gt); 604 } 605 606 void i915_vma_unpin_iomap(struct i915_vma *vma) 607 { 608 GEM_BUG_ON(vma->iomap == NULL); 609 610 i915_vma_flush_writes(vma); 611 612 i915_vma_unpin_fence(vma); 613 i915_vma_unpin(vma); 614 } 615 616 void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags) 617 { 618 struct i915_vma *vma; 619 struct drm_i915_gem_object *obj; 620 621 vma = fetch_and_zero(p_vma); 622 if (!vma) 623 return; 624 625 obj = vma->obj; 626 GEM_BUG_ON(!obj); 627 628 i915_vma_unpin(vma); 629 630 if (flags & I915_VMA_RELEASE_MAP) 631 i915_gem_object_unpin_map(obj); 632 633 i915_gem_object_put(obj); 634 } 635 636 bool i915_vma_misplaced(const struct i915_vma *vma, 637 u64 size, u64 alignment, u64 flags) 638 { 639 if (!drm_mm_node_allocated(&vma->node)) 640 return false; 641 642 if (test_bit(I915_VMA_ERROR_BIT, __i915_vma_flags(vma))) 643 return true; 644 645 if (vma->node.size < size) 646 return true; 647 648 GEM_BUG_ON(alignment && !is_power_of_2(alignment)); 649 if (alignment && !IS_ALIGNED(vma->node.start, alignment)) 650 return true; 651 652 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma)) 653 return true; 654 655 if (flags & PIN_OFFSET_BIAS && 656 vma->node.start < (flags & PIN_OFFSET_MASK)) 657 return true; 658 659 if (flags & PIN_OFFSET_FIXED && 660 vma->node.start != (flags & PIN_OFFSET_MASK)) 661 return true; 662 663 return false; 664 } 665 666 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma) 667 { 668 bool mappable, fenceable; 669 670 GEM_BUG_ON(!i915_vma_is_ggtt(vma)); 671 GEM_BUG_ON(!vma->fence_size); 672 673 fenceable = (vma->node.size >= vma->fence_size && 674 IS_ALIGNED(vma->node.start, vma->fence_alignment)); 675 676 mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end; 677 678 if (mappable && fenceable) 679 set_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma)); 680 else 681 clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma)); 682 } 683 684 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long color) 685 { 686 struct drm_mm_node *node = &vma->node; 687 struct drm_mm_node *other; 688 689 /* 690 * On some machines we have to be careful when putting differing types 691 * of snoopable memory together to avoid the prefetcher crossing memory 692 * domains and dying. During vm initialisation, we decide whether or not 693 * these constraints apply and set the drm_mm.color_adjust 694 * appropriately. 695 */ 696 if (!i915_vm_has_cache_coloring(vma->vm)) 697 return true; 698 699 /* Only valid to be called on an already inserted vma */ 700 GEM_BUG_ON(!drm_mm_node_allocated(node)); 701 GEM_BUG_ON(list_empty(&node->node_list)); 702 703 other = list_prev_entry(node, node_list); 704 if (i915_node_color_differs(other, color) && 705 !drm_mm_hole_follows(other)) 706 return false; 707 708 other = list_next_entry(node, node_list); 709 if (i915_node_color_differs(other, color) && 710 !drm_mm_hole_follows(node)) 711 return false; 712 713 return true; 714 } 715 716 /** 717 * i915_vma_insert - finds a slot for the vma in its address space 718 * @vma: the vma 719 * @size: requested size in bytes (can be larger than the VMA) 720 * @alignment: required alignment 721 * @flags: mask of PIN_* flags to use 722 * 723 * First we try to allocate some free space that meets the requirements for 724 * the VMA. Failiing that, if the flags permit, it will evict an old VMA, 725 * preferrably the oldest idle entry to make room for the new VMA. 726 * 727 * Returns: 728 * 0 on success, negative error code otherwise. 729 */ 730 static int 731 i915_vma_insert(struct i915_vma *vma, struct i915_gem_ww_ctx *ww, 732 u64 size, u64 alignment, u64 flags) 733 { 734 unsigned long color; 735 u64 start, end; 736 int ret; 737 738 GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND)); 739 GEM_BUG_ON(drm_mm_node_allocated(&vma->node)); 740 741 size = max(size, vma->size); 742 alignment = max(alignment, vma->display_alignment); 743 if (flags & PIN_MAPPABLE) { 744 size = max_t(typeof(size), size, vma->fence_size); 745 alignment = max_t(typeof(alignment), 746 alignment, vma->fence_alignment); 747 } 748 749 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE)); 750 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT)); 751 GEM_BUG_ON(!is_power_of_2(alignment)); 752 753 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0; 754 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE)); 755 756 end = vma->vm->total; 757 if (flags & PIN_MAPPABLE) 758 end = min_t(u64, end, i915_vm_to_ggtt(vma->vm)->mappable_end); 759 if (flags & PIN_ZONE_4G) 760 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE); 761 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE)); 762 763 alignment = max(alignment, i915_vm_obj_min_alignment(vma->vm, vma->obj)); 764 /* 765 * for compact-pt we round up the reservation to prevent 766 * any smaller pages being used within the same PDE 767 */ 768 if (NEEDS_COMPACT_PT(vma->vm->i915)) 769 size = round_up(size, alignment); 770 771 /* If binding the object/GGTT view requires more space than the entire 772 * aperture has, reject it early before evicting everything in a vain 773 * attempt to find space. 774 */ 775 if (size > end) { 776 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n", 777 size, flags & PIN_MAPPABLE ? "mappable" : "total", 778 end); 779 return -ENOSPC; 780 } 781 782 color = 0; 783 784 if (i915_vm_has_cache_coloring(vma->vm)) 785 color = vma->obj->cache_level; 786 787 if (flags & PIN_OFFSET_FIXED) { 788 u64 offset = flags & PIN_OFFSET_MASK; 789 if (!IS_ALIGNED(offset, alignment) || 790 range_overflows(offset, size, end)) 791 return -EINVAL; 792 793 ret = i915_gem_gtt_reserve(vma->vm, ww, &vma->node, 794 size, offset, color, 795 flags); 796 if (ret) 797 return ret; 798 } else { 799 /* 800 * We only support huge gtt pages through the 48b PPGTT, 801 * however we also don't want to force any alignment for 802 * objects which need to be tightly packed into the low 32bits. 803 * 804 * Note that we assume that GGTT are limited to 4GiB for the 805 * forseeable future. See also i915_ggtt_offset(). 806 */ 807 if (upper_32_bits(end - 1) && 808 vma->page_sizes.sg > I915_GTT_PAGE_SIZE) { 809 /* 810 * We can't mix 64K and 4K PTEs in the same page-table 811 * (2M block), and so to avoid the ugliness and 812 * complexity of coloring we opt for just aligning 64K 813 * objects to 2M. 814 */ 815 u64 page_alignment = 816 rounddown_pow_of_two(vma->page_sizes.sg | 817 I915_GTT_PAGE_SIZE_2M); 818 819 /* 820 * Check we don't expand for the limited Global GTT 821 * (mappable aperture is even more precious!). This 822 * also checks that we exclude the aliasing-ppgtt. 823 */ 824 GEM_BUG_ON(i915_vma_is_ggtt(vma)); 825 826 alignment = max(alignment, page_alignment); 827 828 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K) 829 size = round_up(size, I915_GTT_PAGE_SIZE_2M); 830 } 831 832 ret = i915_gem_gtt_insert(vma->vm, ww, &vma->node, 833 size, alignment, color, 834 start, end, flags); 835 if (ret) 836 return ret; 837 838 GEM_BUG_ON(vma->node.start < start); 839 GEM_BUG_ON(vma->node.start + vma->node.size > end); 840 } 841 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); 842 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, color)); 843 844 list_add_tail(&vma->vm_link, &vma->vm->bound_list); 845 846 return 0; 847 } 848 849 static void 850 i915_vma_detach(struct i915_vma *vma) 851 { 852 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node)); 853 GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND)); 854 855 /* 856 * And finally now the object is completely decoupled from this 857 * vma, we can drop its hold on the backing storage and allow 858 * it to be reaped by the shrinker. 859 */ 860 list_del(&vma->vm_link); 861 } 862 863 static bool try_qad_pin(struct i915_vma *vma, unsigned int flags) 864 { 865 unsigned int bound; 866 867 bound = atomic_read(&vma->flags); 868 869 if (flags & PIN_VALIDATE) { 870 flags &= I915_VMA_BIND_MASK; 871 872 return (flags & bound) == flags; 873 } 874 875 /* with the lock mandatory for unbind, we don't race here */ 876 flags &= I915_VMA_BIND_MASK; 877 do { 878 if (unlikely(flags & ~bound)) 879 return false; 880 881 if (unlikely(bound & (I915_VMA_OVERFLOW | I915_VMA_ERROR))) 882 return false; 883 884 GEM_BUG_ON(((bound + 1) & I915_VMA_PIN_MASK) == 0); 885 } while (!atomic_try_cmpxchg(&vma->flags, &bound, bound + 1)); 886 887 return true; 888 } 889 890 static struct scatterlist * 891 rotate_pages(struct drm_i915_gem_object *obj, unsigned int offset, 892 unsigned int width, unsigned int height, 893 unsigned int src_stride, unsigned int dst_stride, 894 struct sg_table *st, struct scatterlist *sg) 895 { 896 unsigned int column, row; 897 unsigned int src_idx; 898 899 for (column = 0; column < width; column++) { 900 unsigned int left; 901 902 src_idx = src_stride * (height - 1) + column + offset; 903 for (row = 0; row < height; row++) { 904 st->nents++; 905 /* 906 * We don't need the pages, but need to initialize 907 * the entries so the sg list can be happily traversed. 908 * The only thing we need are DMA addresses. 909 */ 910 sg_set_page(sg, NULL, I915_GTT_PAGE_SIZE, 0); 911 sg_dma_address(sg) = 912 i915_gem_object_get_dma_address(obj, src_idx); 913 sg_dma_len(sg) = I915_GTT_PAGE_SIZE; 914 sg = sg_next(sg); 915 src_idx -= src_stride; 916 } 917 918 left = (dst_stride - height) * I915_GTT_PAGE_SIZE; 919 920 if (!left) 921 continue; 922 923 st->nents++; 924 925 /* 926 * The DE ignores the PTEs for the padding tiles, the sg entry 927 * here is just a conenience to indicate how many padding PTEs 928 * to insert at this spot. 929 */ 930 sg_set_page(sg, NULL, left, 0); 931 sg_dma_address(sg) = 0; 932 sg_dma_len(sg) = left; 933 sg = sg_next(sg); 934 } 935 936 return sg; 937 } 938 939 static noinline struct sg_table * 940 intel_rotate_pages(struct intel_rotation_info *rot_info, 941 struct drm_i915_gem_object *obj) 942 { 943 unsigned int size = intel_rotation_info_size(rot_info); 944 struct drm_i915_private *i915 = to_i915(obj->base.dev); 945 struct sg_table *st; 946 struct scatterlist *sg; 947 int ret = -ENOMEM; 948 int i; 949 950 /* Allocate target SG list. */ 951 st = kmalloc(sizeof(*st), GFP_KERNEL); 952 if (!st) 953 goto err_st_alloc; 954 955 ret = sg_alloc_table(st, size, GFP_KERNEL); 956 if (ret) 957 goto err_sg_alloc; 958 959 st->nents = 0; 960 sg = st->sgl; 961 962 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) 963 sg = rotate_pages(obj, rot_info->plane[i].offset, 964 rot_info->plane[i].width, rot_info->plane[i].height, 965 rot_info->plane[i].src_stride, 966 rot_info->plane[i].dst_stride, 967 st, sg); 968 969 return st; 970 971 err_sg_alloc: 972 kfree(st); 973 err_st_alloc: 974 975 drm_dbg(&i915->drm, "Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n", 976 obj->base.size, rot_info->plane[0].width, 977 rot_info->plane[0].height, size); 978 979 return ERR_PTR(ret); 980 } 981 982 static struct scatterlist * 983 add_padding_pages(unsigned int count, 984 struct sg_table *st, struct scatterlist *sg) 985 { 986 st->nents++; 987 988 /* 989 * The DE ignores the PTEs for the padding tiles, the sg entry 990 * here is just a convenience to indicate how many padding PTEs 991 * to insert at this spot. 992 */ 993 sg_set_page(sg, NULL, count * I915_GTT_PAGE_SIZE, 0); 994 sg_dma_address(sg) = 0; 995 sg_dma_len(sg) = count * I915_GTT_PAGE_SIZE; 996 sg = sg_next(sg); 997 998 return sg; 999 } 1000 1001 static struct scatterlist * 1002 remap_tiled_color_plane_pages(struct drm_i915_gem_object *obj, 1003 unsigned int offset, unsigned int alignment_pad, 1004 unsigned int width, unsigned int height, 1005 unsigned int src_stride, unsigned int dst_stride, 1006 struct sg_table *st, struct scatterlist *sg, 1007 unsigned int *gtt_offset) 1008 { 1009 unsigned int row; 1010 1011 if (!width || !height) 1012 return sg; 1013 1014 if (alignment_pad) 1015 sg = add_padding_pages(alignment_pad, st, sg); 1016 1017 for (row = 0; row < height; row++) { 1018 unsigned int left = width * I915_GTT_PAGE_SIZE; 1019 1020 while (left) { 1021 dma_addr_t addr; 1022 unsigned int length; 1023 1024 /* 1025 * We don't need the pages, but need to initialize 1026 * the entries so the sg list can be happily traversed. 1027 * The only thing we need are DMA addresses. 1028 */ 1029 1030 addr = i915_gem_object_get_dma_address_len(obj, offset, &length); 1031 1032 length = min(left, length); 1033 1034 st->nents++; 1035 1036 sg_set_page(sg, NULL, length, 0); 1037 sg_dma_address(sg) = addr; 1038 sg_dma_len(sg) = length; 1039 sg = sg_next(sg); 1040 1041 offset += length / I915_GTT_PAGE_SIZE; 1042 left -= length; 1043 } 1044 1045 offset += src_stride - width; 1046 1047 left = (dst_stride - width) * I915_GTT_PAGE_SIZE; 1048 1049 if (!left) 1050 continue; 1051 1052 sg = add_padding_pages(left >> PAGE_SHIFT, st, sg); 1053 } 1054 1055 *gtt_offset += alignment_pad + dst_stride * height; 1056 1057 return sg; 1058 } 1059 1060 static struct scatterlist * 1061 remap_contiguous_pages(struct drm_i915_gem_object *obj, 1062 unsigned int obj_offset, 1063 unsigned int count, 1064 struct sg_table *st, struct scatterlist *sg) 1065 { 1066 struct scatterlist *iter; 1067 unsigned int offset; 1068 1069 iter = i915_gem_object_get_sg_dma(obj, obj_offset, &offset); 1070 GEM_BUG_ON(!iter); 1071 1072 do { 1073 unsigned int len; 1074 1075 len = min(sg_dma_len(iter) - (offset << PAGE_SHIFT), 1076 count << PAGE_SHIFT); 1077 sg_set_page(sg, NULL, len, 0); 1078 sg_dma_address(sg) = 1079 sg_dma_address(iter) + (offset << PAGE_SHIFT); 1080 sg_dma_len(sg) = len; 1081 1082 st->nents++; 1083 count -= len >> PAGE_SHIFT; 1084 if (count == 0) 1085 return sg; 1086 1087 sg = __sg_next(sg); 1088 iter = __sg_next(iter); 1089 offset = 0; 1090 } while (1); 1091 } 1092 1093 static struct scatterlist * 1094 remap_linear_color_plane_pages(struct drm_i915_gem_object *obj, 1095 unsigned int obj_offset, unsigned int alignment_pad, 1096 unsigned int size, 1097 struct sg_table *st, struct scatterlist *sg, 1098 unsigned int *gtt_offset) 1099 { 1100 if (!size) 1101 return sg; 1102 1103 if (alignment_pad) 1104 sg = add_padding_pages(alignment_pad, st, sg); 1105 1106 sg = remap_contiguous_pages(obj, obj_offset, size, st, sg); 1107 sg = sg_next(sg); 1108 1109 *gtt_offset += alignment_pad + size; 1110 1111 return sg; 1112 } 1113 1114 static struct scatterlist * 1115 remap_color_plane_pages(const struct intel_remapped_info *rem_info, 1116 struct drm_i915_gem_object *obj, 1117 int color_plane, 1118 struct sg_table *st, struct scatterlist *sg, 1119 unsigned int *gtt_offset) 1120 { 1121 unsigned int alignment_pad = 0; 1122 1123 if (rem_info->plane_alignment) 1124 alignment_pad = ALIGN(*gtt_offset, rem_info->plane_alignment) - *gtt_offset; 1125 1126 if (rem_info->plane[color_plane].linear) 1127 sg = remap_linear_color_plane_pages(obj, 1128 rem_info->plane[color_plane].offset, 1129 alignment_pad, 1130 rem_info->plane[color_plane].size, 1131 st, sg, 1132 gtt_offset); 1133 1134 else 1135 sg = remap_tiled_color_plane_pages(obj, 1136 rem_info->plane[color_plane].offset, 1137 alignment_pad, 1138 rem_info->plane[color_plane].width, 1139 rem_info->plane[color_plane].height, 1140 rem_info->plane[color_plane].src_stride, 1141 rem_info->plane[color_plane].dst_stride, 1142 st, sg, 1143 gtt_offset); 1144 1145 return sg; 1146 } 1147 1148 static noinline struct sg_table * 1149 intel_remap_pages(struct intel_remapped_info *rem_info, 1150 struct drm_i915_gem_object *obj) 1151 { 1152 unsigned int size = intel_remapped_info_size(rem_info); 1153 struct drm_i915_private *i915 = to_i915(obj->base.dev); 1154 struct sg_table *st; 1155 struct scatterlist *sg; 1156 unsigned int gtt_offset = 0; 1157 int ret = -ENOMEM; 1158 int i; 1159 1160 /* Allocate target SG list. */ 1161 st = kmalloc(sizeof(*st), GFP_KERNEL); 1162 if (!st) 1163 goto err_st_alloc; 1164 1165 ret = sg_alloc_table(st, size, GFP_KERNEL); 1166 if (ret) 1167 goto err_sg_alloc; 1168 1169 st->nents = 0; 1170 sg = st->sgl; 1171 1172 for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++) 1173 sg = remap_color_plane_pages(rem_info, obj, i, st, sg, >t_offset); 1174 1175 i915_sg_trim(st); 1176 1177 return st; 1178 1179 err_sg_alloc: 1180 kfree(st); 1181 err_st_alloc: 1182 1183 drm_dbg(&i915->drm, "Failed to create remapped mapping for object size %zu! (%ux%u tiles, %u pages)\n", 1184 obj->base.size, rem_info->plane[0].width, 1185 rem_info->plane[0].height, size); 1186 1187 return ERR_PTR(ret); 1188 } 1189 1190 static noinline struct sg_table * 1191 intel_partial_pages(const struct i915_ggtt_view *view, 1192 struct drm_i915_gem_object *obj) 1193 { 1194 struct sg_table *st; 1195 struct scatterlist *sg; 1196 unsigned int count = view->partial.size; 1197 int ret = -ENOMEM; 1198 1199 st = kmalloc(sizeof(*st), GFP_KERNEL); 1200 if (!st) 1201 goto err_st_alloc; 1202 1203 ret = sg_alloc_table(st, count, GFP_KERNEL); 1204 if (ret) 1205 goto err_sg_alloc; 1206 1207 st->nents = 0; 1208 1209 sg = remap_contiguous_pages(obj, view->partial.offset, count, st, st->sgl); 1210 1211 sg_mark_end(sg); 1212 i915_sg_trim(st); /* Drop any unused tail entries. */ 1213 1214 return st; 1215 1216 err_sg_alloc: 1217 kfree(st); 1218 err_st_alloc: 1219 return ERR_PTR(ret); 1220 } 1221 1222 static int 1223 __i915_vma_get_pages(struct i915_vma *vma) 1224 { 1225 struct sg_table *pages; 1226 1227 /* 1228 * The vma->pages are only valid within the lifespan of the borrowed 1229 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so 1230 * must be the vma->pages. A simple rule is that vma->pages must only 1231 * be accessed when the obj->mm.pages are pinned. 1232 */ 1233 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj)); 1234 1235 switch (vma->ggtt_view.type) { 1236 default: 1237 GEM_BUG_ON(vma->ggtt_view.type); 1238 fallthrough; 1239 case I915_GGTT_VIEW_NORMAL: 1240 pages = vma->obj->mm.pages; 1241 break; 1242 1243 case I915_GGTT_VIEW_ROTATED: 1244 pages = 1245 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj); 1246 break; 1247 1248 case I915_GGTT_VIEW_REMAPPED: 1249 pages = 1250 intel_remap_pages(&vma->ggtt_view.remapped, vma->obj); 1251 break; 1252 1253 case I915_GGTT_VIEW_PARTIAL: 1254 pages = intel_partial_pages(&vma->ggtt_view, vma->obj); 1255 break; 1256 } 1257 1258 if (IS_ERR(pages)) { 1259 drm_err(&vma->vm->i915->drm, 1260 "Failed to get pages for VMA view type %u (%ld)!\n", 1261 vma->ggtt_view.type, PTR_ERR(pages)); 1262 return PTR_ERR(pages); 1263 } 1264 1265 vma->pages = pages; 1266 1267 return 0; 1268 } 1269 1270 I915_SELFTEST_EXPORT int i915_vma_get_pages(struct i915_vma *vma) 1271 { 1272 int err; 1273 1274 if (atomic_add_unless(&vma->pages_count, 1, 0)) 1275 return 0; 1276 1277 err = i915_gem_object_pin_pages(vma->obj); 1278 if (err) 1279 return err; 1280 1281 err = __i915_vma_get_pages(vma); 1282 if (err) 1283 goto err_unpin; 1284 1285 vma->page_sizes = vma->obj->mm.page_sizes; 1286 atomic_inc(&vma->pages_count); 1287 1288 return 0; 1289 1290 err_unpin: 1291 __i915_gem_object_unpin_pages(vma->obj); 1292 1293 return err; 1294 } 1295 1296 static void __vma_put_pages(struct i915_vma *vma, unsigned int count) 1297 { 1298 /* We allocate under vma_get_pages, so beware the shrinker */ 1299 GEM_BUG_ON(atomic_read(&vma->pages_count) < count); 1300 1301 if (atomic_sub_return(count, &vma->pages_count) == 0) { 1302 if (vma->pages != vma->obj->mm.pages) { 1303 sg_free_table(vma->pages); 1304 kfree(vma->pages); 1305 } 1306 vma->pages = NULL; 1307 1308 i915_gem_object_unpin_pages(vma->obj); 1309 } 1310 } 1311 1312 I915_SELFTEST_EXPORT void i915_vma_put_pages(struct i915_vma *vma) 1313 { 1314 if (atomic_add_unless(&vma->pages_count, -1, 1)) 1315 return; 1316 1317 __vma_put_pages(vma, 1); 1318 } 1319 1320 static void vma_unbind_pages(struct i915_vma *vma) 1321 { 1322 unsigned int count; 1323 1324 lockdep_assert_held(&vma->vm->mutex); 1325 1326 /* The upper portion of pages_count is the number of bindings */ 1327 count = atomic_read(&vma->pages_count); 1328 count >>= I915_VMA_PAGES_BIAS; 1329 GEM_BUG_ON(!count); 1330 1331 __vma_put_pages(vma, count | count << I915_VMA_PAGES_BIAS); 1332 } 1333 1334 int i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww, 1335 u64 size, u64 alignment, u64 flags) 1336 { 1337 struct i915_vma_work *work = NULL; 1338 struct dma_fence *moving = NULL; 1339 struct i915_vma_resource *vma_res = NULL; 1340 intel_wakeref_t wakeref = 0; 1341 unsigned int bound; 1342 int err; 1343 1344 assert_vma_held(vma); 1345 GEM_BUG_ON(!ww); 1346 1347 BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND); 1348 BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND); 1349 1350 GEM_BUG_ON(!(flags & (PIN_USER | PIN_GLOBAL))); 1351 1352 /* First try and grab the pin without rebinding the vma */ 1353 if (try_qad_pin(vma, flags)) 1354 return 0; 1355 1356 err = i915_vma_get_pages(vma); 1357 if (err) 1358 return err; 1359 1360 if (vma->obj) { 1361 err = i915_gem_object_get_moving_fence(vma->obj, &moving); 1362 if (err) 1363 return err; 1364 } else { 1365 moving = NULL; 1366 } 1367 1368 if (flags & PIN_GLOBAL) 1369 wakeref = intel_runtime_pm_get(&vma->vm->i915->runtime_pm); 1370 1371 if (flags & vma->vm->bind_async_flags || moving) { 1372 /* lock VM */ 1373 err = i915_vm_lock_objects(vma->vm, ww); 1374 if (err) 1375 goto err_rpm; 1376 1377 work = i915_vma_work(); 1378 if (!work) { 1379 err = -ENOMEM; 1380 goto err_rpm; 1381 } 1382 1383 work->vm = i915_vm_get(vma->vm); 1384 1385 dma_fence_work_chain(&work->base, moving); 1386 1387 /* Allocate enough page directories to used PTE */ 1388 if (vma->vm->allocate_va_range) { 1389 err = i915_vm_alloc_pt_stash(vma->vm, 1390 &work->stash, 1391 vma->size); 1392 if (err) 1393 goto err_fence; 1394 1395 err = i915_vm_map_pt_stash(vma->vm, &work->stash); 1396 if (err) 1397 goto err_fence; 1398 } 1399 } 1400 1401 vma_res = i915_vma_resource_alloc(); 1402 if (IS_ERR(vma_res)) { 1403 err = PTR_ERR(vma_res); 1404 goto err_fence; 1405 } 1406 1407 /* 1408 * Differentiate between user/kernel vma inside the aliasing-ppgtt. 1409 * 1410 * We conflate the Global GTT with the user's vma when using the 1411 * aliasing-ppgtt, but it is still vitally important to try and 1412 * keep the use cases distinct. For example, userptr objects are 1413 * not allowed inside the Global GTT as that will cause lock 1414 * inversions when we have to evict them the mmu_notifier callbacks - 1415 * but they are allowed to be part of the user ppGTT which can never 1416 * be mapped. As such we try to give the distinct users of the same 1417 * mutex, distinct lockclasses [equivalent to how we keep i915_ggtt 1418 * and i915_ppgtt separate]. 1419 * 1420 * NB this may cause us to mask real lock inversions -- while the 1421 * code is safe today, lockdep may not be able to spot future 1422 * transgressions. 1423 */ 1424 err = mutex_lock_interruptible_nested(&vma->vm->mutex, 1425 !(flags & PIN_GLOBAL)); 1426 if (err) 1427 goto err_vma_res; 1428 1429 /* No more allocations allowed now we hold vm->mutex */ 1430 1431 if (unlikely(i915_vma_is_closed(vma))) { 1432 err = -ENOENT; 1433 goto err_unlock; 1434 } 1435 1436 bound = atomic_read(&vma->flags); 1437 if (unlikely(bound & I915_VMA_ERROR)) { 1438 err = -ENOMEM; 1439 goto err_unlock; 1440 } 1441 1442 if (unlikely(!((bound + 1) & I915_VMA_PIN_MASK))) { 1443 err = -EAGAIN; /* pins are meant to be fairly temporary */ 1444 goto err_unlock; 1445 } 1446 1447 if (unlikely(!(flags & ~bound & I915_VMA_BIND_MASK))) { 1448 if (!(flags & PIN_VALIDATE)) 1449 __i915_vma_pin(vma); 1450 goto err_unlock; 1451 } 1452 1453 err = i915_active_acquire(&vma->active); 1454 if (err) 1455 goto err_unlock; 1456 1457 if (!(bound & I915_VMA_BIND_MASK)) { 1458 err = i915_vma_insert(vma, ww, size, alignment, flags); 1459 if (err) 1460 goto err_active; 1461 1462 if (i915_is_ggtt(vma->vm)) 1463 __i915_vma_set_map_and_fenceable(vma); 1464 } 1465 1466 GEM_BUG_ON(!vma->pages); 1467 err = i915_vma_bind(vma, 1468 vma->obj->cache_level, 1469 flags, work, vma_res); 1470 vma_res = NULL; 1471 if (err) 1472 goto err_remove; 1473 1474 /* There should only be at most 2 active bindings (user, global) */ 1475 GEM_BUG_ON(bound + I915_VMA_PAGES_ACTIVE < bound); 1476 atomic_add(I915_VMA_PAGES_ACTIVE, &vma->pages_count); 1477 list_move_tail(&vma->vm_link, &vma->vm->bound_list); 1478 1479 if (!(flags & PIN_VALIDATE)) { 1480 __i915_vma_pin(vma); 1481 GEM_BUG_ON(!i915_vma_is_pinned(vma)); 1482 } 1483 GEM_BUG_ON(!i915_vma_is_bound(vma, flags)); 1484 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags)); 1485 1486 err_remove: 1487 if (!i915_vma_is_bound(vma, I915_VMA_BIND_MASK)) { 1488 i915_vma_detach(vma); 1489 drm_mm_remove_node(&vma->node); 1490 } 1491 err_active: 1492 i915_active_release(&vma->active); 1493 err_unlock: 1494 mutex_unlock(&vma->vm->mutex); 1495 err_vma_res: 1496 i915_vma_resource_free(vma_res); 1497 err_fence: 1498 if (work) 1499 dma_fence_work_commit_imm(&work->base); 1500 err_rpm: 1501 if (wakeref) 1502 intel_runtime_pm_put(&vma->vm->i915->runtime_pm, wakeref); 1503 1504 if (moving) 1505 dma_fence_put(moving); 1506 1507 i915_vma_put_pages(vma); 1508 return err; 1509 } 1510 1511 static void flush_idle_contexts(struct intel_gt *gt) 1512 { 1513 struct intel_engine_cs *engine; 1514 enum intel_engine_id id; 1515 1516 for_each_engine(engine, gt, id) 1517 intel_engine_flush_barriers(engine); 1518 1519 intel_gt_wait_for_idle(gt, MAX_SCHEDULE_TIMEOUT); 1520 } 1521 1522 static int __i915_ggtt_pin(struct i915_vma *vma, struct i915_gem_ww_ctx *ww, 1523 u32 align, unsigned int flags) 1524 { 1525 struct i915_address_space *vm = vma->vm; 1526 int err; 1527 1528 do { 1529 err = i915_vma_pin_ww(vma, ww, 0, align, flags | PIN_GLOBAL); 1530 1531 if (err != -ENOSPC) { 1532 if (!err) { 1533 err = i915_vma_wait_for_bind(vma); 1534 if (err) 1535 i915_vma_unpin(vma); 1536 } 1537 return err; 1538 } 1539 1540 /* Unlike i915_vma_pin, we don't take no for an answer! */ 1541 flush_idle_contexts(vm->gt); 1542 if (mutex_lock_interruptible(&vm->mutex) == 0) { 1543 /* 1544 * We pass NULL ww here, as we don't want to unbind 1545 * locked objects when called from execbuf when pinning 1546 * is removed. This would probably regress badly. 1547 */ 1548 i915_gem_evict_vm(vm, NULL); 1549 mutex_unlock(&vm->mutex); 1550 } 1551 } while (1); 1552 } 1553 1554 int i915_ggtt_pin(struct i915_vma *vma, struct i915_gem_ww_ctx *ww, 1555 u32 align, unsigned int flags) 1556 { 1557 struct i915_gem_ww_ctx _ww; 1558 int err; 1559 1560 GEM_BUG_ON(!i915_vma_is_ggtt(vma)); 1561 1562 if (ww) 1563 return __i915_ggtt_pin(vma, ww, align, flags); 1564 1565 #ifdef CONFIG_LOCKDEP 1566 WARN_ON(dma_resv_held(vma->obj->base.resv)); 1567 #endif 1568 1569 for_i915_gem_ww(&_ww, err, true) { 1570 err = i915_gem_object_lock(vma->obj, &_ww); 1571 if (!err) 1572 err = __i915_ggtt_pin(vma, &_ww, align, flags); 1573 } 1574 1575 return err; 1576 } 1577 1578 static void __vma_close(struct i915_vma *vma, struct intel_gt *gt) 1579 { 1580 /* 1581 * We defer actually closing, unbinding and destroying the VMA until 1582 * the next idle point, or if the object is freed in the meantime. By 1583 * postponing the unbind, we allow for it to be resurrected by the 1584 * client, avoiding the work required to rebind the VMA. This is 1585 * advantageous for DRI, where the client/server pass objects 1586 * between themselves, temporarily opening a local VMA to the 1587 * object, and then closing it again. The same object is then reused 1588 * on the next frame (or two, depending on the depth of the swap queue) 1589 * causing us to rebind the VMA once more. This ends up being a lot 1590 * of wasted work for the steady state. 1591 */ 1592 GEM_BUG_ON(i915_vma_is_closed(vma)); 1593 list_add(&vma->closed_link, >->closed_vma); 1594 } 1595 1596 void i915_vma_close(struct i915_vma *vma) 1597 { 1598 struct intel_gt *gt = vma->vm->gt; 1599 unsigned long flags; 1600 1601 if (i915_vma_is_ggtt(vma)) 1602 return; 1603 1604 GEM_BUG_ON(!atomic_read(&vma->open_count)); 1605 if (atomic_dec_and_lock_irqsave(&vma->open_count, 1606 >->closed_lock, 1607 flags)) { 1608 __vma_close(vma, gt); 1609 spin_unlock_irqrestore(>->closed_lock, flags); 1610 } 1611 } 1612 1613 static void __i915_vma_remove_closed(struct i915_vma *vma) 1614 { 1615 struct intel_gt *gt = vma->vm->gt; 1616 1617 spin_lock_irq(>->closed_lock); 1618 list_del_init(&vma->closed_link); 1619 spin_unlock_irq(>->closed_lock); 1620 } 1621 1622 void i915_vma_reopen(struct i915_vma *vma) 1623 { 1624 if (i915_vma_is_closed(vma)) 1625 __i915_vma_remove_closed(vma); 1626 } 1627 1628 void i915_vma_release(struct kref *ref) 1629 { 1630 struct i915_vma *vma = container_of(ref, typeof(*vma), ref); 1631 1632 i915_vm_put(vma->vm); 1633 i915_active_fini(&vma->active); 1634 GEM_WARN_ON(vma->resource); 1635 i915_vma_free(vma); 1636 } 1637 1638 static void force_unbind(struct i915_vma *vma) 1639 { 1640 if (!drm_mm_node_allocated(&vma->node)) 1641 return; 1642 1643 atomic_and(~I915_VMA_PIN_MASK, &vma->flags); 1644 WARN_ON(__i915_vma_unbind(vma)); 1645 GEM_BUG_ON(drm_mm_node_allocated(&vma->node)); 1646 } 1647 1648 static void release_references(struct i915_vma *vma) 1649 { 1650 struct drm_i915_gem_object *obj = vma->obj; 1651 1652 GEM_BUG_ON(i915_vma_is_active(vma)); 1653 1654 spin_lock(&obj->vma.lock); 1655 list_del(&vma->obj_link); 1656 if (!RB_EMPTY_NODE(&vma->obj_node)) 1657 rb_erase(&vma->obj_node, &obj->vma.tree); 1658 spin_unlock(&obj->vma.lock); 1659 1660 __i915_vma_remove_closed(vma); 1661 1662 __i915_vma_put(vma); 1663 } 1664 1665 /** 1666 * i915_vma_destroy_locked - Remove all weak reference to the vma and put 1667 * the initial reference. 1668 * 1669 * This function should be called when it's decided the vma isn't needed 1670 * anymore. The caller must assure that it doesn't race with another lookup 1671 * plus destroy, typically by taking an appropriate reference. 1672 * 1673 * Current callsites are 1674 * - __i915_gem_object_pages_fini() 1675 * - __i915_vm_close() - Blocks the above function by taking a reference on 1676 * the object. 1677 * - __i915_vma_parked() - Blocks the above functions by taking an open-count on 1678 * the vm and a reference on the object. 1679 * 1680 * Because of locks taken during destruction, a vma is also guaranteed to 1681 * stay alive while the following locks are held if it was looked up while 1682 * holding one of the locks: 1683 * - vm->mutex 1684 * - obj->vma.lock 1685 * - gt->closed_lock 1686 * 1687 * A vma user can also temporarily keep the vma alive while holding a vma 1688 * reference. 1689 */ 1690 void i915_vma_destroy_locked(struct i915_vma *vma) 1691 { 1692 lockdep_assert_held(&vma->vm->mutex); 1693 1694 force_unbind(vma); 1695 release_references(vma); 1696 } 1697 1698 void i915_vma_destroy(struct i915_vma *vma) 1699 { 1700 mutex_lock(&vma->vm->mutex); 1701 force_unbind(vma); 1702 mutex_unlock(&vma->vm->mutex); 1703 release_references(vma); 1704 } 1705 1706 void i915_vma_parked(struct intel_gt *gt) 1707 { 1708 struct i915_vma *vma, *next; 1709 LIST_HEAD(closed); 1710 1711 spin_lock_irq(>->closed_lock); 1712 list_for_each_entry_safe(vma, next, >->closed_vma, closed_link) { 1713 struct drm_i915_gem_object *obj = vma->obj; 1714 struct i915_address_space *vm = vma->vm; 1715 1716 /* XXX All to avoid keeping a reference on i915_vma itself */ 1717 1718 if (!kref_get_unless_zero(&obj->base.refcount)) 1719 continue; 1720 1721 if (!i915_vm_tryopen(vm)) { 1722 i915_gem_object_put(obj); 1723 continue; 1724 } 1725 1726 list_move(&vma->closed_link, &closed); 1727 } 1728 spin_unlock_irq(>->closed_lock); 1729 1730 /* As the GT is held idle, no vma can be reopened as we destroy them */ 1731 list_for_each_entry_safe(vma, next, &closed, closed_link) { 1732 struct drm_i915_gem_object *obj = vma->obj; 1733 struct i915_address_space *vm = vma->vm; 1734 1735 if (i915_gem_object_trylock(obj, NULL)) { 1736 INIT_LIST_HEAD(&vma->closed_link); 1737 i915_vma_destroy(vma); 1738 i915_gem_object_unlock(obj); 1739 } else { 1740 /* back you go.. */ 1741 spin_lock_irq(>->closed_lock); 1742 list_add(&vma->closed_link, >->closed_vma); 1743 spin_unlock_irq(>->closed_lock); 1744 } 1745 1746 i915_gem_object_put(obj); 1747 i915_vm_close(vm); 1748 } 1749 } 1750 1751 static void __i915_vma_iounmap(struct i915_vma *vma) 1752 { 1753 GEM_BUG_ON(i915_vma_is_pinned(vma)); 1754 1755 if (vma->iomap == NULL) 1756 return; 1757 1758 io_mapping_unmap(vma->iomap); 1759 vma->iomap = NULL; 1760 } 1761 1762 void i915_vma_revoke_mmap(struct i915_vma *vma) 1763 { 1764 struct drm_vma_offset_node *node; 1765 u64 vma_offset; 1766 1767 if (!i915_vma_has_userfault(vma)) 1768 return; 1769 1770 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma)); 1771 GEM_BUG_ON(!vma->obj->userfault_count); 1772 1773 node = &vma->mmo->vma_node; 1774 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT; 1775 unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping, 1776 drm_vma_node_offset_addr(node) + vma_offset, 1777 vma->size, 1778 1); 1779 1780 i915_vma_unset_userfault(vma); 1781 if (!--vma->obj->userfault_count) 1782 list_del(&vma->obj->userfault_link); 1783 } 1784 1785 static int 1786 __i915_request_await_bind(struct i915_request *rq, struct i915_vma *vma) 1787 { 1788 return __i915_request_await_exclusive(rq, &vma->active); 1789 } 1790 1791 static int __i915_vma_move_to_active(struct i915_vma *vma, struct i915_request *rq) 1792 { 1793 int err; 1794 1795 /* Wait for the vma to be bound before we start! */ 1796 err = __i915_request_await_bind(rq, vma); 1797 if (err) 1798 return err; 1799 1800 return i915_active_add_request(&vma->active, rq); 1801 } 1802 1803 int _i915_vma_move_to_active(struct i915_vma *vma, 1804 struct i915_request *rq, 1805 struct dma_fence *fence, 1806 unsigned int flags) 1807 { 1808 struct drm_i915_gem_object *obj = vma->obj; 1809 int err; 1810 1811 assert_object_held(obj); 1812 1813 GEM_BUG_ON(!vma->pages); 1814 1815 err = __i915_vma_move_to_active(vma, rq); 1816 if (unlikely(err)) 1817 return err; 1818 1819 if (flags & EXEC_OBJECT_WRITE) { 1820 struct intel_frontbuffer *front; 1821 1822 front = __intel_frontbuffer_get(obj); 1823 if (unlikely(front)) { 1824 if (intel_frontbuffer_invalidate(front, ORIGIN_CS)) 1825 i915_active_add_request(&front->write, rq); 1826 intel_frontbuffer_put(front); 1827 } 1828 1829 if (!(flags & __EXEC_OBJECT_NO_RESERVE)) { 1830 err = dma_resv_reserve_fences(vma->obj->base.resv, 1); 1831 if (unlikely(err)) 1832 return err; 1833 } 1834 1835 if (fence) { 1836 dma_resv_add_fence(vma->obj->base.resv, fence, 1837 DMA_RESV_USAGE_WRITE); 1838 obj->write_domain = I915_GEM_DOMAIN_RENDER; 1839 obj->read_domains = 0; 1840 } 1841 } else { 1842 if (!(flags & __EXEC_OBJECT_NO_RESERVE)) { 1843 err = dma_resv_reserve_fences(vma->obj->base.resv, 1); 1844 if (unlikely(err)) 1845 return err; 1846 } 1847 1848 if (fence) { 1849 dma_resv_add_fence(vma->obj->base.resv, fence, 1850 DMA_RESV_USAGE_READ); 1851 obj->write_domain = 0; 1852 } 1853 } 1854 1855 if (flags & EXEC_OBJECT_NEEDS_FENCE && vma->fence) 1856 i915_active_add_request(&vma->fence->active, rq); 1857 1858 obj->read_domains |= I915_GEM_GPU_DOMAINS; 1859 obj->mm.dirty = true; 1860 1861 GEM_BUG_ON(!i915_vma_is_active(vma)); 1862 return 0; 1863 } 1864 1865 struct dma_fence *__i915_vma_evict(struct i915_vma *vma, bool async) 1866 { 1867 struct i915_vma_resource *vma_res = vma->resource; 1868 struct dma_fence *unbind_fence; 1869 1870 GEM_BUG_ON(i915_vma_is_pinned(vma)); 1871 assert_vma_held_evict(vma); 1872 1873 if (i915_vma_is_map_and_fenceable(vma)) { 1874 /* Force a pagefault for domain tracking on next user access */ 1875 i915_vma_revoke_mmap(vma); 1876 1877 /* 1878 * Check that we have flushed all writes through the GGTT 1879 * before the unbind, other due to non-strict nature of those 1880 * indirect writes they may end up referencing the GGTT PTE 1881 * after the unbind. 1882 * 1883 * Note that we may be concurrently poking at the GGTT_WRITE 1884 * bit from set-domain, as we mark all GGTT vma associated 1885 * with an object. We know this is for another vma, as we 1886 * are currently unbinding this one -- so if this vma will be 1887 * reused, it will be refaulted and have its dirty bit set 1888 * before the next write. 1889 */ 1890 i915_vma_flush_writes(vma); 1891 1892 /* release the fence reg _after_ flushing */ 1893 i915_vma_revoke_fence(vma); 1894 1895 __i915_vma_iounmap(vma); 1896 clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma)); 1897 } 1898 GEM_BUG_ON(vma->fence); 1899 GEM_BUG_ON(i915_vma_has_userfault(vma)); 1900 1901 /* Object backend must be async capable. */ 1902 GEM_WARN_ON(async && !vma->resource->bi.pages_rsgt); 1903 1904 /* If vm is not open, unbind is a nop. */ 1905 vma_res->needs_wakeref = i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND) && 1906 atomic_read(&vma->vm->open); 1907 trace_i915_vma_unbind(vma); 1908 1909 unbind_fence = i915_vma_resource_unbind(vma_res); 1910 vma->resource = NULL; 1911 1912 atomic_and(~(I915_VMA_BIND_MASK | I915_VMA_ERROR | I915_VMA_GGTT_WRITE), 1913 &vma->flags); 1914 1915 i915_vma_detach(vma); 1916 1917 if (!async && unbind_fence) { 1918 dma_fence_wait(unbind_fence, false); 1919 dma_fence_put(unbind_fence); 1920 unbind_fence = NULL; 1921 } 1922 1923 /* 1924 * Binding itself may not have completed until the unbind fence signals, 1925 * so don't drop the pages until that happens, unless the resource is 1926 * async_capable. 1927 */ 1928 1929 vma_unbind_pages(vma); 1930 return unbind_fence; 1931 } 1932 1933 int __i915_vma_unbind(struct i915_vma *vma) 1934 { 1935 int ret; 1936 1937 lockdep_assert_held(&vma->vm->mutex); 1938 assert_vma_held_evict(vma); 1939 1940 if (!drm_mm_node_allocated(&vma->node)) 1941 return 0; 1942 1943 if (i915_vma_is_pinned(vma)) { 1944 vma_print_allocator(vma, "is pinned"); 1945 return -EAGAIN; 1946 } 1947 1948 /* 1949 * After confirming that no one else is pinning this vma, wait for 1950 * any laggards who may have crept in during the wait (through 1951 * a residual pin skipping the vm->mutex) to complete. 1952 */ 1953 ret = i915_vma_sync(vma); 1954 if (ret) 1955 return ret; 1956 1957 GEM_BUG_ON(i915_vma_is_active(vma)); 1958 __i915_vma_evict(vma, false); 1959 1960 drm_mm_remove_node(&vma->node); /* pairs with i915_vma_release() */ 1961 return 0; 1962 } 1963 1964 static struct dma_fence *__i915_vma_unbind_async(struct i915_vma *vma) 1965 { 1966 struct dma_fence *fence; 1967 1968 lockdep_assert_held(&vma->vm->mutex); 1969 1970 if (!drm_mm_node_allocated(&vma->node)) 1971 return NULL; 1972 1973 if (i915_vma_is_pinned(vma) || 1974 &vma->obj->mm.rsgt->table != vma->resource->bi.pages) 1975 return ERR_PTR(-EAGAIN); 1976 1977 /* 1978 * We probably need to replace this with awaiting the fences of the 1979 * object's dma_resv when the vma active goes away. When doing that 1980 * we need to be careful to not add the vma_resource unbind fence 1981 * immediately to the object's dma_resv, because then unbinding 1982 * the next vma from the object, in case there are many, will 1983 * actually await the unbinding of the previous vmas, which is 1984 * undesirable. 1985 */ 1986 if (i915_sw_fence_await_active(&vma->resource->chain, &vma->active, 1987 I915_ACTIVE_AWAIT_EXCL | 1988 I915_ACTIVE_AWAIT_ACTIVE) < 0) { 1989 return ERR_PTR(-EBUSY); 1990 } 1991 1992 fence = __i915_vma_evict(vma, true); 1993 1994 drm_mm_remove_node(&vma->node); /* pairs with i915_vma_release() */ 1995 1996 return fence; 1997 } 1998 1999 int i915_vma_unbind(struct i915_vma *vma) 2000 { 2001 struct i915_address_space *vm = vma->vm; 2002 intel_wakeref_t wakeref = 0; 2003 int err; 2004 2005 assert_object_held_shared(vma->obj); 2006 2007 /* Optimistic wait before taking the mutex */ 2008 err = i915_vma_sync(vma); 2009 if (err) 2010 return err; 2011 2012 if (!drm_mm_node_allocated(&vma->node)) 2013 return 0; 2014 2015 if (i915_vma_is_pinned(vma)) { 2016 vma_print_allocator(vma, "is pinned"); 2017 return -EAGAIN; 2018 } 2019 2020 if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND)) 2021 /* XXX not always required: nop_clear_range */ 2022 wakeref = intel_runtime_pm_get(&vm->i915->runtime_pm); 2023 2024 err = mutex_lock_interruptible_nested(&vma->vm->mutex, !wakeref); 2025 if (err) 2026 goto out_rpm; 2027 2028 err = __i915_vma_unbind(vma); 2029 mutex_unlock(&vm->mutex); 2030 2031 out_rpm: 2032 if (wakeref) 2033 intel_runtime_pm_put(&vm->i915->runtime_pm, wakeref); 2034 return err; 2035 } 2036 2037 int i915_vma_unbind_async(struct i915_vma *vma, bool trylock_vm) 2038 { 2039 struct drm_i915_gem_object *obj = vma->obj; 2040 struct i915_address_space *vm = vma->vm; 2041 intel_wakeref_t wakeref = 0; 2042 struct dma_fence *fence; 2043 int err; 2044 2045 /* 2046 * We need the dma-resv lock since we add the 2047 * unbind fence to the dma-resv object. 2048 */ 2049 assert_object_held(obj); 2050 2051 if (!drm_mm_node_allocated(&vma->node)) 2052 return 0; 2053 2054 if (i915_vma_is_pinned(vma)) { 2055 vma_print_allocator(vma, "is pinned"); 2056 return -EAGAIN; 2057 } 2058 2059 if (!obj->mm.rsgt) 2060 return -EBUSY; 2061 2062 err = dma_resv_reserve_fences(obj->base.resv, 1); 2063 if (err) 2064 return -EBUSY; 2065 2066 /* 2067 * It would be great if we could grab this wakeref from the 2068 * async unbind work if needed, but we can't because it uses 2069 * kmalloc and it's in the dma-fence signalling critical path. 2070 */ 2071 if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND)) 2072 wakeref = intel_runtime_pm_get(&vm->i915->runtime_pm); 2073 2074 if (trylock_vm && !mutex_trylock(&vm->mutex)) { 2075 err = -EBUSY; 2076 goto out_rpm; 2077 } else if (!trylock_vm) { 2078 err = mutex_lock_interruptible_nested(&vm->mutex, !wakeref); 2079 if (err) 2080 goto out_rpm; 2081 } 2082 2083 fence = __i915_vma_unbind_async(vma); 2084 mutex_unlock(&vm->mutex); 2085 if (IS_ERR_OR_NULL(fence)) { 2086 err = PTR_ERR_OR_ZERO(fence); 2087 goto out_rpm; 2088 } 2089 2090 dma_resv_add_fence(obj->base.resv, fence, DMA_RESV_USAGE_READ); 2091 dma_fence_put(fence); 2092 2093 out_rpm: 2094 if (wakeref) 2095 intel_runtime_pm_put(&vm->i915->runtime_pm, wakeref); 2096 return err; 2097 } 2098 2099 int i915_vma_unbind_unlocked(struct i915_vma *vma) 2100 { 2101 int err; 2102 2103 i915_gem_object_lock(vma->obj, NULL); 2104 err = i915_vma_unbind(vma); 2105 i915_gem_object_unlock(vma->obj); 2106 2107 return err; 2108 } 2109 2110 struct i915_vma *i915_vma_make_unshrinkable(struct i915_vma *vma) 2111 { 2112 i915_gem_object_make_unshrinkable(vma->obj); 2113 return vma; 2114 } 2115 2116 void i915_vma_make_shrinkable(struct i915_vma *vma) 2117 { 2118 i915_gem_object_make_shrinkable(vma->obj); 2119 } 2120 2121 void i915_vma_make_purgeable(struct i915_vma *vma) 2122 { 2123 i915_gem_object_make_purgeable(vma->obj); 2124 } 2125 2126 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 2127 #include "selftests/i915_vma.c" 2128 #endif 2129 2130 void i915_vma_module_exit(void) 2131 { 2132 kmem_cache_destroy(slab_vmas); 2133 } 2134 2135 int __init i915_vma_module_init(void) 2136 { 2137 slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); 2138 if (!slab_vmas) 2139 return -ENOMEM; 2140 2141 return 0; 2142 } 2143