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);
50 GEM_BUG_ON(!i915_gem_object_is_active(obj));
51 if (--obj->active_count)
54 /* Prune the shared fence arrays iff completely idle (inc. external) */
55 if (reservation_object_trylock(obj->resv)) {
56 if (reservation_object_test_signaled_rcu(obj->resv, true))
57 reservation_object_add_excl_fence(obj->resv, NULL);
58 reservation_object_unlock(obj->resv);
61 /* Bump our place on the bound list to keep it roughly in LRU order
62 * so that we don't steal from recently used but inactive objects
63 * (unless we are forced to ofc!)
65 spin_lock(&rq->i915->mm.obj_lock);
67 list_move_tail(&obj->mm.link, &rq->i915->mm.bound_list);
68 spin_unlock(&rq->i915->mm.obj_lock);
70 obj->mm.dirty = true; /* be paranoid */
72 if (i915_gem_object_has_active_reference(obj)) {
73 i915_gem_object_clear_active_reference(obj);
74 i915_gem_object_put(obj);
78 static struct i915_vma *
79 vma_create(struct drm_i915_gem_object *obj,
80 struct i915_address_space *vm,
81 const struct i915_ggtt_view *view)
84 struct rb_node *rb, **p;
87 /* The aliasing_ppgtt should never be used directly! */
88 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
90 vma = kmem_cache_zalloc(vm->i915->vmas, GFP_KERNEL);
92 return ERR_PTR(-ENOMEM);
94 for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
95 init_request_active(&vma->last_read[i], i915_vma_retire);
96 init_request_active(&vma->last_fence, NULL);
99 vma->resv = obj->resv;
100 vma->size = obj->base.size;
101 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
103 if (view && view->type != I915_GGTT_VIEW_NORMAL) {
104 vma->ggtt_view = *view;
105 if (view->type == I915_GGTT_VIEW_PARTIAL) {
106 GEM_BUG_ON(range_overflows_t(u64,
107 view->partial.offset,
109 obj->base.size >> PAGE_SHIFT));
110 vma->size = view->partial.size;
111 vma->size <<= PAGE_SHIFT;
112 GEM_BUG_ON(vma->size >= obj->base.size);
113 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
114 vma->size = intel_rotation_info_size(&view->rotated);
115 vma->size <<= PAGE_SHIFT;
119 if (unlikely(vma->size > vm->total))
122 GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
124 if (i915_is_ggtt(vm)) {
125 if (unlikely(overflows_type(vma->size, u32)))
128 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
129 i915_gem_object_get_tiling(obj),
130 i915_gem_object_get_stride(obj));
131 if (unlikely(vma->fence_size < vma->size || /* overflow */
132 vma->fence_size > vm->total))
135 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
137 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
138 i915_gem_object_get_tiling(obj),
139 i915_gem_object_get_stride(obj));
140 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
143 * We put the GGTT vma at the start of the vma-list, followed
144 * by the ppGGTT vma. This allows us to break early when
145 * iterating over only the GGTT vma for an object, see
146 * for_each_ggtt_vma()
148 vma->flags |= I915_VMA_GGTT;
149 list_add(&vma->obj_link, &obj->vma_list);
151 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
152 list_add_tail(&vma->obj_link, &obj->vma_list);
156 p = &obj->vma_tree.rb_node;
158 struct i915_vma *pos;
161 pos = rb_entry(rb, struct i915_vma, obj_node);
162 if (i915_vma_compare(pos, vm, view) < 0)
167 rb_link_node(&vma->obj_node, rb, p);
168 rb_insert_color(&vma->obj_node, &obj->vma_tree);
169 list_add(&vma->vm_link, &vm->unbound_list);
174 kmem_cache_free(vm->i915->vmas, vma);
175 return ERR_PTR(-E2BIG);
178 static struct i915_vma *
179 vma_lookup(struct drm_i915_gem_object *obj,
180 struct i915_address_space *vm,
181 const struct i915_ggtt_view *view)
185 rb = obj->vma_tree.rb_node;
187 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
190 cmp = i915_vma_compare(vma, vm, view);
204 * i915_vma_instance - return the singleton instance of the VMA
205 * @obj: parent &struct drm_i915_gem_object to be mapped
206 * @vm: address space in which the mapping is located
207 * @view: additional mapping requirements
209 * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
210 * the same @view characteristics. If a match is not found, one is created.
211 * Once created, the VMA is kept until either the object is freed, or the
212 * address space is closed.
214 * Must be called with struct_mutex held.
216 * Returns the vma, or an error pointer.
219 i915_vma_instance(struct drm_i915_gem_object *obj,
220 struct i915_address_space *vm,
221 const struct i915_ggtt_view *view)
223 struct i915_vma *vma;
225 lockdep_assert_held(&obj->base.dev->struct_mutex);
226 GEM_BUG_ON(view && !i915_is_ggtt(vm));
227 GEM_BUG_ON(vm->closed);
229 vma = vma_lookup(obj, vm, view);
231 vma = vma_create(obj, vm, view);
233 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
234 GEM_BUG_ON(!IS_ERR(vma) && vma_lookup(obj, vm, view) != vma);
239 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
241 * @cache_level: mapping cache level
242 * @flags: flags like global or local mapping
244 * DMA addresses are taken from the scatter-gather table of this object (or of
245 * this VMA in case of non-default GGTT views) and PTE entries set up.
246 * Note that DMA addresses are also the only part of the SG table we care about.
248 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
255 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
256 GEM_BUG_ON(vma->size > vma->node.size);
258 if (GEM_WARN_ON(range_overflows(vma->node.start,
263 if (GEM_WARN_ON(!flags))
267 if (flags & PIN_GLOBAL)
268 bind_flags |= I915_VMA_GLOBAL_BIND;
269 if (flags & PIN_USER)
270 bind_flags |= I915_VMA_LOCAL_BIND;
272 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
273 if (flags & PIN_UPDATE)
274 bind_flags |= vma_flags;
276 bind_flags &= ~vma_flags;
280 GEM_BUG_ON(!vma->pages);
282 trace_i915_vma_bind(vma, bind_flags);
283 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
287 vma->flags |= bind_flags;
291 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
296 /* Access through the GTT requires the device to be awake. */
297 assert_rpm_wakelock_held(vma->vm->i915);
299 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
300 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
305 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
306 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
310 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
323 err = i915_vma_pin_fence(vma);
327 i915_vma_set_ggtt_write(vma);
331 __i915_vma_unpin(vma);
333 return IO_ERR_PTR(err);
336 void i915_vma_flush_writes(struct i915_vma *vma)
338 if (!i915_vma_has_ggtt_write(vma))
341 i915_gem_flush_ggtt_writes(vma->vm->i915);
343 i915_vma_unset_ggtt_write(vma);
346 void i915_vma_unpin_iomap(struct i915_vma *vma)
348 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
350 GEM_BUG_ON(vma->iomap == NULL);
352 i915_vma_flush_writes(vma);
354 i915_vma_unpin_fence(vma);
358 void i915_vma_unpin_and_release(struct i915_vma **p_vma)
360 struct i915_vma *vma;
361 struct drm_i915_gem_object *obj;
363 vma = fetch_and_zero(p_vma);
372 __i915_gem_object_release_unless_active(obj);
375 bool i915_vma_misplaced(const struct i915_vma *vma,
376 u64 size, u64 alignment, u64 flags)
378 if (!drm_mm_node_allocated(&vma->node))
381 if (vma->node.size < size)
384 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
385 if (alignment && !IS_ALIGNED(vma->node.start, alignment))
388 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
391 if (flags & PIN_OFFSET_BIAS &&
392 vma->node.start < (flags & PIN_OFFSET_MASK))
395 if (flags & PIN_OFFSET_FIXED &&
396 vma->node.start != (flags & PIN_OFFSET_MASK))
402 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
404 bool mappable, fenceable;
406 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
407 GEM_BUG_ON(!vma->fence_size);
410 * Explicitly disable for rotated VMA since the display does not
411 * need the fence and the VMA is not accessible to other users.
413 if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
416 fenceable = (vma->node.size >= vma->fence_size &&
417 IS_ALIGNED(vma->node.start, vma->fence_alignment));
419 mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
421 if (mappable && fenceable)
422 vma->flags |= I915_VMA_CAN_FENCE;
424 vma->flags &= ~I915_VMA_CAN_FENCE;
427 static bool color_differs(struct drm_mm_node *node, unsigned long color)
429 return node->allocated && node->color != color;
432 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
434 struct drm_mm_node *node = &vma->node;
435 struct drm_mm_node *other;
438 * On some machines we have to be careful when putting differing types
439 * of snoopable memory together to avoid the prefetcher crossing memory
440 * domains and dying. During vm initialisation, we decide whether or not
441 * these constraints apply and set the drm_mm.color_adjust
444 if (vma->vm->mm.color_adjust == NULL)
447 /* Only valid to be called on an already inserted vma */
448 GEM_BUG_ON(!drm_mm_node_allocated(node));
449 GEM_BUG_ON(list_empty(&node->node_list));
451 other = list_prev_entry(node, node_list);
452 if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
455 other = list_next_entry(node, node_list);
456 if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
463 * i915_vma_insert - finds a slot for the vma in its address space
465 * @size: requested size in bytes (can be larger than the VMA)
466 * @alignment: required alignment
467 * @flags: mask of PIN_* flags to use
469 * First we try to allocate some free space that meets the requirements for
470 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
471 * preferrably the oldest idle entry to make room for the new VMA.
474 * 0 on success, negative error code otherwise.
477 i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
479 struct drm_i915_private *dev_priv = vma->vm->i915;
480 struct drm_i915_gem_object *obj = vma->obj;
484 GEM_BUG_ON(i915_vma_is_closed(vma));
485 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
486 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
488 size = max(size, vma->size);
489 alignment = max(alignment, vma->display_alignment);
490 if (flags & PIN_MAPPABLE) {
491 size = max_t(typeof(size), size, vma->fence_size);
492 alignment = max_t(typeof(alignment),
493 alignment, vma->fence_alignment);
496 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
497 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
498 GEM_BUG_ON(!is_power_of_2(alignment));
500 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
501 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
503 end = vma->vm->total;
504 if (flags & PIN_MAPPABLE)
505 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
506 if (flags & PIN_ZONE_4G)
507 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
508 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
510 /* If binding the object/GGTT view requires more space than the entire
511 * aperture has, reject it early before evicting everything in a vain
512 * attempt to find space.
515 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n",
516 size, obj->base.size,
517 flags & PIN_MAPPABLE ? "mappable" : "total",
522 ret = i915_gem_object_pin_pages(obj);
526 GEM_BUG_ON(vma->pages);
528 ret = vma->vm->set_pages(vma);
532 if (flags & PIN_OFFSET_FIXED) {
533 u64 offset = flags & PIN_OFFSET_MASK;
534 if (!IS_ALIGNED(offset, alignment) ||
535 range_overflows(offset, size, end)) {
540 ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
541 size, offset, obj->cache_level,
547 * We only support huge gtt pages through the 48b PPGTT,
548 * however we also don't want to force any alignment for
549 * objects which need to be tightly packed into the low 32bits.
551 * Note that we assume that GGTT are limited to 4GiB for the
552 * forseeable future. See also i915_ggtt_offset().
554 if (upper_32_bits(end - 1) &&
555 vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
557 * We can't mix 64K and 4K PTEs in the same page-table
558 * (2M block), and so to avoid the ugliness and
559 * complexity of coloring we opt for just aligning 64K
563 rounddown_pow_of_two(vma->page_sizes.sg |
564 I915_GTT_PAGE_SIZE_2M);
567 * Check we don't expand for the limited Global GTT
568 * (mappable aperture is even more precious!). This
569 * also checks that we exclude the aliasing-ppgtt.
571 GEM_BUG_ON(i915_vma_is_ggtt(vma));
573 alignment = max(alignment, page_alignment);
575 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
576 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
579 ret = i915_gem_gtt_insert(vma->vm, &vma->node,
580 size, alignment, obj->cache_level,
585 GEM_BUG_ON(vma->node.start < start);
586 GEM_BUG_ON(vma->node.start + vma->node.size > end);
588 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
589 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
591 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
593 spin_lock(&dev_priv->mm.obj_lock);
594 list_move_tail(&obj->mm.link, &dev_priv->mm.bound_list);
596 spin_unlock(&dev_priv->mm.obj_lock);
598 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
603 vma->vm->clear_pages(vma);
605 i915_gem_object_unpin_pages(obj);
610 i915_vma_remove(struct i915_vma *vma)
612 struct drm_i915_private *i915 = vma->vm->i915;
613 struct drm_i915_gem_object *obj = vma->obj;
615 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
616 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
618 vma->vm->clear_pages(vma);
620 drm_mm_remove_node(&vma->node);
621 list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
623 /* Since the unbound list is global, only move to that list if
624 * no more VMAs exist.
626 spin_lock(&i915->mm.obj_lock);
627 if (--obj->bind_count == 0)
628 list_move_tail(&obj->mm.link, &i915->mm.unbound_list);
629 spin_unlock(&i915->mm.obj_lock);
631 /* And finally now the object is completely decoupled from this vma,
632 * we can drop its hold on the backing storage and allow it to be
633 * reaped by the shrinker.
635 i915_gem_object_unpin_pages(obj);
636 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
639 int __i915_vma_do_pin(struct i915_vma *vma,
640 u64 size, u64 alignment, u64 flags)
642 const unsigned int bound = vma->flags;
645 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
646 GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
647 GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
649 if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
654 if ((bound & I915_VMA_BIND_MASK) == 0) {
655 ret = i915_vma_insert(vma, size, alignment, flags);
659 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
661 ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
665 GEM_BUG_ON((vma->flags & I915_VMA_BIND_MASK) == 0);
667 if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
668 __i915_vma_set_map_and_fenceable(vma);
670 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
674 if ((bound & I915_VMA_BIND_MASK) == 0) {
675 i915_vma_remove(vma);
676 GEM_BUG_ON(vma->pages);
677 GEM_BUG_ON(vma->flags & I915_VMA_BIND_MASK);
680 __i915_vma_unpin(vma);
684 void i915_vma_close(struct i915_vma *vma)
686 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
688 GEM_BUG_ON(i915_vma_is_closed(vma));
689 vma->flags |= I915_VMA_CLOSED;
692 * We defer actually closing, unbinding and destroying the VMA until
693 * the next idle point, or if the object is freed in the meantime. By
694 * postponing the unbind, we allow for it to be resurrected by the
695 * client, avoiding the work required to rebind the VMA. This is
696 * advantageous for DRI, where the client/server pass objects
697 * between themselves, temporarily opening a local VMA to the
698 * object, and then closing it again. The same object is then reused
699 * on the next frame (or two, depending on the depth of the swap queue)
700 * causing us to rebind the VMA once more. This ends up being a lot
701 * of wasted work for the steady state.
703 list_add_tail(&vma->closed_link, &vma->vm->i915->gt.closed_vma);
706 void i915_vma_reopen(struct i915_vma *vma)
708 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
710 if (vma->flags & I915_VMA_CLOSED) {
711 vma->flags &= ~I915_VMA_CLOSED;
712 list_del(&vma->closed_link);
716 static void __i915_vma_destroy(struct i915_vma *vma)
720 GEM_BUG_ON(vma->node.allocated);
721 GEM_BUG_ON(vma->fence);
723 for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
724 GEM_BUG_ON(i915_gem_active_isset(&vma->last_read[i]));
725 GEM_BUG_ON(i915_gem_active_isset(&vma->last_fence));
727 list_del(&vma->obj_link);
728 list_del(&vma->vm_link);
729 rb_erase(&vma->obj_node, &vma->obj->vma_tree);
731 if (!i915_vma_is_ggtt(vma))
732 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
734 kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
737 void i915_vma_destroy(struct i915_vma *vma)
739 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
741 GEM_BUG_ON(i915_vma_is_active(vma));
742 GEM_BUG_ON(i915_vma_is_pinned(vma));
744 if (i915_vma_is_closed(vma))
745 list_del(&vma->closed_link);
747 WARN_ON(i915_vma_unbind(vma));
748 __i915_vma_destroy(vma);
751 void i915_vma_parked(struct drm_i915_private *i915)
753 struct i915_vma *vma, *next;
755 list_for_each_entry_safe(vma, next, &i915->gt.closed_vma, closed_link) {
756 GEM_BUG_ON(!i915_vma_is_closed(vma));
757 i915_vma_destroy(vma);
760 GEM_BUG_ON(!list_empty(&i915->gt.closed_vma));
763 static void __i915_vma_iounmap(struct i915_vma *vma)
765 GEM_BUG_ON(i915_vma_is_pinned(vma));
767 if (vma->iomap == NULL)
770 io_mapping_unmap(vma->iomap);
774 void i915_vma_revoke_mmap(struct i915_vma *vma)
776 struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
779 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
781 if (!i915_vma_has_userfault(vma))
784 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
785 GEM_BUG_ON(!vma->obj->userfault_count);
787 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
788 unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
789 drm_vma_node_offset_addr(node) + vma_offset,
793 i915_vma_unset_userfault(vma);
794 if (!--vma->obj->userfault_count)
795 list_del(&vma->obj->userfault_link);
798 int i915_vma_unbind(struct i915_vma *vma)
800 struct drm_i915_gem_object *obj = vma->obj;
801 unsigned long active;
804 lockdep_assert_held(&obj->base.dev->struct_mutex);
806 /* First wait upon any activity as retiring the request may
807 * have side-effects such as unpinning or even unbinding this vma.
810 active = i915_vma_get_active(vma);
814 /* When a closed VMA is retired, it is unbound - eek.
815 * In order to prevent it from being recursively closed,
816 * take a pin on the vma so that the second unbind is
819 * Even more scary is that the retire callback may free
820 * the object (last active vma). To prevent the explosion
821 * we defer the actual object free to a worker that can
822 * only proceed once it acquires the struct_mutex (which
823 * we currently hold, therefore it cannot free this object
824 * before we are finished).
828 for_each_active(active, idx) {
829 ret = i915_gem_active_retire(&vma->last_read[idx],
830 &vma->vm->i915->drm.struct_mutex);
836 ret = i915_gem_active_retire(&vma->last_fence,
837 &vma->vm->i915->drm.struct_mutex);
840 __i915_vma_unpin(vma);
844 GEM_BUG_ON(i915_vma_is_active(vma));
846 if (i915_vma_is_pinned(vma))
849 if (!drm_mm_node_allocated(&vma->node))
852 GEM_BUG_ON(obj->bind_count == 0);
853 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
855 if (i915_vma_is_map_and_fenceable(vma)) {
857 * Check that we have flushed all writes through the GGTT
858 * before the unbind, other due to non-strict nature of those
859 * indirect writes they may end up referencing the GGTT PTE
862 i915_vma_flush_writes(vma);
863 GEM_BUG_ON(i915_vma_has_ggtt_write(vma));
865 /* release the fence reg _after_ flushing */
866 ret = i915_vma_put_fence(vma);
870 /* Force a pagefault for domain tracking on next user access */
871 i915_vma_revoke_mmap(vma);
873 __i915_vma_iounmap(vma);
874 vma->flags &= ~I915_VMA_CAN_FENCE;
876 GEM_BUG_ON(vma->fence);
877 GEM_BUG_ON(i915_vma_has_userfault(vma));
879 if (likely(!vma->vm->closed)) {
880 trace_i915_vma_unbind(vma);
881 vma->vm->unbind_vma(vma);
883 vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
885 i915_vma_remove(vma);
890 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
891 #include "selftests/i915_vma.c"