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 "i915_globals.h"
29 #include "intel_ringbuffer.h"
30 #include "intel_frontbuffer.h"
32 #include <drm/drm_gem.h>
34 static struct i915_global_vma {
35 struct i915_global base;
36 struct kmem_cache *slab_vmas;
39 struct i915_vma *i915_vma_alloc(void)
41 return kmem_cache_zalloc(global.slab_vmas, GFP_KERNEL);
44 void i915_vma_free(struct i915_vma *vma)
46 return kmem_cache_free(global.slab_vmas, vma);
49 #if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM)
51 #include <linux/stackdepot.h>
53 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
55 unsigned long *entries;
56 unsigned int nr_entries;
59 if (!vma->node.stack) {
60 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n",
61 vma->node.start, vma->node.size, reason);
65 nr_entries = stack_depot_fetch(vma->node.stack, &entries);
66 stack_trace_snprint(buf, sizeof(buf), entries, nr_entries, 0);
67 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n",
68 vma->node.start, vma->node.size, reason, buf);
73 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
79 static void obj_bump_mru(struct drm_i915_gem_object *obj)
81 struct drm_i915_private *i915 = to_i915(obj->base.dev);
83 spin_lock(&i915->mm.obj_lock);
85 list_move_tail(&obj->mm.link, &i915->mm.bound_list);
86 spin_unlock(&i915->mm.obj_lock);
88 obj->mm.dirty = true; /* be paranoid */
91 static void __i915_vma_retire(struct i915_active *ref)
93 struct i915_vma *vma = container_of(ref, typeof(*vma), active);
94 struct drm_i915_gem_object *obj = vma->obj;
96 GEM_BUG_ON(!i915_gem_object_is_active(obj));
97 if (--obj->active_count)
100 /* Prune the shared fence arrays iff completely idle (inc. external) */
101 if (reservation_object_trylock(obj->resv)) {
102 if (reservation_object_test_signaled_rcu(obj->resv, true))
103 reservation_object_add_excl_fence(obj->resv, NULL);
104 reservation_object_unlock(obj->resv);
108 * Bump our place on the bound list to keep it roughly in LRU order
109 * so that we don't steal from recently used but inactive objects
110 * (unless we are forced to ofc!)
114 if (i915_gem_object_has_active_reference(obj)) {
115 i915_gem_object_clear_active_reference(obj);
116 i915_gem_object_put(obj);
120 static struct i915_vma *
121 vma_create(struct drm_i915_gem_object *obj,
122 struct i915_address_space *vm,
123 const struct i915_ggtt_view *view)
125 struct i915_vma *vma;
126 struct rb_node *rb, **p;
128 /* The aliasing_ppgtt should never be used directly! */
129 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->vm);
131 vma = i915_vma_alloc();
133 return ERR_PTR(-ENOMEM);
135 i915_active_init(vm->i915, &vma->active, __i915_vma_retire);
136 INIT_ACTIVE_REQUEST(&vma->last_fence);
139 vma->ops = &vm->vma_ops;
141 vma->resv = obj->resv;
142 vma->size = obj->base.size;
143 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
145 if (view && view->type != I915_GGTT_VIEW_NORMAL) {
146 vma->ggtt_view = *view;
147 if (view->type == I915_GGTT_VIEW_PARTIAL) {
148 GEM_BUG_ON(range_overflows_t(u64,
149 view->partial.offset,
151 obj->base.size >> PAGE_SHIFT));
152 vma->size = view->partial.size;
153 vma->size <<= PAGE_SHIFT;
154 GEM_BUG_ON(vma->size > obj->base.size);
155 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
156 vma->size = intel_rotation_info_size(&view->rotated);
157 vma->size <<= PAGE_SHIFT;
161 if (unlikely(vma->size > vm->total))
164 GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
166 if (i915_is_ggtt(vm)) {
167 if (unlikely(overflows_type(vma->size, u32)))
170 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
171 i915_gem_object_get_tiling(obj),
172 i915_gem_object_get_stride(obj));
173 if (unlikely(vma->fence_size < vma->size || /* overflow */
174 vma->fence_size > vm->total))
177 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
179 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
180 i915_gem_object_get_tiling(obj),
181 i915_gem_object_get_stride(obj));
182 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
184 vma->flags |= I915_VMA_GGTT;
187 spin_lock(&obj->vma.lock);
190 p = &obj->vma.tree.rb_node;
192 struct i915_vma *pos;
196 pos = rb_entry(rb, struct i915_vma, obj_node);
199 * If the view already exists in the tree, another thread
200 * already created a matching vma, so return the older instance
201 * and dispose of ours.
203 cmp = i915_vma_compare(pos, vm, view);
205 spin_unlock(&obj->vma.lock);
215 rb_link_node(&vma->obj_node, rb, p);
216 rb_insert_color(&vma->obj_node, &obj->vma.tree);
218 if (i915_vma_is_ggtt(vma))
220 * We put the GGTT vma at the start of the vma-list, followed
221 * by the ppGGTT vma. This allows us to break early when
222 * iterating over only the GGTT vma for an object, see
223 * for_each_ggtt_vma()
225 list_add(&vma->obj_link, &obj->vma.list);
227 list_add_tail(&vma->obj_link, &obj->vma.list);
229 spin_unlock(&obj->vma.lock);
231 mutex_lock(&vm->mutex);
232 list_add(&vma->vm_link, &vm->unbound_list);
233 mutex_unlock(&vm->mutex);
239 return ERR_PTR(-E2BIG);
242 static struct i915_vma *
243 vma_lookup(struct drm_i915_gem_object *obj,
244 struct i915_address_space *vm,
245 const struct i915_ggtt_view *view)
249 rb = obj->vma.tree.rb_node;
251 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
254 cmp = i915_vma_compare(vma, vm, view);
268 * i915_vma_instance - return the singleton instance of the VMA
269 * @obj: parent &struct drm_i915_gem_object to be mapped
270 * @vm: address space in which the mapping is located
271 * @view: additional mapping requirements
273 * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
274 * the same @view characteristics. If a match is not found, one is created.
275 * Once created, the VMA is kept until either the object is freed, or the
276 * address space is closed.
278 * Must be called with struct_mutex held.
280 * Returns the vma, or an error pointer.
283 i915_vma_instance(struct drm_i915_gem_object *obj,
284 struct i915_address_space *vm,
285 const struct i915_ggtt_view *view)
287 struct i915_vma *vma;
289 GEM_BUG_ON(view && !i915_is_ggtt(vm));
290 GEM_BUG_ON(vm->closed);
292 spin_lock(&obj->vma.lock);
293 vma = vma_lookup(obj, vm, view);
294 spin_unlock(&obj->vma.lock);
296 /* vma_create() will resolve the race if another creates the vma */
298 vma = vma_create(obj, vm, view);
300 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
305 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
307 * @cache_level: mapping cache level
308 * @flags: flags like global or local mapping
310 * DMA addresses are taken from the scatter-gather table of this object (or of
311 * this VMA in case of non-default GGTT views) and PTE entries set up.
312 * Note that DMA addresses are also the only part of the SG table we care about.
314 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
321 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
322 GEM_BUG_ON(vma->size > vma->node.size);
324 if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start,
329 if (GEM_DEBUG_WARN_ON(!flags))
333 if (flags & PIN_GLOBAL)
334 bind_flags |= I915_VMA_GLOBAL_BIND;
335 if (flags & PIN_USER)
336 bind_flags |= I915_VMA_LOCAL_BIND;
338 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
339 if (flags & PIN_UPDATE)
340 bind_flags |= vma_flags;
342 bind_flags &= ~vma_flags;
346 GEM_BUG_ON(!vma->pages);
348 trace_i915_vma_bind(vma, bind_flags);
349 ret = vma->ops->bind_vma(vma, cache_level, bind_flags);
353 vma->flags |= bind_flags;
357 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
362 /* Access through the GTT requires the device to be awake. */
363 assert_rpm_wakelock_held(vma->vm->i915);
365 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
366 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
371 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
372 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
376 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
389 err = i915_vma_pin_fence(vma);
393 i915_vma_set_ggtt_write(vma);
397 __i915_vma_unpin(vma);
399 return IO_ERR_PTR(err);
402 void i915_vma_flush_writes(struct i915_vma *vma)
404 if (!i915_vma_has_ggtt_write(vma))
407 i915_gem_flush_ggtt_writes(vma->vm->i915);
409 i915_vma_unset_ggtt_write(vma);
412 void i915_vma_unpin_iomap(struct i915_vma *vma)
414 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
416 GEM_BUG_ON(vma->iomap == NULL);
418 i915_vma_flush_writes(vma);
420 i915_vma_unpin_fence(vma);
424 void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags)
426 struct i915_vma *vma;
427 struct drm_i915_gem_object *obj;
429 vma = fetch_and_zero(p_vma);
439 if (flags & I915_VMA_RELEASE_MAP)
440 i915_gem_object_unpin_map(obj);
442 __i915_gem_object_release_unless_active(obj);
445 bool i915_vma_misplaced(const struct i915_vma *vma,
446 u64 size, u64 alignment, u64 flags)
448 if (!drm_mm_node_allocated(&vma->node))
451 if (vma->node.size < size)
454 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
455 if (alignment && !IS_ALIGNED(vma->node.start, alignment))
458 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
461 if (flags & PIN_OFFSET_BIAS &&
462 vma->node.start < (flags & PIN_OFFSET_MASK))
465 if (flags & PIN_OFFSET_FIXED &&
466 vma->node.start != (flags & PIN_OFFSET_MASK))
472 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
474 bool mappable, fenceable;
476 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
477 GEM_BUG_ON(!vma->fence_size);
480 * Explicitly disable for rotated VMA since the display does not
481 * need the fence and the VMA is not accessible to other users.
483 if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
486 fenceable = (vma->node.size >= vma->fence_size &&
487 IS_ALIGNED(vma->node.start, vma->fence_alignment));
489 mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
491 if (mappable && fenceable)
492 vma->flags |= I915_VMA_CAN_FENCE;
494 vma->flags &= ~I915_VMA_CAN_FENCE;
497 static bool color_differs(struct drm_mm_node *node, unsigned long color)
499 return node->allocated && node->color != color;
502 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
504 struct drm_mm_node *node = &vma->node;
505 struct drm_mm_node *other;
508 * On some machines we have to be careful when putting differing types
509 * of snoopable memory together to avoid the prefetcher crossing memory
510 * domains and dying. During vm initialisation, we decide whether or not
511 * these constraints apply and set the drm_mm.color_adjust
514 if (vma->vm->mm.color_adjust == NULL)
517 /* Only valid to be called on an already inserted vma */
518 GEM_BUG_ON(!drm_mm_node_allocated(node));
519 GEM_BUG_ON(list_empty(&node->node_list));
521 other = list_prev_entry(node, node_list);
522 if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
525 other = list_next_entry(node, node_list);
526 if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
532 static void assert_bind_count(const struct drm_i915_gem_object *obj)
535 * Combine the assertion that the object is bound and that we have
536 * pinned its pages. But we should never have bound the object
537 * more than we have pinned its pages. (For complete accuracy, we
538 * assume that no else is pinning the pages, but as a rough assertion
539 * that we will not run into problems later, this will do!)
541 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
545 * i915_vma_insert - finds a slot for the vma in its address space
547 * @size: requested size in bytes (can be larger than the VMA)
548 * @alignment: required alignment
549 * @flags: mask of PIN_* flags to use
551 * First we try to allocate some free space that meets the requirements for
552 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
553 * preferrably the oldest idle entry to make room for the new VMA.
556 * 0 on success, negative error code otherwise.
559 i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
561 struct drm_i915_private *dev_priv = vma->vm->i915;
562 unsigned int cache_level;
566 GEM_BUG_ON(i915_vma_is_closed(vma));
567 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
568 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
570 size = max(size, vma->size);
571 alignment = max(alignment, vma->display_alignment);
572 if (flags & PIN_MAPPABLE) {
573 size = max_t(typeof(size), size, vma->fence_size);
574 alignment = max_t(typeof(alignment),
575 alignment, vma->fence_alignment);
578 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
579 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
580 GEM_BUG_ON(!is_power_of_2(alignment));
582 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
583 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
585 end = vma->vm->total;
586 if (flags & PIN_MAPPABLE)
587 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
588 if (flags & PIN_ZONE_4G)
589 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
590 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
592 /* If binding the object/GGTT view requires more space than the entire
593 * aperture has, reject it early before evicting everything in a vain
594 * attempt to find space.
597 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n",
598 size, flags & PIN_MAPPABLE ? "mappable" : "total",
604 ret = i915_gem_object_pin_pages(vma->obj);
608 cache_level = vma->obj->cache_level;
613 GEM_BUG_ON(vma->pages);
615 ret = vma->ops->set_pages(vma);
619 if (flags & PIN_OFFSET_FIXED) {
620 u64 offset = flags & PIN_OFFSET_MASK;
621 if (!IS_ALIGNED(offset, alignment) ||
622 range_overflows(offset, size, end)) {
627 ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
628 size, offset, cache_level,
634 * We only support huge gtt pages through the 48b PPGTT,
635 * however we also don't want to force any alignment for
636 * objects which need to be tightly packed into the low 32bits.
638 * Note that we assume that GGTT are limited to 4GiB for the
639 * forseeable future. See also i915_ggtt_offset().
641 if (upper_32_bits(end - 1) &&
642 vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
644 * We can't mix 64K and 4K PTEs in the same page-table
645 * (2M block), and so to avoid the ugliness and
646 * complexity of coloring we opt for just aligning 64K
650 rounddown_pow_of_two(vma->page_sizes.sg |
651 I915_GTT_PAGE_SIZE_2M);
654 * Check we don't expand for the limited Global GTT
655 * (mappable aperture is even more precious!). This
656 * also checks that we exclude the aliasing-ppgtt.
658 GEM_BUG_ON(i915_vma_is_ggtt(vma));
660 alignment = max(alignment, page_alignment);
662 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
663 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
666 ret = i915_gem_gtt_insert(vma->vm, &vma->node,
667 size, alignment, cache_level,
672 GEM_BUG_ON(vma->node.start < start);
673 GEM_BUG_ON(vma->node.start + vma->node.size > end);
675 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
676 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, cache_level));
678 mutex_lock(&vma->vm->mutex);
679 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
680 mutex_unlock(&vma->vm->mutex);
683 struct drm_i915_gem_object *obj = vma->obj;
685 spin_lock(&dev_priv->mm.obj_lock);
686 list_move_tail(&obj->mm.link, &dev_priv->mm.bound_list);
688 spin_unlock(&dev_priv->mm.obj_lock);
690 assert_bind_count(obj);
696 vma->ops->clear_pages(vma);
699 i915_gem_object_unpin_pages(vma->obj);
704 i915_vma_remove(struct i915_vma *vma)
706 struct drm_i915_private *i915 = vma->vm->i915;
708 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
709 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
711 vma->ops->clear_pages(vma);
713 mutex_lock(&vma->vm->mutex);
714 drm_mm_remove_node(&vma->node);
715 list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
716 mutex_unlock(&vma->vm->mutex);
719 * Since the unbound list is global, only move to that list if
720 * no more VMAs exist.
723 struct drm_i915_gem_object *obj = vma->obj;
725 spin_lock(&i915->mm.obj_lock);
726 if (--obj->bind_count == 0)
727 list_move_tail(&obj->mm.link, &i915->mm.unbound_list);
728 spin_unlock(&i915->mm.obj_lock);
731 * And finally now the object is completely decoupled from this
732 * vma, we can drop its hold on the backing storage and allow
733 * it to be reaped by the shrinker.
735 i915_gem_object_unpin_pages(obj);
736 assert_bind_count(obj);
740 int __i915_vma_do_pin(struct i915_vma *vma,
741 u64 size, u64 alignment, u64 flags)
743 const unsigned int bound = vma->flags;
746 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
747 GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
748 GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
750 if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
755 if ((bound & I915_VMA_BIND_MASK) == 0) {
756 ret = i915_vma_insert(vma, size, alignment, flags);
760 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
762 ret = i915_vma_bind(vma, vma->obj ? vma->obj->cache_level : 0, flags);
766 GEM_BUG_ON((vma->flags & I915_VMA_BIND_MASK) == 0);
768 if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
769 __i915_vma_set_map_and_fenceable(vma);
771 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
775 if ((bound & I915_VMA_BIND_MASK) == 0) {
776 i915_vma_remove(vma);
777 GEM_BUG_ON(vma->pages);
778 GEM_BUG_ON(vma->flags & I915_VMA_BIND_MASK);
781 __i915_vma_unpin(vma);
785 void i915_vma_close(struct i915_vma *vma)
787 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
789 GEM_BUG_ON(i915_vma_is_closed(vma));
790 vma->flags |= I915_VMA_CLOSED;
793 * We defer actually closing, unbinding and destroying the VMA until
794 * the next idle point, or if the object is freed in the meantime. By
795 * postponing the unbind, we allow for it to be resurrected by the
796 * client, avoiding the work required to rebind the VMA. This is
797 * advantageous for DRI, where the client/server pass objects
798 * between themselves, temporarily opening a local VMA to the
799 * object, and then closing it again. The same object is then reused
800 * on the next frame (or two, depending on the depth of the swap queue)
801 * causing us to rebind the VMA once more. This ends up being a lot
802 * of wasted work for the steady state.
804 list_add_tail(&vma->closed_link, &vma->vm->i915->gt.closed_vma);
807 void i915_vma_reopen(struct i915_vma *vma)
809 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
811 if (vma->flags & I915_VMA_CLOSED) {
812 vma->flags &= ~I915_VMA_CLOSED;
813 list_del(&vma->closed_link);
817 static void __i915_vma_destroy(struct i915_vma *vma)
819 GEM_BUG_ON(vma->node.allocated);
820 GEM_BUG_ON(vma->fence);
822 GEM_BUG_ON(i915_active_request_isset(&vma->last_fence));
824 mutex_lock(&vma->vm->mutex);
825 list_del(&vma->vm_link);
826 mutex_unlock(&vma->vm->mutex);
829 struct drm_i915_gem_object *obj = vma->obj;
831 spin_lock(&obj->vma.lock);
832 list_del(&vma->obj_link);
833 rb_erase(&vma->obj_node, &vma->obj->vma.tree);
834 spin_unlock(&obj->vma.lock);
837 i915_active_fini(&vma->active);
842 void i915_vma_destroy(struct i915_vma *vma)
844 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
846 GEM_BUG_ON(i915_vma_is_active(vma));
847 GEM_BUG_ON(i915_vma_is_pinned(vma));
849 if (i915_vma_is_closed(vma))
850 list_del(&vma->closed_link);
852 WARN_ON(i915_vma_unbind(vma));
853 __i915_vma_destroy(vma);
856 void i915_vma_parked(struct drm_i915_private *i915)
858 struct i915_vma *vma, *next;
860 list_for_each_entry_safe(vma, next, &i915->gt.closed_vma, closed_link) {
861 GEM_BUG_ON(!i915_vma_is_closed(vma));
862 i915_vma_destroy(vma);
865 GEM_BUG_ON(!list_empty(&i915->gt.closed_vma));
868 static void __i915_vma_iounmap(struct i915_vma *vma)
870 GEM_BUG_ON(i915_vma_is_pinned(vma));
872 if (vma->iomap == NULL)
875 io_mapping_unmap(vma->iomap);
879 void i915_vma_revoke_mmap(struct i915_vma *vma)
881 struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
884 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
886 if (!i915_vma_has_userfault(vma))
889 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
890 GEM_BUG_ON(!vma->obj->userfault_count);
892 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
893 unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
894 drm_vma_node_offset_addr(node) + vma_offset,
898 i915_vma_unset_userfault(vma);
899 if (!--vma->obj->userfault_count)
900 list_del(&vma->obj->userfault_link);
903 static void export_fence(struct i915_vma *vma,
904 struct i915_request *rq,
907 struct reservation_object *resv = vma->resv;
910 * Ignore errors from failing to allocate the new fence, we can't
911 * handle an error right now. Worst case should be missed
912 * synchronisation leading to rendering corruption.
914 reservation_object_lock(resv, NULL);
915 if (flags & EXEC_OBJECT_WRITE)
916 reservation_object_add_excl_fence(resv, &rq->fence);
917 else if (reservation_object_reserve_shared(resv, 1) == 0)
918 reservation_object_add_shared_fence(resv, &rq->fence);
919 reservation_object_unlock(resv);
922 int i915_vma_move_to_active(struct i915_vma *vma,
923 struct i915_request *rq,
926 struct drm_i915_gem_object *obj = vma->obj;
928 lockdep_assert_held(&rq->i915->drm.struct_mutex);
929 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
932 * Add a reference if we're newly entering the active list.
933 * The order in which we add operations to the retirement queue is
934 * vital here: mark_active adds to the start of the callback list,
935 * such that subsequent callbacks are called first. Therefore we
936 * add the active reference first and queue for it to be dropped
939 if (!vma->active.count)
942 if (unlikely(i915_active_ref(&vma->active, rq->fence.context, rq))) {
943 if (!vma->active.count)
948 GEM_BUG_ON(!i915_vma_is_active(vma));
949 GEM_BUG_ON(!obj->active_count);
951 obj->write_domain = 0;
952 if (flags & EXEC_OBJECT_WRITE) {
953 obj->write_domain = I915_GEM_DOMAIN_RENDER;
955 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
956 __i915_active_request_set(&obj->frontbuffer_write, rq);
958 obj->read_domains = 0;
960 obj->read_domains |= I915_GEM_GPU_DOMAINS;
962 if (flags & EXEC_OBJECT_NEEDS_FENCE)
963 __i915_active_request_set(&vma->last_fence, rq);
965 export_fence(vma, rq, flags);
969 int i915_vma_unbind(struct i915_vma *vma)
973 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
976 * First wait upon any activity as retiring the request may
977 * have side-effects such as unpinning or even unbinding this vma.
980 if (i915_vma_is_active(vma)) {
982 * When a closed VMA is retired, it is unbound - eek.
983 * In order to prevent it from being recursively closed,
984 * take a pin on the vma so that the second unbind is
987 * Even more scary is that the retire callback may free
988 * the object (last active vma). To prevent the explosion
989 * we defer the actual object free to a worker that can
990 * only proceed once it acquires the struct_mutex (which
991 * we currently hold, therefore it cannot free this object
992 * before we are finished).
996 ret = i915_active_wait(&vma->active);
1000 ret = i915_active_request_retire(&vma->last_fence,
1001 &vma->vm->i915->drm.struct_mutex);
1003 __i915_vma_unpin(vma);
1007 GEM_BUG_ON(i915_vma_is_active(vma));
1009 if (i915_vma_is_pinned(vma)) {
1010 vma_print_allocator(vma, "is pinned");
1014 if (!drm_mm_node_allocated(&vma->node))
1017 if (i915_vma_is_map_and_fenceable(vma)) {
1019 * Check that we have flushed all writes through the GGTT
1020 * before the unbind, other due to non-strict nature of those
1021 * indirect writes they may end up referencing the GGTT PTE
1024 i915_vma_flush_writes(vma);
1025 GEM_BUG_ON(i915_vma_has_ggtt_write(vma));
1027 /* release the fence reg _after_ flushing */
1028 ret = i915_vma_put_fence(vma);
1032 /* Force a pagefault for domain tracking on next user access */
1033 i915_vma_revoke_mmap(vma);
1035 __i915_vma_iounmap(vma);
1036 vma->flags &= ~I915_VMA_CAN_FENCE;
1038 GEM_BUG_ON(vma->fence);
1039 GEM_BUG_ON(i915_vma_has_userfault(vma));
1041 if (likely(!vma->vm->closed)) {
1042 trace_i915_vma_unbind(vma);
1043 vma->ops->unbind_vma(vma);
1045 vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
1047 i915_vma_remove(vma);
1052 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1053 #include "selftests/i915_vma.c"
1056 static void i915_global_vma_shrink(void)
1058 kmem_cache_shrink(global.slab_vmas);
1061 static void i915_global_vma_exit(void)
1063 kmem_cache_destroy(global.slab_vmas);
1066 static struct i915_global_vma global = { {
1067 .shrink = i915_global_vma_shrink,
1068 .exit = i915_global_vma_exit,
1071 int __init i915_global_vma_init(void)
1073 global.slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
1074 if (!global.slab_vmas)
1077 i915_global_register(&global.base);