drm/i915: Exercise i915_vma_pin/i915_vma_insert
[linux-2.6-block.git] / drivers / gpu / drm / i915 / i915_vma.c
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,
35                 struct drm_i915_gem_request *rq)
36 {
37         const unsigned int idx = rq->engine->id;
38         struct i915_vma *vma =
39                 container_of(active, struct i915_vma, last_read[idx]);
40         struct drm_i915_gem_object *obj = vma->obj;
41
42         GEM_BUG_ON(!i915_vma_has_active_engine(vma, idx));
43
44         i915_vma_clear_active(vma, idx);
45         if (i915_vma_is_active(vma))
46                 return;
47
48         GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
49         list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
50         if (unlikely(i915_vma_is_closed(vma) && !i915_vma_is_pinned(vma)))
51                 WARN_ON(i915_vma_unbind(vma));
52
53         GEM_BUG_ON(!i915_gem_object_is_active(obj));
54         if (--obj->active_count)
55                 return;
56
57         /* Bump our place on the bound list to keep it roughly in LRU order
58          * so that we don't steal from recently used but inactive objects
59          * (unless we are forced to ofc!)
60          */
61         if (obj->bind_count)
62                 list_move_tail(&obj->global_link, &rq->i915->mm.bound_list);
63
64         obj->mm.dirty = true; /* be paranoid  */
65
66         if (i915_gem_object_has_active_reference(obj)) {
67                 i915_gem_object_clear_active_reference(obj);
68                 i915_gem_object_put(obj);
69         }
70 }
71
72 static struct i915_vma *
73 vma_create(struct drm_i915_gem_object *obj,
74            struct i915_address_space *vm,
75            const struct i915_ggtt_view *view)
76 {
77         struct i915_vma *vma;
78         struct rb_node *rb, **p;
79         int i;
80
81         /* The aliasing_ppgtt should never be used directly! */
82         GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
83
84         vma = kmem_cache_zalloc(vm->i915->vmas, GFP_KERNEL);
85         if (vma == NULL)
86                 return ERR_PTR(-ENOMEM);
87
88         INIT_LIST_HEAD(&vma->exec_list);
89         for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
90                 init_request_active(&vma->last_read[i], i915_vma_retire);
91         init_request_active(&vma->last_fence, NULL);
92         vma->vm = vm;
93         vma->obj = obj;
94         vma->size = obj->base.size;
95         vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
96
97         if (view && view->type != I915_GGTT_VIEW_NORMAL) {
98                 vma->ggtt_view = *view;
99                 if (view->type == I915_GGTT_VIEW_PARTIAL) {
100                         GEM_BUG_ON(range_overflows_t(u64,
101                                                      view->partial.offset,
102                                                      view->partial.size,
103                                                      obj->base.size >> PAGE_SHIFT));
104                         vma->size = view->partial.size;
105                         vma->size <<= PAGE_SHIFT;
106                         GEM_BUG_ON(vma->size >= obj->base.size);
107                 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
108                         vma->size = intel_rotation_info_size(&view->rotated);
109                         vma->size <<= PAGE_SHIFT;
110                 }
111         }
112
113         if (unlikely(vma->size > vm->total))
114                 goto err_vma;
115
116         GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
117
118         if (i915_is_ggtt(vm)) {
119                 if (unlikely(overflows_type(vma->size, u32)))
120                         goto err_vma;
121
122                 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
123                                                       i915_gem_object_get_tiling(obj),
124                                                       i915_gem_object_get_stride(obj));
125                 if (unlikely(vma->fence_size < vma->size || /* overflow */
126                              vma->fence_size > vm->total))
127                         goto err_vma;
128
129                 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
130
131                 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
132                                                                 i915_gem_object_get_tiling(obj),
133                                                                 i915_gem_object_get_stride(obj));
134                 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
135
136                 vma->flags |= I915_VMA_GGTT;
137                 list_add(&vma->obj_link, &obj->vma_list);
138         } else {
139                 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
140                 list_add_tail(&vma->obj_link, &obj->vma_list);
141         }
142
143         rb = NULL;
144         p = &obj->vma_tree.rb_node;
145         while (*p) {
146                 struct i915_vma *pos;
147
148                 rb = *p;
149                 pos = rb_entry(rb, struct i915_vma, obj_node);
150                 if (i915_vma_compare(pos, vm, view) < 0)
151                         p = &rb->rb_right;
152                 else
153                         p = &rb->rb_left;
154         }
155         rb_link_node(&vma->obj_node, rb, p);
156         rb_insert_color(&vma->obj_node, &obj->vma_tree);
157         list_add(&vma->vm_link, &vm->unbound_list);
158
159         return vma;
160
161 err_vma:
162         kmem_cache_free(vm->i915->vmas, vma);
163         return ERR_PTR(-E2BIG);
164 }
165
166 static struct i915_vma *
167 vma_lookup(struct drm_i915_gem_object *obj,
168            struct i915_address_space *vm,
169            const struct i915_ggtt_view *view)
170 {
171         struct rb_node *rb;
172
173         rb = obj->vma_tree.rb_node;
174         while (rb) {
175                 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
176                 long cmp;
177
178                 cmp = i915_vma_compare(vma, vm, view);
179                 if (cmp == 0)
180                         return vma;
181
182                 if (cmp < 0)
183                         rb = rb->rb_right;
184                 else
185                         rb = rb->rb_left;
186         }
187
188         return NULL;
189 }
190
191 /**
192  * i915_vma_instance - return the singleton instance of the VMA
193  * @obj: parent &struct drm_i915_gem_object to be mapped
194  * @vm: address space in which the mapping is located
195  * @view: additional mapping requirements
196  *
197  * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
198  * the same @view characteristics. If a match is not found, one is created.
199  * Once created, the VMA is kept until either the object is freed, or the
200  * address space is closed.
201  *
202  * Must be called with struct_mutex held.
203  *
204  * Returns the vma, or an error pointer.
205  */
206 struct i915_vma *
207 i915_vma_instance(struct drm_i915_gem_object *obj,
208                   struct i915_address_space *vm,
209                   const struct i915_ggtt_view *view)
210 {
211         struct i915_vma *vma;
212
213         lockdep_assert_held(&obj->base.dev->struct_mutex);
214         GEM_BUG_ON(view && !i915_is_ggtt(vm));
215         GEM_BUG_ON(vm->closed);
216
217         vma = vma_lookup(obj, vm, view);
218         if (!vma)
219                 vma = vma_create(obj, vm, view);
220
221         GEM_BUG_ON(!IS_ERR(vma) && i915_vma_is_closed(vma));
222         GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
223         GEM_BUG_ON(!IS_ERR(vma) && vma_lookup(obj, vm, view) != vma);
224         return vma;
225 }
226
227 /**
228  * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
229  * @vma: VMA to map
230  * @cache_level: mapping cache level
231  * @flags: flags like global or local mapping
232  *
233  * DMA addresses are taken from the scatter-gather table of this object (or of
234  * this VMA in case of non-default GGTT views) and PTE entries set up.
235  * Note that DMA addresses are also the only part of the SG table we care about.
236  */
237 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
238                   u32 flags)
239 {
240         u32 bind_flags;
241         u32 vma_flags;
242         int ret;
243
244         if (WARN_ON(flags == 0))
245                 return -EINVAL;
246
247         bind_flags = 0;
248         if (flags & PIN_GLOBAL)
249                 bind_flags |= I915_VMA_GLOBAL_BIND;
250         if (flags & PIN_USER)
251                 bind_flags |= I915_VMA_LOCAL_BIND;
252
253         vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
254         if (flags & PIN_UPDATE)
255                 bind_flags |= vma_flags;
256         else
257                 bind_flags &= ~vma_flags;
258         if (bind_flags == 0)
259                 return 0;
260
261         if (GEM_WARN_ON(range_overflows(vma->node.start,
262                                         vma->node.size,
263                                         vma->vm->total)))
264                 return -ENODEV;
265
266         if (vma_flags == 0 && vma->vm->allocate_va_range) {
267                 trace_i915_va_alloc(vma);
268                 ret = vma->vm->allocate_va_range(vma->vm,
269                                                  vma->node.start,
270                                                  vma->node.size);
271                 if (ret)
272                         return ret;
273         }
274
275         trace_i915_vma_bind(vma, bind_flags);
276         ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
277         if (ret)
278                 return ret;
279
280         vma->flags |= bind_flags;
281         return 0;
282 }
283
284 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
285 {
286         void __iomem *ptr;
287
288         /* Access through the GTT requires the device to be awake. */
289         assert_rpm_wakelock_held(vma->vm->i915);
290
291         lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
292         if (WARN_ON(!i915_vma_is_map_and_fenceable(vma)))
293                 return IO_ERR_PTR(-ENODEV);
294
295         GEM_BUG_ON(!i915_vma_is_ggtt(vma));
296         GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
297
298         ptr = vma->iomap;
299         if (ptr == NULL) {
300                 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->mappable,
301                                         vma->node.start,
302                                         vma->node.size);
303                 if (ptr == NULL)
304                         return IO_ERR_PTR(-ENOMEM);
305
306                 vma->iomap = ptr;
307         }
308
309         __i915_vma_pin(vma);
310         return ptr;
311 }
312
313 void i915_vma_unpin_and_release(struct i915_vma **p_vma)
314 {
315         struct i915_vma *vma;
316         struct drm_i915_gem_object *obj;
317
318         vma = fetch_and_zero(p_vma);
319         if (!vma)
320                 return;
321
322         obj = vma->obj;
323
324         i915_vma_unpin(vma);
325         i915_vma_close(vma);
326
327         __i915_gem_object_release_unless_active(obj);
328 }
329
330 bool i915_vma_misplaced(const struct i915_vma *vma,
331                         u64 size, u64 alignment, u64 flags)
332 {
333         if (!drm_mm_node_allocated(&vma->node))
334                 return false;
335
336         if (vma->node.size < size)
337                 return true;
338
339         GEM_BUG_ON(alignment && !is_power_of_2(alignment));
340         if (alignment && !IS_ALIGNED(vma->node.start, alignment))
341                 return true;
342
343         if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
344                 return true;
345
346         if (flags & PIN_OFFSET_BIAS &&
347             vma->node.start < (flags & PIN_OFFSET_MASK))
348                 return true;
349
350         if (flags & PIN_OFFSET_FIXED &&
351             vma->node.start != (flags & PIN_OFFSET_MASK))
352                 return true;
353
354         return false;
355 }
356
357 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
358 {
359         bool mappable, fenceable;
360
361         GEM_BUG_ON(!i915_vma_is_ggtt(vma));
362         GEM_BUG_ON(!vma->fence_size);
363
364         /*
365          * Explicitly disable for rotated VMA since the display does not
366          * need the fence and the VMA is not accessible to other users.
367          */
368         if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
369                 return;
370
371         fenceable = (vma->node.size >= vma->fence_size &&
372                      IS_ALIGNED(vma->node.start, vma->fence_alignment));
373
374         mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
375
376         if (mappable && fenceable)
377                 vma->flags |= I915_VMA_CAN_FENCE;
378         else
379                 vma->flags &= ~I915_VMA_CAN_FENCE;
380 }
381
382 static bool color_differs(struct drm_mm_node *node, unsigned long color)
383 {
384         return node->allocated && node->color != color;
385 }
386
387 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
388 {
389         struct drm_mm_node *node = &vma->node;
390         struct drm_mm_node *other;
391
392         /*
393          * On some machines we have to be careful when putting differing types
394          * of snoopable memory together to avoid the prefetcher crossing memory
395          * domains and dying. During vm initialisation, we decide whether or not
396          * these constraints apply and set the drm_mm.color_adjust
397          * appropriately.
398          */
399         if (vma->vm->mm.color_adjust == NULL)
400                 return true;
401
402         /* Only valid to be called on an already inserted vma */
403         GEM_BUG_ON(!drm_mm_node_allocated(node));
404         GEM_BUG_ON(list_empty(&node->node_list));
405
406         other = list_prev_entry(node, node_list);
407         if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
408                 return false;
409
410         other = list_next_entry(node, node_list);
411         if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
412                 return false;
413
414         return true;
415 }
416
417 /**
418  * i915_vma_insert - finds a slot for the vma in its address space
419  * @vma: the vma
420  * @size: requested size in bytes (can be larger than the VMA)
421  * @alignment: required alignment
422  * @flags: mask of PIN_* flags to use
423  *
424  * First we try to allocate some free space that meets the requirements for
425  * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
426  * preferrably the oldest idle entry to make room for the new VMA.
427  *
428  * Returns:
429  * 0 on success, negative error code otherwise.
430  */
431 static int
432 i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
433 {
434         struct drm_i915_private *dev_priv = vma->vm->i915;
435         struct drm_i915_gem_object *obj = vma->obj;
436         u64 start, end;
437         int ret;
438
439         GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
440         GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
441
442         size = max(size, vma->size);
443         alignment = max(alignment, vma->display_alignment);
444         if (flags & PIN_MAPPABLE) {
445                 size = max_t(typeof(size), size, vma->fence_size);
446                 alignment = max_t(typeof(alignment),
447                                   alignment, vma->fence_alignment);
448         }
449
450         GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
451         GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
452         GEM_BUG_ON(!is_power_of_2(alignment));
453
454         start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
455         GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
456
457         end = vma->vm->total;
458         if (flags & PIN_MAPPABLE)
459                 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
460         if (flags & PIN_ZONE_4G)
461                 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
462         GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
463
464         /* If binding the object/GGTT view requires more space than the entire
465          * aperture has, reject it early before evicting everything in a vain
466          * attempt to find space.
467          */
468         if (size > end) {
469                 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n",
470                           size, obj->base.size,
471                           flags & PIN_MAPPABLE ? "mappable" : "total",
472                           end);
473                 return -E2BIG;
474         }
475
476         ret = i915_gem_object_pin_pages(obj);
477         if (ret)
478                 return ret;
479
480         if (flags & PIN_OFFSET_FIXED) {
481                 u64 offset = flags & PIN_OFFSET_MASK;
482                 if (!IS_ALIGNED(offset, alignment) ||
483                     range_overflows(offset, size, end)) {
484                         ret = -EINVAL;
485                         goto err_unpin;
486                 }
487
488                 ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
489                                            size, offset, obj->cache_level,
490                                            flags);
491                 if (ret)
492                         goto err_unpin;
493         } else {
494                 ret = i915_gem_gtt_insert(vma->vm, &vma->node,
495                                           size, alignment, obj->cache_level,
496                                           start, end, flags);
497                 if (ret)
498                         goto err_unpin;
499
500                 GEM_BUG_ON(vma->node.start < start);
501                 GEM_BUG_ON(vma->node.start + vma->node.size > end);
502         }
503         GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
504         GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
505
506         list_move_tail(&obj->global_link, &dev_priv->mm.bound_list);
507         list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
508         obj->bind_count++;
509         GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
510
511         return 0;
512
513 err_unpin:
514         i915_gem_object_unpin_pages(obj);
515         return ret;
516 }
517
518 int __i915_vma_do_pin(struct i915_vma *vma,
519                       u64 size, u64 alignment, u64 flags)
520 {
521         unsigned int bound = vma->flags;
522         int ret;
523
524         lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
525         GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
526         GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
527
528         if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
529                 ret = -EBUSY;
530                 goto err;
531         }
532
533         if ((bound & I915_VMA_BIND_MASK) == 0) {
534                 ret = i915_vma_insert(vma, size, alignment, flags);
535                 if (ret)
536                         goto err;
537         }
538
539         ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
540         if (ret)
541                 goto err;
542
543         if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
544                 __i915_vma_set_map_and_fenceable(vma);
545
546         GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
547         GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
548         return 0;
549
550 err:
551         __i915_vma_unpin(vma);
552         return ret;
553 }
554
555 void i915_vma_destroy(struct i915_vma *vma)
556 {
557         GEM_BUG_ON(vma->node.allocated);
558         GEM_BUG_ON(i915_vma_is_active(vma));
559         GEM_BUG_ON(!i915_vma_is_closed(vma));
560         GEM_BUG_ON(vma->fence);
561
562         list_del(&vma->vm_link);
563         if (!i915_vma_is_ggtt(vma))
564                 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
565
566         kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
567 }
568
569 void i915_vma_close(struct i915_vma *vma)
570 {
571         GEM_BUG_ON(i915_vma_is_closed(vma));
572         vma->flags |= I915_VMA_CLOSED;
573
574         list_del(&vma->obj_link);
575         rb_erase(&vma->obj_node, &vma->obj->vma_tree);
576
577         if (!i915_vma_is_active(vma) && !i915_vma_is_pinned(vma))
578                 WARN_ON(i915_vma_unbind(vma));
579 }
580
581 static void __i915_vma_iounmap(struct i915_vma *vma)
582 {
583         GEM_BUG_ON(i915_vma_is_pinned(vma));
584
585         if (vma->iomap == NULL)
586                 return;
587
588         io_mapping_unmap(vma->iomap);
589         vma->iomap = NULL;
590 }
591
592 int i915_vma_unbind(struct i915_vma *vma)
593 {
594         struct drm_i915_gem_object *obj = vma->obj;
595         unsigned long active;
596         int ret;
597
598         lockdep_assert_held(&obj->base.dev->struct_mutex);
599
600         /* First wait upon any activity as retiring the request may
601          * have side-effects such as unpinning or even unbinding this vma.
602          */
603         active = i915_vma_get_active(vma);
604         if (active) {
605                 int idx;
606
607                 /* When a closed VMA is retired, it is unbound - eek.
608                  * In order to prevent it from being recursively closed,
609                  * take a pin on the vma so that the second unbind is
610                  * aborted.
611                  *
612                  * Even more scary is that the retire callback may free
613                  * the object (last active vma). To prevent the explosion
614                  * we defer the actual object free to a worker that can
615                  * only proceed once it acquires the struct_mutex (which
616                  * we currently hold, therefore it cannot free this object
617                  * before we are finished).
618                  */
619                 __i915_vma_pin(vma);
620
621                 for_each_active(active, idx) {
622                         ret = i915_gem_active_retire(&vma->last_read[idx],
623                                                      &vma->vm->i915->drm.struct_mutex);
624                         if (ret)
625                                 break;
626                 }
627
628                 __i915_vma_unpin(vma);
629                 if (ret)
630                         return ret;
631
632                 GEM_BUG_ON(i915_vma_is_active(vma));
633         }
634
635         if (i915_vma_is_pinned(vma))
636                 return -EBUSY;
637
638         if (!drm_mm_node_allocated(&vma->node))
639                 goto destroy;
640
641         GEM_BUG_ON(obj->bind_count == 0);
642         GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
643
644         if (i915_vma_is_map_and_fenceable(vma)) {
645                 /* release the fence reg _after_ flushing */
646                 ret = i915_vma_put_fence(vma);
647                 if (ret)
648                         return ret;
649
650                 /* Force a pagefault for domain tracking on next user access */
651                 i915_gem_release_mmap(obj);
652
653                 __i915_vma_iounmap(vma);
654                 vma->flags &= ~I915_VMA_CAN_FENCE;
655         }
656
657         if (likely(!vma->vm->closed)) {
658                 trace_i915_vma_unbind(vma);
659                 vma->vm->unbind_vma(vma);
660         }
661         vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
662
663         drm_mm_remove_node(&vma->node);
664         list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
665
666         if (vma->pages != obj->mm.pages) {
667                 GEM_BUG_ON(!vma->pages);
668                 sg_free_table(vma->pages);
669                 kfree(vma->pages);
670         }
671         vma->pages = NULL;
672
673         /* Since the unbound list is global, only move to that list if
674          * no more VMAs exist. */
675         if (--obj->bind_count == 0)
676                 list_move_tail(&obj->global_link,
677                                &to_i915(obj->base.dev)->mm.unbound_list);
678
679         /* And finally now the object is completely decoupled from this vma,
680          * we can drop its hold on the backing storage and allow it to be
681          * reaped by the shrinker.
682          */
683         i915_gem_object_unpin_pages(obj);
684         GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
685
686 destroy:
687         if (unlikely(i915_vma_is_closed(vma)))
688                 i915_vma_destroy(vma);
689
690         return 0;
691 }
692
693 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
694 #include "selftests/i915_vma.c"
695 #endif