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 "gt/intel_engine.h"
31 #include "gt/intel_gt.h"
34 #include "i915_globals.h"
35 #include "i915_trace.h"
38 static struct i915_global_vma {
39 struct i915_global base;
40 struct kmem_cache *slab_vmas;
43 struct i915_vma *i915_vma_alloc(void)
45 return kmem_cache_zalloc(global.slab_vmas, GFP_KERNEL);
48 void i915_vma_free(struct i915_vma *vma)
50 return kmem_cache_free(global.slab_vmas, vma);
53 #if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM)
55 #include <linux/stackdepot.h>
57 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
59 unsigned long *entries;
60 unsigned int nr_entries;
63 if (!vma->node.stack) {
64 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n",
65 vma->node.start, vma->node.size, reason);
69 nr_entries = stack_depot_fetch(vma->node.stack, &entries);
70 stack_trace_snprint(buf, sizeof(buf), entries, nr_entries, 0);
71 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n",
72 vma->node.start, vma->node.size, reason, buf);
77 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
83 static inline struct i915_vma *active_to_vma(struct i915_active *ref)
85 return container_of(ref, typeof(struct i915_vma), active);
88 static int __i915_vma_active(struct i915_active *ref)
90 return i915_vma_tryget(active_to_vma(ref)) ? 0 : -ENOENT;
93 static void __i915_vma_retire(struct i915_active *ref)
95 i915_vma_put(active_to_vma(ref));
98 static struct i915_vma *
99 vma_create(struct drm_i915_gem_object *obj,
100 struct i915_address_space *vm,
101 const struct i915_ggtt_view *view)
103 struct i915_vma *vma;
104 struct rb_node *rb, **p;
106 /* The aliasing_ppgtt should never be used directly! */
107 GEM_BUG_ON(vm == &vm->i915->ggtt.alias->vm);
109 vma = i915_vma_alloc();
111 return ERR_PTR(-ENOMEM);
114 vma->ops = &vm->vma_ops;
116 vma->resv = obj->base.resv;
117 vma->size = obj->base.size;
118 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
120 i915_active_init(vm->i915, &vma->active,
121 __i915_vma_active, __i915_vma_retire);
123 /* Declare ourselves safe for use inside shrinkers */
124 if (IS_ENABLED(CONFIG_LOCKDEP)) {
125 fs_reclaim_acquire(GFP_KERNEL);
126 might_lock(&vma->active.mutex);
127 fs_reclaim_release(GFP_KERNEL);
130 INIT_LIST_HEAD(&vma->closed_link);
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;
145 } else if (view->type == I915_GGTT_VIEW_REMAPPED) {
146 vma->size = intel_remapped_info_size(&view->remapped);
147 vma->size <<= PAGE_SHIFT;
151 if (unlikely(vma->size > vm->total))
154 GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
156 if (i915_is_ggtt(vm)) {
157 if (unlikely(overflows_type(vma->size, u32)))
160 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
161 i915_gem_object_get_tiling(obj),
162 i915_gem_object_get_stride(obj));
163 if (unlikely(vma->fence_size < vma->size || /* overflow */
164 vma->fence_size > vm->total))
167 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
169 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
170 i915_gem_object_get_tiling(obj),
171 i915_gem_object_get_stride(obj));
172 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
174 vma->flags |= I915_VMA_GGTT;
177 spin_lock(&obj->vma.lock);
180 p = &obj->vma.tree.rb_node;
182 struct i915_vma *pos;
186 pos = rb_entry(rb, struct i915_vma, obj_node);
189 * If the view already exists in the tree, another thread
190 * already created a matching vma, so return the older instance
191 * and dispose of ours.
193 cmp = i915_vma_compare(pos, vm, view);
195 spin_unlock(&obj->vma.lock);
205 rb_link_node(&vma->obj_node, rb, p);
206 rb_insert_color(&vma->obj_node, &obj->vma.tree);
208 if (i915_vma_is_ggtt(vma))
210 * We put the GGTT vma at the start of the vma-list, followed
211 * by the ppGGTT vma. This allows us to break early when
212 * iterating over only the GGTT vma for an object, see
213 * for_each_ggtt_vma()
215 list_add(&vma->obj_link, &obj->vma.list);
217 list_add_tail(&vma->obj_link, &obj->vma.list);
219 spin_unlock(&obj->vma.lock);
221 mutex_lock(&vm->mutex);
222 list_add(&vma->vm_link, &vm->unbound_list);
223 mutex_unlock(&vm->mutex);
229 return ERR_PTR(-E2BIG);
232 static struct i915_vma *
233 vma_lookup(struct drm_i915_gem_object *obj,
234 struct i915_address_space *vm,
235 const struct i915_ggtt_view *view)
239 rb = obj->vma.tree.rb_node;
241 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
244 cmp = i915_vma_compare(vma, vm, view);
258 * i915_vma_instance - return the singleton instance of the VMA
259 * @obj: parent &struct drm_i915_gem_object to be mapped
260 * @vm: address space in which the mapping is located
261 * @view: additional mapping requirements
263 * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
264 * the same @view characteristics. If a match is not found, one is created.
265 * Once created, the VMA is kept until either the object is freed, or the
266 * address space is closed.
268 * Must be called with struct_mutex held.
270 * Returns the vma, or an error pointer.
273 i915_vma_instance(struct drm_i915_gem_object *obj,
274 struct i915_address_space *vm,
275 const struct i915_ggtt_view *view)
277 struct i915_vma *vma;
279 GEM_BUG_ON(view && !i915_is_ggtt(vm));
280 GEM_BUG_ON(vm->closed);
282 spin_lock(&obj->vma.lock);
283 vma = vma_lookup(obj, vm, view);
284 spin_unlock(&obj->vma.lock);
286 /* vma_create() will resolve the race if another creates the vma */
288 vma = vma_create(obj, vm, view);
290 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
295 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
297 * @cache_level: mapping cache level
298 * @flags: flags like global or local mapping
300 * DMA addresses are taken from the scatter-gather table of this object (or of
301 * this VMA in case of non-default GGTT views) and PTE entries set up.
302 * Note that DMA addresses are also the only part of the SG table we care about.
304 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
311 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
312 GEM_BUG_ON(vma->size > vma->node.size);
314 if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start,
319 if (GEM_DEBUG_WARN_ON(!flags))
323 if (flags & PIN_GLOBAL)
324 bind_flags |= I915_VMA_GLOBAL_BIND;
325 if (flags & PIN_USER)
326 bind_flags |= I915_VMA_LOCAL_BIND;
328 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
329 if (flags & PIN_UPDATE)
330 bind_flags |= vma_flags;
332 bind_flags &= ~vma_flags;
336 GEM_BUG_ON(!vma->pages);
338 trace_i915_vma_bind(vma, bind_flags);
339 ret = vma->ops->bind_vma(vma, cache_level, bind_flags);
343 vma->flags |= bind_flags;
347 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
352 /* Access through the GTT requires the device to be awake. */
353 assert_rpm_wakelock_held(&vma->vm->i915->runtime_pm);
355 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
356 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
361 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
362 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
366 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
379 err = i915_vma_pin_fence(vma);
383 i915_vma_set_ggtt_write(vma);
387 __i915_vma_unpin(vma);
389 return IO_ERR_PTR(err);
392 void i915_vma_flush_writes(struct i915_vma *vma)
394 if (!i915_vma_has_ggtt_write(vma))
397 intel_gt_flush_ggtt_writes(vma->vm->gt);
399 i915_vma_unset_ggtt_write(vma);
402 void i915_vma_unpin_iomap(struct i915_vma *vma)
404 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
406 GEM_BUG_ON(vma->iomap == NULL);
408 i915_vma_flush_writes(vma);
410 i915_vma_unpin_fence(vma);
414 void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags)
416 struct i915_vma *vma;
417 struct drm_i915_gem_object *obj;
419 vma = fetch_and_zero(p_vma);
429 if (flags & I915_VMA_RELEASE_MAP)
430 i915_gem_object_unpin_map(obj);
432 i915_gem_object_put(obj);
435 bool i915_vma_misplaced(const struct i915_vma *vma,
436 u64 size, u64 alignment, u64 flags)
438 if (!drm_mm_node_allocated(&vma->node))
441 if (vma->node.size < size)
444 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
445 if (alignment && !IS_ALIGNED(vma->node.start, alignment))
448 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
451 if (flags & PIN_OFFSET_BIAS &&
452 vma->node.start < (flags & PIN_OFFSET_MASK))
455 if (flags & PIN_OFFSET_FIXED &&
456 vma->node.start != (flags & PIN_OFFSET_MASK))
462 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
464 bool mappable, fenceable;
466 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
467 GEM_BUG_ON(!vma->fence_size);
469 fenceable = (vma->node.size >= vma->fence_size &&
470 IS_ALIGNED(vma->node.start, vma->fence_alignment));
472 mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
474 if (mappable && fenceable)
475 vma->flags |= I915_VMA_CAN_FENCE;
477 vma->flags &= ~I915_VMA_CAN_FENCE;
480 static bool color_differs(struct drm_mm_node *node, unsigned long color)
482 return node->allocated && node->color != color;
485 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
487 struct drm_mm_node *node = &vma->node;
488 struct drm_mm_node *other;
491 * On some machines we have to be careful when putting differing types
492 * of snoopable memory together to avoid the prefetcher crossing memory
493 * domains and dying. During vm initialisation, we decide whether or not
494 * these constraints apply and set the drm_mm.color_adjust
497 if (vma->vm->mm.color_adjust == NULL)
500 /* Only valid to be called on an already inserted vma */
501 GEM_BUG_ON(!drm_mm_node_allocated(node));
502 GEM_BUG_ON(list_empty(&node->node_list));
504 other = list_prev_entry(node, node_list);
505 if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
508 other = list_next_entry(node, node_list);
509 if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
515 static void assert_bind_count(const struct drm_i915_gem_object *obj)
518 * Combine the assertion that the object is bound and that we have
519 * pinned its pages. But we should never have bound the object
520 * more than we have pinned its pages. (For complete accuracy, we
521 * assume that no else is pinning the pages, but as a rough assertion
522 * that we will not run into problems later, this will do!)
524 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < atomic_read(&obj->bind_count));
528 * i915_vma_insert - finds a slot for the vma in its address space
530 * @size: requested size in bytes (can be larger than the VMA)
531 * @alignment: required alignment
532 * @flags: mask of PIN_* flags to use
534 * First we try to allocate some free space that meets the requirements for
535 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
536 * preferrably the oldest idle entry to make room for the new VMA.
539 * 0 on success, negative error code otherwise.
542 i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
544 struct drm_i915_private *dev_priv = vma->vm->i915;
545 unsigned int cache_level;
549 GEM_BUG_ON(i915_vma_is_closed(vma));
550 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
551 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
553 size = max(size, vma->size);
554 alignment = max(alignment, vma->display_alignment);
555 if (flags & PIN_MAPPABLE) {
556 size = max_t(typeof(size), size, vma->fence_size);
557 alignment = max_t(typeof(alignment),
558 alignment, vma->fence_alignment);
561 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
562 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
563 GEM_BUG_ON(!is_power_of_2(alignment));
565 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
566 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
568 end = vma->vm->total;
569 if (flags & PIN_MAPPABLE)
570 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
571 if (flags & PIN_ZONE_4G)
572 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
573 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
575 /* If binding the object/GGTT view requires more space than the entire
576 * aperture has, reject it early before evicting everything in a vain
577 * attempt to find space.
580 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n",
581 size, flags & PIN_MAPPABLE ? "mappable" : "total",
587 ret = i915_gem_object_pin_pages(vma->obj);
591 cache_level = vma->obj->cache_level;
596 GEM_BUG_ON(vma->pages);
598 ret = vma->ops->set_pages(vma);
602 if (flags & PIN_OFFSET_FIXED) {
603 u64 offset = flags & PIN_OFFSET_MASK;
604 if (!IS_ALIGNED(offset, alignment) ||
605 range_overflows(offset, size, end)) {
610 ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
611 size, offset, cache_level,
617 * We only support huge gtt pages through the 48b PPGTT,
618 * however we also don't want to force any alignment for
619 * objects which need to be tightly packed into the low 32bits.
621 * Note that we assume that GGTT are limited to 4GiB for the
622 * forseeable future. See also i915_ggtt_offset().
624 if (upper_32_bits(end - 1) &&
625 vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
627 * We can't mix 64K and 4K PTEs in the same page-table
628 * (2M block), and so to avoid the ugliness and
629 * complexity of coloring we opt for just aligning 64K
633 rounddown_pow_of_two(vma->page_sizes.sg |
634 I915_GTT_PAGE_SIZE_2M);
637 * Check we don't expand for the limited Global GTT
638 * (mappable aperture is even more precious!). This
639 * also checks that we exclude the aliasing-ppgtt.
641 GEM_BUG_ON(i915_vma_is_ggtt(vma));
643 alignment = max(alignment, page_alignment);
645 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
646 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
649 ret = i915_gem_gtt_insert(vma->vm, &vma->node,
650 size, alignment, cache_level,
655 GEM_BUG_ON(vma->node.start < start);
656 GEM_BUG_ON(vma->node.start + vma->node.size > end);
658 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
659 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, cache_level));
661 mutex_lock(&vma->vm->mutex);
662 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
663 mutex_unlock(&vma->vm->mutex);
666 atomic_inc(&vma->obj->bind_count);
667 assert_bind_count(vma->obj);
673 vma->ops->clear_pages(vma);
676 i915_gem_object_unpin_pages(vma->obj);
681 i915_vma_remove(struct i915_vma *vma)
683 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
684 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
686 vma->ops->clear_pages(vma);
688 mutex_lock(&vma->vm->mutex);
689 drm_mm_remove_node(&vma->node);
690 list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
691 mutex_unlock(&vma->vm->mutex);
694 * Since the unbound list is global, only move to that list if
695 * no more VMAs exist.
698 struct drm_i915_gem_object *obj = vma->obj;
700 atomic_dec(&obj->bind_count);
703 * And finally now the object is completely decoupled from this
704 * vma, we can drop its hold on the backing storage and allow
705 * it to be reaped by the shrinker.
707 i915_gem_object_unpin_pages(obj);
708 assert_bind_count(obj);
712 int __i915_vma_do_pin(struct i915_vma *vma,
713 u64 size, u64 alignment, u64 flags)
715 const unsigned int bound = vma->flags;
718 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
719 GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
720 GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
722 if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
727 if ((bound & I915_VMA_BIND_MASK) == 0) {
728 ret = i915_vma_insert(vma, size, alignment, flags);
732 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
734 ret = i915_vma_bind(vma, vma->obj ? vma->obj->cache_level : 0, flags);
738 GEM_BUG_ON((vma->flags & I915_VMA_BIND_MASK) == 0);
740 if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
741 __i915_vma_set_map_and_fenceable(vma);
743 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
747 if ((bound & I915_VMA_BIND_MASK) == 0) {
748 i915_vma_remove(vma);
749 GEM_BUG_ON(vma->pages);
750 GEM_BUG_ON(vma->flags & I915_VMA_BIND_MASK);
753 __i915_vma_unpin(vma);
757 void i915_vma_close(struct i915_vma *vma)
759 struct drm_i915_private *i915 = vma->vm->i915;
762 GEM_BUG_ON(i915_vma_is_closed(vma));
765 * We defer actually closing, unbinding and destroying the VMA until
766 * the next idle point, or if the object is freed in the meantime. By
767 * postponing the unbind, we allow for it to be resurrected by the
768 * client, avoiding the work required to rebind the VMA. This is
769 * advantageous for DRI, where the client/server pass objects
770 * between themselves, temporarily opening a local VMA to the
771 * object, and then closing it again. The same object is then reused
772 * on the next frame (or two, depending on the depth of the swap queue)
773 * causing us to rebind the VMA once more. This ends up being a lot
774 * of wasted work for the steady state.
776 spin_lock_irqsave(&i915->gt.closed_lock, flags);
777 list_add(&vma->closed_link, &i915->gt.closed_vma);
778 spin_unlock_irqrestore(&i915->gt.closed_lock, flags);
781 static void __i915_vma_remove_closed(struct i915_vma *vma)
783 struct drm_i915_private *i915 = vma->vm->i915;
785 if (!i915_vma_is_closed(vma))
788 spin_lock_irq(&i915->gt.closed_lock);
789 list_del_init(&vma->closed_link);
790 spin_unlock_irq(&i915->gt.closed_lock);
793 void i915_vma_reopen(struct i915_vma *vma)
795 __i915_vma_remove_closed(vma);
798 static void __i915_vma_destroy(struct i915_vma *vma)
800 GEM_BUG_ON(vma->node.allocated);
801 GEM_BUG_ON(vma->fence);
803 mutex_lock(&vma->vm->mutex);
804 list_del(&vma->vm_link);
805 mutex_unlock(&vma->vm->mutex);
808 struct drm_i915_gem_object *obj = vma->obj;
810 spin_lock(&obj->vma.lock);
811 list_del(&vma->obj_link);
812 rb_erase(&vma->obj_node, &vma->obj->vma.tree);
813 spin_unlock(&obj->vma.lock);
816 i915_active_fini(&vma->active);
821 void i915_vma_destroy(struct i915_vma *vma)
823 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
825 GEM_BUG_ON(i915_vma_is_pinned(vma));
827 __i915_vma_remove_closed(vma);
829 WARN_ON(i915_vma_unbind(vma));
830 GEM_BUG_ON(i915_vma_is_active(vma));
832 __i915_vma_destroy(vma);
835 void i915_vma_parked(struct drm_i915_private *i915)
837 struct i915_vma *vma, *next;
839 spin_lock_irq(&i915->gt.closed_lock);
840 list_for_each_entry_safe(vma, next, &i915->gt.closed_vma, closed_link) {
841 list_del_init(&vma->closed_link);
842 spin_unlock_irq(&i915->gt.closed_lock);
844 i915_vma_destroy(vma);
846 spin_lock_irq(&i915->gt.closed_lock);
848 spin_unlock_irq(&i915->gt.closed_lock);
851 static void __i915_vma_iounmap(struct i915_vma *vma)
853 GEM_BUG_ON(i915_vma_is_pinned(vma));
855 if (vma->iomap == NULL)
858 io_mapping_unmap(vma->iomap);
862 void i915_vma_revoke_mmap(struct i915_vma *vma)
864 struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
867 lockdep_assert_held(&vma->vm->mutex);
869 if (!i915_vma_has_userfault(vma))
872 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
873 GEM_BUG_ON(!vma->obj->userfault_count);
875 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
876 unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
877 drm_vma_node_offset_addr(node) + vma_offset,
881 i915_vma_unset_userfault(vma);
882 if (!--vma->obj->userfault_count)
883 list_del(&vma->obj->userfault_link);
886 int i915_vma_move_to_active(struct i915_vma *vma,
887 struct i915_request *rq,
890 struct drm_i915_gem_object *obj = vma->obj;
893 assert_vma_held(vma);
894 assert_object_held(obj);
895 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
898 * Add a reference if we're newly entering the active list.
899 * The order in which we add operations to the retirement queue is
900 * vital here: mark_active adds to the start of the callback list,
901 * such that subsequent callbacks are called first. Therefore we
902 * add the active reference first and queue for it to be dropped
905 err = i915_active_ref(&vma->active, rq->timeline, rq);
909 if (flags & EXEC_OBJECT_WRITE) {
910 if (intel_frontbuffer_invalidate(obj->frontbuffer, ORIGIN_CS))
911 i915_active_ref(&obj->frontbuffer->write,
915 dma_resv_add_excl_fence(vma->resv, &rq->fence);
916 obj->write_domain = I915_GEM_DOMAIN_RENDER;
917 obj->read_domains = 0;
919 err = dma_resv_reserve_shared(vma->resv, 1);
923 dma_resv_add_shared_fence(vma->resv, &rq->fence);
924 obj->write_domain = 0;
926 obj->read_domains |= I915_GEM_GPU_DOMAINS;
927 obj->mm.dirty = true;
929 GEM_BUG_ON(!i915_vma_is_active(vma));
933 int i915_vma_unbind(struct i915_vma *vma)
937 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
940 * First wait upon any activity as retiring the request may
941 * have side-effects such as unpinning or even unbinding this vma.
944 if (i915_vma_is_active(vma)) {
946 * When a closed VMA is retired, it is unbound - eek.
947 * In order to prevent it from being recursively closed,
948 * take a pin on the vma so that the second unbind is
951 * Even more scary is that the retire callback may free
952 * the object (last active vma). To prevent the explosion
953 * we defer the actual object free to a worker that can
954 * only proceed once it acquires the struct_mutex (which
955 * we currently hold, therefore it cannot free this object
956 * before we are finished).
959 ret = i915_active_wait(&vma->active);
960 __i915_vma_unpin(vma);
964 GEM_BUG_ON(i915_vma_is_active(vma));
966 if (i915_vma_is_pinned(vma)) {
967 vma_print_allocator(vma, "is pinned");
971 if (!drm_mm_node_allocated(&vma->node))
974 if (i915_vma_is_map_and_fenceable(vma)) {
976 * Check that we have flushed all writes through the GGTT
977 * before the unbind, other due to non-strict nature of those
978 * indirect writes they may end up referencing the GGTT PTE
981 i915_vma_flush_writes(vma);
982 GEM_BUG_ON(i915_vma_has_ggtt_write(vma));
984 /* release the fence reg _after_ flushing */
985 mutex_lock(&vma->vm->mutex);
986 ret = i915_vma_revoke_fence(vma);
987 mutex_unlock(&vma->vm->mutex);
991 /* Force a pagefault for domain tracking on next user access */
992 mutex_lock(&vma->vm->mutex);
993 i915_vma_revoke_mmap(vma);
994 mutex_unlock(&vma->vm->mutex);
996 __i915_vma_iounmap(vma);
997 vma->flags &= ~I915_VMA_CAN_FENCE;
999 GEM_BUG_ON(vma->fence);
1000 GEM_BUG_ON(i915_vma_has_userfault(vma));
1002 if (likely(!vma->vm->closed)) {
1003 trace_i915_vma_unbind(vma);
1004 vma->ops->unbind_vma(vma);
1006 vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
1008 i915_vma_remove(vma);
1013 struct i915_vma *i915_vma_make_unshrinkable(struct i915_vma *vma)
1015 i915_gem_object_make_unshrinkable(vma->obj);
1019 void i915_vma_make_shrinkable(struct i915_vma *vma)
1021 i915_gem_object_make_shrinkable(vma->obj);
1024 void i915_vma_make_purgeable(struct i915_vma *vma)
1026 i915_gem_object_make_purgeable(vma->obj);
1029 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1030 #include "selftests/i915_vma.c"
1033 static void i915_global_vma_shrink(void)
1035 kmem_cache_shrink(global.slab_vmas);
1038 static void i915_global_vma_exit(void)
1040 kmem_cache_destroy(global.slab_vmas);
1043 static struct i915_global_vma global = { {
1044 .shrink = i915_global_vma_shrink,
1045 .exit = i915_global_vma_exit,
1048 int __init i915_global_vma_init(void)
1050 global.slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
1051 if (!global.slab_vmas)
1054 i915_global_register(&global.base);