drm/i915: Pin fence for iomap
[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         for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
89                 init_request_active(&vma->last_read[i], i915_vma_retire);
90         init_request_active(&vma->last_fence, NULL);
91         vma->vm = vm;
92         vma->obj = obj;
93         vma->resv = obj->resv;
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         GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
245         GEM_BUG_ON(vma->size > vma->node.size);
246
247         if (GEM_WARN_ON(range_overflows(vma->node.start,
248                                         vma->node.size,
249                                         vma->vm->total)))
250                 return -ENODEV;
251
252         if (GEM_WARN_ON(!flags))
253                 return -EINVAL;
254
255         bind_flags = 0;
256         if (flags & PIN_GLOBAL)
257                 bind_flags |= I915_VMA_GLOBAL_BIND;
258         if (flags & PIN_USER)
259                 bind_flags |= I915_VMA_LOCAL_BIND;
260
261         vma_flags = vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
262         if (flags & PIN_UPDATE)
263                 bind_flags |= vma_flags;
264         else
265                 bind_flags &= ~vma_flags;
266         if (bind_flags == 0)
267                 return 0;
268
269         GEM_BUG_ON(!vma->pages);
270
271         trace_i915_vma_bind(vma, bind_flags);
272         ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
273         if (ret)
274                 return ret;
275
276         vma->flags |= bind_flags;
277         return 0;
278 }
279
280 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
281 {
282         void __iomem *ptr;
283         int err;
284
285         /* Access through the GTT requires the device to be awake. */
286         assert_rpm_wakelock_held(vma->vm->i915);
287
288         lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
289         if (WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
290                 err = -ENODEV;
291                 goto err;
292         }
293
294         GEM_BUG_ON(!i915_vma_is_ggtt(vma));
295         GEM_BUG_ON((vma->flags & I915_VMA_GLOBAL_BIND) == 0);
296
297         ptr = vma->iomap;
298         if (ptr == NULL) {
299                 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->mappable,
300                                         vma->node.start,
301                                         vma->node.size);
302                 if (ptr == NULL) {
303                         err = -ENOMEM;
304                         goto err;
305                 }
306
307                 vma->iomap = ptr;
308         }
309
310         __i915_vma_pin(vma);
311
312         err = i915_vma_get_fence(vma);
313         if (err)
314                 goto err_unpin;
315
316         i915_vma_pin_fence(vma);
317
318         return ptr;
319
320 err_unpin:
321         __i915_vma_unpin(vma);
322 err:
323         return IO_ERR_PTR(err);
324 }
325
326 void i915_vma_unpin_iomap(struct i915_vma *vma)
327 {
328         lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
329
330         GEM_BUG_ON(vma->iomap == NULL);
331
332         i915_vma_unpin_fence(vma);
333         i915_vma_unpin(vma);
334 }
335
336 void i915_vma_unpin_and_release(struct i915_vma **p_vma)
337 {
338         struct i915_vma *vma;
339         struct drm_i915_gem_object *obj;
340
341         vma = fetch_and_zero(p_vma);
342         if (!vma)
343                 return;
344
345         obj = vma->obj;
346
347         i915_vma_unpin(vma);
348         i915_vma_close(vma);
349
350         __i915_gem_object_release_unless_active(obj);
351 }
352
353 bool i915_vma_misplaced(const struct i915_vma *vma,
354                         u64 size, u64 alignment, u64 flags)
355 {
356         if (!drm_mm_node_allocated(&vma->node))
357                 return false;
358
359         if (vma->node.size < size)
360                 return true;
361
362         GEM_BUG_ON(alignment && !is_power_of_2(alignment));
363         if (alignment && !IS_ALIGNED(vma->node.start, alignment))
364                 return true;
365
366         if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
367                 return true;
368
369         if (flags & PIN_OFFSET_BIAS &&
370             vma->node.start < (flags & PIN_OFFSET_MASK))
371                 return true;
372
373         if (flags & PIN_OFFSET_FIXED &&
374             vma->node.start != (flags & PIN_OFFSET_MASK))
375                 return true;
376
377         return false;
378 }
379
380 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
381 {
382         bool mappable, fenceable;
383
384         GEM_BUG_ON(!i915_vma_is_ggtt(vma));
385         GEM_BUG_ON(!vma->fence_size);
386
387         /*
388          * Explicitly disable for rotated VMA since the display does not
389          * need the fence and the VMA is not accessible to other users.
390          */
391         if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
392                 return;
393
394         fenceable = (vma->node.size >= vma->fence_size &&
395                      IS_ALIGNED(vma->node.start, vma->fence_alignment));
396
397         mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
398
399         if (mappable && fenceable)
400                 vma->flags |= I915_VMA_CAN_FENCE;
401         else
402                 vma->flags &= ~I915_VMA_CAN_FENCE;
403 }
404
405 static bool color_differs(struct drm_mm_node *node, unsigned long color)
406 {
407         return node->allocated && node->color != color;
408 }
409
410 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level)
411 {
412         struct drm_mm_node *node = &vma->node;
413         struct drm_mm_node *other;
414
415         /*
416          * On some machines we have to be careful when putting differing types
417          * of snoopable memory together to avoid the prefetcher crossing memory
418          * domains and dying. During vm initialisation, we decide whether or not
419          * these constraints apply and set the drm_mm.color_adjust
420          * appropriately.
421          */
422         if (vma->vm->mm.color_adjust == NULL)
423                 return true;
424
425         /* Only valid to be called on an already inserted vma */
426         GEM_BUG_ON(!drm_mm_node_allocated(node));
427         GEM_BUG_ON(list_empty(&node->node_list));
428
429         other = list_prev_entry(node, node_list);
430         if (color_differs(other, cache_level) && !drm_mm_hole_follows(other))
431                 return false;
432
433         other = list_next_entry(node, node_list);
434         if (color_differs(other, cache_level) && !drm_mm_hole_follows(node))
435                 return false;
436
437         return true;
438 }
439
440 /**
441  * i915_vma_insert - finds a slot for the vma in its address space
442  * @vma: the vma
443  * @size: requested size in bytes (can be larger than the VMA)
444  * @alignment: required alignment
445  * @flags: mask of PIN_* flags to use
446  *
447  * First we try to allocate some free space that meets the requirements for
448  * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
449  * preferrably the oldest idle entry to make room for the new VMA.
450  *
451  * Returns:
452  * 0 on success, negative error code otherwise.
453  */
454 static int
455 i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
456 {
457         struct drm_i915_private *dev_priv = vma->vm->i915;
458         struct drm_i915_gem_object *obj = vma->obj;
459         u64 start, end;
460         int ret;
461
462         GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
463         GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
464
465         size = max(size, vma->size);
466         alignment = max(alignment, vma->display_alignment);
467         if (flags & PIN_MAPPABLE) {
468                 size = max_t(typeof(size), size, vma->fence_size);
469                 alignment = max_t(typeof(alignment),
470                                   alignment, vma->fence_alignment);
471         }
472
473         GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
474         GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
475         GEM_BUG_ON(!is_power_of_2(alignment));
476
477         start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
478         GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
479
480         end = vma->vm->total;
481         if (flags & PIN_MAPPABLE)
482                 end = min_t(u64, end, dev_priv->ggtt.mappable_end);
483         if (flags & PIN_ZONE_4G)
484                 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
485         GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
486
487         /* If binding the object/GGTT view requires more space than the entire
488          * aperture has, reject it early before evicting everything in a vain
489          * attempt to find space.
490          */
491         if (size > end) {
492                 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n",
493                           size, obj->base.size,
494                           flags & PIN_MAPPABLE ? "mappable" : "total",
495                           end);
496                 return -ENOSPC;
497         }
498
499         ret = i915_gem_object_pin_pages(obj);
500         if (ret)
501                 return ret;
502
503         GEM_BUG_ON(vma->pages);
504
505         ret = vma->vm->set_pages(vma);
506         if (ret)
507                 goto err_unpin;
508
509         if (flags & PIN_OFFSET_FIXED) {
510                 u64 offset = flags & PIN_OFFSET_MASK;
511                 if (!IS_ALIGNED(offset, alignment) ||
512                     range_overflows(offset, size, end)) {
513                         ret = -EINVAL;
514                         goto err_clear;
515                 }
516
517                 ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
518                                            size, offset, obj->cache_level,
519                                            flags);
520                 if (ret)
521                         goto err_clear;
522         } else {
523                 /*
524                  * We only support huge gtt pages through the 48b PPGTT,
525                  * however we also don't want to force any alignment for
526                  * objects which need to be tightly packed into the low 32bits.
527                  *
528                  * Note that we assume that GGTT are limited to 4GiB for the
529                  * forseeable future. See also i915_ggtt_offset().
530                  */
531                 if (upper_32_bits(end - 1) &&
532                     vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
533                         /*
534                          * We can't mix 64K and 4K PTEs in the same page-table
535                          * (2M block), and so to avoid the ugliness and
536                          * complexity of coloring we opt for just aligning 64K
537                          * objects to 2M.
538                          */
539                         u64 page_alignment =
540                                 rounddown_pow_of_two(vma->page_sizes.sg |
541                                                      I915_GTT_PAGE_SIZE_2M);
542
543                         /*
544                          * Check we don't expand for the limited Global GTT
545                          * (mappable aperture is even more precious!). This
546                          * also checks that we exclude the aliasing-ppgtt.
547                          */
548                         GEM_BUG_ON(i915_vma_is_ggtt(vma));
549
550                         alignment = max(alignment, page_alignment);
551
552                         if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
553                                 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
554                 }
555
556                 ret = i915_gem_gtt_insert(vma->vm, &vma->node,
557                                           size, alignment, obj->cache_level,
558                                           start, end, flags);
559                 if (ret)
560                         goto err_clear;
561
562                 GEM_BUG_ON(vma->node.start < start);
563                 GEM_BUG_ON(vma->node.start + vma->node.size > end);
564         }
565         GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
566         GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
567
568         list_move_tail(&obj->global_link, &dev_priv->mm.bound_list);
569         list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
570         obj->bind_count++;
571         GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
572
573         return 0;
574
575 err_clear:
576         vma->vm->clear_pages(vma);
577 err_unpin:
578         i915_gem_object_unpin_pages(obj);
579         return ret;
580 }
581
582 static void
583 i915_vma_remove(struct i915_vma *vma)
584 {
585         struct drm_i915_gem_object *obj = vma->obj;
586
587         GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
588         GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
589
590         vma->vm->clear_pages(vma);
591
592         drm_mm_remove_node(&vma->node);
593         list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
594
595         /* Since the unbound list is global, only move to that list if
596          * no more VMAs exist.
597          */
598         if (--obj->bind_count == 0)
599                 list_move_tail(&obj->global_link,
600                                &to_i915(obj->base.dev)->mm.unbound_list);
601
602         /* And finally now the object is completely decoupled from this vma,
603          * we can drop its hold on the backing storage and allow it to be
604          * reaped by the shrinker.
605          */
606         i915_gem_object_unpin_pages(obj);
607         GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < obj->bind_count);
608 }
609
610 int __i915_vma_do_pin(struct i915_vma *vma,
611                       u64 size, u64 alignment, u64 flags)
612 {
613         const unsigned int bound = vma->flags;
614         int ret;
615
616         lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
617         GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
618         GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
619
620         if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
621                 ret = -EBUSY;
622                 goto err_unpin;
623         }
624
625         if ((bound & I915_VMA_BIND_MASK) == 0) {
626                 ret = i915_vma_insert(vma, size, alignment, flags);
627                 if (ret)
628                         goto err_unpin;
629         }
630
631         ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
632         if (ret)
633                 goto err_remove;
634
635         if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
636                 __i915_vma_set_map_and_fenceable(vma);
637
638         GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
639         GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
640         return 0;
641
642 err_remove:
643         if ((bound & I915_VMA_BIND_MASK) == 0) {
644                 i915_vma_remove(vma);
645                 GEM_BUG_ON(vma->pages);
646         }
647 err_unpin:
648         __i915_vma_unpin(vma);
649         return ret;
650 }
651
652 static void i915_vma_destroy(struct i915_vma *vma)
653 {
654         int i;
655
656         GEM_BUG_ON(vma->node.allocated);
657         GEM_BUG_ON(i915_vma_is_active(vma));
658         GEM_BUG_ON(!i915_vma_is_closed(vma));
659         GEM_BUG_ON(vma->fence);
660
661         for (i = 0; i < ARRAY_SIZE(vma->last_read); i++)
662                 GEM_BUG_ON(i915_gem_active_isset(&vma->last_read[i]));
663         GEM_BUG_ON(i915_gem_active_isset(&vma->last_fence));
664
665         list_del(&vma->vm_link);
666         if (!i915_vma_is_ggtt(vma))
667                 i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
668
669         kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
670 }
671
672 void i915_vma_close(struct i915_vma *vma)
673 {
674         GEM_BUG_ON(i915_vma_is_closed(vma));
675         vma->flags |= I915_VMA_CLOSED;
676
677         list_del(&vma->obj_link);
678         rb_erase(&vma->obj_node, &vma->obj->vma_tree);
679
680         if (!i915_vma_is_active(vma) && !i915_vma_is_pinned(vma))
681                 WARN_ON(i915_vma_unbind(vma));
682 }
683
684 static void __i915_vma_iounmap(struct i915_vma *vma)
685 {
686         GEM_BUG_ON(i915_vma_is_pinned(vma));
687
688         if (vma->iomap == NULL)
689                 return;
690
691         io_mapping_unmap(vma->iomap);
692         vma->iomap = NULL;
693 }
694
695 int i915_vma_unbind(struct i915_vma *vma)
696 {
697         struct drm_i915_gem_object *obj = vma->obj;
698         unsigned long active;
699         int ret;
700
701         lockdep_assert_held(&obj->base.dev->struct_mutex);
702
703         /* First wait upon any activity as retiring the request may
704          * have side-effects such as unpinning or even unbinding this vma.
705          */
706         active = i915_vma_get_active(vma);
707         if (active) {
708                 int idx;
709
710                 /* When a closed VMA is retired, it is unbound - eek.
711                  * In order to prevent it from being recursively closed,
712                  * take a pin on the vma so that the second unbind is
713                  * aborted.
714                  *
715                  * Even more scary is that the retire callback may free
716                  * the object (last active vma). To prevent the explosion
717                  * we defer the actual object free to a worker that can
718                  * only proceed once it acquires the struct_mutex (which
719                  * we currently hold, therefore it cannot free this object
720                  * before we are finished).
721                  */
722                 __i915_vma_pin(vma);
723
724                 for_each_active(active, idx) {
725                         ret = i915_gem_active_retire(&vma->last_read[idx],
726                                                      &vma->vm->i915->drm.struct_mutex);
727                         if (ret)
728                                 break;
729                 }
730
731                 if (!ret) {
732                         ret = i915_gem_active_retire(&vma->last_fence,
733                                                      &vma->vm->i915->drm.struct_mutex);
734                 }
735
736                 __i915_vma_unpin(vma);
737                 if (ret)
738                         return ret;
739         }
740         GEM_BUG_ON(i915_vma_is_active(vma));
741
742         if (i915_vma_is_pinned(vma))
743                 return -EBUSY;
744
745         if (!drm_mm_node_allocated(&vma->node))
746                 goto destroy;
747
748         GEM_BUG_ON(obj->bind_count == 0);
749         GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
750
751         if (i915_vma_is_map_and_fenceable(vma)) {
752                 /* release the fence reg _after_ flushing */
753                 ret = i915_vma_put_fence(vma);
754                 if (ret)
755                         return ret;
756
757                 /* Force a pagefault for domain tracking on next user access */
758                 i915_gem_release_mmap(obj);
759
760                 __i915_vma_iounmap(vma);
761                 vma->flags &= ~I915_VMA_CAN_FENCE;
762         }
763
764         if (likely(!vma->vm->closed)) {
765                 trace_i915_vma_unbind(vma);
766                 vma->vm->unbind_vma(vma);
767         }
768         vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
769
770         i915_vma_remove(vma);
771
772 destroy:
773         if (unlikely(i915_vma_is_closed(vma)))
774                 i915_vma_destroy(vma);
775
776         return 0;
777 }
778
779 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
780 #include "selftests/i915_vma.c"
781 #endif