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>
33 #if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM)
35 #include <linux/stackdepot.h>
37 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
39 unsigned long entries[12];
40 struct stack_trace trace = {
42 .max_entries = ARRAY_SIZE(entries),
46 if (!vma->node.stack) {
47 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n",
48 vma->node.start, vma->node.size, reason);
52 depot_fetch_stack(vma->node.stack, &trace);
53 snprint_stack_trace(buf, sizeof(buf), &trace, 0);
54 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n",
55 vma->node.start, vma->node.size, reason, buf);
60 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
66 static void obj_bump_mru(struct drm_i915_gem_object *obj)
68 struct drm_i915_private *i915 = to_i915(obj->base.dev);
70 spin_lock(&i915->mm.obj_lock);
72 list_move_tail(&obj->mm.link, &i915->mm.bound_list);
73 spin_unlock(&i915->mm.obj_lock);
75 obj->mm.dirty = true; /* be paranoid */
78 static void __i915_vma_retire(struct i915_active *ref)
80 struct i915_vma *vma = container_of(ref, typeof(*vma), active);
81 struct drm_i915_gem_object *obj = vma->obj;
83 GEM_BUG_ON(!i915_gem_object_is_active(obj));
84 if (--obj->active_count)
87 /* Prune the shared fence arrays iff completely idle (inc. external) */
88 if (reservation_object_trylock(obj->resv)) {
89 if (reservation_object_test_signaled_rcu(obj->resv, true))
90 reservation_object_add_excl_fence(obj->resv, NULL);
91 reservation_object_unlock(obj->resv);
95 * Bump our place on the bound list to keep it roughly in LRU order
96 * so that we don't steal from recently used but inactive objects
97 * (unless we are forced to ofc!)
101 if (i915_gem_object_has_active_reference(obj)) {
102 i915_gem_object_clear_active_reference(obj);
103 i915_gem_object_put(obj);
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 *vma;
113 struct rb_node *rb, **p;
115 /* The aliasing_ppgtt should never be used directly! */
116 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->vm);
118 vma = kmem_cache_zalloc(vm->i915->vmas, GFP_KERNEL);
120 return ERR_PTR(-ENOMEM);
122 i915_active_init(vm->i915, &vma->active, __i915_vma_retire);
123 INIT_ACTIVE_REQUEST(&vma->last_fence);
126 vma->ops = &vm->vma_ops;
128 vma->resv = obj->resv;
129 vma->size = obj->base.size;
130 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
132 if (view && view->type != I915_GGTT_VIEW_NORMAL) {
133 vma->ggtt_view = *view;
134 if (view->type == I915_GGTT_VIEW_PARTIAL) {
135 GEM_BUG_ON(range_overflows_t(u64,
136 view->partial.offset,
138 obj->base.size >> PAGE_SHIFT));
139 vma->size = view->partial.size;
140 vma->size <<= PAGE_SHIFT;
141 GEM_BUG_ON(vma->size > obj->base.size);
142 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
143 vma->size = intel_rotation_info_size(&view->rotated);
144 vma->size <<= PAGE_SHIFT;
148 if (unlikely(vma->size > vm->total))
151 GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
153 if (i915_is_ggtt(vm)) {
154 if (unlikely(overflows_type(vma->size, u32)))
157 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
158 i915_gem_object_get_tiling(obj),
159 i915_gem_object_get_stride(obj));
160 if (unlikely(vma->fence_size < vma->size || /* overflow */
161 vma->fence_size > vm->total))
164 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
166 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
167 i915_gem_object_get_tiling(obj),
168 i915_gem_object_get_stride(obj));
169 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
171 vma->flags |= I915_VMA_GGTT;
174 spin_lock(&obj->vma.lock);
177 p = &obj->vma.tree.rb_node;
179 struct i915_vma *pos;
183 pos = rb_entry(rb, struct i915_vma, obj_node);
186 * If the view already exists in the tree, another thread
187 * already created a matching vma, so return the older instance
188 * and dispose of ours.
190 cmp = i915_vma_compare(pos, vm, view);
192 spin_unlock(&obj->vma.lock);
193 kmem_cache_free(vm->i915->vmas, vma);
202 rb_link_node(&vma->obj_node, rb, p);
203 rb_insert_color(&vma->obj_node, &obj->vma.tree);
205 if (i915_vma_is_ggtt(vma))
207 * We put the GGTT vma at the start of the vma-list, followed
208 * by the ppGGTT vma. This allows us to break early when
209 * iterating over only the GGTT vma for an object, see
210 * for_each_ggtt_vma()
212 list_add(&vma->obj_link, &obj->vma.list);
214 list_add_tail(&vma->obj_link, &obj->vma.list);
216 spin_unlock(&obj->vma.lock);
218 mutex_lock(&vm->mutex);
219 list_add(&vma->vm_link, &vm->unbound_list);
220 mutex_unlock(&vm->mutex);
225 kmem_cache_free(vm->i915->vmas, vma);
226 return ERR_PTR(-E2BIG);
229 static struct i915_vma *
230 vma_lookup(struct drm_i915_gem_object *obj,
231 struct i915_address_space *vm,
232 const struct i915_ggtt_view *view)
236 rb = obj->vma.tree.rb_node;
238 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
241 cmp = i915_vma_compare(vma, vm, view);
255 * i915_vma_instance - return the singleton instance of the VMA
256 * @obj: parent &struct drm_i915_gem_object to be mapped
257 * @vm: address space in which the mapping is located
258 * @view: additional mapping requirements
260 * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
261 * the same @view characteristics. If a match is not found, one is created.
262 * Once created, the VMA is kept until either the object is freed, or the
263 * address space is closed.
265 * Must be called with struct_mutex held.
267 * Returns the vma, or an error pointer.
270 i915_vma_instance(struct drm_i915_gem_object *obj,
271 struct i915_address_space *vm,
272 const struct i915_ggtt_view *view)
274 struct i915_vma *vma;
276 GEM_BUG_ON(view && !i915_is_ggtt(vm));
277 GEM_BUG_ON(vm->closed);
279 spin_lock(&obj->vma.lock);
280 vma = vma_lookup(obj, vm, view);
281 spin_unlock(&obj->vma.lock);
283 /* vma_create() will resolve the race if another creates the vma */
285 vma = vma_create(obj, vm, view);
287 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
292 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
294 * @cache_level: mapping cache level
295 * @flags: flags like global or local mapping
297 * DMA addresses are taken from the scatter-gather table of this object (or of
298 * this VMA in case of non-default GGTT views) and PTE entries set up.
299 * Note that DMA addresses are also the only part of the SG table we care about.
301 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
308 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
309 GEM_BUG_ON(vma->size > vma->node.size);
311 if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start,
316 if (GEM_DEBUG_WARN_ON(!flags))
320 if (flags & PIN_GLOBAL)
321 bind_flags |= I915_VMA_GLOBAL_BIND;
322 if (flags & PIN_USER)
323 bind_flags |= I915_VMA_LOCAL_BIND;
325 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
326 if (flags & PIN_UPDATE)
327 bind_flags |= vma_flags;
329 bind_flags &= ~vma_flags;
333 GEM_BUG_ON(!vma->pages);
335 trace_i915_vma_bind(vma, bind_flags);
336 ret = vma->ops->bind_vma(vma, cache_level, bind_flags);
340 vma->flags |= bind_flags;
344 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
349 /* Access through the GTT requires the device to be awake. */
350 assert_rpm_wakelock_held(vma->vm->i915);
352 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
353 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
358 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
359 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
363 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
376 err = i915_vma_pin_fence(vma);
380 i915_vma_set_ggtt_write(vma);
384 __i915_vma_unpin(vma);
386 return IO_ERR_PTR(err);
389 void i915_vma_flush_writes(struct i915_vma *vma)
391 if (!i915_vma_has_ggtt_write(vma))
394 i915_gem_flush_ggtt_writes(vma->vm->i915);
396 i915_vma_unset_ggtt_write(vma);
399 void i915_vma_unpin_iomap(struct i915_vma *vma)
401 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
403 GEM_BUG_ON(vma->iomap == NULL);
405 i915_vma_flush_writes(vma);
407 i915_vma_unpin_fence(vma);
411 void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags)
413 struct i915_vma *vma;
414 struct drm_i915_gem_object *obj;
416 vma = fetch_and_zero(p_vma);
426 if (flags & I915_VMA_RELEASE_MAP)
427 i915_gem_object_unpin_map(obj);
429 __i915_gem_object_release_unless_active(obj);
432 bool i915_vma_misplaced(const struct i915_vma *vma,
433 u64 size, u64 alignment, u64 flags)
435 if (!drm_mm_node_allocated(&vma->node))
438 if (vma->node.size < size)
441 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
442 if (alignment && !IS_ALIGNED(vma->node.start, alignment))
445 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
448 if (flags & PIN_OFFSET_BIAS &&
449 vma->node.start < (flags & PIN_OFFSET_MASK))
452 if (flags & PIN_OFFSET_FIXED &&
453 vma->node.start != (flags & PIN_OFFSET_MASK))
459 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
461 bool mappable, fenceable;
463 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
464 GEM_BUG_ON(!vma->fence_size);
467 * Explicitly disable for rotated VMA since the display does not
468 * need the fence and the VMA is not accessible to other users.
470 if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
473 fenceable = (vma->node.size >= vma->fence_size &&
474 IS_ALIGNED(vma->node.start, vma->fence_alignment));
476 mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
478 if (mappable && fenceable)
479 vma->flags |= I915_VMA_CAN_FENCE;
481 vma->flags &= ~I915_VMA_CAN_FENCE;
484 static bool color_differs(struct drm_mm_node *node, unsigned long color)
486 return node->allocated && node->color != color;
489 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
491 struct drm_mm_node *node = &vma->node;
492 struct drm_mm_node *other;
495 * On some machines we have to be careful when putting differing types
496 * of snoopable memory together to avoid the prefetcher crossing memory
497 * domains and dying. During vm initialisation, we decide whether or not
498 * these constraints apply and set the drm_mm.color_adjust
501 if (vma->vm->mm.color_adjust == NULL)
504 /* Only valid to be called on an already inserted vma */
505 GEM_BUG_ON(!drm_mm_node_allocated(node));
506 GEM_BUG_ON(list_empty(&node->node_list));
508 other = list_prev_entry(node, node_list);
509 if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
512 other = list_next_entry(node, node_list);
513 if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
519 static void assert_bind_count(const struct drm_i915_gem_object *obj)
522 * Combine the assertion that the object is bound and that we have
523 * pinned its pages. But we should never have bound the object
524 * more than we have pinned its pages. (For complete accuracy, we
525 * assume that no else is pinning the pages, but as a rough assertion
526 * that we will not run into problems later, this will do!)
528 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
532 * i915_vma_insert - finds a slot for the vma in its address space
534 * @size: requested size in bytes (can be larger than the VMA)
535 * @alignment: required alignment
536 * @flags: mask of PIN_* flags to use
538 * First we try to allocate some free space that meets the requirements for
539 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
540 * preferrably the oldest idle entry to make room for the new VMA.
543 * 0 on success, negative error code otherwise.
546 i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
548 struct drm_i915_private *dev_priv = vma->vm->i915;
549 unsigned int cache_level;
553 GEM_BUG_ON(i915_vma_is_closed(vma));
554 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
555 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
557 size = max(size, vma->size);
558 alignment = max(alignment, vma->display_alignment);
559 if (flags & PIN_MAPPABLE) {
560 size = max_t(typeof(size), size, vma->fence_size);
561 alignment = max_t(typeof(alignment),
562 alignment, vma->fence_alignment);
565 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
566 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
567 GEM_BUG_ON(!is_power_of_2(alignment));
569 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
570 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
572 end = vma->vm->total;
573 if (flags & PIN_MAPPABLE)
574 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
575 if (flags & PIN_ZONE_4G)
576 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
577 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
579 /* If binding the object/GGTT view requires more space than the entire
580 * aperture has, reject it early before evicting everything in a vain
581 * attempt to find space.
584 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n",
585 size, flags & PIN_MAPPABLE ? "mappable" : "total",
591 ret = i915_gem_object_pin_pages(vma->obj);
595 cache_level = vma->obj->cache_level;
600 GEM_BUG_ON(vma->pages);
602 ret = vma->ops->set_pages(vma);
606 if (flags & PIN_OFFSET_FIXED) {
607 u64 offset = flags & PIN_OFFSET_MASK;
608 if (!IS_ALIGNED(offset, alignment) ||
609 range_overflows(offset, size, end)) {
614 ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
615 size, offset, cache_level,
621 * We only support huge gtt pages through the 48b PPGTT,
622 * however we also don't want to force any alignment for
623 * objects which need to be tightly packed into the low 32bits.
625 * Note that we assume that GGTT are limited to 4GiB for the
626 * forseeable future. See also i915_ggtt_offset().
628 if (upper_32_bits(end - 1) &&
629 vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
631 * We can't mix 64K and 4K PTEs in the same page-table
632 * (2M block), and so to avoid the ugliness and
633 * complexity of coloring we opt for just aligning 64K
637 rounddown_pow_of_two(vma->page_sizes.sg |
638 I915_GTT_PAGE_SIZE_2M);
641 * Check we don't expand for the limited Global GTT
642 * (mappable aperture is even more precious!). This
643 * also checks that we exclude the aliasing-ppgtt.
645 GEM_BUG_ON(i915_vma_is_ggtt(vma));
647 alignment = max(alignment, page_alignment);
649 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
650 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
653 ret = i915_gem_gtt_insert(vma->vm, &vma->node,
654 size, alignment, cache_level,
659 GEM_BUG_ON(vma->node.start < start);
660 GEM_BUG_ON(vma->node.start + vma->node.size > end);
662 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
663 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, cache_level));
665 mutex_lock(&vma->vm->mutex);
666 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
667 mutex_unlock(&vma->vm->mutex);
670 struct drm_i915_gem_object *obj = vma->obj;
672 spin_lock(&dev_priv->mm.obj_lock);
673 list_move_tail(&obj->mm.link, &dev_priv->mm.bound_list);
675 spin_unlock(&dev_priv->mm.obj_lock);
677 assert_bind_count(obj);
683 vma->ops->clear_pages(vma);
686 i915_gem_object_unpin_pages(vma->obj);
691 i915_vma_remove(struct i915_vma *vma)
693 struct drm_i915_private *i915 = vma->vm->i915;
695 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
696 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
698 vma->ops->clear_pages(vma);
700 mutex_lock(&vma->vm->mutex);
701 drm_mm_remove_node(&vma->node);
702 list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
703 mutex_unlock(&vma->vm->mutex);
706 * Since the unbound list is global, only move to that list if
707 * no more VMAs exist.
710 struct drm_i915_gem_object *obj = vma->obj;
712 spin_lock(&i915->mm.obj_lock);
713 if (--obj->bind_count == 0)
714 list_move_tail(&obj->mm.link, &i915->mm.unbound_list);
715 spin_unlock(&i915->mm.obj_lock);
718 * And finally now the object is completely decoupled from this
719 * vma, we can drop its hold on the backing storage and allow
720 * it to be reaped by the shrinker.
722 i915_gem_object_unpin_pages(obj);
723 assert_bind_count(obj);
727 int __i915_vma_do_pin(struct i915_vma *vma,
728 u64 size, u64 alignment, u64 flags)
730 const unsigned int bound = vma->flags;
733 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
734 GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
735 GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
737 if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
742 if ((bound & I915_VMA_BIND_MASK) == 0) {
743 ret = i915_vma_insert(vma, size, alignment, flags);
747 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
749 ret = i915_vma_bind(vma, vma->obj ? vma->obj->cache_level : 0, flags);
753 GEM_BUG_ON((vma->flags & I915_VMA_BIND_MASK) == 0);
755 if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
756 __i915_vma_set_map_and_fenceable(vma);
758 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
762 if ((bound & I915_VMA_BIND_MASK) == 0) {
763 i915_vma_remove(vma);
764 GEM_BUG_ON(vma->pages);
765 GEM_BUG_ON(vma->flags & I915_VMA_BIND_MASK);
768 __i915_vma_unpin(vma);
772 void i915_vma_close(struct i915_vma *vma)
774 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
776 GEM_BUG_ON(i915_vma_is_closed(vma));
777 vma->flags |= I915_VMA_CLOSED;
780 * We defer actually closing, unbinding and destroying the VMA until
781 * the next idle point, or if the object is freed in the meantime. By
782 * postponing the unbind, we allow for it to be resurrected by the
783 * client, avoiding the work required to rebind the VMA. This is
784 * advantageous for DRI, where the client/server pass objects
785 * between themselves, temporarily opening a local VMA to the
786 * object, and then closing it again. The same object is then reused
787 * on the next frame (or two, depending on the depth of the swap queue)
788 * causing us to rebind the VMA once more. This ends up being a lot
789 * of wasted work for the steady state.
791 list_add_tail(&vma->closed_link, &vma->vm->i915->gt.closed_vma);
794 void i915_vma_reopen(struct i915_vma *vma)
796 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
798 if (vma->flags & I915_VMA_CLOSED) {
799 vma->flags &= ~I915_VMA_CLOSED;
800 list_del(&vma->closed_link);
804 static void __i915_vma_destroy(struct i915_vma *vma)
806 struct drm_i915_private *i915 = vma->vm->i915;
808 GEM_BUG_ON(vma->node.allocated);
809 GEM_BUG_ON(vma->fence);
811 GEM_BUG_ON(i915_active_request_isset(&vma->last_fence));
813 mutex_lock(&vma->vm->mutex);
814 list_del(&vma->vm_link);
815 mutex_unlock(&vma->vm->mutex);
818 struct drm_i915_gem_object *obj = vma->obj;
820 spin_lock(&obj->vma.lock);
821 list_del(&vma->obj_link);
822 rb_erase(&vma->obj_node, &vma->obj->vma.tree);
823 spin_unlock(&obj->vma.lock);
826 i915_active_fini(&vma->active);
828 kmem_cache_free(i915->vmas, vma);
831 void i915_vma_destroy(struct i915_vma *vma)
833 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
835 GEM_BUG_ON(i915_vma_is_active(vma));
836 GEM_BUG_ON(i915_vma_is_pinned(vma));
838 if (i915_vma_is_closed(vma))
839 list_del(&vma->closed_link);
841 WARN_ON(i915_vma_unbind(vma));
842 __i915_vma_destroy(vma);
845 void i915_vma_parked(struct drm_i915_private *i915)
847 struct i915_vma *vma, *next;
849 list_for_each_entry_safe(vma, next, &i915->gt.closed_vma, closed_link) {
850 GEM_BUG_ON(!i915_vma_is_closed(vma));
851 i915_vma_destroy(vma);
854 GEM_BUG_ON(!list_empty(&i915->gt.closed_vma));
857 static void __i915_vma_iounmap(struct i915_vma *vma)
859 GEM_BUG_ON(i915_vma_is_pinned(vma));
861 if (vma->iomap == NULL)
864 io_mapping_unmap(vma->iomap);
868 void i915_vma_revoke_mmap(struct i915_vma *vma)
870 struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
873 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
875 if (!i915_vma_has_userfault(vma))
878 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
879 GEM_BUG_ON(!vma->obj->userfault_count);
881 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
882 unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
883 drm_vma_node_offset_addr(node) + vma_offset,
887 i915_vma_unset_userfault(vma);
888 if (!--vma->obj->userfault_count)
889 list_del(&vma->obj->userfault_link);
892 static void export_fence(struct i915_vma *vma,
893 struct i915_request *rq,
896 struct reservation_object *resv = vma->resv;
899 * Ignore errors from failing to allocate the new fence, we can't
900 * handle an error right now. Worst case should be missed
901 * synchronisation leading to rendering corruption.
903 reservation_object_lock(resv, NULL);
904 if (flags & EXEC_OBJECT_WRITE)
905 reservation_object_add_excl_fence(resv, &rq->fence);
906 else if (reservation_object_reserve_shared(resv, 1) == 0)
907 reservation_object_add_shared_fence(resv, &rq->fence);
908 reservation_object_unlock(resv);
911 int i915_vma_move_to_active(struct i915_vma *vma,
912 struct i915_request *rq,
915 struct drm_i915_gem_object *obj = vma->obj;
917 lockdep_assert_held(&rq->i915->drm.struct_mutex);
918 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
921 * Add a reference if we're newly entering the active list.
922 * The order in which we add operations to the retirement queue is
923 * vital here: mark_active adds to the start of the callback list,
924 * such that subsequent callbacks are called first. Therefore we
925 * add the active reference first and queue for it to be dropped
928 if (!vma->active.count)
931 if (unlikely(i915_active_ref(&vma->active, rq->fence.context, rq))) {
932 if (!vma->active.count)
937 GEM_BUG_ON(!i915_vma_is_active(vma));
938 GEM_BUG_ON(!obj->active_count);
940 obj->write_domain = 0;
941 if (flags & EXEC_OBJECT_WRITE) {
942 obj->write_domain = I915_GEM_DOMAIN_RENDER;
944 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
945 __i915_active_request_set(&obj->frontbuffer_write, rq);
947 obj->read_domains = 0;
949 obj->read_domains |= I915_GEM_GPU_DOMAINS;
951 if (flags & EXEC_OBJECT_NEEDS_FENCE)
952 __i915_active_request_set(&vma->last_fence, rq);
954 export_fence(vma, rq, flags);
958 int i915_vma_unbind(struct i915_vma *vma)
962 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
965 * First wait upon any activity as retiring the request may
966 * have side-effects such as unpinning or even unbinding this vma.
969 if (i915_vma_is_active(vma)) {
971 * When a closed VMA is retired, it is unbound - eek.
972 * In order to prevent it from being recursively closed,
973 * take a pin on the vma so that the second unbind is
976 * Even more scary is that the retire callback may free
977 * the object (last active vma). To prevent the explosion
978 * we defer the actual object free to a worker that can
979 * only proceed once it acquires the struct_mutex (which
980 * we currently hold, therefore it cannot free this object
981 * before we are finished).
985 ret = i915_active_wait(&vma->active);
989 ret = i915_active_request_retire(&vma->last_fence,
990 &vma->vm->i915->drm.struct_mutex);
992 __i915_vma_unpin(vma);
996 GEM_BUG_ON(i915_vma_is_active(vma));
998 if (i915_vma_is_pinned(vma)) {
999 vma_print_allocator(vma, "is pinned");
1003 if (!drm_mm_node_allocated(&vma->node))
1006 if (i915_vma_is_map_and_fenceable(vma)) {
1008 * Check that we have flushed all writes through the GGTT
1009 * before the unbind, other due to non-strict nature of those
1010 * indirect writes they may end up referencing the GGTT PTE
1013 i915_vma_flush_writes(vma);
1014 GEM_BUG_ON(i915_vma_has_ggtt_write(vma));
1016 /* release the fence reg _after_ flushing */
1017 ret = i915_vma_put_fence(vma);
1021 /* Force a pagefault for domain tracking on next user access */
1022 i915_vma_revoke_mmap(vma);
1024 __i915_vma_iounmap(vma);
1025 vma->flags &= ~I915_VMA_CAN_FENCE;
1027 GEM_BUG_ON(vma->fence);
1028 GEM_BUG_ON(i915_vma_has_userfault(vma));
1030 if (likely(!vma->vm->closed)) {
1031 trace_i915_vma_unbind(vma);
1032 vma->ops->unbind_vma(vma);
1034 vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
1036 i915_vma_remove(vma);
1041 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1042 #include "selftests/i915_vma.c"