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