xref: /openbmc/linux/drivers/gpu/drm/i915/i915_vma.c (revision 51c7b447)
1 /*
2  * Copyright © 2016 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include "i915_vma.h"
26 
27 #include "i915_drv.h"
28 #include "i915_globals.h"
29 #include "intel_ringbuffer.h"
30 #include "intel_frontbuffer.h"
31 
32 #include <drm/drm_gem.h>
33 
34 static struct i915_global_vma {
35 	struct i915_global base;
36 	struct kmem_cache *slab_vmas;
37 } global;
38 
39 struct i915_vma *i915_vma_alloc(void)
40 {
41 	return kmem_cache_zalloc(global.slab_vmas, GFP_KERNEL);
42 }
43 
44 void i915_vma_free(struct i915_vma *vma)
45 {
46 	return kmem_cache_free(global.slab_vmas, vma);
47 }
48 
49 #if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM)
50 
51 #include <linux/stackdepot.h>
52 
53 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
54 {
55 	unsigned long entries[12];
56 	struct stack_trace trace = {
57 		.entries = entries,
58 		.max_entries = ARRAY_SIZE(entries),
59 	};
60 	char buf[512];
61 
62 	if (!vma->node.stack) {
63 		DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n",
64 				 vma->node.start, vma->node.size, reason);
65 		return;
66 	}
67 
68 	depot_fetch_stack(vma->node.stack, &trace);
69 	snprint_stack_trace(buf, sizeof(buf), &trace, 0);
70 	DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n",
71 			 vma->node.start, vma->node.size, reason, buf);
72 }
73 
74 #else
75 
76 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
77 {
78 }
79 
80 #endif
81 
82 static void obj_bump_mru(struct drm_i915_gem_object *obj)
83 {
84 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
85 
86 	spin_lock(&i915->mm.obj_lock);
87 	if (obj->bind_count)
88 		list_move_tail(&obj->mm.link, &i915->mm.bound_list);
89 	spin_unlock(&i915->mm.obj_lock);
90 
91 	obj->mm.dirty = true; /* be paranoid  */
92 }
93 
94 static void __i915_vma_retire(struct i915_active *ref)
95 {
96 	struct i915_vma *vma = container_of(ref, typeof(*vma), active);
97 	struct drm_i915_gem_object *obj = vma->obj;
98 
99 	GEM_BUG_ON(!i915_gem_object_is_active(obj));
100 	if (--obj->active_count)
101 		return;
102 
103 	/* Prune the shared fence arrays iff completely idle (inc. external) */
104 	if (reservation_object_trylock(obj->resv)) {
105 		if (reservation_object_test_signaled_rcu(obj->resv, true))
106 			reservation_object_add_excl_fence(obj->resv, NULL);
107 		reservation_object_unlock(obj->resv);
108 	}
109 
110 	/*
111 	 * Bump our place on the bound list to keep it roughly in LRU order
112 	 * so that we don't steal from recently used but inactive objects
113 	 * (unless we are forced to ofc!)
114 	 */
115 	obj_bump_mru(obj);
116 
117 	if (i915_gem_object_has_active_reference(obj)) {
118 		i915_gem_object_clear_active_reference(obj);
119 		i915_gem_object_put(obj);
120 	}
121 }
122 
123 static struct i915_vma *
124 vma_create(struct drm_i915_gem_object *obj,
125 	   struct i915_address_space *vm,
126 	   const struct i915_ggtt_view *view)
127 {
128 	struct i915_vma *vma;
129 	struct rb_node *rb, **p;
130 
131 	/* The aliasing_ppgtt should never be used directly! */
132 	GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->vm);
133 
134 	vma = i915_vma_alloc();
135 	if (vma == NULL)
136 		return ERR_PTR(-ENOMEM);
137 
138 	i915_active_init(vm->i915, &vma->active, __i915_vma_retire);
139 	INIT_ACTIVE_REQUEST(&vma->last_fence);
140 
141 	vma->vm = vm;
142 	vma->ops = &vm->vma_ops;
143 	vma->obj = obj;
144 	vma->resv = obj->resv;
145 	vma->size = obj->base.size;
146 	vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
147 
148 	if (view && view->type != I915_GGTT_VIEW_NORMAL) {
149 		vma->ggtt_view = *view;
150 		if (view->type == I915_GGTT_VIEW_PARTIAL) {
151 			GEM_BUG_ON(range_overflows_t(u64,
152 						     view->partial.offset,
153 						     view->partial.size,
154 						     obj->base.size >> PAGE_SHIFT));
155 			vma->size = view->partial.size;
156 			vma->size <<= PAGE_SHIFT;
157 			GEM_BUG_ON(vma->size > obj->base.size);
158 		} else if (view->type == I915_GGTT_VIEW_ROTATED) {
159 			vma->size = intel_rotation_info_size(&view->rotated);
160 			vma->size <<= PAGE_SHIFT;
161 		}
162 	}
163 
164 	if (unlikely(vma->size > vm->total))
165 		goto err_vma;
166 
167 	GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
168 
169 	if (i915_is_ggtt(vm)) {
170 		if (unlikely(overflows_type(vma->size, u32)))
171 			goto err_vma;
172 
173 		vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
174 						      i915_gem_object_get_tiling(obj),
175 						      i915_gem_object_get_stride(obj));
176 		if (unlikely(vma->fence_size < vma->size || /* overflow */
177 			     vma->fence_size > vm->total))
178 			goto err_vma;
179 
180 		GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
181 
182 		vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
183 								i915_gem_object_get_tiling(obj),
184 								i915_gem_object_get_stride(obj));
185 		GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
186 
187 		vma->flags |= I915_VMA_GGTT;
188 	}
189 
190 	spin_lock(&obj->vma.lock);
191 
192 	rb = NULL;
193 	p = &obj->vma.tree.rb_node;
194 	while (*p) {
195 		struct i915_vma *pos;
196 		long cmp;
197 
198 		rb = *p;
199 		pos = rb_entry(rb, struct i915_vma, obj_node);
200 
201 		/*
202 		 * If the view already exists in the tree, another thread
203 		 * already created a matching vma, so return the older instance
204 		 * and dispose of ours.
205 		 */
206 		cmp = i915_vma_compare(pos, vm, view);
207 		if (cmp == 0) {
208 			spin_unlock(&obj->vma.lock);
209 			i915_vma_free(vma);
210 			return pos;
211 		}
212 
213 		if (cmp < 0)
214 			p = &rb->rb_right;
215 		else
216 			p = &rb->rb_left;
217 	}
218 	rb_link_node(&vma->obj_node, rb, p);
219 	rb_insert_color(&vma->obj_node, &obj->vma.tree);
220 
221 	if (i915_vma_is_ggtt(vma))
222 		/*
223 		 * We put the GGTT vma at the start of the vma-list, followed
224 		 * by the ppGGTT vma. This allows us to break early when
225 		 * iterating over only the GGTT vma for an object, see
226 		 * for_each_ggtt_vma()
227 		 */
228 		list_add(&vma->obj_link, &obj->vma.list);
229 	else
230 		list_add_tail(&vma->obj_link, &obj->vma.list);
231 
232 	spin_unlock(&obj->vma.lock);
233 
234 	mutex_lock(&vm->mutex);
235 	list_add(&vma->vm_link, &vm->unbound_list);
236 	mutex_unlock(&vm->mutex);
237 
238 	return vma;
239 
240 err_vma:
241 	i915_vma_free(vma);
242 	return ERR_PTR(-E2BIG);
243 }
244 
245 static struct i915_vma *
246 vma_lookup(struct drm_i915_gem_object *obj,
247 	   struct i915_address_space *vm,
248 	   const struct i915_ggtt_view *view)
249 {
250 	struct rb_node *rb;
251 
252 	rb = obj->vma.tree.rb_node;
253 	while (rb) {
254 		struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
255 		long cmp;
256 
257 		cmp = i915_vma_compare(vma, vm, view);
258 		if (cmp == 0)
259 			return vma;
260 
261 		if (cmp < 0)
262 			rb = rb->rb_right;
263 		else
264 			rb = rb->rb_left;
265 	}
266 
267 	return NULL;
268 }
269 
270 /**
271  * i915_vma_instance - return the singleton instance of the VMA
272  * @obj: parent &struct drm_i915_gem_object to be mapped
273  * @vm: address space in which the mapping is located
274  * @view: additional mapping requirements
275  *
276  * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
277  * the same @view characteristics. If a match is not found, one is created.
278  * Once created, the VMA is kept until either the object is freed, or the
279  * address space is closed.
280  *
281  * Must be called with struct_mutex held.
282  *
283  * Returns the vma, or an error pointer.
284  */
285 struct i915_vma *
286 i915_vma_instance(struct drm_i915_gem_object *obj,
287 		  struct i915_address_space *vm,
288 		  const struct i915_ggtt_view *view)
289 {
290 	struct i915_vma *vma;
291 
292 	GEM_BUG_ON(view && !i915_is_ggtt(vm));
293 	GEM_BUG_ON(vm->closed);
294 
295 	spin_lock(&obj->vma.lock);
296 	vma = vma_lookup(obj, vm, view);
297 	spin_unlock(&obj->vma.lock);
298 
299 	/* vma_create() will resolve the race if another creates the vma */
300 	if (unlikely(!vma))
301 		vma = vma_create(obj, vm, view);
302 
303 	GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
304 	return vma;
305 }
306 
307 /**
308  * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
309  * @vma: VMA to map
310  * @cache_level: mapping cache level
311  * @flags: flags like global or local mapping
312  *
313  * DMA addresses are taken from the scatter-gather table of this object (or of
314  * this VMA in case of non-default GGTT views) and PTE entries set up.
315  * Note that DMA addresses are also the only part of the SG table we care about.
316  */
317 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
318 		  u32 flags)
319 {
320 	u32 bind_flags;
321 	u32 vma_flags;
322 	int ret;
323 
324 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
325 	GEM_BUG_ON(vma->size > vma->node.size);
326 
327 	if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start,
328 					      vma->node.size,
329 					      vma->vm->total)))
330 		return -ENODEV;
331 
332 	if (GEM_DEBUG_WARN_ON(!flags))
333 		return -EINVAL;
334 
335 	bind_flags = 0;
336 	if (flags & PIN_GLOBAL)
337 		bind_flags |= I915_VMA_GLOBAL_BIND;
338 	if (flags & PIN_USER)
339 		bind_flags |= I915_VMA_LOCAL_BIND;
340 
341 	vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
342 	if (flags & PIN_UPDATE)
343 		bind_flags |= vma_flags;
344 	else
345 		bind_flags &= ~vma_flags;
346 	if (bind_flags == 0)
347 		return 0;
348 
349 	GEM_BUG_ON(!vma->pages);
350 
351 	trace_i915_vma_bind(vma, bind_flags);
352 	ret = vma->ops->bind_vma(vma, cache_level, bind_flags);
353 	if (ret)
354 		return ret;
355 
356 	vma->flags |= bind_flags;
357 	return 0;
358 }
359 
360 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
361 {
362 	void __iomem *ptr;
363 	int err;
364 
365 	/* Access through the GTT requires the device to be awake. */
366 	assert_rpm_wakelock_held(vma->vm->i915);
367 
368 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
369 	if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
370 		err = -ENODEV;
371 		goto err;
372 	}
373 
374 	GEM_BUG_ON(!i915_vma_is_ggtt(vma));
375 	GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
376 
377 	ptr = vma->iomap;
378 	if (ptr == NULL) {
379 		ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
380 					vma->node.start,
381 					vma->node.size);
382 		if (ptr == NULL) {
383 			err = -ENOMEM;
384 			goto err;
385 		}
386 
387 		vma->iomap = ptr;
388 	}
389 
390 	__i915_vma_pin(vma);
391 
392 	err = i915_vma_pin_fence(vma);
393 	if (err)
394 		goto err_unpin;
395 
396 	i915_vma_set_ggtt_write(vma);
397 	return ptr;
398 
399 err_unpin:
400 	__i915_vma_unpin(vma);
401 err:
402 	return IO_ERR_PTR(err);
403 }
404 
405 void i915_vma_flush_writes(struct i915_vma *vma)
406 {
407 	if (!i915_vma_has_ggtt_write(vma))
408 		return;
409 
410 	i915_gem_flush_ggtt_writes(vma->vm->i915);
411 
412 	i915_vma_unset_ggtt_write(vma);
413 }
414 
415 void i915_vma_unpin_iomap(struct i915_vma *vma)
416 {
417 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
418 
419 	GEM_BUG_ON(vma->iomap == NULL);
420 
421 	i915_vma_flush_writes(vma);
422 
423 	i915_vma_unpin_fence(vma);
424 	i915_vma_unpin(vma);
425 }
426 
427 void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags)
428 {
429 	struct i915_vma *vma;
430 	struct drm_i915_gem_object *obj;
431 
432 	vma = fetch_and_zero(p_vma);
433 	if (!vma)
434 		return;
435 
436 	obj = vma->obj;
437 	GEM_BUG_ON(!obj);
438 
439 	i915_vma_unpin(vma);
440 	i915_vma_close(vma);
441 
442 	if (flags & I915_VMA_RELEASE_MAP)
443 		i915_gem_object_unpin_map(obj);
444 
445 	__i915_gem_object_release_unless_active(obj);
446 }
447 
448 bool i915_vma_misplaced(const struct i915_vma *vma,
449 			u64 size, u64 alignment, u64 flags)
450 {
451 	if (!drm_mm_node_allocated(&vma->node))
452 		return false;
453 
454 	if (vma->node.size < size)
455 		return true;
456 
457 	GEM_BUG_ON(alignment && !is_power_of_2(alignment));
458 	if (alignment && !IS_ALIGNED(vma->node.start, alignment))
459 		return true;
460 
461 	if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
462 		return true;
463 
464 	if (flags & PIN_OFFSET_BIAS &&
465 	    vma->node.start < (flags & PIN_OFFSET_MASK))
466 		return true;
467 
468 	if (flags & PIN_OFFSET_FIXED &&
469 	    vma->node.start != (flags & PIN_OFFSET_MASK))
470 		return true;
471 
472 	return false;
473 }
474 
475 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
476 {
477 	bool mappable, fenceable;
478 
479 	GEM_BUG_ON(!i915_vma_is_ggtt(vma));
480 	GEM_BUG_ON(!vma->fence_size);
481 
482 	/*
483 	 * Explicitly disable for rotated VMA since the display does not
484 	 * need the fence and the VMA is not accessible to other users.
485 	 */
486 	if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
487 		return;
488 
489 	fenceable = (vma->node.size >= vma->fence_size &&
490 		     IS_ALIGNED(vma->node.start, vma->fence_alignment));
491 
492 	mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
493 
494 	if (mappable && fenceable)
495 		vma->flags |= I915_VMA_CAN_FENCE;
496 	else
497 		vma->flags &= ~I915_VMA_CAN_FENCE;
498 }
499 
500 static bool color_differs(struct drm_mm_node *node, unsigned long color)
501 {
502 	return node->allocated && node->color != color;
503 }
504 
505 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
506 {
507 	struct drm_mm_node *node = &vma->node;
508 	struct drm_mm_node *other;
509 
510 	/*
511 	 * On some machines we have to be careful when putting differing types
512 	 * of snoopable memory together to avoid the prefetcher crossing memory
513 	 * domains and dying. During vm initialisation, we decide whether or not
514 	 * these constraints apply and set the drm_mm.color_adjust
515 	 * appropriately.
516 	 */
517 	if (vma->vm->mm.color_adjust == NULL)
518 		return true;
519 
520 	/* Only valid to be called on an already inserted vma */
521 	GEM_BUG_ON(!drm_mm_node_allocated(node));
522 	GEM_BUG_ON(list_empty(&node->node_list));
523 
524 	other = list_prev_entry(node, node_list);
525 	if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
526 		return false;
527 
528 	other = list_next_entry(node, node_list);
529 	if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
530 		return false;
531 
532 	return true;
533 }
534 
535 static void assert_bind_count(const struct drm_i915_gem_object *obj)
536 {
537 	/*
538 	 * Combine the assertion that the object is bound and that we have
539 	 * pinned its pages. But we should never have bound the object
540 	 * more than we have pinned its pages. (For complete accuracy, we
541 	 * assume that no else is pinning the pages, but as a rough assertion
542 	 * that we will not run into problems later, this will do!)
543 	 */
544 	GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
545 }
546 
547 /**
548  * i915_vma_insert - finds a slot for the vma in its address space
549  * @vma: the vma
550  * @size: requested size in bytes (can be larger than the VMA)
551  * @alignment: required alignment
552  * @flags: mask of PIN_* flags to use
553  *
554  * First we try to allocate some free space that meets the requirements for
555  * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
556  * preferrably the oldest idle entry to make room for the new VMA.
557  *
558  * Returns:
559  * 0 on success, negative error code otherwise.
560  */
561 static int
562 i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
563 {
564 	struct drm_i915_private *dev_priv = vma->vm->i915;
565 	unsigned int cache_level;
566 	u64 start, end;
567 	int ret;
568 
569 	GEM_BUG_ON(i915_vma_is_closed(vma));
570 	GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
571 	GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
572 
573 	size = max(size, vma->size);
574 	alignment = max(alignment, vma->display_alignment);
575 	if (flags & PIN_MAPPABLE) {
576 		size = max_t(typeof(size), size, vma->fence_size);
577 		alignment = max_t(typeof(alignment),
578 				  alignment, vma->fence_alignment);
579 	}
580 
581 	GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
582 	GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
583 	GEM_BUG_ON(!is_power_of_2(alignment));
584 
585 	start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
586 	GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
587 
588 	end = vma->vm->total;
589 	if (flags & PIN_MAPPABLE)
590 		end = min_t(u64, end, dev_priv->ggtt.mappable_end);
591 	if (flags & PIN_ZONE_4G)
592 		end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
593 	GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
594 
595 	/* If binding the object/GGTT view requires more space than the entire
596 	 * aperture has, reject it early before evicting everything in a vain
597 	 * attempt to find space.
598 	 */
599 	if (size > end) {
600 		DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n",
601 			  size, flags & PIN_MAPPABLE ? "mappable" : "total",
602 			  end);
603 		return -ENOSPC;
604 	}
605 
606 	if (vma->obj) {
607 		ret = i915_gem_object_pin_pages(vma->obj);
608 		if (ret)
609 			return ret;
610 
611 		cache_level = vma->obj->cache_level;
612 	} else {
613 		cache_level = 0;
614 	}
615 
616 	GEM_BUG_ON(vma->pages);
617 
618 	ret = vma->ops->set_pages(vma);
619 	if (ret)
620 		goto err_unpin;
621 
622 	if (flags & PIN_OFFSET_FIXED) {
623 		u64 offset = flags & PIN_OFFSET_MASK;
624 		if (!IS_ALIGNED(offset, alignment) ||
625 		    range_overflows(offset, size, end)) {
626 			ret = -EINVAL;
627 			goto err_clear;
628 		}
629 
630 		ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
631 					   size, offset, cache_level,
632 					   flags);
633 		if (ret)
634 			goto err_clear;
635 	} else {
636 		/*
637 		 * We only support huge gtt pages through the 48b PPGTT,
638 		 * however we also don't want to force any alignment for
639 		 * objects which need to be tightly packed into the low 32bits.
640 		 *
641 		 * Note that we assume that GGTT are limited to 4GiB for the
642 		 * forseeable future. See also i915_ggtt_offset().
643 		 */
644 		if (upper_32_bits(end - 1) &&
645 		    vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
646 			/*
647 			 * We can't mix 64K and 4K PTEs in the same page-table
648 			 * (2M block), and so to avoid the ugliness and
649 			 * complexity of coloring we opt for just aligning 64K
650 			 * objects to 2M.
651 			 */
652 			u64 page_alignment =
653 				rounddown_pow_of_two(vma->page_sizes.sg |
654 						     I915_GTT_PAGE_SIZE_2M);
655 
656 			/*
657 			 * Check we don't expand for the limited Global GTT
658 			 * (mappable aperture is even more precious!). This
659 			 * also checks that we exclude the aliasing-ppgtt.
660 			 */
661 			GEM_BUG_ON(i915_vma_is_ggtt(vma));
662 
663 			alignment = max(alignment, page_alignment);
664 
665 			if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
666 				size = round_up(size, I915_GTT_PAGE_SIZE_2M);
667 		}
668 
669 		ret = i915_gem_gtt_insert(vma->vm, &vma->node,
670 					  size, alignment, cache_level,
671 					  start, end, flags);
672 		if (ret)
673 			goto err_clear;
674 
675 		GEM_BUG_ON(vma->node.start < start);
676 		GEM_BUG_ON(vma->node.start + vma->node.size > end);
677 	}
678 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
679 	GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, cache_level));
680 
681 	mutex_lock(&vma->vm->mutex);
682 	list_move_tail(&vma->vm_link, &vma->vm->bound_list);
683 	mutex_unlock(&vma->vm->mutex);
684 
685 	if (vma->obj) {
686 		struct drm_i915_gem_object *obj = vma->obj;
687 
688 		spin_lock(&dev_priv->mm.obj_lock);
689 		list_move_tail(&obj->mm.link, &dev_priv->mm.bound_list);
690 		obj->bind_count++;
691 		spin_unlock(&dev_priv->mm.obj_lock);
692 
693 		assert_bind_count(obj);
694 	}
695 
696 	return 0;
697 
698 err_clear:
699 	vma->ops->clear_pages(vma);
700 err_unpin:
701 	if (vma->obj)
702 		i915_gem_object_unpin_pages(vma->obj);
703 	return ret;
704 }
705 
706 static void
707 i915_vma_remove(struct i915_vma *vma)
708 {
709 	struct drm_i915_private *i915 = vma->vm->i915;
710 
711 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
712 	GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
713 
714 	vma->ops->clear_pages(vma);
715 
716 	mutex_lock(&vma->vm->mutex);
717 	drm_mm_remove_node(&vma->node);
718 	list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
719 	mutex_unlock(&vma->vm->mutex);
720 
721 	/*
722 	 * Since the unbound list is global, only move to that list if
723 	 * no more VMAs exist.
724 	 */
725 	if (vma->obj) {
726 		struct drm_i915_gem_object *obj = vma->obj;
727 
728 		spin_lock(&i915->mm.obj_lock);
729 		if (--obj->bind_count == 0)
730 			list_move_tail(&obj->mm.link, &i915->mm.unbound_list);
731 		spin_unlock(&i915->mm.obj_lock);
732 
733 		/*
734 		 * And finally now the object is completely decoupled from this
735 		 * vma, we can drop its hold on the backing storage and allow
736 		 * it to be reaped by the shrinker.
737 		 */
738 		i915_gem_object_unpin_pages(obj);
739 		assert_bind_count(obj);
740 	}
741 }
742 
743 int __i915_vma_do_pin(struct i915_vma *vma,
744 		      u64 size, u64 alignment, u64 flags)
745 {
746 	const unsigned int bound = vma->flags;
747 	int ret;
748 
749 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
750 	GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
751 	GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
752 
753 	if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
754 		ret = -EBUSY;
755 		goto err_unpin;
756 	}
757 
758 	if ((bound & I915_VMA_BIND_MASK) == 0) {
759 		ret = i915_vma_insert(vma, size, alignment, flags);
760 		if (ret)
761 			goto err_unpin;
762 	}
763 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
764 
765 	ret = i915_vma_bind(vma, vma->obj ? vma->obj->cache_level : 0, flags);
766 	if (ret)
767 		goto err_remove;
768 
769 	GEM_BUG_ON((vma->flags & I915_VMA_BIND_MASK) == 0);
770 
771 	if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
772 		__i915_vma_set_map_and_fenceable(vma);
773 
774 	GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
775 	return 0;
776 
777 err_remove:
778 	if ((bound & I915_VMA_BIND_MASK) == 0) {
779 		i915_vma_remove(vma);
780 		GEM_BUG_ON(vma->pages);
781 		GEM_BUG_ON(vma->flags & I915_VMA_BIND_MASK);
782 	}
783 err_unpin:
784 	__i915_vma_unpin(vma);
785 	return ret;
786 }
787 
788 void i915_vma_close(struct i915_vma *vma)
789 {
790 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
791 
792 	GEM_BUG_ON(i915_vma_is_closed(vma));
793 	vma->flags |= I915_VMA_CLOSED;
794 
795 	/*
796 	 * We defer actually closing, unbinding and destroying the VMA until
797 	 * the next idle point, or if the object is freed in the meantime. By
798 	 * postponing the unbind, we allow for it to be resurrected by the
799 	 * client, avoiding the work required to rebind the VMA. This is
800 	 * advantageous for DRI, where the client/server pass objects
801 	 * between themselves, temporarily opening a local VMA to the
802 	 * object, and then closing it again. The same object is then reused
803 	 * on the next frame (or two, depending on the depth of the swap queue)
804 	 * causing us to rebind the VMA once more. This ends up being a lot
805 	 * of wasted work for the steady state.
806 	 */
807 	list_add_tail(&vma->closed_link, &vma->vm->i915->gt.closed_vma);
808 }
809 
810 void i915_vma_reopen(struct i915_vma *vma)
811 {
812 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
813 
814 	if (vma->flags & I915_VMA_CLOSED) {
815 		vma->flags &= ~I915_VMA_CLOSED;
816 		list_del(&vma->closed_link);
817 	}
818 }
819 
820 static void __i915_vma_destroy(struct i915_vma *vma)
821 {
822 	GEM_BUG_ON(vma->node.allocated);
823 	GEM_BUG_ON(vma->fence);
824 
825 	GEM_BUG_ON(i915_active_request_isset(&vma->last_fence));
826 
827 	mutex_lock(&vma->vm->mutex);
828 	list_del(&vma->vm_link);
829 	mutex_unlock(&vma->vm->mutex);
830 
831 	if (vma->obj) {
832 		struct drm_i915_gem_object *obj = vma->obj;
833 
834 		spin_lock(&obj->vma.lock);
835 		list_del(&vma->obj_link);
836 		rb_erase(&vma->obj_node, &vma->obj->vma.tree);
837 		spin_unlock(&obj->vma.lock);
838 	}
839 
840 	i915_active_fini(&vma->active);
841 
842 	i915_vma_free(vma);
843 }
844 
845 void i915_vma_destroy(struct i915_vma *vma)
846 {
847 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
848 
849 	GEM_BUG_ON(i915_vma_is_active(vma));
850 	GEM_BUG_ON(i915_vma_is_pinned(vma));
851 
852 	if (i915_vma_is_closed(vma))
853 		list_del(&vma->closed_link);
854 
855 	WARN_ON(i915_vma_unbind(vma));
856 	__i915_vma_destroy(vma);
857 }
858 
859 void i915_vma_parked(struct drm_i915_private *i915)
860 {
861 	struct i915_vma *vma, *next;
862 
863 	list_for_each_entry_safe(vma, next, &i915->gt.closed_vma, closed_link) {
864 		GEM_BUG_ON(!i915_vma_is_closed(vma));
865 		i915_vma_destroy(vma);
866 	}
867 
868 	GEM_BUG_ON(!list_empty(&i915->gt.closed_vma));
869 }
870 
871 static void __i915_vma_iounmap(struct i915_vma *vma)
872 {
873 	GEM_BUG_ON(i915_vma_is_pinned(vma));
874 
875 	if (vma->iomap == NULL)
876 		return;
877 
878 	io_mapping_unmap(vma->iomap);
879 	vma->iomap = NULL;
880 }
881 
882 void i915_vma_revoke_mmap(struct i915_vma *vma)
883 {
884 	struct drm_vma_offset_node *node = &vma->obj->base.vma_node;
885 	u64 vma_offset;
886 
887 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
888 
889 	if (!i915_vma_has_userfault(vma))
890 		return;
891 
892 	GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
893 	GEM_BUG_ON(!vma->obj->userfault_count);
894 
895 	vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
896 	unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
897 			    drm_vma_node_offset_addr(node) + vma_offset,
898 			    vma->size,
899 			    1);
900 
901 	i915_vma_unset_userfault(vma);
902 	if (!--vma->obj->userfault_count)
903 		list_del(&vma->obj->userfault_link);
904 }
905 
906 static void export_fence(struct i915_vma *vma,
907 			 struct i915_request *rq,
908 			 unsigned int flags)
909 {
910 	struct reservation_object *resv = vma->resv;
911 
912 	/*
913 	 * Ignore errors from failing to allocate the new fence, we can't
914 	 * handle an error right now. Worst case should be missed
915 	 * synchronisation leading to rendering corruption.
916 	 */
917 	reservation_object_lock(resv, NULL);
918 	if (flags & EXEC_OBJECT_WRITE)
919 		reservation_object_add_excl_fence(resv, &rq->fence);
920 	else if (reservation_object_reserve_shared(resv, 1) == 0)
921 		reservation_object_add_shared_fence(resv, &rq->fence);
922 	reservation_object_unlock(resv);
923 }
924 
925 int i915_vma_move_to_active(struct i915_vma *vma,
926 			    struct i915_request *rq,
927 			    unsigned int flags)
928 {
929 	struct drm_i915_gem_object *obj = vma->obj;
930 
931 	lockdep_assert_held(&rq->i915->drm.struct_mutex);
932 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
933 
934 	/*
935 	 * Add a reference if we're newly entering the active list.
936 	 * The order in which we add operations to the retirement queue is
937 	 * vital here: mark_active adds to the start of the callback list,
938 	 * such that subsequent callbacks are called first. Therefore we
939 	 * add the active reference first and queue for it to be dropped
940 	 * *last*.
941 	 */
942 	if (!vma->active.count)
943 		obj->active_count++;
944 
945 	if (unlikely(i915_active_ref(&vma->active, rq->fence.context, rq))) {
946 		if (!vma->active.count)
947 			obj->active_count--;
948 		return -ENOMEM;
949 	}
950 
951 	GEM_BUG_ON(!i915_vma_is_active(vma));
952 	GEM_BUG_ON(!obj->active_count);
953 
954 	obj->write_domain = 0;
955 	if (flags & EXEC_OBJECT_WRITE) {
956 		obj->write_domain = I915_GEM_DOMAIN_RENDER;
957 
958 		if (intel_fb_obj_invalidate(obj, ORIGIN_CS))
959 			__i915_active_request_set(&obj->frontbuffer_write, rq);
960 
961 		obj->read_domains = 0;
962 	}
963 	obj->read_domains |= I915_GEM_GPU_DOMAINS;
964 
965 	if (flags & EXEC_OBJECT_NEEDS_FENCE)
966 		__i915_active_request_set(&vma->last_fence, rq);
967 
968 	export_fence(vma, rq, flags);
969 	return 0;
970 }
971 
972 int i915_vma_unbind(struct i915_vma *vma)
973 {
974 	int ret;
975 
976 	lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
977 
978 	/*
979 	 * First wait upon any activity as retiring the request may
980 	 * have side-effects such as unpinning or even unbinding this vma.
981 	 */
982 	might_sleep();
983 	if (i915_vma_is_active(vma)) {
984 		/*
985 		 * When a closed VMA is retired, it is unbound - eek.
986 		 * In order to prevent it from being recursively closed,
987 		 * take a pin on the vma so that the second unbind is
988 		 * aborted.
989 		 *
990 		 * Even more scary is that the retire callback may free
991 		 * the object (last active vma). To prevent the explosion
992 		 * we defer the actual object free to a worker that can
993 		 * only proceed once it acquires the struct_mutex (which
994 		 * we currently hold, therefore it cannot free this object
995 		 * before we are finished).
996 		 */
997 		__i915_vma_pin(vma);
998 
999 		ret = i915_active_wait(&vma->active);
1000 		if (ret)
1001 			goto unpin;
1002 
1003 		ret = i915_active_request_retire(&vma->last_fence,
1004 					      &vma->vm->i915->drm.struct_mutex);
1005 unpin:
1006 		__i915_vma_unpin(vma);
1007 		if (ret)
1008 			return ret;
1009 	}
1010 	GEM_BUG_ON(i915_vma_is_active(vma));
1011 
1012 	if (i915_vma_is_pinned(vma)) {
1013 		vma_print_allocator(vma, "is pinned");
1014 		return -EBUSY;
1015 	}
1016 
1017 	if (!drm_mm_node_allocated(&vma->node))
1018 		return 0;
1019 
1020 	if (i915_vma_is_map_and_fenceable(vma)) {
1021 		/*
1022 		 * Check that we have flushed all writes through the GGTT
1023 		 * before the unbind, other due to non-strict nature of those
1024 		 * indirect writes they may end up referencing the GGTT PTE
1025 		 * after the unbind.
1026 		 */
1027 		i915_vma_flush_writes(vma);
1028 		GEM_BUG_ON(i915_vma_has_ggtt_write(vma));
1029 
1030 		/* release the fence reg _after_ flushing */
1031 		ret = i915_vma_put_fence(vma);
1032 		if (ret)
1033 			return ret;
1034 
1035 		/* Force a pagefault for domain tracking on next user access */
1036 		i915_vma_revoke_mmap(vma);
1037 
1038 		__i915_vma_iounmap(vma);
1039 		vma->flags &= ~I915_VMA_CAN_FENCE;
1040 	}
1041 	GEM_BUG_ON(vma->fence);
1042 	GEM_BUG_ON(i915_vma_has_userfault(vma));
1043 
1044 	if (likely(!vma->vm->closed)) {
1045 		trace_i915_vma_unbind(vma);
1046 		vma->ops->unbind_vma(vma);
1047 	}
1048 	vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
1049 
1050 	i915_vma_remove(vma);
1051 
1052 	return 0;
1053 }
1054 
1055 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1056 #include "selftests/i915_vma.c"
1057 #endif
1058 
1059 static void i915_global_vma_shrink(void)
1060 {
1061 	kmem_cache_shrink(global.slab_vmas);
1062 }
1063 
1064 static void i915_global_vma_exit(void)
1065 {
1066 	kmem_cache_destroy(global.slab_vmas);
1067 }
1068 
1069 static struct i915_global_vma global = { {
1070 	.shrink = i915_global_vma_shrink,
1071 	.exit = i915_global_vma_exit,
1072 } };
1073 
1074 int __init i915_global_vma_init(void)
1075 {
1076 	global.slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
1077 	if (!global.slab_vmas)
1078 		return -ENOMEM;
1079 
1080 	i915_global_register(&global.base);
1081 	return 0;
1082 }
1083