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 struct i915_vma_active {
67 struct i915_gem_active base;
74 __i915_vma_retire(struct i915_vma *vma, struct i915_request *rq)
76 struct drm_i915_gem_object *obj = vma->obj;
78 GEM_BUG_ON(!i915_vma_is_active(vma));
79 if (--vma->active_count)
82 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
83 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
85 GEM_BUG_ON(!i915_gem_object_is_active(obj));
86 if (--obj->active_count)
89 /* Prune the shared fence arrays iff completely idle (inc. external) */
90 if (reservation_object_trylock(obj->resv)) {
91 if (reservation_object_test_signaled_rcu(obj->resv, true))
92 reservation_object_add_excl_fence(obj->resv, NULL);
93 reservation_object_unlock(obj->resv);
96 /* Bump our place on the bound list to keep it roughly in LRU order
97 * so that we don't steal from recently used but inactive objects
98 * (unless we are forced to ofc!)
100 spin_lock(&rq->i915->mm.obj_lock);
102 list_move_tail(&obj->mm.link, &rq->i915->mm.bound_list);
103 spin_unlock(&rq->i915->mm.obj_lock);
105 obj->mm.dirty = true; /* be paranoid */
107 if (i915_gem_object_has_active_reference(obj)) {
108 i915_gem_object_clear_active_reference(obj);
109 i915_gem_object_put(obj);
114 i915_vma_retire(struct i915_gem_active *base, struct i915_request *rq)
116 struct i915_vma_active *active =
117 container_of(base, typeof(*active), base);
119 __i915_vma_retire(active->vma, rq);
123 i915_vma_last_retire(struct i915_gem_active *base, struct i915_request *rq)
125 __i915_vma_retire(container_of(base, struct i915_vma, last_active), rq);
128 static struct i915_vma *
129 vma_create(struct drm_i915_gem_object *obj,
130 struct i915_address_space *vm,
131 const struct i915_ggtt_view *view)
133 struct i915_vma *vma;
134 struct rb_node *rb, **p;
136 /* The aliasing_ppgtt should never be used directly! */
137 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->vm);
139 vma = kmem_cache_zalloc(vm->i915->vmas, GFP_KERNEL);
141 return ERR_PTR(-ENOMEM);
143 vma->active = RB_ROOT;
145 init_request_active(&vma->last_active, i915_vma_last_retire);
146 init_request_active(&vma->last_fence, NULL);
148 vma->ops = &vm->vma_ops;
150 vma->resv = obj->resv;
151 vma->size = obj->base.size;
152 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
154 if (view && view->type != I915_GGTT_VIEW_NORMAL) {
155 vma->ggtt_view = *view;
156 if (view->type == I915_GGTT_VIEW_PARTIAL) {
157 GEM_BUG_ON(range_overflows_t(u64,
158 view->partial.offset,
160 obj->base.size >> PAGE_SHIFT));
161 vma->size = view->partial.size;
162 vma->size <<= PAGE_SHIFT;
163 GEM_BUG_ON(vma->size > obj->base.size);
164 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
165 vma->size = intel_rotation_info_size(&view->rotated);
166 vma->size <<= PAGE_SHIFT;
170 if (unlikely(vma->size > vm->total))
173 GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
175 if (i915_is_ggtt(vm)) {
176 if (unlikely(overflows_type(vma->size, u32)))
179 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
180 i915_gem_object_get_tiling(obj),
181 i915_gem_object_get_stride(obj));
182 if (unlikely(vma->fence_size < vma->size || /* overflow */
183 vma->fence_size > vm->total))
186 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
188 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
189 i915_gem_object_get_tiling(obj),
190 i915_gem_object_get_stride(obj));
191 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
194 * We put the GGTT vma at the start of the vma-list, followed
195 * by the ppGGTT vma. This allows us to break early when
196 * iterating over only the GGTT vma for an object, see
197 * for_each_ggtt_vma()
199 vma->flags |= I915_VMA_GGTT;
200 list_add(&vma->obj_link, &obj->vma_list);
202 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
203 list_add_tail(&vma->obj_link, &obj->vma_list);
207 p = &obj->vma_tree.rb_node;
209 struct i915_vma *pos;
212 pos = rb_entry(rb, struct i915_vma, obj_node);
213 if (i915_vma_compare(pos, vm, view) < 0)
218 rb_link_node(&vma->obj_node, rb, p);
219 rb_insert_color(&vma->obj_node, &obj->vma_tree);
220 list_add(&vma->vm_link, &vm->unbound_list);
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 lockdep_assert_held(&obj->base.dev->struct_mutex);
277 GEM_BUG_ON(view && !i915_is_ggtt(vm));
278 GEM_BUG_ON(vm->closed);
280 vma = vma_lookup(obj, vm, view);
282 vma = vma_create(obj, vm, view);
284 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
285 GEM_BUG_ON(!IS_ERR(vma) && vma_lookup(obj, vm, view) != vma);
290 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
292 * @cache_level: mapping cache level
293 * @flags: flags like global or local mapping
295 * DMA addresses are taken from the scatter-gather table of this object (or of
296 * this VMA in case of non-default GGTT views) and PTE entries set up.
297 * Note that DMA addresses are also the only part of the SG table we care about.
299 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
306 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
307 GEM_BUG_ON(vma->size > vma->node.size);
309 if (GEM_WARN_ON(range_overflows(vma->node.start,
314 if (GEM_WARN_ON(!flags))
318 if (flags & PIN_GLOBAL)
319 bind_flags |= I915_VMA_GLOBAL_BIND;
320 if (flags & PIN_USER)
321 bind_flags |= I915_VMA_LOCAL_BIND;
323 vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
324 if (flags & PIN_UPDATE)
325 bind_flags |= vma_flags;
327 bind_flags &= ~vma_flags;
331 GEM_BUG_ON(!vma->pages);
333 trace_i915_vma_bind(vma, bind_flags);
334 ret = vma->ops->bind_vma(vma, cache_level, bind_flags);
338 vma->flags |= bind_flags;
342 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
347 /* Access through the GTT requires the device to be awake. */
348 assert_rpm_wakelock_held(vma->vm->i915);
350 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
351 if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
356 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
357 GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
361 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
374 err = i915_vma_pin_fence(vma);
378 i915_vma_set_ggtt_write(vma);
382 __i915_vma_unpin(vma);
384 return IO_ERR_PTR(err);
387 void i915_vma_flush_writes(struct i915_vma *vma)
389 if (!i915_vma_has_ggtt_write(vma))
392 i915_gem_flush_ggtt_writes(vma->vm->i915);
394 i915_vma_unset_ggtt_write(vma);
397 void i915_vma_unpin_iomap(struct i915_vma *vma)
399 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
401 GEM_BUG_ON(vma->iomap == NULL);
403 i915_vma_flush_writes(vma);
405 i915_vma_unpin_fence(vma);
409 void i915_vma_unpin_and_release(struct i915_vma **p_vma)
411 struct i915_vma *vma;
412 struct drm_i915_gem_object *obj;
414 vma = fetch_and_zero(p_vma);
424 __i915_gem_object_release_unless_active(obj);
427 bool i915_vma_misplaced(const struct i915_vma *vma,
428 u64 size, u64 alignment, u64 flags)
430 if (!drm_mm_node_allocated(&vma->node))
433 if (vma->node.size < size)
436 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
437 if (alignment && !IS_ALIGNED(vma->node.start, alignment))
440 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
443 if (flags & PIN_OFFSET_BIAS &&
444 vma->node.start < (flags & PIN_OFFSET_MASK))
447 if (flags & PIN_OFFSET_FIXED &&
448 vma->node.start != (flags & PIN_OFFSET_MASK))
454 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
456 bool mappable, fenceable;
458 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
459 GEM_BUG_ON(!vma->fence_size);
462 * Explicitly disable for rotated VMA since the display does not
463 * need the fence and the VMA is not accessible to other users.
465 if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
468 fenceable = (vma->node.size >= vma->fence_size &&
469 IS_ALIGNED(vma->node.start, vma->fence_alignment));
471 mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
473 if (mappable && fenceable)
474 vma->flags |= I915_VMA_CAN_FENCE;
476 vma->flags &= ~I915_VMA_CAN_FENCE;
479 static bool color_differs(struct drm_mm_node *node, unsigned long color)
481 return node->allocated && node->color != color;
484 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
486 struct drm_mm_node *node = &vma->node;
487 struct drm_mm_node *other;
490 * On some machines we have to be careful when putting differing types
491 * of snoopable memory together to avoid the prefetcher crossing memory
492 * domains and dying. During vm initialisation, we decide whether or not
493 * these constraints apply and set the drm_mm.color_adjust
496 if (vma->vm->mm.color_adjust == NULL)
499 /* Only valid to be called on an already inserted vma */
500 GEM_BUG_ON(!drm_mm_node_allocated(node));
501 GEM_BUG_ON(list_empty(&node->node_list));
503 other = list_prev_entry(node, node_list);
504 if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
507 other = list_next_entry(node, node_list);
508 if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
514 static void assert_bind_count(const struct drm_i915_gem_object *obj)
517 * Combine the assertion that the object is bound and that we have
518 * pinned its pages. But we should never have bound the object
519 * more than we have pinned its pages. (For complete accuracy, we
520 * assume that no else is pinning the pages, but as a rough assertion
521 * that we will not run into problems later, this will do!)
523 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
527 * i915_vma_insert - finds a slot for the vma in its address space
529 * @size: requested size in bytes (can be larger than the VMA)
530 * @alignment: required alignment
531 * @flags: mask of PIN_* flags to use
533 * First we try to allocate some free space that meets the requirements for
534 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
535 * preferrably the oldest idle entry to make room for the new VMA.
538 * 0 on success, negative error code otherwise.
541 i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
543 struct drm_i915_private *dev_priv = vma->vm->i915;
544 unsigned int cache_level;
548 GEM_BUG_ON(i915_vma_is_closed(vma));
549 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
550 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
552 size = max(size, vma->size);
553 alignment = max(alignment, vma->display_alignment);
554 if (flags & PIN_MAPPABLE) {
555 size = max_t(typeof(size), size, vma->fence_size);
556 alignment = max_t(typeof(alignment),
557 alignment, vma->fence_alignment);
560 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
561 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
562 GEM_BUG_ON(!is_power_of_2(alignment));
564 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
565 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
567 end = vma->vm->total;
568 if (flags & PIN_MAPPABLE)
569 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
570 if (flags & PIN_ZONE_4G)
571 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
572 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
574 /* If binding the object/GGTT view requires more space than the entire
575 * aperture has, reject it early before evicting everything in a vain
576 * attempt to find space.
579 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n",
580 size, flags & PIN_MAPPABLE ? "mappable" : "total",
586 ret = i915_gem_object_pin_pages(vma->obj);
590 cache_level = vma->obj->cache_level;
595 GEM_BUG_ON(vma->pages);
597 ret = vma->ops->set_pages(vma);
601 if (flags & PIN_OFFSET_FIXED) {
602 u64 offset = flags & PIN_OFFSET_MASK;
603 if (!IS_ALIGNED(offset, alignment) ||
604 range_overflows(offset, size, end)) {
609 ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
610 size, offset, cache_level,
616 * We only support huge gtt pages through the 48b PPGTT,
617 * however we also don't want to force any alignment for
618 * objects which need to be tightly packed into the low 32bits.
620 * Note that we assume that GGTT are limited to 4GiB for the
621 * forseeable future. See also i915_ggtt_offset().
623 if (upper_32_bits(end - 1) &&
624 vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
626 * We can't mix 64K and 4K PTEs in the same page-table
627 * (2M block), and so to avoid the ugliness and
628 * complexity of coloring we opt for just aligning 64K
632 rounddown_pow_of_two(vma->page_sizes.sg |
633 I915_GTT_PAGE_SIZE_2M);
636 * Check we don't expand for the limited Global GTT
637 * (mappable aperture is even more precious!). This
638 * also checks that we exclude the aliasing-ppgtt.
640 GEM_BUG_ON(i915_vma_is_ggtt(vma));
642 alignment = max(alignment, page_alignment);
644 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
645 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
648 ret = i915_gem_gtt_insert(vma->vm, &vma->node,
649 size, alignment, cache_level,
654 GEM_BUG_ON(vma->node.start < start);
655 GEM_BUG_ON(vma->node.start + vma->node.size > end);
657 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
658 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, cache_level));
660 list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
663 struct drm_i915_gem_object *obj = vma->obj;
665 spin_lock(&dev_priv->mm.obj_lock);
666 list_move_tail(&obj->mm.link, &dev_priv->mm.bound_list);
668 spin_unlock(&dev_priv->mm.obj_lock);
670 assert_bind_count(obj);
676 vma->ops->clear_pages(vma);
679 i915_gem_object_unpin_pages(vma->obj);
684 i915_vma_remove(struct i915_vma *vma)
686 struct drm_i915_private *i915 = vma->vm->i915;
688 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
689 GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
691 vma->ops->clear_pages(vma);
693 drm_mm_remove_node(&vma->node);
694 list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
697 * Since the unbound list is global, only move to that list if
698 * no more VMAs exist.
701 struct drm_i915_gem_object *obj = vma->obj;
703 spin_lock(&i915->mm.obj_lock);
704 if (--obj->bind_count == 0)
705 list_move_tail(&obj->mm.link, &i915->mm.unbound_list);
706 spin_unlock(&i915->mm.obj_lock);
709 * And finally now the object is completely decoupled from this
710 * vma, we can drop its hold on the backing storage and allow
711 * it to be reaped by the shrinker.
713 i915_gem_object_unpin_pages(obj);
714 assert_bind_count(obj);
718 int __i915_vma_do_pin(struct i915_vma *vma,
719 u64 size, u64 alignment, u64 flags)
721 const unsigned int bound = vma->flags;
724 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
725 GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
726 GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
728 if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
733 if ((bound & I915_VMA_BIND_MASK) == 0) {
734 ret = i915_vma_insert(vma, size, alignment, flags);
738 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
740 ret = i915_vma_bind(vma, vma->obj ? vma->obj->cache_level : 0, flags);
744 GEM_BUG_ON((vma->flags & I915_VMA_BIND_MASK) == 0);
746 if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
747 __i915_vma_set_map_and_fenceable(vma);
749 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
753 if ((bound & I915_VMA_BIND_MASK) == 0) {
754 i915_vma_remove(vma);
755 GEM_BUG_ON(vma->pages);
756 GEM_BUG_ON(vma->flags & I915_VMA_BIND_MASK);
759 __i915_vma_unpin(vma);
763 void i915_vma_close(struct i915_vma *vma)
765 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
767 GEM_BUG_ON(i915_vma_is_closed(vma));
768 vma->flags |= I915_VMA_CLOSED;
771 * We defer actually closing, unbinding and destroying the VMA until
772 * the next idle point, or if the object is freed in the meantime. By
773 * postponing the unbind, we allow for it to be resurrected by the
774 * client, avoiding the work required to rebind the VMA. This is
775 * advantageous for DRI, where the client/server pass objects
776 * between themselves, temporarily opening a local VMA to the
777 * object, and then closing it again. The same object is then reused
778 * on the next frame (or two, depending on the depth of the swap queue)
779 * causing us to rebind the VMA once more. This ends up being a lot
780 * of wasted work for the steady state.
782 list_add_tail(&vma->closed_link, &vma->vm->i915->gt.closed_vma);
785 void i915_vma_reopen(struct i915_vma *vma)
787 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
789 if (vma->flags & I915_VMA_CLOSED) {
790 vma->flags &= ~I915_VMA_CLOSED;
791 list_del(&vma->closed_link);
795 static void __i915_vma_destroy(struct i915_vma *vma)
797 struct drm_i915_private *i915 = vma->vm->i915;
798 struct i915_vma_active *iter, *n;
800 GEM_BUG_ON(vma->node.allocated);
801 GEM_BUG_ON(vma->fence);
803 GEM_BUG_ON(i915_gem_active_isset(&vma->last_fence));
805 list_del(&vma->obj_link);
806 list_del(&vma->vm_link);
808 rb_erase(&vma->obj_node, &vma->obj->vma_tree);
810 if (!i915_vma_is_ggtt(vma))
811 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
813 rbtree_postorder_for_each_entry_safe(iter, n, &vma->active, node) {
814 GEM_BUG_ON(i915_gem_active_isset(&iter->base));
818 kmem_cache_free(i915->vmas, vma);
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_active(vma));
826 GEM_BUG_ON(i915_vma_is_pinned(vma));
828 if (i915_vma_is_closed(vma))
829 list_del(&vma->closed_link);
831 WARN_ON(i915_vma_unbind(vma));
832 __i915_vma_destroy(vma);
835 void i915_vma_parked(struct drm_i915_private *i915)
837 struct i915_vma *vma, *next;
839 list_for_each_entry_safe(vma, next, &i915->gt.closed_vma, closed_link) {
840 GEM_BUG_ON(!i915_vma_is_closed(vma));
841 i915_vma_destroy(vma);
844 GEM_BUG_ON(!list_empty(&i915->gt.closed_vma));
847 static void __i915_vma_iounmap(struct i915_vma *vma)
849 GEM_BUG_ON(i915_vma_is_pinned(vma));
851 if (vma->iomap == NULL)
854 io_mapping_unmap(vma->iomap);
858 void i915_vma_revoke_mmap(struct i915_vma *vma)
860 struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
863 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
865 if (!i915_vma_has_userfault(vma))
868 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
869 GEM_BUG_ON(!vma->obj->userfault_count);
871 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
872 unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
873 drm_vma_node_offset_addr(node) + vma_offset,
877 i915_vma_unset_userfault(vma);
878 if (!--vma->obj->userfault_count)
879 list_del(&vma->obj->userfault_link);
882 static void export_fence(struct i915_vma *vma,
883 struct i915_request *rq,
886 struct reservation_object *resv = vma->resv;
889 * Ignore errors from failing to allocate the new fence, we can't
890 * handle an error right now. Worst case should be missed
891 * synchronisation leading to rendering corruption.
893 reservation_object_lock(resv, NULL);
894 if (flags & EXEC_OBJECT_WRITE)
895 reservation_object_add_excl_fence(resv, &rq->fence);
896 else if (reservation_object_reserve_shared(resv) == 0)
897 reservation_object_add_shared_fence(resv, &rq->fence);
898 reservation_object_unlock(resv);
901 static struct i915_gem_active *active_instance(struct i915_vma *vma, u64 idx)
903 struct i915_vma_active *active;
904 struct rb_node **p, *parent;
905 struct i915_request *old;
908 * We track the most recently used timeline to skip a rbtree search
909 * for the common case, under typical loads we never need the rbtree
910 * at all. We can reuse the last_active slot if it is empty, that is
911 * after the previous activity has been retired, or if the active
912 * matches the current timeline.
914 * Note that we allow the timeline to be active simultaneously in
915 * the rbtree and the last_active cache. We do this to avoid having
916 * to search and replace the rbtree element for a new timeline, with
917 * the cost being that we must be aware that the vma may be retired
918 * twice for the same timeline (as the older rbtree element will be
919 * retired before the new request added to last_active).
921 old = i915_gem_active_raw(&vma->last_active,
922 &vma->vm->i915->drm.struct_mutex);
923 if (!old || old->fence.context == idx)
926 /* Move the currently active fence into the rbtree */
927 idx = old->fence.context;
930 p = &vma->active.rb_node;
934 active = rb_entry(parent, struct i915_vma_active, node);
935 if (active->timeline == idx)
938 if (active->timeline < idx)
939 p = &parent->rb_right;
941 p = &parent->rb_left;
944 active = kmalloc(sizeof(*active), GFP_KERNEL);
946 /* kmalloc may retire the vma->last_active request (thanks shrinker)! */
947 if (unlikely(!i915_gem_active_raw(&vma->last_active,
948 &vma->vm->i915->drm.struct_mutex))) {
953 if (unlikely(!active))
954 return ERR_PTR(-ENOMEM);
956 init_request_active(&active->base, i915_vma_retire);
958 active->timeline = idx;
960 rb_link_node(&active->node, parent, p);
961 rb_insert_color(&active->node, &vma->active);
965 * Overwrite the previous active slot in the rbtree with last_active,
966 * leaving last_active zeroed. If the previous slot is still active,
967 * we must be careful as we now only expect to receive one retire
968 * callback not two, and so much undo the active counting for the
971 if (i915_gem_active_isset(&active->base)) {
972 /* Retire ourselves from the old rq->active_list */
973 __list_del_entry(&active->base.link);
975 GEM_BUG_ON(!vma->active_count);
977 GEM_BUG_ON(list_empty(&vma->last_active.link));
978 list_replace_init(&vma->last_active.link, &active->base.link);
979 active->base.request = fetch_and_zero(&vma->last_active.request);
982 return &vma->last_active;
985 int i915_vma_move_to_active(struct i915_vma *vma,
986 struct i915_request *rq,
989 struct drm_i915_gem_object *obj = vma->obj;
990 struct i915_gem_active *active;
992 lockdep_assert_held(&rq->i915->drm.struct_mutex);
993 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
995 active = active_instance(vma, rq->fence.context);
997 return PTR_ERR(active);
1000 * Add a reference if we're newly entering the active list.
1001 * The order in which we add operations to the retirement queue is
1002 * vital here: mark_active adds to the start of the callback list,
1003 * such that subsequent callbacks are called first. Therefore we
1004 * add the active reference first and queue for it to be dropped
1007 if (!i915_gem_active_isset(active) && !vma->active_count++) {
1008 list_move_tail(&vma->vm_link, &vma->vm->active_list);
1009 obj->active_count++;
1011 i915_gem_active_set(active, rq);
1012 GEM_BUG_ON(!i915_vma_is_active(vma));
1013 GEM_BUG_ON(!obj->active_count);
1015 obj->write_domain = 0;
1016 if (flags & EXEC_OBJECT_WRITE) {
1017 obj->write_domain = I915_GEM_DOMAIN_RENDER;
1019 if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
1020 i915_gem_active_set(&obj->frontbuffer_write, rq);
1022 obj->read_domains = 0;
1024 obj->read_domains |= I915_GEM_GPU_DOMAINS;
1026 if (flags & EXEC_OBJECT_NEEDS_FENCE)
1027 i915_gem_active_set(&vma->last_fence, rq);
1029 export_fence(vma, rq, flags);
1033 int i915_vma_unbind(struct i915_vma *vma)
1037 lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
1040 * First wait upon any activity as retiring the request may
1041 * have side-effects such as unpinning or even unbinding this vma.
1044 if (i915_vma_is_active(vma)) {
1045 struct i915_vma_active *active, *n;
1048 * When a closed VMA is retired, it is unbound - eek.
1049 * In order to prevent it from being recursively closed,
1050 * take a pin on the vma so that the second unbind is
1053 * Even more scary is that the retire callback may free
1054 * the object (last active vma). To prevent the explosion
1055 * we defer the actual object free to a worker that can
1056 * only proceed once it acquires the struct_mutex (which
1057 * we currently hold, therefore it cannot free this object
1058 * before we are finished).
1060 __i915_vma_pin(vma);
1062 ret = i915_gem_active_retire(&vma->last_active,
1063 &vma->vm->i915->drm.struct_mutex);
1067 rbtree_postorder_for_each_entry_safe(active, n,
1068 &vma->active, node) {
1069 ret = i915_gem_active_retire(&active->base,
1070 &vma->vm->i915->drm.struct_mutex);
1075 ret = i915_gem_active_retire(&vma->last_fence,
1076 &vma->vm->i915->drm.struct_mutex);
1078 __i915_vma_unpin(vma);
1082 GEM_BUG_ON(i915_vma_is_active(vma));
1084 if (i915_vma_is_pinned(vma)) {
1085 vma_print_allocator(vma, "is pinned");
1089 if (!drm_mm_node_allocated(&vma->node))
1092 if (i915_vma_is_map_and_fenceable(vma)) {
1094 * Check that we have flushed all writes through the GGTT
1095 * before the unbind, other due to non-strict nature of those
1096 * indirect writes they may end up referencing the GGTT PTE
1099 i915_vma_flush_writes(vma);
1100 GEM_BUG_ON(i915_vma_has_ggtt_write(vma));
1102 /* release the fence reg _after_ flushing */
1103 ret = i915_vma_put_fence(vma);
1107 /* Force a pagefault for domain tracking on next user access */
1108 i915_vma_revoke_mmap(vma);
1110 __i915_vma_iounmap(vma);
1111 vma->flags &= ~I915_VMA_CAN_FENCE;
1113 GEM_BUG_ON(vma->fence);
1114 GEM_BUG_ON(i915_vma_has_userfault(vma));
1116 if (likely(!vma->vm->closed)) {
1117 trace_i915_vma_unbind(vma);
1118 vma->ops->unbind_vma(vma);
1120 vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
1122 i915_vma_remove(vma);
1127 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1128 #include "selftests/i915_vma.c"