xref: /dragonfly/sys/dev/drm/i915/i915_vma.c (revision 3f2dd94a)
14be47400SFrançois Tigeot /*
24be47400SFrançois Tigeot  * Copyright © 2016 Intel Corporation
34be47400SFrançois Tigeot  *
44be47400SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
54be47400SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
64be47400SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
74be47400SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84be47400SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
94be47400SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
104be47400SFrançois Tigeot  *
114be47400SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
124be47400SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
134be47400SFrançois Tigeot  * Software.
144be47400SFrançois Tigeot  *
154be47400SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
164be47400SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174be47400SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
184be47400SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
194be47400SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
204be47400SFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
214be47400SFrançois Tigeot  * IN THE SOFTWARE.
224be47400SFrançois Tigeot  *
234be47400SFrançois Tigeot  */
244be47400SFrançois Tigeot 
254be47400SFrançois Tigeot #include "i915_vma.h"
264be47400SFrançois Tigeot 
274be47400SFrançois Tigeot #include "i915_drv.h"
284be47400SFrançois Tigeot #include "intel_ringbuffer.h"
294be47400SFrançois Tigeot #include "intel_frontbuffer.h"
304be47400SFrançois Tigeot 
314be47400SFrançois Tigeot #include <drm/drm_gem.h>
324be47400SFrançois Tigeot 
334be47400SFrançois Tigeot static void
i915_vma_retire(struct i915_gem_active * active,struct drm_i915_gem_request * rq)344be47400SFrançois Tigeot i915_vma_retire(struct i915_gem_active *active,
354be47400SFrançois Tigeot 		struct drm_i915_gem_request *rq)
364be47400SFrançois Tigeot {
374be47400SFrançois Tigeot 	const unsigned int idx = rq->engine->id;
384be47400SFrançois Tigeot 	struct i915_vma *vma =
394be47400SFrançois Tigeot 		container_of(active, struct i915_vma, last_read[idx]);
404be47400SFrançois Tigeot 	struct drm_i915_gem_object *obj = vma->obj;
414be47400SFrançois Tigeot 
424be47400SFrançois Tigeot 	GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx));
434be47400SFrançois Tigeot 
444be47400SFrançois Tigeot 	i915_vma_clear_active(vma, idx);
454be47400SFrançois Tigeot 	if (i915_vma_is_active(vma))
464be47400SFrançois Tigeot 		return;
474be47400SFrançois Tigeot 
48a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
494be47400SFrançois Tigeot 	list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
504be47400SFrançois Tigeot 	if (unlikely(i915_vma_is_closed(vma) && !i915_vma_is_pinned(vma)))
514be47400SFrançois Tigeot 		WARN_ON(i915_vma_unbind(vma));
524be47400SFrançois Tigeot 
534be47400SFrançois Tigeot 	GEM_BUG_ON(!i915_gem_object_is_active(obj));
544be47400SFrançois Tigeot 	if (--obj->active_count)
554be47400SFrançois Tigeot 		return;
564be47400SFrançois Tigeot 
57*3f2dd94aSFrançois Tigeot 	/* Prune the shared fence arrays iff completely idle (inc. external) */
58*3f2dd94aSFrançois Tigeot 	if (reservation_object_trylock(obj->resv)) {
59*3f2dd94aSFrançois Tigeot 		if (reservation_object_test_signaled_rcu(obj->resv, true))
60*3f2dd94aSFrançois Tigeot 			reservation_object_add_excl_fence(obj->resv, NULL);
61*3f2dd94aSFrançois Tigeot 		reservation_object_unlock(obj->resv);
62*3f2dd94aSFrançois Tigeot 	}
63*3f2dd94aSFrançois Tigeot 
644be47400SFrançois Tigeot 	/* Bump our place on the bound list to keep it roughly in LRU order
654be47400SFrançois Tigeot 	 * so that we don't steal from recently used but inactive objects
664be47400SFrançois Tigeot 	 * (unless we are forced to ofc!)
674be47400SFrançois Tigeot 	 */
68*3f2dd94aSFrançois Tigeot 	lockmgr(&rq->i915->mm.obj_lock, LK_EXCLUSIVE);
694be47400SFrançois Tigeot 	if (obj->bind_count)
70*3f2dd94aSFrançois Tigeot 		list_move_tail(&obj->mm.link, &rq->i915->mm.bound_list);
71*3f2dd94aSFrançois Tigeot 	lockmgr(&rq->i915->mm.obj_lock, LK_RELEASE);
724be47400SFrançois Tigeot 
734be47400SFrançois Tigeot 	obj->mm.dirty = true; /* be paranoid  */
744be47400SFrançois Tigeot 
754be47400SFrançois Tigeot 	if (i915_gem_object_has_active_reference(obj)) {
764be47400SFrançois Tigeot 		i915_gem_object_clear_active_reference(obj);
774be47400SFrançois Tigeot 		i915_gem_object_put(obj);
784be47400SFrançois Tigeot 	}
794be47400SFrançois Tigeot }
804be47400SFrançois Tigeot 
814be47400SFrançois Tigeot static struct i915_vma *
vma_create(struct drm_i915_gem_object * obj,struct i915_address_space * vm,const struct i915_ggtt_view * view)82a85cb24fSFrançois Tigeot vma_create(struct drm_i915_gem_object *obj,
834be47400SFrançois Tigeot 	   struct i915_address_space *vm,
844be47400SFrançois Tigeot 	   const struct i915_ggtt_view *view)
854be47400SFrançois Tigeot {
864be47400SFrançois Tigeot 	struct i915_vma *vma;
874be47400SFrançois Tigeot 	struct rb_node *rb, **p;
884be47400SFrançois Tigeot 	int i;
894be47400SFrançois Tigeot 
90a85cb24fSFrançois Tigeot 	/* The aliasing_ppgtt should never be used directly! */
91a85cb24fSFrançois Tigeot 	GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
924be47400SFrançois Tigeot 
93a85cb24fSFrançois Tigeot 	vma = kmem_cache_zalloc(vm->i915->vmas, GFP_KERNEL);
944be47400SFrançois Tigeot 	if (vma == NULL)
954be47400SFrançois Tigeot 		return ERR_PTR(-ENOMEM);
964be47400SFrançois Tigeot 
974be47400SFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
984be47400SFrançois Tigeot 		init_request_active(&vma->last_read[i], i915_vma_retire);
994be47400SFrançois Tigeot 	init_request_active(&vma->last_fence, NULL);
1004be47400SFrançois Tigeot 	vma->vm = vm;
1014be47400SFrançois Tigeot 	vma->obj = obj;
102*3f2dd94aSFrançois Tigeot 	vma->resv = obj->resv;
1034be47400SFrançois Tigeot 	vma->size = obj->base.size;
104a85cb24fSFrançois Tigeot 	vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
1054be47400SFrançois Tigeot 
106a85cb24fSFrançois Tigeot 	if (view && view->type != I915_GGTT_VIEW_NORMAL) {
1074be47400SFrançois Tigeot 		vma->ggtt_view = *view;
1084be47400SFrançois Tigeot 		if (view->type == I915_GGTT_VIEW_PARTIAL) {
109a85cb24fSFrançois Tigeot 			GEM_BUG_ON(range_overflows_t(u64,
110a85cb24fSFrançois Tigeot 						     view->partial.offset,
111a85cb24fSFrançois Tigeot 						     view->partial.size,
112a85cb24fSFrançois Tigeot 						     obj->base.size >> PAGE_SHIFT));
113a85cb24fSFrançois Tigeot 			vma->size = view->partial.size;
1144be47400SFrançois Tigeot 			vma->size <<= PAGE_SHIFT;
115a85cb24fSFrançois Tigeot 			GEM_BUG_ON(vma->size >= obj->base.size);
1164be47400SFrançois Tigeot 		} else if (view->type == I915_GGTT_VIEW_ROTATED) {
117a85cb24fSFrançois Tigeot 			vma->size = intel_rotation_info_size(&view->rotated);
1184be47400SFrançois Tigeot 			vma->size <<= PAGE_SHIFT;
1194be47400SFrançois Tigeot 		}
1204be47400SFrançois Tigeot 	}
1214be47400SFrançois Tigeot 
122a85cb24fSFrançois Tigeot 	if (unlikely(vma->size > vm->total))
123a85cb24fSFrançois Tigeot 		goto err_vma;
124a85cb24fSFrançois Tigeot 
125a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
126a85cb24fSFrançois Tigeot 
1274be47400SFrançois Tigeot 	if (i915_is_ggtt(vm)) {
128a85cb24fSFrançois Tigeot 		if (unlikely(overflows_type(vma->size, u32)))
129a85cb24fSFrançois Tigeot 			goto err_vma;
130a85cb24fSFrançois Tigeot 
131a85cb24fSFrançois Tigeot 		vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
132a85cb24fSFrançois Tigeot 						      i915_gem_object_get_tiling(obj),
133a85cb24fSFrançois Tigeot 						      i915_gem_object_get_stride(obj));
134a85cb24fSFrançois Tigeot 		if (unlikely(vma->fence_size < vma->size || /* overflow */
135a85cb24fSFrançois Tigeot 			     vma->fence_size > vm->total))
136a85cb24fSFrançois Tigeot 			goto err_vma;
137a85cb24fSFrançois Tigeot 
138a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
139a85cb24fSFrançois Tigeot 
140a85cb24fSFrançois Tigeot 		vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
141a85cb24fSFrançois Tigeot 								i915_gem_object_get_tiling(obj),
142a85cb24fSFrançois Tigeot 								i915_gem_object_get_stride(obj));
143a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
144a85cb24fSFrançois Tigeot 
1454be47400SFrançois Tigeot 		vma->flags |= I915_VMA_GGTT;
1464be47400SFrançois Tigeot 		list_add(&vma->obj_link, &obj->vma_list);
1474be47400SFrançois Tigeot 	} else {
1484be47400SFrançois Tigeot 		i915_ppgtt_get(i915_vm_to_ppgtt(vm));
1494be47400SFrançois Tigeot 		list_add_tail(&vma->obj_link, &obj->vma_list);
1504be47400SFrançois Tigeot 	}
1514be47400SFrançois Tigeot 
1524be47400SFrançois Tigeot 	rb = NULL;
1534be47400SFrançois Tigeot 	p = &obj->vma_tree.rb_node;
1544be47400SFrançois Tigeot 	while (*p) {
1554be47400SFrançois Tigeot 		struct i915_vma *pos;
1564be47400SFrançois Tigeot 
1574be47400SFrançois Tigeot 		rb = *p;
1584be47400SFrançois Tigeot 		pos = rb_entry(rb, struct i915_vma, obj_node);
1594be47400SFrançois Tigeot 		if (i915_vma_compare(pos, vm, view) < 0)
1604be47400SFrançois Tigeot 			p = &rb->rb_right;
1614be47400SFrançois Tigeot 		else
1624be47400SFrançois Tigeot 			p = &rb->rb_left;
1634be47400SFrançois Tigeot 	}
1644be47400SFrançois Tigeot 	rb_link_node(&vma->obj_node, rb, p);
1654be47400SFrançois Tigeot 	rb_insert_color(&vma->obj_node, &obj->vma_tree);
166a85cb24fSFrançois Tigeot 	list_add(&vma->vm_link, &vm->unbound_list);
1674be47400SFrançois Tigeot 
1684be47400SFrançois Tigeot 	return vma;
169a85cb24fSFrançois Tigeot 
170a85cb24fSFrançois Tigeot err_vma:
171a85cb24fSFrançois Tigeot 	kmem_cache_free(vm->i915->vmas, vma);
172a85cb24fSFrançois Tigeot 	return ERR_PTR(-E2BIG);
1734be47400SFrançois Tigeot }
1744be47400SFrançois Tigeot 
175a85cb24fSFrançois Tigeot static struct i915_vma *
vma_lookup(struct drm_i915_gem_object * obj,struct i915_address_space * vm,const struct i915_ggtt_view * view)176a85cb24fSFrançois Tigeot vma_lookup(struct drm_i915_gem_object *obj,
1774be47400SFrançois Tigeot 	   struct i915_address_space *vm,
1784be47400SFrançois Tigeot 	   const struct i915_ggtt_view *view)
1794be47400SFrançois Tigeot {
180a85cb24fSFrançois Tigeot 	struct rb_node *rb;
181a85cb24fSFrançois Tigeot 
182a85cb24fSFrançois Tigeot 	rb = obj->vma_tree.rb_node;
183a85cb24fSFrançois Tigeot 	while (rb) {
184a85cb24fSFrançois Tigeot 		struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
185a85cb24fSFrançois Tigeot 		long cmp;
186a85cb24fSFrançois Tigeot 
187a85cb24fSFrançois Tigeot 		cmp = i915_vma_compare(vma, vm, view);
188a85cb24fSFrançois Tigeot 		if (cmp == 0)
189a85cb24fSFrançois Tigeot 			return vma;
190a85cb24fSFrançois Tigeot 
191a85cb24fSFrançois Tigeot 		if (cmp < 0)
192a85cb24fSFrançois Tigeot 			rb = rb->rb_right;
193a85cb24fSFrançois Tigeot 		else
194a85cb24fSFrançois Tigeot 			rb = rb->rb_left;
195a85cb24fSFrançois Tigeot 	}
196a85cb24fSFrançois Tigeot 
197a85cb24fSFrançois Tigeot 	return NULL;
198a85cb24fSFrançois Tigeot }
199a85cb24fSFrançois Tigeot 
200a85cb24fSFrançois Tigeot /**
201a85cb24fSFrançois Tigeot  * i915_vma_instance - return the singleton instance of the VMA
202a85cb24fSFrançois Tigeot  * @obj: parent &struct drm_i915_gem_object to be mapped
203a85cb24fSFrançois Tigeot  * @vm: address space in which the mapping is located
204a85cb24fSFrançois Tigeot  * @view: additional mapping requirements
205a85cb24fSFrançois Tigeot  *
206a85cb24fSFrançois Tigeot  * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
207a85cb24fSFrançois Tigeot  * the same @view characteristics. If a match is not found, one is created.
208a85cb24fSFrançois Tigeot  * Once created, the VMA is kept until either the object is freed, or the
209a85cb24fSFrançois Tigeot  * address space is closed.
210a85cb24fSFrançois Tigeot  *
211a85cb24fSFrançois Tigeot  * Must be called with struct_mutex held.
212a85cb24fSFrançois Tigeot  *
213a85cb24fSFrançois Tigeot  * Returns the vma, or an error pointer.
214a85cb24fSFrançois Tigeot  */
215a85cb24fSFrançois Tigeot struct i915_vma *
i915_vma_instance(struct drm_i915_gem_object * obj,struct i915_address_space * vm,const struct i915_ggtt_view * view)216a85cb24fSFrançois Tigeot i915_vma_instance(struct drm_i915_gem_object *obj,
217a85cb24fSFrançois Tigeot 		  struct i915_address_space *vm,
218a85cb24fSFrançois Tigeot 		  const struct i915_ggtt_view *view)
219a85cb24fSFrançois Tigeot {
220a85cb24fSFrançois Tigeot 	struct i915_vma *vma;
221a85cb24fSFrançois Tigeot 
2224be47400SFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
2234be47400SFrançois Tigeot 	GEM_BUG_ON(view && !i915_is_ggtt(vm));
224a85cb24fSFrançois Tigeot 	GEM_BUG_ON(vm->closed);
2254be47400SFrançois Tigeot 
226a85cb24fSFrançois Tigeot 	vma = vma_lookup(obj, vm, view);
227a85cb24fSFrançois Tigeot 	if (!vma)
228a85cb24fSFrançois Tigeot 		vma = vma_create(obj, vm, view);
229a85cb24fSFrançois Tigeot 
230a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!IS_ERR(vma) && i915_vma_is_closed(vma));
231a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
232a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!IS_ERR(vma) && vma_lookup(obj, vm, view) != vma);
233a85cb24fSFrançois Tigeot 	return vma;
2344be47400SFrançois Tigeot }
2354be47400SFrançois Tigeot 
2364be47400SFrançois Tigeot /**
2374be47400SFrançois Tigeot  * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
2384be47400SFrançois Tigeot  * @vma: VMA to map
2394be47400SFrançois Tigeot  * @cache_level: mapping cache level
2404be47400SFrançois Tigeot  * @flags: flags like global or local mapping
2414be47400SFrançois Tigeot  *
2424be47400SFrançois Tigeot  * DMA addresses are taken from the scatter-gather table of this object (or of
2434be47400SFrançois Tigeot  * this VMA in case of non-default GGTT views) and PTE entries set up.
2444be47400SFrançois Tigeot  * Note that DMA addresses are also the only part of the SG table we care about.
2454be47400SFrançois Tigeot  */
i915_vma_bind(struct i915_vma * vma,enum i915_cache_level cache_level,u32 flags)2464be47400SFrançois Tigeot int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
2474be47400SFrançois Tigeot 		  u32 flags)
2484be47400SFrançois Tigeot {
2494be47400SFrançois Tigeot 	u32 bind_flags;
2504be47400SFrançois Tigeot 	u32 vma_flags;
2514be47400SFrançois Tigeot 	int ret;
2524be47400SFrançois Tigeot 
253a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
254a85cb24fSFrançois Tigeot 	GEM_BUG_ON(vma->size > vma->node.size);
255a85cb24fSFrançois Tigeot 
256a85cb24fSFrançois Tigeot 	if (GEM_WARN_ON(range_overflows(vma->node.start,
257a85cb24fSFrançois Tigeot 					vma->node.size,
258a85cb24fSFrançois Tigeot 					vma->vm->total)))
259a85cb24fSFrançois Tigeot 		return -ENODEV;
260a85cb24fSFrançois Tigeot 
261a85cb24fSFrançois Tigeot 	if (GEM_WARN_ON(!flags))
2624be47400SFrançois Tigeot 		return -EINVAL;
2634be47400SFrançois Tigeot 
2644be47400SFrançois Tigeot 	bind_flags = 0;
2654be47400SFrançois Tigeot 	if (flags & PIN_GLOBAL)
2664be47400SFrançois Tigeot 		bind_flags |= I915_VMA_GLOBAL_BIND;
2674be47400SFrançois Tigeot 	if (flags & PIN_USER)
2684be47400SFrançois Tigeot 		bind_flags |= I915_VMA_LOCAL_BIND;
2694be47400SFrançois Tigeot 
2704be47400SFrançois Tigeot 	vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
2714be47400SFrançois Tigeot 	if (flags & PIN_UPDATE)
2724be47400SFrançois Tigeot 		bind_flags |= vma_flags;
2734be47400SFrançois Tigeot 	else
2744be47400SFrançois Tigeot 		bind_flags &= ~vma_flags;
2754be47400SFrançois Tigeot 	if (bind_flags == 0)
2764be47400SFrançois Tigeot 		return 0;
2774be47400SFrançois Tigeot 
278*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!vma->pages);
279*3f2dd94aSFrançois Tigeot 
2804be47400SFrançois Tigeot 	trace_i915_vma_bind(vma, bind_flags);
2814be47400SFrançois Tigeot 	ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
2824be47400SFrançois Tigeot 	if (ret)
2834be47400SFrançois Tigeot 		return ret;
2844be47400SFrançois Tigeot 
2854be47400SFrançois Tigeot 	vma->flags |= bind_flags;
2864be47400SFrançois Tigeot 	return 0;
2874be47400SFrançois Tigeot }
2884be47400SFrançois Tigeot 
i915_vma_pin_iomap(struct i915_vma * vma)2894be47400SFrançois Tigeot void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
2904be47400SFrançois Tigeot {
2914be47400SFrançois Tigeot 	void __iomem *ptr;
292*3f2dd94aSFrançois Tigeot 	int err;
2934be47400SFrançois Tigeot 
2944be47400SFrançois Tigeot 	/* Access through the GTT requires the device to be awake. */
295a85cb24fSFrançois Tigeot 	assert_rpm_wakelock_held(vma->vm->i915);
2964be47400SFrançois Tigeot 
297a85cb24fSFrançois Tigeot 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
298*3f2dd94aSFrançois Tigeot 	if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
299*3f2dd94aSFrançois Tigeot 		err = -ENODEV;
300*3f2dd94aSFrançois Tigeot 		goto err;
301*3f2dd94aSFrançois Tigeot 	}
3024be47400SFrançois Tigeot 
3034be47400SFrançois Tigeot 	GEM_BUG_ON(!i915_vma_is_ggtt(vma));
3044be47400SFrançois Tigeot 	GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
3054be47400SFrançois Tigeot 
3064be47400SFrançois Tigeot 	ptr = vma->iomap;
3074be47400SFrançois Tigeot 	if (ptr == NULL) {
3084be47400SFrançois Tigeot 		ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->mappable,
3094be47400SFrançois Tigeot 					vma->node.start,
3104be47400SFrançois Tigeot 					vma->node.size);
311*3f2dd94aSFrançois Tigeot 		if (ptr == NULL) {
312*3f2dd94aSFrançois Tigeot 			err = -ENOMEM;
313*3f2dd94aSFrançois Tigeot 			goto err;
314*3f2dd94aSFrançois Tigeot 		}
3154be47400SFrançois Tigeot 
3164be47400SFrançois Tigeot 		vma->iomap = ptr;
3174be47400SFrançois Tigeot 	}
3184be47400SFrançois Tigeot 
3194be47400SFrançois Tigeot 	__i915_vma_pin(vma);
320*3f2dd94aSFrançois Tigeot 
321*3f2dd94aSFrançois Tigeot 	err = i915_vma_pin_fence(vma);
322*3f2dd94aSFrançois Tigeot 	if (err)
323*3f2dd94aSFrançois Tigeot 		goto err_unpin;
324*3f2dd94aSFrançois Tigeot 
3254be47400SFrançois Tigeot 	return ptr;
326*3f2dd94aSFrançois Tigeot 
327*3f2dd94aSFrançois Tigeot err_unpin:
328*3f2dd94aSFrançois Tigeot 	__i915_vma_unpin(vma);
329*3f2dd94aSFrançois Tigeot err:
330*3f2dd94aSFrançois Tigeot 	return IO_ERR_PTR(err);
331*3f2dd94aSFrançois Tigeot }
332*3f2dd94aSFrançois Tigeot 
i915_vma_unpin_iomap(struct i915_vma * vma)333*3f2dd94aSFrançois Tigeot void i915_vma_unpin_iomap(struct i915_vma *vma)
334*3f2dd94aSFrançois Tigeot {
335*3f2dd94aSFrançois Tigeot 	lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
336*3f2dd94aSFrançois Tigeot 
337*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(vma->iomap == NULL);
338*3f2dd94aSFrançois Tigeot 
339*3f2dd94aSFrançois Tigeot 	i915_vma_unpin_fence(vma);
340*3f2dd94aSFrançois Tigeot 	i915_vma_unpin(vma);
3414be47400SFrançois Tigeot }
3424be47400SFrançois Tigeot 
i915_vma_unpin_and_release(struct i915_vma ** p_vma)3434be47400SFrançois Tigeot void i915_vma_unpin_and_release(struct i915_vma **p_vma)
3444be47400SFrançois Tigeot {
3454be47400SFrançois Tigeot 	struct i915_vma *vma;
3464be47400SFrançois Tigeot 	struct drm_i915_gem_object *obj;
3474be47400SFrançois Tigeot 
3484be47400SFrançois Tigeot 	vma = fetch_and_zero(p_vma);
3494be47400SFrançois Tigeot 	if (!vma)
3504be47400SFrançois Tigeot 		return;
3514be47400SFrançois Tigeot 
3524be47400SFrançois Tigeot 	obj = vma->obj;
3534be47400SFrançois Tigeot 
3544be47400SFrançois Tigeot 	i915_vma_unpin(vma);
3554be47400SFrançois Tigeot 	i915_vma_close(vma);
3564be47400SFrançois Tigeot 
3574be47400SFrançois Tigeot 	__i915_gem_object_release_unless_active(obj);
3584be47400SFrançois Tigeot }
3594be47400SFrançois Tigeot 
i915_vma_misplaced(const struct i915_vma * vma,u64 size,u64 alignment,u64 flags)360a85cb24fSFrançois Tigeot bool i915_vma_misplaced(const struct i915_vma *vma,
361a85cb24fSFrançois Tigeot 			u64 size, u64 alignment, u64 flags)
3624be47400SFrançois Tigeot {
3634be47400SFrançois Tigeot 	if (!drm_mm_node_allocated(&vma->node))
3644be47400SFrançois Tigeot 		return false;
3654be47400SFrançois Tigeot 
3664be47400SFrançois Tigeot 	if (vma->node.size < size)
3674be47400SFrançois Tigeot 		return true;
3684be47400SFrançois Tigeot 
369a85cb24fSFrançois Tigeot 	GEM_BUG_ON(alignment && !is_power_of_2(alignment));
370a85cb24fSFrançois Tigeot 	if (alignment && !IS_ALIGNED(vma->node.start, alignment))
3714be47400SFrançois Tigeot 		return true;
3724be47400SFrançois Tigeot 
3734be47400SFrançois Tigeot 	if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
3744be47400SFrançois Tigeot 		return true;
3754be47400SFrançois Tigeot 
3764be47400SFrançois Tigeot 	if (flags & PIN_OFFSET_BIAS &&
3774be47400SFrançois Tigeot 	    vma->node.start < (flags & PIN_OFFSET_MASK))
3784be47400SFrançois Tigeot 		return true;
3794be47400SFrançois Tigeot 
3804be47400SFrançois Tigeot 	if (flags & PIN_OFFSET_FIXED &&
3814be47400SFrançois Tigeot 	    vma->node.start != (flags & PIN_OFFSET_MASK))
3824be47400SFrançois Tigeot 		return true;
3834be47400SFrançois Tigeot 
3844be47400SFrançois Tigeot 	return false;
3854be47400SFrançois Tigeot }
3864be47400SFrançois Tigeot 
__i915_vma_set_map_and_fenceable(struct i915_vma * vma)3874be47400SFrançois Tigeot void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
3884be47400SFrançois Tigeot {
3894be47400SFrançois Tigeot 	bool mappable, fenceable;
3904be47400SFrançois Tigeot 
391a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!i915_vma_is_ggtt(vma));
392a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!vma->fence_size);
3934be47400SFrançois Tigeot 
3944be47400SFrançois Tigeot 	/*
3954be47400SFrançois Tigeot 	 * Explicitly disable for rotated VMA since the display does not
3964be47400SFrançois Tigeot 	 * need the fence and the VMA is not accessible to other users.
3974be47400SFrançois Tigeot 	 */
398a85cb24fSFrançois Tigeot 	if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
399a85cb24fSFrançois Tigeot 		return;
400a85cb24fSFrançois Tigeot 
401a85cb24fSFrançois Tigeot 	fenceable = (vma->node.size >= vma->fence_size &&
402a85cb24fSFrançois Tigeot 		     IS_ALIGNED(vma->node.start, vma->fence_alignment));
403a85cb24fSFrançois Tigeot 
404a85cb24fSFrançois Tigeot 	mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
405a85cb24fSFrançois Tigeot 
406a85cb24fSFrançois Tigeot 	if (mappable && fenceable)
4074be47400SFrançois Tigeot 		vma->flags |= I915_VMA_CAN_FENCE;
4084be47400SFrançois Tigeot 	else
4094be47400SFrançois Tigeot 		vma->flags &= ~I915_VMA_CAN_FENCE;
4104be47400SFrançois Tigeot }
4114be47400SFrançois Tigeot 
color_differs(struct drm_mm_node * node,unsigned long color)412a85cb24fSFrançois Tigeot static bool color_differs(struct drm_mm_node *node, unsigned long color)
4134be47400SFrançois Tigeot {
414a85cb24fSFrançois Tigeot 	return node->allocated && node->color != color;
415a85cb24fSFrançois Tigeot }
416a85cb24fSFrançois Tigeot 
i915_gem_valid_gtt_space(struct i915_vma * vma,unsigned long cache_level)417a85cb24fSFrançois Tigeot bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
418a85cb24fSFrançois Tigeot {
419a85cb24fSFrançois Tigeot 	struct drm_mm_node *node = &vma->node;
4204be47400SFrançois Tigeot 	struct drm_mm_node *other;
4214be47400SFrançois Tigeot 
4224be47400SFrançois Tigeot 	/*
4234be47400SFrançois Tigeot 	 * On some machines we have to be careful when putting differing types
4244be47400SFrançois Tigeot 	 * of snoopable memory together to avoid the prefetcher crossing memory
4254be47400SFrançois Tigeot 	 * domains and dying. During vm initialisation, we decide whether or not
4264be47400SFrançois Tigeot 	 * these constraints apply and set the drm_mm.color_adjust
4274be47400SFrançois Tigeot 	 * appropriately.
4284be47400SFrançois Tigeot 	 */
4294be47400SFrançois Tigeot 	if (vma->vm->mm.color_adjust == NULL)
4304be47400SFrançois Tigeot 		return true;
4314be47400SFrançois Tigeot 
432a85cb24fSFrançois Tigeot 	/* Only valid to be called on an already inserted vma */
433a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!drm_mm_node_allocated(node));
434a85cb24fSFrançois Tigeot 	GEM_BUG_ON(list_empty(&node->node_list));
4354be47400SFrançois Tigeot 
436a85cb24fSFrançois Tigeot 	other = list_prev_entry(node, node_list);
437a85cb24fSFrançois Tigeot 	if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
4384be47400SFrançois Tigeot 		return false;
4394be47400SFrançois Tigeot 
440a85cb24fSFrançois Tigeot 	other = list_next_entry(node, node_list);
441a85cb24fSFrançois Tigeot 	if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
4424be47400SFrançois Tigeot 		return false;
4434be47400SFrançois Tigeot 
4444be47400SFrançois Tigeot 	return true;
4454be47400SFrançois Tigeot }
4464be47400SFrançois Tigeot 
4474be47400SFrançois Tigeot /**
4484be47400SFrançois Tigeot  * i915_vma_insert - finds a slot for the vma in its address space
4494be47400SFrançois Tigeot  * @vma: the vma
4504be47400SFrançois Tigeot  * @size: requested size in bytes (can be larger than the VMA)
4514be47400SFrançois Tigeot  * @alignment: required alignment
4524be47400SFrançois Tigeot  * @flags: mask of PIN_* flags to use
4534be47400SFrançois Tigeot  *
4544be47400SFrançois Tigeot  * First we try to allocate some free space that meets the requirements for
4554be47400SFrançois Tigeot  * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
4564be47400SFrançois Tigeot  * preferrably the oldest idle entry to make room for the new VMA.
4574be47400SFrançois Tigeot  *
4584be47400SFrançois Tigeot  * Returns:
4594be47400SFrançois Tigeot  * 0 on success, negative error code otherwise.
4604be47400SFrançois Tigeot  */
4614be47400SFrançois Tigeot static int
i915_vma_insert(struct i915_vma * vma,u64 size,u64 alignment,u64 flags)4624be47400SFrançois Tigeot i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
4634be47400SFrançois Tigeot {
464a85cb24fSFrançois Tigeot 	struct drm_i915_private *dev_priv = vma->vm->i915;
4654be47400SFrançois Tigeot 	struct drm_i915_gem_object *obj = vma->obj;
4664be47400SFrançois Tigeot 	u64 start, end;
4674be47400SFrançois Tigeot 	int ret;
4684be47400SFrançois Tigeot 
4694be47400SFrançois Tigeot 	GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
4704be47400SFrançois Tigeot 	GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
4714be47400SFrançois Tigeot 
4724be47400SFrançois Tigeot 	size = max(size, vma->size);
473a85cb24fSFrançois Tigeot 	alignment = max(alignment, vma->display_alignment);
474a85cb24fSFrançois Tigeot 	if (flags & PIN_MAPPABLE) {
475a85cb24fSFrançois Tigeot 		size = max_t(typeof(size), size, vma->fence_size);
476a85cb24fSFrançois Tigeot 		alignment = max_t(typeof(alignment),
477a85cb24fSFrançois Tigeot 				  alignment, vma->fence_alignment);
478a85cb24fSFrançois Tigeot 	}
4794be47400SFrançois Tigeot 
480a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
481a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
482a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!is_power_of_2(alignment));
4834be47400SFrançois Tigeot 
4844be47400SFrançois Tigeot 	start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
485a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
4864be47400SFrançois Tigeot 
4874be47400SFrançois Tigeot 	end = vma->vm->total;
4884be47400SFrançois Tigeot 	if (flags & PIN_MAPPABLE)
4894be47400SFrançois Tigeot 		end = min_t(u64, end, dev_priv->ggtt.mappable_end);
4904be47400SFrançois Tigeot 	if (flags & PIN_ZONE_4G)
491a85cb24fSFrançois Tigeot 		end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
492a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
4934be47400SFrançois Tigeot 
4944be47400SFrançois Tigeot 	/* If binding the object/GGTT view requires more space than the entire
4954be47400SFrançois Tigeot 	 * aperture has, reject it early before evicting everything in a vain
4964be47400SFrançois Tigeot 	 * attempt to find space.
4974be47400SFrançois Tigeot 	 */
4984be47400SFrançois Tigeot 	if (size > end) {
4994be47400SFrançois Tigeot 		DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n",
5004be47400SFrançois Tigeot 			  size, obj->base.size,
5014be47400SFrançois Tigeot 			  flags & PIN_MAPPABLE ? "mappable" : "total",
5024be47400SFrançois Tigeot 			  end);
503*3f2dd94aSFrançois Tigeot 		return -ENOSPC;
5044be47400SFrançois Tigeot 	}
5054be47400SFrançois Tigeot 
5064be47400SFrançois Tigeot 	ret = i915_gem_object_pin_pages(obj);
5074be47400SFrançois Tigeot 	if (ret)
5084be47400SFrançois Tigeot 		return ret;
5094be47400SFrançois Tigeot 
510*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(vma->pages);
511*3f2dd94aSFrançois Tigeot 
512*3f2dd94aSFrançois Tigeot 	ret = vma->vm->set_pages(vma);
513*3f2dd94aSFrançois Tigeot 	if (ret)
514*3f2dd94aSFrançois Tigeot 		goto err_unpin;
515*3f2dd94aSFrançois Tigeot 
5164be47400SFrançois Tigeot 	if (flags & PIN_OFFSET_FIXED) {
5174be47400SFrançois Tigeot 		u64 offset = flags & PIN_OFFSET_MASK;
518a85cb24fSFrançois Tigeot 		if (!IS_ALIGNED(offset, alignment) ||
519a85cb24fSFrançois Tigeot 		    range_overflows(offset, size, end)) {
5204be47400SFrançois Tigeot 			ret = -EINVAL;
521*3f2dd94aSFrançois Tigeot 			goto err_clear;
5224be47400SFrançois Tigeot 		}
5234be47400SFrançois Tigeot 
524a85cb24fSFrançois Tigeot 		ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
525a85cb24fSFrançois Tigeot 					   size, offset, obj->cache_level,
526a85cb24fSFrançois Tigeot 					   flags);
5274be47400SFrançois Tigeot 		if (ret)
528*3f2dd94aSFrançois Tigeot 			goto err_clear;
5294be47400SFrançois Tigeot 	} else {
530*3f2dd94aSFrançois Tigeot 		/*
531*3f2dd94aSFrançois Tigeot 		 * We only support huge gtt pages through the 48b PPGTT,
532*3f2dd94aSFrançois Tigeot 		 * however we also don't want to force any alignment for
533*3f2dd94aSFrançois Tigeot 		 * objects which need to be tightly packed into the low 32bits.
534*3f2dd94aSFrançois Tigeot 		 *
535*3f2dd94aSFrançois Tigeot 		 * Note that we assume that GGTT are limited to 4GiB for the
536*3f2dd94aSFrançois Tigeot 		 * forseeable future. See also i915_ggtt_offset().
537*3f2dd94aSFrançois Tigeot 		 */
538*3f2dd94aSFrançois Tigeot 		if (upper_32_bits(end - 1) &&
539*3f2dd94aSFrançois Tigeot 		    vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
540*3f2dd94aSFrançois Tigeot 			/*
541*3f2dd94aSFrançois Tigeot 			 * We can't mix 64K and 4K PTEs in the same page-table
542*3f2dd94aSFrançois Tigeot 			 * (2M block), and so to avoid the ugliness and
543*3f2dd94aSFrançois Tigeot 			 * complexity of coloring we opt for just aligning 64K
544*3f2dd94aSFrançois Tigeot 			 * objects to 2M.
545*3f2dd94aSFrançois Tigeot 			 */
546*3f2dd94aSFrançois Tigeot 			u64 page_alignment =
547*3f2dd94aSFrançois Tigeot 				rounddown_pow_of_two(vma->page_sizes.sg |
548*3f2dd94aSFrançois Tigeot 						     I915_GTT_PAGE_SIZE_2M);
549*3f2dd94aSFrançois Tigeot 
550*3f2dd94aSFrançois Tigeot 			/*
551*3f2dd94aSFrançois Tigeot 			 * Check we don't expand for the limited Global GTT
552*3f2dd94aSFrançois Tigeot 			 * (mappable aperture is even more precious!). This
553*3f2dd94aSFrançois Tigeot 			 * also checks that we exclude the aliasing-ppgtt.
554*3f2dd94aSFrançois Tigeot 			 */
555*3f2dd94aSFrançois Tigeot 			GEM_BUG_ON(i915_vma_is_ggtt(vma));
556*3f2dd94aSFrançois Tigeot 
557*3f2dd94aSFrançois Tigeot 			alignment = max(alignment, page_alignment);
558*3f2dd94aSFrançois Tigeot 
559*3f2dd94aSFrançois Tigeot 			if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
560*3f2dd94aSFrançois Tigeot 				size = round_up(size, I915_GTT_PAGE_SIZE_2M);
561*3f2dd94aSFrançois Tigeot 		}
562*3f2dd94aSFrançois Tigeot 
563a85cb24fSFrançois Tigeot 		ret = i915_gem_gtt_insert(vma->vm, &vma->node,
564a85cb24fSFrançois Tigeot 					  size, alignment, obj->cache_level,
565a85cb24fSFrançois Tigeot 					  start, end, flags);
566a85cb24fSFrançois Tigeot 		if (ret)
567*3f2dd94aSFrançois Tigeot 			goto err_clear;
5684be47400SFrançois Tigeot 
5694be47400SFrançois Tigeot 		GEM_BUG_ON(vma->node.start < start);
5704be47400SFrançois Tigeot 		GEM_BUG_ON(vma->node.start + vma->node.size > end);
5714be47400SFrançois Tigeot 	}
572a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
5734be47400SFrançois Tigeot 	GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
5744be47400SFrançois Tigeot 
5754be47400SFrançois Tigeot 	list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
576*3f2dd94aSFrançois Tigeot 
577*3f2dd94aSFrançois Tigeot 	lockmgr(&dev_priv->mm.obj_lock, LK_EXCLUSIVE);
578*3f2dd94aSFrançois Tigeot 	list_move_tail(&obj->mm.link, &dev_priv->mm.bound_list);
5794be47400SFrançois Tigeot 	obj->bind_count++;
580*3f2dd94aSFrançois Tigeot 	lockmgr(&dev_priv->mm.obj_lock, LK_RELEASE);
581*3f2dd94aSFrançois Tigeot 
5824be47400SFrançois Tigeot 	GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
5834be47400SFrançois Tigeot 
5844be47400SFrançois Tigeot 	return 0;
5854be47400SFrançois Tigeot 
586*3f2dd94aSFrançois Tigeot err_clear:
587*3f2dd94aSFrançois Tigeot 	vma->vm->clear_pages(vma);
5884be47400SFrançois Tigeot err_unpin:
5894be47400SFrançois Tigeot 	i915_gem_object_unpin_pages(obj);
5904be47400SFrançois Tigeot 	return ret;
5914be47400SFrançois Tigeot }
5924be47400SFrançois Tigeot 
593a85cb24fSFrançois Tigeot static void
i915_vma_remove(struct i915_vma * vma)594a85cb24fSFrançois Tigeot i915_vma_remove(struct i915_vma *vma)
595a85cb24fSFrançois Tigeot {
596*3f2dd94aSFrançois Tigeot 	struct drm_i915_private *i915 = vma->vm->i915;
597a85cb24fSFrançois Tigeot 	struct drm_i915_gem_object *obj = vma->obj;
598a85cb24fSFrançois Tigeot 
599a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
600a85cb24fSFrançois Tigeot 	GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
601a85cb24fSFrançois Tigeot 
602*3f2dd94aSFrançois Tigeot 	vma->vm->clear_pages(vma);
603*3f2dd94aSFrançois Tigeot 
604a85cb24fSFrançois Tigeot 	drm_mm_remove_node(&vma->node);
605a85cb24fSFrançois Tigeot 	list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
606a85cb24fSFrançois Tigeot 
607a85cb24fSFrançois Tigeot 	/* Since the unbound list is global, only move to that list if
608a85cb24fSFrançois Tigeot 	 * no more VMAs exist.
609a85cb24fSFrançois Tigeot 	 */
610*3f2dd94aSFrançois Tigeot 	lockmgr(&i915->mm.obj_lock, LK_EXCLUSIVE);
611a85cb24fSFrançois Tigeot 	if (--obj->bind_count == 0)
612*3f2dd94aSFrançois Tigeot 		list_move_tail(&obj->mm.link, &i915->mm.unbound_list);
613*3f2dd94aSFrançois Tigeot 	lockmgr(&i915->mm.obj_lock, LK_RELEASE);
614a85cb24fSFrançois Tigeot 
615a85cb24fSFrançois Tigeot 	/* And finally now the object is completely decoupled from this vma,
616a85cb24fSFrançois Tigeot 	 * we can drop its hold on the backing storage and allow it to be
617a85cb24fSFrançois Tigeot 	 * reaped by the shrinker.
618a85cb24fSFrançois Tigeot 	 */
619a85cb24fSFrançois Tigeot 	i915_gem_object_unpin_pages(obj);
620a85cb24fSFrançois Tigeot 	GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
621a85cb24fSFrançois Tigeot }
622a85cb24fSFrançois Tigeot 
__i915_vma_do_pin(struct i915_vma * vma,u64 size,u64 alignment,u64 flags)6234be47400SFrançois Tigeot int __i915_vma_do_pin(struct i915_vma *vma,
6244be47400SFrançois Tigeot 		      u64 size, u64 alignment, u64 flags)
6254be47400SFrançois Tigeot {
626a85cb24fSFrançois Tigeot 	const unsigned int bound = vma->flags;
6274be47400SFrançois Tigeot 	int ret;
6284be47400SFrançois Tigeot 
629a85cb24fSFrançois Tigeot 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
6304be47400SFrançois Tigeot 	GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
6314be47400SFrançois Tigeot 	GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
6324be47400SFrançois Tigeot 
6334be47400SFrançois Tigeot 	if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
6344be47400SFrançois Tigeot 		ret = -EBUSY;
635a85cb24fSFrançois Tigeot 		goto err_unpin;
6364be47400SFrançois Tigeot 	}
6374be47400SFrançois Tigeot 
6384be47400SFrançois Tigeot 	if ((bound & I915_VMA_BIND_MASK) == 0) {
6394be47400SFrançois Tigeot 		ret = i915_vma_insert(vma, size, alignment, flags);
6404be47400SFrançois Tigeot 		if (ret)
641a85cb24fSFrançois Tigeot 			goto err_unpin;
6424be47400SFrançois Tigeot 	}
6434be47400SFrançois Tigeot 
6444be47400SFrançois Tigeot 	ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
6454be47400SFrançois Tigeot 	if (ret)
646a85cb24fSFrançois Tigeot 		goto err_remove;
6474be47400SFrançois Tigeot 
6484be47400SFrançois Tigeot 	if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
6494be47400SFrançois Tigeot 		__i915_vma_set_map_and_fenceable(vma);
6504be47400SFrançois Tigeot 
651a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
6524be47400SFrançois Tigeot 	GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
6534be47400SFrançois Tigeot 	return 0;
6544be47400SFrançois Tigeot 
655a85cb24fSFrançois Tigeot err_remove:
656a85cb24fSFrançois Tigeot 	if ((bound & I915_VMA_BIND_MASK) == 0) {
657a85cb24fSFrançois Tigeot 		i915_vma_remove(vma);
658*3f2dd94aSFrançois Tigeot 		GEM_BUG_ON(vma->pages);
659a85cb24fSFrançois Tigeot 	}
660a85cb24fSFrançois Tigeot err_unpin:
6614be47400SFrançois Tigeot 	__i915_vma_unpin(vma);
6624be47400SFrançois Tigeot 	return ret;
6634be47400SFrançois Tigeot }
6644be47400SFrançois Tigeot 
i915_vma_destroy(struct i915_vma * vma)665*3f2dd94aSFrançois Tigeot static void i915_vma_destroy(struct i915_vma *vma)
6664be47400SFrançois Tigeot {
667*3f2dd94aSFrançois Tigeot 	int i;
668*3f2dd94aSFrançois Tigeot 
6694be47400SFrançois Tigeot 	GEM_BUG_ON(vma->node.allocated);
6704be47400SFrançois Tigeot 	GEM_BUG_ON(i915_vma_is_active(vma));
6714be47400SFrançois Tigeot 	GEM_BUG_ON(!i915_vma_is_closed(vma));
6724be47400SFrançois Tigeot 	GEM_BUG_ON(vma->fence);
6734be47400SFrançois Tigeot 
674*3f2dd94aSFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
675*3f2dd94aSFrançois Tigeot 		GEM_BUG_ON(i915_gem_active_isset(&vma->last_read[i]));
676*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(i915_gem_active_isset(&vma->last_fence));
677*3f2dd94aSFrançois Tigeot 
6784be47400SFrançois Tigeot 	list_del(&vma->vm_link);
6794be47400SFrançois Tigeot 	if (!i915_vma_is_ggtt(vma))
6804be47400SFrançois Tigeot 		i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
6814be47400SFrançois Tigeot 
6824be47400SFrançois Tigeot 	kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
6834be47400SFrançois Tigeot }
6844be47400SFrançois Tigeot 
i915_vma_close(struct i915_vma * vma)6854be47400SFrançois Tigeot void i915_vma_close(struct i915_vma *vma)
6864be47400SFrançois Tigeot {
6874be47400SFrançois Tigeot 	GEM_BUG_ON(i915_vma_is_closed(vma));
6884be47400SFrançois Tigeot 	vma->flags |= I915_VMA_CLOSED;
6894be47400SFrançois Tigeot 
6904be47400SFrançois Tigeot 	list_del(&vma->obj_link);
6914be47400SFrançois Tigeot 	rb_erase(&vma->obj_node, &vma->obj->vma_tree);
6924be47400SFrançois Tigeot 
6934be47400SFrançois Tigeot 	if (!i915_vma_is_active(vma) && !i915_vma_is_pinned(vma))
6944be47400SFrançois Tigeot 		WARN_ON(i915_vma_unbind(vma));
6954be47400SFrançois Tigeot }
6964be47400SFrançois Tigeot 
__i915_vma_iounmap(struct i915_vma * vma)6974be47400SFrançois Tigeot static void __i915_vma_iounmap(struct i915_vma *vma)
6984be47400SFrançois Tigeot {
6994be47400SFrançois Tigeot 	GEM_BUG_ON(i915_vma_is_pinned(vma));
7004be47400SFrançois Tigeot 
7014be47400SFrançois Tigeot 	if (vma->iomap == NULL)
7024be47400SFrançois Tigeot 		return;
7034be47400SFrançois Tigeot 
7044be47400SFrançois Tigeot 	io_mapping_unmap(vma->iomap);
7054be47400SFrançois Tigeot 	vma->iomap = NULL;
7064be47400SFrançois Tigeot }
7074be47400SFrançois Tigeot 
708*3f2dd94aSFrançois Tigeot #ifdef __DragonFly__
709*3f2dd94aSFrançois Tigeot /*
710*3f2dd94aSFrançois Tigeot  * XXX: this is a bit of a hammer approach
711*3f2dd94aSFrançois Tigeot  * On DragonFly, we invalidate the entire set of gem object pages
712*3f2dd94aSFrançois Tigeot  * and not just the ones belonging to a particular VMA
713*3f2dd94aSFrançois Tigeot  */
drm_vma_node_unmap(struct drm_vma_offset_node * node,struct address_space * file_mapping)714*3f2dd94aSFrançois Tigeot static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
715*3f2dd94aSFrançois Tigeot 				      struct address_space *file_mapping)
716*3f2dd94aSFrançois Tigeot {
717*3f2dd94aSFrançois Tigeot 	struct drm_i915_gem_object *obj = container_of(
718*3f2dd94aSFrançois Tigeot 		node,struct drm_i915_gem_object, base.vma_node);
719*3f2dd94aSFrançois Tigeot 	vm_object_t devobj;
720*3f2dd94aSFrançois Tigeot 	vm_page_t m;
721*3f2dd94aSFrançois Tigeot 	int i, page_count;
722*3f2dd94aSFrançois Tigeot 
723*3f2dd94aSFrançois Tigeot 	devobj = cdev_pager_lookup(obj);
724*3f2dd94aSFrançois Tigeot 	if (devobj != NULL) {
725*3f2dd94aSFrançois Tigeot 		page_count = OFF_TO_IDX(obj->base.size);
726*3f2dd94aSFrançois Tigeot 
727*3f2dd94aSFrançois Tigeot 		VM_OBJECT_LOCK(devobj);
728*3f2dd94aSFrançois Tigeot 		vm_object_page_remove(devobj, 0, 0, false);
729*3f2dd94aSFrançois Tigeot #if 1
730*3f2dd94aSFrançois Tigeot 		for (i = 0; i < page_count; i++) {
731*3f2dd94aSFrançois Tigeot 			m = vm_page_lookup_busy_wait(devobj, i, TRUE, "915unm");
732*3f2dd94aSFrançois Tigeot 			if (m == NULL)
733*3f2dd94aSFrançois Tigeot 				continue;
734*3f2dd94aSFrançois Tigeot 			cdev_pager_free_page(devobj, m);
735*3f2dd94aSFrançois Tigeot 		}
736*3f2dd94aSFrançois Tigeot #endif
737*3f2dd94aSFrançois Tigeot 		VM_OBJECT_UNLOCK(devobj);
738*3f2dd94aSFrançois Tigeot 		vm_object_deallocate(devobj);
739*3f2dd94aSFrançois Tigeot 	}
740*3f2dd94aSFrançois Tigeot }
741*3f2dd94aSFrançois Tigeot #endif
742*3f2dd94aSFrançois Tigeot 
i915_vma_revoke_mmap(struct i915_vma * vma)743*3f2dd94aSFrançois Tigeot void i915_vma_revoke_mmap(struct i915_vma *vma)
744*3f2dd94aSFrançois Tigeot {
745*3f2dd94aSFrançois Tigeot 	struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
746*3f2dd94aSFrançois Tigeot 	u64 vma_offset;
747*3f2dd94aSFrançois Tigeot 
748*3f2dd94aSFrançois Tigeot 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
749*3f2dd94aSFrançois Tigeot 
750*3f2dd94aSFrançois Tigeot 	if (!i915_vma_has_userfault(vma))
751*3f2dd94aSFrançois Tigeot 		return;
752*3f2dd94aSFrançois Tigeot 
753*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
754*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!vma->obj->userfault_count);
755*3f2dd94aSFrançois Tigeot 
756*3f2dd94aSFrançois Tigeot 	vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
757*3f2dd94aSFrançois Tigeot #ifdef __DragonFly__
758*3f2dd94aSFrançois Tigeot 	drm_vma_node_unmap(node, NULL);
759*3f2dd94aSFrançois Tigeot #else
760*3f2dd94aSFrançois Tigeot 	unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
761*3f2dd94aSFrançois Tigeot 			    drm_vma_node_offset_addr(node) + vma_offset,
762*3f2dd94aSFrançois Tigeot 			    vma->size,
763*3f2dd94aSFrançois Tigeot 			    1);
764*3f2dd94aSFrançois Tigeot #endif
765*3f2dd94aSFrançois Tigeot 
766*3f2dd94aSFrançois Tigeot 	i915_vma_unset_userfault(vma);
767*3f2dd94aSFrançois Tigeot 	if (!--vma->obj->userfault_count)
768*3f2dd94aSFrançois Tigeot 		list_del(&vma->obj->userfault_link);
769*3f2dd94aSFrançois Tigeot }
770*3f2dd94aSFrançois Tigeot 
i915_vma_unbind(struct i915_vma * vma)7714be47400SFrançois Tigeot int i915_vma_unbind(struct i915_vma *vma)
7724be47400SFrançois Tigeot {
7734be47400SFrançois Tigeot 	struct drm_i915_gem_object *obj = vma->obj;
7744be47400SFrançois Tigeot 	unsigned long active;
7754be47400SFrançois Tigeot 	int ret;
7764be47400SFrançois Tigeot 
7774be47400SFrançois Tigeot 	lockdep_assert_held(&obj->base.dev->struct_mutex);
7784be47400SFrançois Tigeot 
7794be47400SFrançois Tigeot 	/* First wait upon any activity as retiring the request may
7804be47400SFrançois Tigeot 	 * have side-effects such as unpinning or even unbinding this vma.
7814be47400SFrançois Tigeot 	 */
7824be47400SFrançois Tigeot 	active = i915_vma_get_active(vma);
7834be47400SFrançois Tigeot 	if (active) {
7844be47400SFrançois Tigeot 		int idx;
7854be47400SFrançois Tigeot 
7864be47400SFrançois Tigeot 		/* When a closed VMA is retired, it is unbound - eek.
7874be47400SFrançois Tigeot 		 * In order to prevent it from being recursively closed,
7884be47400SFrançois Tigeot 		 * take a pin on the vma so that the second unbind is
7894be47400SFrançois Tigeot 		 * aborted.
7904be47400SFrançois Tigeot 		 *
7914be47400SFrançois Tigeot 		 * Even more scary is that the retire callback may free
7924be47400SFrançois Tigeot 		 * the object (last active vma). To prevent the explosion
7934be47400SFrançois Tigeot 		 * we defer the actual object free to a worker that can
7944be47400SFrançois Tigeot 		 * only proceed once it acquires the struct_mutex (which
7954be47400SFrançois Tigeot 		 * we currently hold, therefore it cannot free this object
7964be47400SFrançois Tigeot 		 * before we are finished).
7974be47400SFrançois Tigeot 		 */
7984be47400SFrançois Tigeot 		__i915_vma_pin(vma);
7994be47400SFrançois Tigeot 
8004be47400SFrançois Tigeot 		for_each_active(active, idx) {
8014be47400SFrançois Tigeot 			ret = i915_gem_active_retire(&vma->last_read[idx],
802a85cb24fSFrançois Tigeot 						     &vma->vm->i915->drm.struct_mutex);
8034be47400SFrançois Tigeot 			if (ret)
8044be47400SFrançois Tigeot 				break;
8054be47400SFrançois Tigeot 		}
8064be47400SFrançois Tigeot 
807a85cb24fSFrançois Tigeot 		if (!ret) {
808a85cb24fSFrançois Tigeot 			ret = i915_gem_active_retire(&vma->last_fence,
809a85cb24fSFrançois Tigeot 						     &vma->vm->i915->drm.struct_mutex);
810a85cb24fSFrançois Tigeot 		}
811a85cb24fSFrançois Tigeot 
8124be47400SFrançois Tigeot 		__i915_vma_unpin(vma);
8134be47400SFrançois Tigeot 		if (ret)
8144be47400SFrançois Tigeot 			return ret;
8154be47400SFrançois Tigeot 	}
816*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(i915_vma_is_active(vma));
8174be47400SFrançois Tigeot 
8184be47400SFrançois Tigeot 	if (i915_vma_is_pinned(vma))
8194be47400SFrançois Tigeot 		return -EBUSY;
8204be47400SFrançois Tigeot 
8214be47400SFrançois Tigeot 	if (!drm_mm_node_allocated(&vma->node))
8224be47400SFrançois Tigeot 		goto destroy;
8234be47400SFrançois Tigeot 
8244be47400SFrançois Tigeot 	GEM_BUG_ON(obj->bind_count == 0);
8254be47400SFrançois Tigeot 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
8264be47400SFrançois Tigeot 
8274be47400SFrançois Tigeot 	if (i915_vma_is_map_and_fenceable(vma)) {
8284be47400SFrançois Tigeot 		/* release the fence reg _after_ flushing */
8294be47400SFrançois Tigeot 		ret = i915_vma_put_fence(vma);
8304be47400SFrançois Tigeot 		if (ret)
8314be47400SFrançois Tigeot 			return ret;
8324be47400SFrançois Tigeot 
8334be47400SFrançois Tigeot 		/* Force a pagefault for domain tracking on next user access */
834*3f2dd94aSFrançois Tigeot 		i915_vma_revoke_mmap(vma);
8354be47400SFrançois Tigeot 
8364be47400SFrançois Tigeot 		__i915_vma_iounmap(vma);
8374be47400SFrançois Tigeot 		vma->flags &= ~I915_VMA_CAN_FENCE;
8384be47400SFrançois Tigeot 	}
839*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(vma->fence);
840*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(i915_vma_has_userfault(vma));
8414be47400SFrançois Tigeot 
8424be47400SFrançois Tigeot 	if (likely(!vma->vm->closed)) {
8434be47400SFrançois Tigeot 		trace_i915_vma_unbind(vma);
8444be47400SFrançois Tigeot 		vma->vm->unbind_vma(vma);
8454be47400SFrançois Tigeot 	}
8464be47400SFrançois Tigeot 	vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
8474be47400SFrançois Tigeot 
848a85cb24fSFrançois Tigeot 	i915_vma_remove(vma);
8494be47400SFrançois Tigeot 
8504be47400SFrançois Tigeot destroy:
8514be47400SFrançois Tigeot 	if (unlikely(i915_vma_is_closed(vma)))
8524be47400SFrançois Tigeot 		i915_vma_destroy(vma);
8534be47400SFrançois Tigeot 
8544be47400SFrançois Tigeot 	return 0;
8554be47400SFrançois Tigeot }
8564be47400SFrançois Tigeot 
857a85cb24fSFrançois Tigeot #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
858a85cb24fSFrançois Tigeot #include "selftests/i915_vma.c"
859a85cb24fSFrançois Tigeot #endif
860