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
28 #include "intel_ringbuffer.h"
29 #include "intel_frontbuffer.h"
31 #include <drm/drm_gem.h>
34 i915_vma_retire(struct i915_gem_active *active, struct i915_request *rq)
36 const unsigned int idx = rq->engine->id;
37 struct i915_vma *vma =
38 container_of(active, struct i915_vma, last_read[idx]);
39 struct drm_i915_gem_object *obj = vma->obj;
41 GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx));
43 i915_vma_clear_active(vma, idx);
44 if (i915_vma_is_active(vma))
47 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
48 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
49 if (unlikely(i915_vma_is_closed(vma) && !i915_vma_is_pinned(vma)))
50 WARN_ON(i915_vma_unbind(vma));
52 GEM_BUG_ON(!i915_gem_object_is_active(obj));
53 if (--obj->active_count)
56 /* Prune the shared fence arrays iff completely idle (inc. external) */
57 if (reservation_object_trylock(obj->resv)) {
58 if (reservation_object_test_signaled_rcu(obj->resv, true))
59 reservation_object_add_excl_fence(obj->resv, NULL);
60 reservation_object_unlock(obj->resv);
63 /* Bump our place on the bound list to keep it roughly in LRU order
64 * so that we don't steal from recently used but inactive objects
65 * (unless we are forced to ofc!)
67 spin_lock(&rq->i915->mm.obj_lock);
69 list_move_tail(&obj->mm.link, &rq->i915->mm.bound_list);
70 spin_unlock(&rq->i915->mm.obj_lock);
72 obj->mm.dirty = true; /* be paranoid */
74 if (i915_gem_object_has_active_reference(obj)) {
75 i915_gem_object_clear_active_reference(obj);
76 i915_gem_object_put(obj);
80 static struct i915_vma *
81 vma_create(struct drm_i915_gem_object *obj,
82 struct i915_address_space *vm,
83 const struct i915_ggtt_view *view)
86 struct rb_node *rb, **p;
89 /* The aliasing_ppgtt should never be used directly! */
90 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
92 vma = kmem_cache_zalloc(vm->i915->vmas, GFP_KERNEL);
94 return ERR_PTR(-ENOMEM);
96 for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
97 init_request_active(&vma->last_read[i], i915_vma_retire);
98 init_request_active(&vma->last_fence, NULL);
101 vma->resv = obj->resv;
102 vma->size = obj->base.size;
103 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
105 if (view && view->type != I915_GGTT_VIEW_NORMAL) {
106 vma->ggtt_view = *view;
107 if (view->type == I915_GGTT_VIEW_PARTIAL) {
108 GEM_BUG_ON(range_overflows_t(u64,
109 view->partial.offset,
111 obj->base.size >> PAGE_SHIFT));
112 vma->size = view->partial.size;
113 vma->size <<= PAGE_SHIFT;
114 GEM_BUG_ON(vma->size >= obj->base.size);
115 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
116 vma->size = intel_rotation_info_size(&view->rotated);
117 vma->size <<= PAGE_SHIFT;
121 if (unlikely(vma->size > vm->total))
124 GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
126 if (i915_is_ggtt(vm)) {
127 if (unlikely(overflows_type(vma->size, u32)))
130 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
131 i915_gem_object_get_tiling(obj),
132 i915_gem_object_get_stride(obj));
133 if (unlikely(vma->fence_size < vma->size || /* overflow */
134 vma->fence_size > vm->total))
137 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
139 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
140 i915_gem_object_get_tiling(obj),
141 i915_gem_object_get_stride(obj));
142 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
145 * We put the GGTT vma at the start of the vma-list, followed
146 * by the ppGGTT vma. This allows us to break early when
147 * iterating over only the GGTT vma for an object, see
148 * for_each_ggtt_vma()
150 vma->flags |= I915_VMA_GGTT;
151 list_add(&vma->obj_link, &obj->vma_list);
153 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
154 list_add_tail(&vma->obj_link, &obj->vma_list);
158 p = &obj->vma_tree.rb_node;
160 struct i915_vma *pos;
163 pos = rb_entry(rb, struct i915_vma, obj_node);
164 if (i915_vma_compare(pos, vm, view) < 0)
169 rb_link_node(&vma->obj_node, rb, p);
170 rb_insert_color(&vma->obj_node, &obj->vma_tree);
171 list_add(&vma->vm_link, &vm->unbound_list);
176 kmem_cache_free(vm->i915->vmas, vma);
177 return ERR_PTR(-E2BIG);
180 static struct i915_vma *
181 vma_lookup(struct drm_i915_gem_object *obj,
182 struct i915_address_space *vm,
183 const struct i915_ggtt_view *view)
187 rb = obj->vma_tree.rb_node;
189 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
192 cmp = i915_vma_compare(vma, vm, view);
206 * i915_vma_instance - return the singleton instance of the VMA
207 * @obj: parent &struct drm_i915_gem_object to be mapped
208 * @vm: address space in which the mapping is located
209 * @view: additional mapping requirements
211 * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
212 * the same @view characteristics. If a match is not found, one is created.
213 * Once created, the VMA is kept until either the object is freed, or the
214 * address space is closed.
216 * Must be called with struct_mutex held.
218 * Returns the vma, or an error pointer.
221 i915_vma_instance(struct drm_i915_gem_object *obj,
222 struct i915_address_space *vm,
223 const struct i915_ggtt_view *view)
225 struct i915_vma *vma;
227 lockdep_assert_held(&obj->base.dev->struct_mutex);
228 GEM_BUG_ON(view && !i915_is_ggtt(vm));
229 GEM_BUG_ON(vm->closed);
231 vma = vma_lookup(obj, vm, view);
233 vma = vma_create(obj, vm, view);
235 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_is_closed(vma));
236 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
237 GEM_BUG_ON(!IS_ERR(vma) && vma_lookup(obj, vm, view) != vma);
242 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
244 * @cache_level: mapping cache level
245 * @flags: flags like global or local mapping
247 * DMA addresses are taken from the scatter-gather table of this object (or of
248 * this VMA in case of non-default GGTT views) and PTE entries set up.
249 * Note that DMA addresses are also the only part of the SG table we care about.
251 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
258 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
259 GEM_BUG_ON(vma->size > vma->node.size);
261 if (GEM_WARN_ON(range_overflows(vma->node.start,
266 if (GEM_WARN_ON(!flags))
270 if (flags & PIN_GLOBAL)
271 bind_flags |= I915_VMA_GLOBAL_BIND;
272 if (flags & PIN_USER)
273 bind_flags |= I915_VMA_LOCAL_BIND;
275 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
276 if (flags & PIN_UPDATE)
277 bind_flags |= vma_flags;
279 bind_flags &= ~vma_flags;
283 GEM_BUG_ON(!vma->pages);
285 trace_i915_vma_bind(vma, bind_flags);
286 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
290 vma->flags |= bind_flags;
294 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
299 /* Access through the GTT requires the device to be awake. */
300 assert_rpm_wakelock_held(vma->vm->i915);
302 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
303 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
308 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
309 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
313 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
326 err = i915_vma_pin_fence(vma);
330 i915_vma_set_ggtt_write(vma);
334 __i915_vma_unpin(vma);
336 return IO_ERR_PTR(err);
339 void i915_vma_flush_writes(struct i915_vma *vma)
341 if (!i915_vma_has_ggtt_write(vma))
344 i915_gem_flush_ggtt_writes(vma->vm->i915);
346 i915_vma_unset_ggtt_write(vma);
349 void i915_vma_unpin_iomap(struct i915_vma *vma)
351 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
353 GEM_BUG_ON(vma->iomap == NULL);
355 i915_vma_flush_writes(vma);
357 i915_vma_unpin_fence(vma);
361 void i915_vma_unpin_and_release(struct i915_vma **p_vma)
363 struct i915_vma *vma;
364 struct drm_i915_gem_object *obj;
366 vma = fetch_and_zero(p_vma);
375 __i915_gem_object_release_unless_active(obj);
378 bool i915_vma_misplaced(const struct i915_vma *vma,
379 u64 size, u64 alignment, u64 flags)
381 if (!drm_mm_node_allocated(&vma->node))
384 if (vma->node.size < size)
387 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
388 if (alignment && !IS_ALIGNED(vma->node.start, alignment))
391 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
394 if (flags & PIN_OFFSET_BIAS &&
395 vma->node.start < (flags & PIN_OFFSET_MASK))
398 if (flags & PIN_OFFSET_FIXED &&
399 vma->node.start != (flags & PIN_OFFSET_MASK))
405 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
407 bool mappable, fenceable;
409 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
410 GEM_BUG_ON(!vma->fence_size);
413 * Explicitly disable for rotated VMA since the display does not
414 * need the fence and the VMA is not accessible to other users.
416 if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
419 fenceable = (vma->node.size >= vma->fence_size &&
420 IS_ALIGNED(vma->node.start, vma->fence_alignment));
422 mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
424 if (mappable && fenceable)
425 vma->flags |= I915_VMA_CAN_FENCE;
427 vma->flags &= ~I915_VMA_CAN_FENCE;
430 static bool color_differs(struct drm_mm_node *node, unsigned long color)
432 return node->allocated && node->color != color;
435 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
437 struct drm_mm_node *node = &vma->node;
438 struct drm_mm_node *other;
441 * On some machines we have to be careful when putting differing types
442 * of snoopable memory together to avoid the prefetcher crossing memory
443 * domains and dying. During vm initialisation, we decide whether or not
444 * these constraints apply and set the drm_mm.color_adjust
447 if (vma->vm->mm.color_adjust == NULL)
450 /* Only valid to be called on an already inserted vma */
451 GEM_BUG_ON(!drm_mm_node_allocated(node));
452 GEM_BUG_ON(list_empty(&node->node_list));
454 other = list_prev_entry(node, node_list);
455 if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
458 other = list_next_entry(node, node_list);
459 if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
466 * i915_vma_insert - finds a slot for the vma in its address space
468 * @size: requested size in bytes (can be larger than the VMA)
469 * @alignment: required alignment
470 * @flags: mask of PIN_* flags to use
472 * First we try to allocate some free space that meets the requirements for
473 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
474 * preferrably the oldest idle entry to make room for the new VMA.
477 * 0 on success, negative error code otherwise.
480 i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
482 struct drm_i915_private *dev_priv = vma->vm->i915;
483 struct drm_i915_gem_object *obj = vma->obj;
487 GEM_BUG_ON(i915_vma_is_closed(vma));
488 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
489 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
491 size = max(size, vma->size);
492 alignment = max(alignment, vma->display_alignment);
493 if (flags & PIN_MAPPABLE) {
494 size = max_t(typeof(size), size, vma->fence_size);
495 alignment = max_t(typeof(alignment),
496 alignment, vma->fence_alignment);
499 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
500 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
501 GEM_BUG_ON(!is_power_of_2(alignment));
503 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
504 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
506 end = vma->vm->total;
507 if (flags & PIN_MAPPABLE)
508 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
509 if (flags & PIN_ZONE_4G)
510 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
511 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
513 /* If binding the object/GGTT view requires more space than the entire
514 * aperture has, reject it early before evicting everything in a vain
515 * attempt to find space.
518 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n",
519 size, obj->base.size,
520 flags & PIN_MAPPABLE ? "mappable" : "total",
525 ret = i915_gem_object_pin_pages(obj);
529 GEM_BUG_ON(vma->pages);
531 ret = vma->vm->set_pages(vma);
535 if (flags & PIN_OFFSET_FIXED) {
536 u64 offset = flags & PIN_OFFSET_MASK;
537 if (!IS_ALIGNED(offset, alignment) ||
538 range_overflows(offset, size, end)) {
543 ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
544 size, offset, obj->cache_level,
550 * We only support huge gtt pages through the 48b PPGTT,
551 * however we also don't want to force any alignment for
552 * objects which need to be tightly packed into the low 32bits.
554 * Note that we assume that GGTT are limited to 4GiB for the
555 * forseeable future. See also i915_ggtt_offset().
557 if (upper_32_bits(end - 1) &&
558 vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
560 * We can't mix 64K and 4K PTEs in the same page-table
561 * (2M block), and so to avoid the ugliness and
562 * complexity of coloring we opt for just aligning 64K
566 rounddown_pow_of_two(vma->page_sizes.sg |
567 I915_GTT_PAGE_SIZE_2M);
570 * Check we don't expand for the limited Global GTT
571 * (mappable aperture is even more precious!). This
572 * also checks that we exclude the aliasing-ppgtt.
574 GEM_BUG_ON(i915_vma_is_ggtt(vma));
576 alignment = max(alignment, page_alignment);
578 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
579 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
582 ret = i915_gem_gtt_insert(vma->vm, &vma->node,
583 size, alignment, obj->cache_level,
588 GEM_BUG_ON(vma->node.start < start);
589 GEM_BUG_ON(vma->node.start + vma->node.size > end);
591 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
592 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
594 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
596 spin_lock(&dev_priv->mm.obj_lock);
597 list_move_tail(&obj->mm.link, &dev_priv->mm.bound_list);
599 spin_unlock(&dev_priv->mm.obj_lock);
601 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
606 vma->vm->clear_pages(vma);
608 i915_gem_object_unpin_pages(obj);
613 i915_vma_remove(struct i915_vma *vma)
615 struct drm_i915_private *i915 = vma->vm->i915;
616 struct drm_i915_gem_object *obj = vma->obj;
618 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
619 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
621 vma->vm->clear_pages(vma);
623 drm_mm_remove_node(&vma->node);
624 list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
626 /* Since the unbound list is global, only move to that list if
627 * no more VMAs exist.
629 spin_lock(&i915->mm.obj_lock);
630 if (--obj->bind_count == 0)
631 list_move_tail(&obj->mm.link, &i915->mm.unbound_list);
632 spin_unlock(&i915->mm.obj_lock);
634 /* And finally now the object is completely decoupled from this vma,
635 * we can drop its hold on the backing storage and allow it to be
636 * reaped by the shrinker.
638 i915_gem_object_unpin_pages(obj);
639 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
642 int __i915_vma_do_pin(struct i915_vma *vma,
643 u64 size, u64 alignment, u64 flags)
645 const unsigned int bound = vma->flags;
648 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
649 GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
650 GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
652 if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
657 if ((bound & I915_VMA_BIND_MASK) == 0) {
658 ret = i915_vma_insert(vma, size, alignment, flags);
662 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
664 ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
668 GEM_BUG_ON((vma->flags & I915_VMA_BIND_MASK) == 0);
670 if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
671 __i915_vma_set_map_and_fenceable(vma);
673 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
677 if ((bound & I915_VMA_BIND_MASK) == 0) {
678 i915_vma_remove(vma);
679 GEM_BUG_ON(vma->pages);
680 GEM_BUG_ON(vma->flags & I915_VMA_BIND_MASK);
683 __i915_vma_unpin(vma);
687 static void i915_vma_destroy(struct i915_vma *vma)
691 GEM_BUG_ON(vma->node.allocated);
692 GEM_BUG_ON(i915_vma_is_active(vma));
693 GEM_BUG_ON(!i915_vma_is_closed(vma));
694 GEM_BUG_ON(vma->fence);
696 for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
697 GEM_BUG_ON(i915_gem_active_isset(&vma->last_read[i]));
698 GEM_BUG_ON(i915_gem_active_isset(&vma->last_fence));
700 list_del(&vma->obj_link);
701 list_del(&vma->vm_link);
703 if (!i915_vma_is_ggtt(vma))
704 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
706 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
709 void i915_vma_close(struct i915_vma *vma)
711 GEM_BUG_ON(i915_vma_is_closed(vma));
712 vma->flags |= I915_VMA_CLOSED;
714 rb_erase(&vma->obj_node, &vma->obj->vma_tree);
716 if (!i915_vma_is_active(vma) && !i915_vma_is_pinned(vma))
717 WARN_ON(i915_vma_unbind(vma));
720 static void __i915_vma_iounmap(struct i915_vma *vma)
722 GEM_BUG_ON(i915_vma_is_pinned(vma));
724 if (vma->iomap == NULL)
727 io_mapping_unmap(vma->iomap);
731 void i915_vma_revoke_mmap(struct i915_vma *vma)
733 struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
736 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
738 if (!i915_vma_has_userfault(vma))
741 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
742 GEM_BUG_ON(!vma->obj->userfault_count);
744 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
745 unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
746 drm_vma_node_offset_addr(node) + vma_offset,
750 i915_vma_unset_userfault(vma);
751 if (!--vma->obj->userfault_count)
752 list_del(&vma->obj->userfault_link);
755 int i915_vma_unbind(struct i915_vma *vma)
757 struct drm_i915_gem_object *obj = vma->obj;
758 unsigned long active;
761 lockdep_assert_held(&obj->base.dev->struct_mutex);
763 /* First wait upon any activity as retiring the request may
764 * have side-effects such as unpinning or even unbinding this vma.
767 active = i915_vma_get_active(vma);
771 /* When a closed VMA is retired, it is unbound - eek.
772 * In order to prevent it from being recursively closed,
773 * take a pin on the vma so that the second unbind is
776 * Even more scary is that the retire callback may free
777 * the object (last active vma). To prevent the explosion
778 * we defer the actual object free to a worker that can
779 * only proceed once it acquires the struct_mutex (which
780 * we currently hold, therefore it cannot free this object
781 * before we are finished).
785 for_each_active(active, idx) {
786 ret = i915_gem_active_retire(&vma->last_read[idx],
787 &vma->vm->i915->drm.struct_mutex);
793 ret = i915_gem_active_retire(&vma->last_fence,
794 &vma->vm->i915->drm.struct_mutex);
797 __i915_vma_unpin(vma);
801 GEM_BUG_ON(i915_vma_is_active(vma));
803 if (i915_vma_is_pinned(vma))
806 if (!drm_mm_node_allocated(&vma->node))
809 GEM_BUG_ON(obj->bind_count == 0);
810 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
812 if (i915_vma_is_map_and_fenceable(vma)) {
814 * Check that we have flushed all writes through the GGTT
815 * before the unbind, other due to non-strict nature of those
816 * indirect writes they may end up referencing the GGTT PTE
819 i915_vma_flush_writes(vma);
820 GEM_BUG_ON(i915_vma_has_ggtt_write(vma));
822 /* release the fence reg _after_ flushing */
823 ret = i915_vma_put_fence(vma);
827 /* Force a pagefault for domain tracking on next user access */
828 i915_vma_revoke_mmap(vma);
830 __i915_vma_iounmap(vma);
831 vma->flags &= ~I915_VMA_CAN_FENCE;
833 GEM_BUG_ON(vma->fence);
834 GEM_BUG_ON(i915_vma_has_userfault(vma));
836 if (likely(!vma->vm->closed)) {
837 trace_i915_vma_unbind(vma);
838 vma->vm->unbind_vma(vma);
840 vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
842 i915_vma_remove(vma);
845 if (unlikely(i915_vma_is_closed(vma)))
846 i915_vma_destroy(vma);
851 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
852 #include "selftests/i915_vma.c"