2 * Copyright © 2016 Intel Corporation
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:
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
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
25 #include <linux/sched/mm.h>
26 #include <drm/drm_gem.h>
28 #include "display/intel_frontbuffer.h"
30 #include "gem/i915_gem_lmem.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"
37 #include "i915_sw_fence_work.h"
38 #include "i915_trace.h"
40 #include "i915_vma_resource.h"
42 static inline void assert_vma_held_evict(const struct i915_vma *vma)
45 * We may be forced to unbind when the vm is dead, to clean it up.
46 * This is the only exception to the requirement of the object lock
49 if (atomic_read(&vma->vm->open))
50 assert_object_held_shared(vma->obj);
53 static struct kmem_cache *slab_vmas;
55 static struct i915_vma *i915_vma_alloc(void)
57 return kmem_cache_zalloc(slab_vmas, GFP_KERNEL);
60 static void i915_vma_free(struct i915_vma *vma)
62 return kmem_cache_free(slab_vmas, vma);
65 #if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM)
67 #include <linux/stackdepot.h>
69 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
73 if (!vma->node.stack) {
74 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n",
75 vma->node.start, vma->node.size, reason);
79 stack_depot_snprint(vma->node.stack, buf, sizeof(buf), 0);
80 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n",
81 vma->node.start, vma->node.size, reason, buf);
86 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
92 static inline struct i915_vma *active_to_vma(struct i915_active *ref)
94 return container_of(ref, typeof(struct i915_vma), active);
97 static int __i915_vma_active(struct i915_active *ref)
99 return i915_vma_tryget(active_to_vma(ref)) ? 0 : -ENOENT;
102 static void __i915_vma_retire(struct i915_active *ref)
104 i915_vma_put(active_to_vma(ref));
107 static struct i915_vma *
108 vma_create(struct drm_i915_gem_object *obj,
109 struct i915_address_space *vm,
110 const struct i915_ggtt_view *view)
112 struct i915_vma *pos = ERR_PTR(-E2BIG);
113 struct i915_vma *vma;
114 struct rb_node *rb, **p;
116 /* The aliasing_ppgtt should never be used directly! */
117 GEM_BUG_ON(vm == &vm->gt->ggtt->alias->vm);
119 vma = i915_vma_alloc();
121 return ERR_PTR(-ENOMEM);
123 kref_init(&vma->ref);
124 vma->vm = i915_vm_get(vm);
125 vma->ops = &vm->vma_ops;
127 vma->size = obj->base.size;
128 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
130 i915_active_init(&vma->active, __i915_vma_active, __i915_vma_retire, 0);
132 /* Declare ourselves safe for use inside shrinkers */
133 if (IS_ENABLED(CONFIG_LOCKDEP)) {
134 fs_reclaim_acquire(GFP_KERNEL);
135 might_lock(&vma->active.mutex);
136 fs_reclaim_release(GFP_KERNEL);
139 INIT_LIST_HEAD(&vma->closed_link);
141 if (view && view->type != I915_GGTT_VIEW_NORMAL) {
142 vma->ggtt_view = *view;
143 if (view->type == I915_GGTT_VIEW_PARTIAL) {
144 GEM_BUG_ON(range_overflows_t(u64,
145 view->partial.offset,
147 obj->base.size >> PAGE_SHIFT));
148 vma->size = view->partial.size;
149 vma->size <<= PAGE_SHIFT;
150 GEM_BUG_ON(vma->size > obj->base.size);
151 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
152 vma->size = intel_rotation_info_size(&view->rotated);
153 vma->size <<= PAGE_SHIFT;
154 } else if (view->type == I915_GGTT_VIEW_REMAPPED) {
155 vma->size = intel_remapped_info_size(&view->remapped);
156 vma->size <<= PAGE_SHIFT;
160 if (unlikely(vma->size > vm->total))
163 GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
165 spin_lock(&obj->vma.lock);
167 if (i915_is_ggtt(vm)) {
168 if (unlikely(overflows_type(vma->size, u32)))
171 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
172 i915_gem_object_get_tiling(obj),
173 i915_gem_object_get_stride(obj));
174 if (unlikely(vma->fence_size < vma->size || /* overflow */
175 vma->fence_size > vm->total))
178 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
180 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
181 i915_gem_object_get_tiling(obj),
182 i915_gem_object_get_stride(obj));
183 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
185 __set_bit(I915_VMA_GGTT_BIT, __i915_vma_flags(vma));
189 p = &obj->vma.tree.rb_node;
194 pos = rb_entry(rb, struct i915_vma, obj_node);
197 * If the view already exists in the tree, another thread
198 * already created a matching vma, so return the older instance
199 * and dispose of ours.
201 cmp = i915_vma_compare(pos, vm, view);
209 rb_link_node(&vma->obj_node, rb, p);
210 rb_insert_color(&vma->obj_node, &obj->vma.tree);
212 if (i915_vma_is_ggtt(vma))
214 * We put the GGTT vma at the start of the vma-list, followed
215 * by the ppGGTT vma. This allows us to break early when
216 * iterating over only the GGTT vma for an object, see
217 * for_each_ggtt_vma()
219 list_add(&vma->obj_link, &obj->vma.list);
221 list_add_tail(&vma->obj_link, &obj->vma.list);
223 spin_unlock(&obj->vma.lock);
228 spin_unlock(&obj->vma.lock);
235 static struct i915_vma *
236 i915_vma_lookup(struct drm_i915_gem_object *obj,
237 struct i915_address_space *vm,
238 const struct i915_ggtt_view *view)
242 rb = obj->vma.tree.rb_node;
244 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
247 cmp = i915_vma_compare(vma, vm, view);
261 * i915_vma_instance - return the singleton instance of the VMA
262 * @obj: parent &struct drm_i915_gem_object to be mapped
263 * @vm: address space in which the mapping is located
264 * @view: additional mapping requirements
266 * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
267 * the same @view characteristics. If a match is not found, one is created.
268 * Once created, the VMA is kept until either the object is freed, or the
269 * address space is closed.
271 * Returns the vma, or an error pointer.
274 i915_vma_instance(struct drm_i915_gem_object *obj,
275 struct i915_address_space *vm,
276 const struct i915_ggtt_view *view)
278 struct i915_vma *vma;
280 GEM_BUG_ON(view && !i915_is_ggtt_or_dpt(vm));
281 GEM_BUG_ON(!atomic_read(&vm->open));
283 spin_lock(&obj->vma.lock);
284 vma = i915_vma_lookup(obj, vm, view);
285 spin_unlock(&obj->vma.lock);
287 /* vma_create() will resolve the race if another creates the vma */
289 vma = vma_create(obj, vm, view);
291 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
295 struct i915_vma_work {
296 struct dma_fence_work base;
297 struct i915_address_space *vm;
298 struct i915_vm_pt_stash stash;
299 struct i915_vma_resource *vma_res;
300 struct drm_i915_gem_object *pinned;
301 struct i915_sw_dma_fence_cb cb;
302 enum i915_cache_level cache_level;
306 static void __vma_bind(struct dma_fence_work *work)
308 struct i915_vma_work *vw = container_of(work, typeof(*vw), base);
309 struct i915_vma_resource *vma_res = vw->vma_res;
311 vma_res->ops->bind_vma(vma_res->vm, &vw->stash,
312 vma_res, vw->cache_level, vw->flags);
316 static void __vma_release(struct dma_fence_work *work)
318 struct i915_vma_work *vw = container_of(work, typeof(*vw), base);
321 i915_gem_object_put(vw->pinned);
323 i915_vm_free_pt_stash(vw->vm, &vw->stash);
326 i915_vma_resource_put(vw->vma_res);
329 static const struct dma_fence_work_ops bind_ops = {
332 .release = __vma_release,
335 struct i915_vma_work *i915_vma_work(void)
337 struct i915_vma_work *vw;
339 vw = kzalloc(sizeof(*vw), GFP_KERNEL);
343 dma_fence_work_init(&vw->base, &bind_ops);
344 vw->base.dma.error = -EAGAIN; /* disable the worker by default */
349 int i915_vma_wait_for_bind(struct i915_vma *vma)
353 if (rcu_access_pointer(vma->active.excl.fence)) {
354 struct dma_fence *fence;
357 fence = dma_fence_get_rcu_safe(&vma->active.excl.fence);
360 err = dma_fence_wait(fence, true);
361 dma_fence_put(fence);
368 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
369 static int i915_vma_verify_bind_complete(struct i915_vma *vma)
371 struct dma_fence *fence = i915_active_fence_get(&vma->active.excl);
377 if (dma_fence_is_signaled(fence))
382 dma_fence_put(fence);
387 #define i915_vma_verify_bind_complete(_vma) 0
390 I915_SELFTEST_EXPORT void
391 i915_vma_resource_init_from_vma(struct i915_vma_resource *vma_res,
392 struct i915_vma *vma)
394 struct drm_i915_gem_object *obj = vma->obj;
396 i915_vma_resource_init(vma_res, vma->vm, vma->pages, &vma->page_sizes,
397 obj->mm.rsgt, i915_gem_object_is_readonly(obj),
398 i915_gem_object_is_lmem(obj), obj->mm.region,
399 vma->ops, vma->private, vma->node.start,
400 vma->node.size, vma->size);
404 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
406 * @cache_level: mapping cache level
407 * @flags: flags like global or local mapping
408 * @work: preallocated worker for allocating and binding the PTE
409 * @vma_res: pointer to a preallocated vma resource. The resource is either
412 * DMA addresses are taken from the scatter-gather table of this object (or of
413 * this VMA in case of non-default GGTT views) and PTE entries set up.
414 * Note that DMA addresses are also the only part of the SG table we care about.
416 int i915_vma_bind(struct i915_vma *vma,
417 enum i915_cache_level cache_level,
419 struct i915_vma_work *work,
420 struct i915_vma_resource *vma_res)
426 lockdep_assert_held(&vma->vm->mutex);
427 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
428 GEM_BUG_ON(vma->size > vma->node.size);
430 if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start,
433 i915_vma_resource_free(vma_res);
437 if (GEM_DEBUG_WARN_ON(!flags)) {
438 i915_vma_resource_free(vma_res);
443 bind_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
445 vma_flags = atomic_read(&vma->flags);
446 vma_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
448 bind_flags &= ~vma_flags;
449 if (bind_flags == 0) {
450 i915_vma_resource_free(vma_res);
454 GEM_BUG_ON(!atomic_read(&vma->pages_count));
456 /* Wait for or await async unbinds touching our range */
457 if (work && bind_flags & vma->vm->bind_async_flags)
458 ret = i915_vma_resource_bind_dep_await(vma->vm,
464 __GFP_RETRY_MAYFAIL |
467 ret = i915_vma_resource_bind_dep_sync(vma->vm, vma->node.start,
468 vma->node.size, true);
470 i915_vma_resource_free(vma_res);
474 if (vma->resource || !vma_res) {
475 /* Rebinding with an additional I915_VMA_*_BIND */
476 GEM_WARN_ON(!vma_flags);
477 i915_vma_resource_free(vma_res);
479 i915_vma_resource_init_from_vma(vma_res, vma);
480 vma->resource = vma_res;
482 trace_i915_vma_bind(vma, bind_flags);
483 if (work && bind_flags & vma->vm->bind_async_flags) {
484 struct dma_fence *prev;
486 work->vma_res = i915_vma_resource_get(vma->resource);
487 work->cache_level = cache_level;
488 work->flags = bind_flags;
491 * Note we only want to chain up to the migration fence on
492 * the pages (not the object itself). As we don't track that,
493 * yet, we have to use the exclusive fence instead.
495 * Also note that we do not want to track the async vma as
496 * part of the obj->resv->excl_fence as it only affects
497 * execution and not content or object's backing store lifetime.
499 prev = i915_active_set_exclusive(&vma->active, &work->base.dma);
501 __i915_sw_fence_await_dma_fence(&work->base.chain,
507 work->base.dma.error = 0; /* enable the queue_work() */
510 * If we don't have the refcounted pages list, keep a reference
511 * on the object to avoid waiting for the async bind to
512 * complete in the object destruction path.
514 if (!work->vma_res->bi.pages_rsgt)
515 work->pinned = i915_gem_object_get(vma->obj);
518 ret = i915_gem_object_wait_moving_fence(vma->obj, true);
520 i915_vma_resource_free(vma->resource);
521 vma->resource = NULL;
526 vma->ops->bind_vma(vma->vm, NULL, vma->resource, cache_level,
530 atomic_or(bind_flags, &vma->flags);
534 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
539 if (!i915_gem_object_is_lmem(vma->obj)) {
540 if (GEM_WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
546 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
547 GEM_BUG_ON(!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND));
548 GEM_BUG_ON(i915_vma_verify_bind_complete(vma));
550 ptr = READ_ONCE(vma->iomap);
553 * TODO: consider just using i915_gem_object_pin_map() for lmem
554 * instead, which already supports mapping non-contiguous chunks
555 * of pages, that way we can also drop the
556 * I915_BO_ALLOC_CONTIGUOUS when allocating the object.
558 if (i915_gem_object_is_lmem(vma->obj))
559 ptr = i915_gem_object_lmem_io_map(vma->obj, 0,
560 vma->obj->base.size);
562 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
570 if (unlikely(cmpxchg(&vma->iomap, NULL, ptr))) {
571 io_mapping_unmap(ptr);
578 err = i915_vma_pin_fence(vma);
582 i915_vma_set_ggtt_write(vma);
584 /* NB Access through the GTT requires the device to be awake. */
588 __i915_vma_unpin(vma);
590 return IO_ERR_PTR(err);
593 void i915_vma_flush_writes(struct i915_vma *vma)
595 if (i915_vma_unset_ggtt_write(vma))
596 intel_gt_flush_ggtt_writes(vma->vm->gt);
599 void i915_vma_unpin_iomap(struct i915_vma *vma)
601 GEM_BUG_ON(vma->iomap == NULL);
603 i915_vma_flush_writes(vma);
605 i915_vma_unpin_fence(vma);
609 void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags)
611 struct i915_vma *vma;
612 struct drm_i915_gem_object *obj;
614 vma = fetch_and_zero(p_vma);
623 if (flags & I915_VMA_RELEASE_MAP)
624 i915_gem_object_unpin_map(obj);
626 i915_gem_object_put(obj);
629 bool i915_vma_misplaced(const struct i915_vma *vma,
630 u64 size, u64 alignment, u64 flags)
632 if (!drm_mm_node_allocated(&vma->node))
635 if (test_bit(I915_VMA_ERROR_BIT, __i915_vma_flags(vma)))
638 if (vma->node.size < size)
641 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
642 if (alignment && !IS_ALIGNED(vma->node.start, alignment))
645 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
648 if (flags & PIN_OFFSET_BIAS &&
649 vma->node.start < (flags & PIN_OFFSET_MASK))
652 if (flags & PIN_OFFSET_FIXED &&
653 vma->node.start != (flags & PIN_OFFSET_MASK))
659 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
661 bool mappable, fenceable;
663 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
664 GEM_BUG_ON(!vma->fence_size);
666 fenceable = (vma->node.size >= vma->fence_size &&
667 IS_ALIGNED(vma->node.start, vma->fence_alignment));
669 mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
671 if (mappable && fenceable)
672 set_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
674 clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
677 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long color)
679 struct drm_mm_node *node = &vma->node;
680 struct drm_mm_node *other;
683 * On some machines we have to be careful when putting differing types
684 * of snoopable memory together to avoid the prefetcher crossing memory
685 * domains and dying. During vm initialisation, we decide whether or not
686 * these constraints apply and set the drm_mm.color_adjust
689 if (!i915_vm_has_cache_coloring(vma->vm))
692 /* Only valid to be called on an already inserted vma */
693 GEM_BUG_ON(!drm_mm_node_allocated(node));
694 GEM_BUG_ON(list_empty(&node->node_list));
696 other = list_prev_entry(node, node_list);
697 if (i915_node_color_differs(other, color) &&
698 !drm_mm_hole_follows(other))
701 other = list_next_entry(node, node_list);
702 if (i915_node_color_differs(other, color) &&
703 !drm_mm_hole_follows(node))
710 * i915_vma_insert - finds a slot for the vma in its address space
712 * @size: requested size in bytes (can be larger than the VMA)
713 * @alignment: required alignment
714 * @flags: mask of PIN_* flags to use
716 * First we try to allocate some free space that meets the requirements for
717 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
718 * preferrably the oldest idle entry to make room for the new VMA.
721 * 0 on success, negative error code otherwise.
724 i915_vma_insert(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
725 u64 size, u64 alignment, u64 flags)
731 GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
732 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
734 size = max(size, vma->size);
735 alignment = max(alignment, vma->display_alignment);
736 if (flags & PIN_MAPPABLE) {
737 size = max_t(typeof(size), size, vma->fence_size);
738 alignment = max_t(typeof(alignment),
739 alignment, vma->fence_alignment);
742 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
743 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
744 GEM_BUG_ON(!is_power_of_2(alignment));
746 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
747 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
749 end = vma->vm->total;
750 if (flags & PIN_MAPPABLE)
751 end = min_t(u64, end, i915_vm_to_ggtt(vma->vm)->mappable_end);
752 if (flags & PIN_ZONE_4G)
753 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
754 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
756 /* If binding the object/GGTT view requires more space than the entire
757 * aperture has, reject it early before evicting everything in a vain
758 * attempt to find space.
761 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n",
762 size, flags & PIN_MAPPABLE ? "mappable" : "total",
768 if (i915_vm_has_cache_coloring(vma->vm))
769 color = vma->obj->cache_level;
771 if (flags & PIN_OFFSET_FIXED) {
772 u64 offset = flags & PIN_OFFSET_MASK;
773 if (!IS_ALIGNED(offset, alignment) ||
774 range_overflows(offset, size, end))
777 ret = i915_gem_gtt_reserve(vma->vm, ww, &vma->node,
784 * We only support huge gtt pages through the 48b PPGTT,
785 * however we also don't want to force any alignment for
786 * objects which need to be tightly packed into the low 32bits.
788 * Note that we assume that GGTT are limited to 4GiB for the
789 * forseeable future. See also i915_ggtt_offset().
791 if (upper_32_bits(end - 1) &&
792 vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
794 * We can't mix 64K and 4K PTEs in the same page-table
795 * (2M block), and so to avoid the ugliness and
796 * complexity of coloring we opt for just aligning 64K
800 rounddown_pow_of_two(vma->page_sizes.sg |
801 I915_GTT_PAGE_SIZE_2M);
804 * Check we don't expand for the limited Global GTT
805 * (mappable aperture is even more precious!). This
806 * also checks that we exclude the aliasing-ppgtt.
808 GEM_BUG_ON(i915_vma_is_ggtt(vma));
810 alignment = max(alignment, page_alignment);
812 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
813 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
816 ret = i915_gem_gtt_insert(vma->vm, ww, &vma->node,
817 size, alignment, color,
822 GEM_BUG_ON(vma->node.start < start);
823 GEM_BUG_ON(vma->node.start + vma->node.size > end);
825 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
826 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, color));
828 list_add_tail(&vma->vm_link, &vma->vm->bound_list);
834 i915_vma_detach(struct i915_vma *vma)
836 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
837 GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
840 * And finally now the object is completely decoupled from this
841 * vma, we can drop its hold on the backing storage and allow
842 * it to be reaped by the shrinker.
844 list_del(&vma->vm_link);
847 static bool try_qad_pin(struct i915_vma *vma, unsigned int flags)
851 bound = atomic_read(&vma->flags);
853 if (flags & PIN_VALIDATE) {
854 flags &= I915_VMA_BIND_MASK;
856 return (flags & bound) == flags;
859 /* with the lock mandatory for unbind, we don't race here */
860 flags &= I915_VMA_BIND_MASK;
862 if (unlikely(flags & ~bound))
865 if (unlikely(bound & (I915_VMA_OVERFLOW | I915_VMA_ERROR)))
868 GEM_BUG_ON(((bound + 1) & I915_VMA_PIN_MASK) == 0);
869 } while (!atomic_try_cmpxchg(&vma->flags, &bound, bound + 1));
874 static struct scatterlist *
875 rotate_pages(struct drm_i915_gem_object *obj, unsigned int offset,
876 unsigned int width, unsigned int height,
877 unsigned int src_stride, unsigned int dst_stride,
878 struct sg_table *st, struct scatterlist *sg)
880 unsigned int column, row;
881 unsigned int src_idx;
883 for (column = 0; column < width; column++) {
886 src_idx = src_stride * (height - 1) + column + offset;
887 for (row = 0; row < height; row++) {
890 * We don't need the pages, but need to initialize
891 * the entries so the sg list can be happily traversed.
892 * The only thing we need are DMA addresses.
894 sg_set_page(sg, NULL, I915_GTT_PAGE_SIZE, 0);
896 i915_gem_object_get_dma_address(obj, src_idx);
897 sg_dma_len(sg) = I915_GTT_PAGE_SIZE;
899 src_idx -= src_stride;
902 left = (dst_stride - height) * I915_GTT_PAGE_SIZE;
910 * The DE ignores the PTEs for the padding tiles, the sg entry
911 * here is just a conenience to indicate how many padding PTEs
912 * to insert at this spot.
914 sg_set_page(sg, NULL, left, 0);
915 sg_dma_address(sg) = 0;
916 sg_dma_len(sg) = left;
923 static noinline struct sg_table *
924 intel_rotate_pages(struct intel_rotation_info *rot_info,
925 struct drm_i915_gem_object *obj)
927 unsigned int size = intel_rotation_info_size(rot_info);
928 struct drm_i915_private *i915 = to_i915(obj->base.dev);
930 struct scatterlist *sg;
934 /* Allocate target SG list. */
935 st = kmalloc(sizeof(*st), GFP_KERNEL);
939 ret = sg_alloc_table(st, size, GFP_KERNEL);
946 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
947 sg = rotate_pages(obj, rot_info->plane[i].offset,
948 rot_info->plane[i].width, rot_info->plane[i].height,
949 rot_info->plane[i].src_stride,
950 rot_info->plane[i].dst_stride,
959 drm_dbg(&i915->drm, "Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
960 obj->base.size, rot_info->plane[0].width,
961 rot_info->plane[0].height, size);
966 static struct scatterlist *
967 remap_pages(struct drm_i915_gem_object *obj,
968 unsigned int offset, unsigned int alignment_pad,
969 unsigned int width, unsigned int height,
970 unsigned int src_stride, unsigned int dst_stride,
971 struct sg_table *st, struct scatterlist *sg)
975 if (!width || !height)
982 * The DE ignores the PTEs for the padding tiles, the sg entry
983 * here is just a convenience to indicate how many padding PTEs
984 * to insert at this spot.
986 sg_set_page(sg, NULL, alignment_pad * 4096, 0);
987 sg_dma_address(sg) = 0;
988 sg_dma_len(sg) = alignment_pad * 4096;
992 for (row = 0; row < height; row++) {
993 unsigned int left = width * I915_GTT_PAGE_SIZE;
1000 * We don't need the pages, but need to initialize
1001 * the entries so the sg list can be happily traversed.
1002 * The only thing we need are DMA addresses.
1005 addr = i915_gem_object_get_dma_address_len(obj, offset, &length);
1007 length = min(left, length);
1011 sg_set_page(sg, NULL, length, 0);
1012 sg_dma_address(sg) = addr;
1013 sg_dma_len(sg) = length;
1016 offset += length / I915_GTT_PAGE_SIZE;
1020 offset += src_stride - width;
1022 left = (dst_stride - width) * I915_GTT_PAGE_SIZE;
1030 * The DE ignores the PTEs for the padding tiles, the sg entry
1031 * here is just a conenience to indicate how many padding PTEs
1032 * to insert at this spot.
1034 sg_set_page(sg, NULL, left, 0);
1035 sg_dma_address(sg) = 0;
1036 sg_dma_len(sg) = left;
1043 static noinline struct sg_table *
1044 intel_remap_pages(struct intel_remapped_info *rem_info,
1045 struct drm_i915_gem_object *obj)
1047 unsigned int size = intel_remapped_info_size(rem_info);
1048 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1049 struct sg_table *st;
1050 struct scatterlist *sg;
1051 unsigned int gtt_offset = 0;
1055 /* Allocate target SG list. */
1056 st = kmalloc(sizeof(*st), GFP_KERNEL);
1060 ret = sg_alloc_table(st, size, GFP_KERNEL);
1067 for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++) {
1068 unsigned int alignment_pad = 0;
1070 if (rem_info->plane_alignment)
1071 alignment_pad = ALIGN(gtt_offset, rem_info->plane_alignment) - gtt_offset;
1073 sg = remap_pages(obj,
1074 rem_info->plane[i].offset, alignment_pad,
1075 rem_info->plane[i].width, rem_info->plane[i].height,
1076 rem_info->plane[i].src_stride, rem_info->plane[i].dst_stride,
1079 gtt_offset += alignment_pad +
1080 rem_info->plane[i].dst_stride * rem_info->plane[i].height;
1091 drm_dbg(&i915->drm, "Failed to create remapped mapping for object size %zu! (%ux%u tiles, %u pages)\n",
1092 obj->base.size, rem_info->plane[0].width,
1093 rem_info->plane[0].height, size);
1095 return ERR_PTR(ret);
1098 static noinline struct sg_table *
1099 intel_partial_pages(const struct i915_ggtt_view *view,
1100 struct drm_i915_gem_object *obj)
1102 struct sg_table *st;
1103 struct scatterlist *sg, *iter;
1104 unsigned int count = view->partial.size;
1105 unsigned int offset;
1108 st = kmalloc(sizeof(*st), GFP_KERNEL);
1112 ret = sg_alloc_table(st, count, GFP_KERNEL);
1116 iter = i915_gem_object_get_sg_dma(obj, view->partial.offset, &offset);
1124 len = min(sg_dma_len(iter) - (offset << PAGE_SHIFT),
1125 count << PAGE_SHIFT);
1126 sg_set_page(sg, NULL, len, 0);
1127 sg_dma_address(sg) =
1128 sg_dma_address(iter) + (offset << PAGE_SHIFT);
1129 sg_dma_len(sg) = len;
1132 count -= len >> PAGE_SHIFT;
1135 i915_sg_trim(st); /* Drop any unused tail entries. */
1141 iter = __sg_next(iter);
1148 return ERR_PTR(ret);
1152 __i915_vma_get_pages(struct i915_vma *vma)
1154 struct sg_table *pages;
1157 * The vma->pages are only valid within the lifespan of the borrowed
1158 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
1159 * must be the vma->pages. A simple rule is that vma->pages must only
1160 * be accessed when the obj->mm.pages are pinned.
1162 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
1164 switch (vma->ggtt_view.type) {
1166 GEM_BUG_ON(vma->ggtt_view.type);
1168 case I915_GGTT_VIEW_NORMAL:
1169 pages = vma->obj->mm.pages;
1172 case I915_GGTT_VIEW_ROTATED:
1174 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
1177 case I915_GGTT_VIEW_REMAPPED:
1179 intel_remap_pages(&vma->ggtt_view.remapped, vma->obj);
1182 case I915_GGTT_VIEW_PARTIAL:
1183 pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
1187 if (IS_ERR(pages)) {
1188 drm_err(&vma->vm->i915->drm,
1189 "Failed to get pages for VMA view type %u (%ld)!\n",
1190 vma->ggtt_view.type, PTR_ERR(pages));
1191 return PTR_ERR(pages);
1199 I915_SELFTEST_EXPORT int i915_vma_get_pages(struct i915_vma *vma)
1203 if (atomic_add_unless(&vma->pages_count, 1, 0))
1206 err = i915_gem_object_pin_pages(vma->obj);
1210 err = __i915_vma_get_pages(vma);
1214 vma->page_sizes = vma->obj->mm.page_sizes;
1215 atomic_inc(&vma->pages_count);
1220 __i915_gem_object_unpin_pages(vma->obj);
1225 static void __vma_put_pages(struct i915_vma *vma, unsigned int count)
1227 /* We allocate under vma_get_pages, so beware the shrinker */
1228 GEM_BUG_ON(atomic_read(&vma->pages_count) < count);
1230 if (atomic_sub_return(count, &vma->pages_count) == 0) {
1231 if (vma->pages != vma->obj->mm.pages) {
1232 sg_free_table(vma->pages);
1237 i915_gem_object_unpin_pages(vma->obj);
1241 I915_SELFTEST_EXPORT void i915_vma_put_pages(struct i915_vma *vma)
1243 if (atomic_add_unless(&vma->pages_count, -1, 1))
1246 __vma_put_pages(vma, 1);
1249 static void vma_unbind_pages(struct i915_vma *vma)
1253 lockdep_assert_held(&vma->vm->mutex);
1255 /* The upper portion of pages_count is the number of bindings */
1256 count = atomic_read(&vma->pages_count);
1257 count >>= I915_VMA_PAGES_BIAS;
1260 __vma_put_pages(vma, count | count << I915_VMA_PAGES_BIAS);
1263 int i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
1264 u64 size, u64 alignment, u64 flags)
1266 struct i915_vma_work *work = NULL;
1267 struct dma_fence *moving = NULL;
1268 struct i915_vma_resource *vma_res = NULL;
1269 intel_wakeref_t wakeref = 0;
1273 assert_vma_held(vma);
1276 BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
1277 BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
1279 GEM_BUG_ON(!(flags & (PIN_USER | PIN_GLOBAL)));
1281 /* First try and grab the pin without rebinding the vma */
1282 if (try_qad_pin(vma, flags))
1285 err = i915_vma_get_pages(vma);
1289 if (flags & PIN_GLOBAL)
1290 wakeref = intel_runtime_pm_get(&vma->vm->i915->runtime_pm);
1292 moving = vma->obj ? i915_gem_object_get_moving_fence(vma->obj) : NULL;
1293 if (flags & vma->vm->bind_async_flags || moving) {
1295 err = i915_vm_lock_objects(vma->vm, ww);
1299 work = i915_vma_work();
1305 work->vm = i915_vm_get(vma->vm);
1307 dma_fence_work_chain(&work->base, moving);
1309 /* Allocate enough page directories to used PTE */
1310 if (vma->vm->allocate_va_range) {
1311 err = i915_vm_alloc_pt_stash(vma->vm,
1317 err = i915_vm_map_pt_stash(vma->vm, &work->stash);
1323 vma_res = i915_vma_resource_alloc();
1324 if (IS_ERR(vma_res)) {
1325 err = PTR_ERR(vma_res);
1330 * Differentiate between user/kernel vma inside the aliasing-ppgtt.
1332 * We conflate the Global GTT with the user's vma when using the
1333 * aliasing-ppgtt, but it is still vitally important to try and
1334 * keep the use cases distinct. For example, userptr objects are
1335 * not allowed inside the Global GTT as that will cause lock
1336 * inversions when we have to evict them the mmu_notifier callbacks -
1337 * but they are allowed to be part of the user ppGTT which can never
1338 * be mapped. As such we try to give the distinct users of the same
1339 * mutex, distinct lockclasses [equivalent to how we keep i915_ggtt
1340 * and i915_ppgtt separate].
1342 * NB this may cause us to mask real lock inversions -- while the
1343 * code is safe today, lockdep may not be able to spot future
1346 err = mutex_lock_interruptible_nested(&vma->vm->mutex,
1347 !(flags & PIN_GLOBAL));
1351 /* No more allocations allowed now we hold vm->mutex */
1353 if (unlikely(i915_vma_is_closed(vma))) {
1358 bound = atomic_read(&vma->flags);
1359 if (unlikely(bound & I915_VMA_ERROR)) {
1364 if (unlikely(!((bound + 1) & I915_VMA_PIN_MASK))) {
1365 err = -EAGAIN; /* pins are meant to be fairly temporary */
1369 if (unlikely(!(flags & ~bound & I915_VMA_BIND_MASK))) {
1370 if (!(flags & PIN_VALIDATE))
1371 __i915_vma_pin(vma);
1375 err = i915_active_acquire(&vma->active);
1379 if (!(bound & I915_VMA_BIND_MASK)) {
1380 err = i915_vma_insert(vma, ww, size, alignment, flags);
1384 if (i915_is_ggtt(vma->vm))
1385 __i915_vma_set_map_and_fenceable(vma);
1388 GEM_BUG_ON(!vma->pages);
1389 err = i915_vma_bind(vma,
1390 vma->obj->cache_level,
1391 flags, work, vma_res);
1396 /* There should only be at most 2 active bindings (user, global) */
1397 GEM_BUG_ON(bound + I915_VMA_PAGES_ACTIVE < bound);
1398 atomic_add(I915_VMA_PAGES_ACTIVE, &vma->pages_count);
1399 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
1401 if (!(flags & PIN_VALIDATE)) {
1402 __i915_vma_pin(vma);
1403 GEM_BUG_ON(!i915_vma_is_pinned(vma));
1405 GEM_BUG_ON(!i915_vma_is_bound(vma, flags));
1406 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
1409 if (!i915_vma_is_bound(vma, I915_VMA_BIND_MASK)) {
1410 i915_vma_detach(vma);
1411 drm_mm_remove_node(&vma->node);
1414 i915_active_release(&vma->active);
1416 mutex_unlock(&vma->vm->mutex);
1418 i915_vma_resource_free(vma_res);
1421 dma_fence_work_commit_imm(&work->base);
1424 intel_runtime_pm_put(&vma->vm->i915->runtime_pm, wakeref);
1427 dma_fence_put(moving);
1429 i915_vma_put_pages(vma);
1433 static void flush_idle_contexts(struct intel_gt *gt)
1435 struct intel_engine_cs *engine;
1436 enum intel_engine_id id;
1438 for_each_engine(engine, gt, id)
1439 intel_engine_flush_barriers(engine);
1441 intel_gt_wait_for_idle(gt, MAX_SCHEDULE_TIMEOUT);
1444 static int __i915_ggtt_pin(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
1445 u32 align, unsigned int flags)
1447 struct i915_address_space *vm = vma->vm;
1451 err = i915_vma_pin_ww(vma, ww, 0, align, flags | PIN_GLOBAL);
1453 if (err != -ENOSPC) {
1455 err = i915_vma_wait_for_bind(vma);
1457 i915_vma_unpin(vma);
1462 /* Unlike i915_vma_pin, we don't take no for an answer! */
1463 flush_idle_contexts(vm->gt);
1464 if (mutex_lock_interruptible(&vm->mutex) == 0) {
1466 * We pass NULL ww here, as we don't want to unbind
1467 * locked objects when called from execbuf when pinning
1468 * is removed. This would probably regress badly.
1470 i915_gem_evict_vm(vm, NULL);
1471 mutex_unlock(&vm->mutex);
1476 int i915_ggtt_pin(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
1477 u32 align, unsigned int flags)
1479 struct i915_gem_ww_ctx _ww;
1482 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
1485 return __i915_ggtt_pin(vma, ww, align, flags);
1487 #ifdef CONFIG_LOCKDEP
1488 WARN_ON(dma_resv_held(vma->obj->base.resv));
1491 for_i915_gem_ww(&_ww, err, true) {
1492 err = i915_gem_object_lock(vma->obj, &_ww);
1494 err = __i915_ggtt_pin(vma, &_ww, align, flags);
1500 static void __vma_close(struct i915_vma *vma, struct intel_gt *gt)
1503 * We defer actually closing, unbinding and destroying the VMA until
1504 * the next idle point, or if the object is freed in the meantime. By
1505 * postponing the unbind, we allow for it to be resurrected by the
1506 * client, avoiding the work required to rebind the VMA. This is
1507 * advantageous for DRI, where the client/server pass objects
1508 * between themselves, temporarily opening a local VMA to the
1509 * object, and then closing it again. The same object is then reused
1510 * on the next frame (or two, depending on the depth of the swap queue)
1511 * causing us to rebind the VMA once more. This ends up being a lot
1512 * of wasted work for the steady state.
1514 GEM_BUG_ON(i915_vma_is_closed(vma));
1515 list_add(&vma->closed_link, >->closed_vma);
1518 void i915_vma_close(struct i915_vma *vma)
1520 struct intel_gt *gt = vma->vm->gt;
1521 unsigned long flags;
1523 if (i915_vma_is_ggtt(vma))
1526 GEM_BUG_ON(!atomic_read(&vma->open_count));
1527 if (atomic_dec_and_lock_irqsave(&vma->open_count,
1530 __vma_close(vma, gt);
1531 spin_unlock_irqrestore(>->closed_lock, flags);
1535 static void __i915_vma_remove_closed(struct i915_vma *vma)
1537 struct intel_gt *gt = vma->vm->gt;
1539 spin_lock_irq(>->closed_lock);
1540 list_del_init(&vma->closed_link);
1541 spin_unlock_irq(>->closed_lock);
1544 void i915_vma_reopen(struct i915_vma *vma)
1546 if (i915_vma_is_closed(vma))
1547 __i915_vma_remove_closed(vma);
1550 void i915_vma_release(struct kref *ref)
1552 struct i915_vma *vma = container_of(ref, typeof(*vma), ref);
1553 struct drm_i915_gem_object *obj = vma->obj;
1555 if (drm_mm_node_allocated(&vma->node)) {
1556 mutex_lock(&vma->vm->mutex);
1557 atomic_and(~I915_VMA_PIN_MASK, &vma->flags);
1558 WARN_ON(__i915_vma_unbind(vma));
1559 mutex_unlock(&vma->vm->mutex);
1560 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
1562 GEM_BUG_ON(i915_vma_is_active(vma));
1564 spin_lock(&obj->vma.lock);
1565 list_del(&vma->obj_link);
1566 if (!RB_EMPTY_NODE(&vma->obj_node))
1567 rb_erase(&vma->obj_node, &obj->vma.tree);
1568 spin_unlock(&obj->vma.lock);
1570 __i915_vma_remove_closed(vma);
1571 i915_vm_put(vma->vm);
1573 i915_active_fini(&vma->active);
1574 GEM_WARN_ON(vma->resource);
1578 void i915_vma_parked(struct intel_gt *gt)
1580 struct i915_vma *vma, *next;
1583 spin_lock_irq(>->closed_lock);
1584 list_for_each_entry_safe(vma, next, >->closed_vma, closed_link) {
1585 struct drm_i915_gem_object *obj = vma->obj;
1586 struct i915_address_space *vm = vma->vm;
1588 /* XXX All to avoid keeping a reference on i915_vma itself */
1590 if (!kref_get_unless_zero(&obj->base.refcount))
1593 if (!i915_vm_tryopen(vm)) {
1594 i915_gem_object_put(obj);
1598 list_move(&vma->closed_link, &closed);
1600 spin_unlock_irq(>->closed_lock);
1602 /* As the GT is held idle, no vma can be reopened as we destroy them */
1603 list_for_each_entry_safe(vma, next, &closed, closed_link) {
1604 struct drm_i915_gem_object *obj = vma->obj;
1605 struct i915_address_space *vm = vma->vm;
1607 if (i915_gem_object_trylock(obj, NULL)) {
1608 INIT_LIST_HEAD(&vma->closed_link);
1609 __i915_vma_put(vma);
1610 i915_gem_object_unlock(obj);
1613 spin_lock_irq(>->closed_lock);
1614 list_add(&vma->closed_link, >->closed_vma);
1615 spin_unlock_irq(>->closed_lock);
1618 i915_gem_object_put(obj);
1623 static void __i915_vma_iounmap(struct i915_vma *vma)
1625 GEM_BUG_ON(i915_vma_is_pinned(vma));
1627 if (vma->iomap == NULL)
1630 io_mapping_unmap(vma->iomap);
1634 void i915_vma_revoke_mmap(struct i915_vma *vma)
1636 struct drm_vma_offset_node *node;
1639 if (!i915_vma_has_userfault(vma))
1642 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
1643 GEM_BUG_ON(!vma->obj->userfault_count);
1645 node = &vma->mmo->vma_node;
1646 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
1647 unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
1648 drm_vma_node_offset_addr(node) + vma_offset,
1652 i915_vma_unset_userfault(vma);
1653 if (!--vma->obj->userfault_count)
1654 list_del(&vma->obj->userfault_link);
1658 __i915_request_await_bind(struct i915_request *rq, struct i915_vma *vma)
1660 return __i915_request_await_exclusive(rq, &vma->active);
1663 static int __i915_vma_move_to_active(struct i915_vma *vma, struct i915_request *rq)
1667 /* Wait for the vma to be bound before we start! */
1668 err = __i915_request_await_bind(rq, vma);
1672 return i915_active_add_request(&vma->active, rq);
1675 int _i915_vma_move_to_active(struct i915_vma *vma,
1676 struct i915_request *rq,
1677 struct dma_fence *fence,
1680 struct drm_i915_gem_object *obj = vma->obj;
1683 assert_object_held(obj);
1685 GEM_BUG_ON(!vma->pages);
1687 err = __i915_vma_move_to_active(vma, rq);
1691 if (flags & EXEC_OBJECT_WRITE) {
1692 struct intel_frontbuffer *front;
1694 front = __intel_frontbuffer_get(obj);
1695 if (unlikely(front)) {
1696 if (intel_frontbuffer_invalidate(front, ORIGIN_CS))
1697 i915_active_add_request(&front->write, rq);
1698 intel_frontbuffer_put(front);
1702 dma_resv_add_excl_fence(vma->obj->base.resv, fence);
1703 obj->write_domain = I915_GEM_DOMAIN_RENDER;
1704 obj->read_domains = 0;
1707 if (!(flags & __EXEC_OBJECT_NO_RESERVE)) {
1708 err = dma_resv_reserve_shared(vma->obj->base.resv, 1);
1714 dma_resv_add_shared_fence(vma->obj->base.resv, fence);
1715 obj->write_domain = 0;
1719 if (flags & EXEC_OBJECT_NEEDS_FENCE && vma->fence)
1720 i915_active_add_request(&vma->fence->active, rq);
1722 obj->read_domains |= I915_GEM_GPU_DOMAINS;
1723 obj->mm.dirty = true;
1725 GEM_BUG_ON(!i915_vma_is_active(vma));
1729 struct dma_fence *__i915_vma_evict(struct i915_vma *vma, bool async)
1731 struct i915_vma_resource *vma_res = vma->resource;
1732 struct dma_fence *unbind_fence;
1734 GEM_BUG_ON(i915_vma_is_pinned(vma));
1735 assert_vma_held_evict(vma);
1737 if (i915_vma_is_map_and_fenceable(vma)) {
1738 /* Force a pagefault for domain tracking on next user access */
1739 i915_vma_revoke_mmap(vma);
1742 * Check that we have flushed all writes through the GGTT
1743 * before the unbind, other due to non-strict nature of those
1744 * indirect writes they may end up referencing the GGTT PTE
1747 * Note that we may be concurrently poking at the GGTT_WRITE
1748 * bit from set-domain, as we mark all GGTT vma associated
1749 * with an object. We know this is for another vma, as we
1750 * are currently unbinding this one -- so if this vma will be
1751 * reused, it will be refaulted and have its dirty bit set
1752 * before the next write.
1754 i915_vma_flush_writes(vma);
1756 /* release the fence reg _after_ flushing */
1757 i915_vma_revoke_fence(vma);
1759 __i915_vma_iounmap(vma);
1760 clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
1762 GEM_BUG_ON(vma->fence);
1763 GEM_BUG_ON(i915_vma_has_userfault(vma));
1765 /* Object backend must be async capable. */
1766 GEM_WARN_ON(async && !vma->resource->bi.pages_rsgt);
1768 /* If vm is not open, unbind is a nop. */
1769 vma_res->needs_wakeref = i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND) &&
1770 atomic_read(&vma->vm->open);
1771 trace_i915_vma_unbind(vma);
1773 unbind_fence = i915_vma_resource_unbind(vma_res);
1774 vma->resource = NULL;
1776 atomic_and(~(I915_VMA_BIND_MASK | I915_VMA_ERROR | I915_VMA_GGTT_WRITE),
1779 i915_vma_detach(vma);
1781 if (!async && unbind_fence) {
1782 dma_fence_wait(unbind_fence, false);
1783 dma_fence_put(unbind_fence);
1784 unbind_fence = NULL;
1788 * Binding itself may not have completed until the unbind fence signals,
1789 * so don't drop the pages until that happens, unless the resource is
1793 vma_unbind_pages(vma);
1794 return unbind_fence;
1797 int __i915_vma_unbind(struct i915_vma *vma)
1801 lockdep_assert_held(&vma->vm->mutex);
1802 assert_vma_held_evict(vma);
1804 if (!drm_mm_node_allocated(&vma->node))
1807 if (i915_vma_is_pinned(vma)) {
1808 vma_print_allocator(vma, "is pinned");
1813 * After confirming that no one else is pinning this vma, wait for
1814 * any laggards who may have crept in during the wait (through
1815 * a residual pin skipping the vm->mutex) to complete.
1817 ret = i915_vma_sync(vma);
1821 GEM_BUG_ON(i915_vma_is_active(vma));
1822 __i915_vma_evict(vma, false);
1824 drm_mm_remove_node(&vma->node); /* pairs with i915_vma_release() */
1828 static struct dma_fence *__i915_vma_unbind_async(struct i915_vma *vma)
1830 struct dma_fence *fence;
1832 lockdep_assert_held(&vma->vm->mutex);
1834 if (!drm_mm_node_allocated(&vma->node))
1837 if (i915_vma_is_pinned(vma) ||
1838 &vma->obj->mm.rsgt->table != vma->resource->bi.pages)
1839 return ERR_PTR(-EAGAIN);
1842 * We probably need to replace this with awaiting the fences of the
1843 * object's dma_resv when the vma active goes away. When doing that
1844 * we need to be careful to not add the vma_resource unbind fence
1845 * immediately to the object's dma_resv, because then unbinding
1846 * the next vma from the object, in case there are many, will
1847 * actually await the unbinding of the previous vmas, which is
1850 if (i915_sw_fence_await_active(&vma->resource->chain, &vma->active,
1851 I915_ACTIVE_AWAIT_EXCL |
1852 I915_ACTIVE_AWAIT_ACTIVE) < 0) {
1853 return ERR_PTR(-EBUSY);
1856 fence = __i915_vma_evict(vma, true);
1858 drm_mm_remove_node(&vma->node); /* pairs with i915_vma_release() */
1863 int i915_vma_unbind(struct i915_vma *vma)
1865 struct i915_address_space *vm = vma->vm;
1866 intel_wakeref_t wakeref = 0;
1869 assert_object_held_shared(vma->obj);
1871 /* Optimistic wait before taking the mutex */
1872 err = i915_vma_sync(vma);
1876 if (!drm_mm_node_allocated(&vma->node))
1879 if (i915_vma_is_pinned(vma)) {
1880 vma_print_allocator(vma, "is pinned");
1884 if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
1885 /* XXX not always required: nop_clear_range */
1886 wakeref = intel_runtime_pm_get(&vm->i915->runtime_pm);
1888 err = mutex_lock_interruptible_nested(&vma->vm->mutex, !wakeref);
1892 err = __i915_vma_unbind(vma);
1893 mutex_unlock(&vm->mutex);
1897 intel_runtime_pm_put(&vm->i915->runtime_pm, wakeref);
1901 int i915_vma_unbind_async(struct i915_vma *vma, bool trylock_vm)
1903 struct drm_i915_gem_object *obj = vma->obj;
1904 struct i915_address_space *vm = vma->vm;
1905 intel_wakeref_t wakeref = 0;
1906 struct dma_fence *fence;
1910 * We need the dma-resv lock since we add the
1911 * unbind fence to the dma-resv object.
1913 assert_object_held(obj);
1915 if (!drm_mm_node_allocated(&vma->node))
1918 if (i915_vma_is_pinned(vma)) {
1919 vma_print_allocator(vma, "is pinned");
1926 err = dma_resv_reserve_shared(obj->base.resv, 1);
1931 * It would be great if we could grab this wakeref from the
1932 * async unbind work if needed, but we can't because it uses
1933 * kmalloc and it's in the dma-fence signalling critical path.
1935 if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
1936 wakeref = intel_runtime_pm_get(&vm->i915->runtime_pm);
1938 if (trylock_vm && !mutex_trylock(&vm->mutex)) {
1941 } else if (!trylock_vm) {
1942 err = mutex_lock_interruptible_nested(&vm->mutex, !wakeref);
1947 fence = __i915_vma_unbind_async(vma);
1948 mutex_unlock(&vm->mutex);
1949 if (IS_ERR_OR_NULL(fence)) {
1950 err = PTR_ERR_OR_ZERO(fence);
1954 dma_resv_add_shared_fence(obj->base.resv, fence);
1955 dma_fence_put(fence);
1959 intel_runtime_pm_put(&vm->i915->runtime_pm, wakeref);
1963 int i915_vma_unbind_unlocked(struct i915_vma *vma)
1967 i915_gem_object_lock(vma->obj, NULL);
1968 err = i915_vma_unbind(vma);
1969 i915_gem_object_unlock(vma->obj);
1974 struct i915_vma *i915_vma_make_unshrinkable(struct i915_vma *vma)
1976 i915_gem_object_make_unshrinkable(vma->obj);
1980 void i915_vma_make_shrinkable(struct i915_vma *vma)
1982 i915_gem_object_make_shrinkable(vma->obj);
1985 void i915_vma_make_purgeable(struct i915_vma *vma)
1987 i915_gem_object_make_purgeable(vma->obj);
1990 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1991 #include "selftests/i915_vma.c"
1994 void i915_vma_module_exit(void)
1996 kmem_cache_destroy(slab_vmas);
1999 int __init i915_vma_module_init(void)
2001 slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);