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