i915_vma.c revision 1.5 1 /* $NetBSD: i915_vma.c,v 1.5 2021/12/19 01:44:57 riastradh Exp $ */
2
3 /*
4 * Copyright 2016 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 */
26
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: i915_vma.c,v 1.5 2021/12/19 01:44:57 riastradh Exp $");
29
30 #include <linux/sched/mm.h>
31 #include <drm/drm_gem.h>
32
33 #include "display/intel_frontbuffer.h"
34
35 #include "gt/intel_engine.h"
36 #include "gt/intel_engine_heartbeat.h"
37 #include "gt/intel_gt.h"
38 #include "gt/intel_gt_requests.h"
39
40 #include "i915_drv.h"
41 #include "i915_globals.h"
42 #include "i915_sw_fence_work.h"
43 #include "i915_trace.h"
44 #include "i915_vma.h"
45
46 static struct i915_global_vma {
47 struct i915_global base;
48 struct kmem_cache *slab_vmas;
49 } global;
50
51 struct i915_vma *i915_vma_alloc(void)
52 {
53 return kmem_cache_zalloc(global.slab_vmas, GFP_KERNEL);
54 }
55
56 void i915_vma_free(struct i915_vma *vma)
57 {
58 return kmem_cache_free(global.slab_vmas, vma);
59 }
60
61 #if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM)
62
63 #include <linux/stackdepot.h>
64
65 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
66 {
67 unsigned long *entries;
68 unsigned int nr_entries;
69 char buf[512];
70
71 if (!vma->node.stack) {
72 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: unknown owner\n",
73 vma->node.start, vma->node.size, reason);
74 return;
75 }
76
77 nr_entries = stack_depot_fetch(vma->node.stack, &entries);
78 stack_trace_snprint(buf, sizeof(buf), entries, nr_entries, 0);
79 DRM_DEBUG_DRIVER("vma.node [%08llx + %08llx] %s: inserted at %s\n",
80 vma->node.start, vma->node.size, reason, buf);
81 }
82
83 #else
84
85 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
86 {
87 }
88
89 #endif
90
91 static inline struct i915_vma *active_to_vma(struct i915_active *ref)
92 {
93 return container_of(ref, typeof(struct i915_vma), active);
94 }
95
96 static int __i915_vma_active(struct i915_active *ref)
97 {
98 return i915_vma_tryget(active_to_vma(ref)) ? 0 : -ENOENT;
99 }
100
101 __i915_active_call
102 static void __i915_vma_retire(struct i915_active *ref)
103 {
104 i915_vma_put(active_to_vma(ref));
105 }
106
107 #ifdef __NetBSD__
108 struct i915_vma_key {
109 struct i915_address_space *vm;
110 const struct i915_ggtt_view *view;
111 };
112
113 static int
114 compare_vma(void *cookie, const void *va, const void *vb)
115 {
116 const struct i915_vma *a = va;
117 const struct i915_vma *b = vb;
118 long cmp = i915_vma_compare(__UNCONST(a), b->vm, &b->ggtt_view);
119
120 return (cmp < 0 ? -1 : cmp > 0 ? +1 : 0);
121 }
122
123 static int
124 compare_vma_key(void *cookie, const void *vn, const void *vk)
125 {
126 const struct i915_vma *vma = vn;
127 const struct i915_vma_key *key = vk;
128 long cmp = i915_vma_compare(__UNCONST(vma), key->vm, key->view);
129
130 return (cmp < 0 ? -1 : cmp > 0 ? +1 : 0);
131 }
132
133 static const rb_tree_ops_t vma_tree_rb_ops = {
134 .rbto_compare_nodes = compare_vma,
135 .rbto_compare_key = compare_vma_key,
136 .rbto_node_offset = offsetof(struct i915_vma, obj_node),
137 };
138 #endif
139
140 void
141 i915_vma_tree_init(struct drm_i915_gem_object *obj)
142 {
143 #ifdef __NetBSD__
144 rb_tree_init(&obj->vma_tree.rbr_tree, &vma_tree_rb_ops);
145 #else
146 obj->vma_tree = RB_ROOT;
147 #endif
148 }
149
150 static struct i915_vma *
151 vma_create(struct drm_i915_gem_object *obj,
152 struct i915_address_space *vm,
153 const struct i915_ggtt_view *view)
154 {
155 struct i915_vma *vma;
156 struct rb_node *rb, **p;
157
158 /* The aliasing_ppgtt should never be used directly! */
159 GEM_BUG_ON(vm == &vm->gt->ggtt->alias->vm);
160
161 vma = i915_vma_alloc();
162 if (vma == NULL)
163 return ERR_PTR(-ENOMEM);
164
165 kref_init(&vma->ref);
166 mutex_init(&vma->pages_mutex);
167 vma->vm = i915_vm_get(vm);
168 vma->ops = &vm->vma_ops;
169 vma->obj = obj;
170 vma->resv = obj->base.resv;
171 vma->size = obj->base.size;
172 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
173
174 i915_active_init(&vma->active, __i915_vma_active, __i915_vma_retire);
175
176 #ifndef __NetBSD__ /* XXX fs reclaim */
177 /* Declare ourselves safe for use inside shrinkers */
178 if (IS_ENABLED(CONFIG_LOCKDEP)) {
179 fs_reclaim_acquire(GFP_KERNEL);
180 might_lock(&vma->active.mutex);
181 fs_reclaim_release(GFP_KERNEL);
182 }
183 #endif
184
185 INIT_LIST_HEAD(&vma->closed_link);
186
187 if (view && view->type != I915_GGTT_VIEW_NORMAL) {
188 vma->ggtt_view = *view;
189 if (view->type == I915_GGTT_VIEW_PARTIAL) {
190 GEM_BUG_ON(range_overflows_t(u64,
191 view->partial.offset,
192 view->partial.size,
193 obj->base.size >> PAGE_SHIFT));
194 vma->size = view->partial.size;
195 vma->size <<= PAGE_SHIFT;
196 GEM_BUG_ON(vma->size > obj->base.size);
197 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
198 vma->size = intel_rotation_info_size(&view->rotated);
199 vma->size <<= PAGE_SHIFT;
200 } else if (view->type == I915_GGTT_VIEW_REMAPPED) {
201 vma->size = intel_remapped_info_size(&view->remapped);
202 vma->size <<= PAGE_SHIFT;
203 }
204 }
205
206 if (unlikely(vma->size > vm->total))
207 goto err_vma;
208
209 GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
210
211 if (i915_is_ggtt(vm)) {
212 if (unlikely(overflows_type(vma->size, u32)))
213 goto err_vma;
214
215 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
216 i915_gem_object_get_tiling(obj),
217 i915_gem_object_get_stride(obj));
218 if (unlikely(vma->fence_size < vma->size || /* overflow */
219 vma->fence_size > vm->total))
220 goto err_vma;
221
222 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
223
224 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
225 i915_gem_object_get_tiling(obj),
226 i915_gem_object_get_stride(obj));
227 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
228
229 __set_bit(I915_VMA_GGTT_BIT, __i915_vma_flags(vma));
230 }
231
232 #ifdef __NetBSD__
233 __USE(rb);
234 __USE(p);
235 struct i915_vma *collision __diagused;
236 collision = rb_tree_insert_node(&obj->vma_tree.rbr_tree, vma);
237 KASSERT(collision == vma);
238 #else
239 spin_lock(&obj->vma.lock);
240
241 rb = NULL;
242 p = &obj->vma.tree.rb_node;
243 while (*p) {
244 struct i915_vma *pos;
245 long cmp;
246
247 rb = *p;
248 pos = rb_entry(rb, struct i915_vma, obj_node);
249
250 /*
251 * If the view already exists in the tree, another thread
252 * already created a matching vma, so return the older instance
253 * and dispose of ours.
254 */
255 cmp = i915_vma_compare(pos, vm, view);
256 if (cmp == 0) {
257 spin_unlock(&obj->vma.lock);
258 i915_vma_free(vma);
259 return pos;
260 }
261
262 if (cmp < 0)
263 p = &rb->rb_right;
264 else
265 p = &rb->rb_left;
266 }
267 rb_link_node(&vma->obj_node, rb, p);
268 rb_insert_color(&vma->obj_node, &obj->vma.tree);
269 #endif
270
271 if (i915_vma_is_ggtt(vma))
272 /*
273 * We put the GGTT vma at the start of the vma-list, followed
274 * by the ppGGTT vma. This allows us to break early when
275 * iterating over only the GGTT vma for an object, see
276 * for_each_ggtt_vma()
277 */
278 list_add(&vma->obj_link, &obj->vma.list);
279 else
280 list_add_tail(&vma->obj_link, &obj->vma.list);
281
282 spin_unlock(&obj->vma.lock);
283
284 return vma;
285
286 err_vma:
287 i915_vma_free(vma);
288 return ERR_PTR(-E2BIG);
289 }
290
291 static struct i915_vma *
292 vma_lookup(struct drm_i915_gem_object *obj,
293 struct i915_address_space *vm,
294 const struct i915_ggtt_view *view)
295 {
296 #ifdef __NetBSD__
297 const struct i915_vma_key key = { .vm = vm, .view = view };
298
299 return rb_tree_find_node(&obj->vma_tree.rbr_tree, &key);
300 #else
301 struct rb_node *rb;
302
303 rb = obj->vma.tree.rb_node;
304 while (rb) {
305 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
306 long cmp;
307
308 cmp = i915_vma_compare(vma, vm, view);
309 if (cmp == 0)
310 return vma;
311
312 if (cmp < 0)
313 rb = rb->rb_right;
314 else
315 rb = rb->rb_left;
316 }
317
318 return NULL;
319 #endif
320 }
321
322 /**
323 * i915_vma_instance - return the singleton instance of the VMA
324 * @obj: parent &struct drm_i915_gem_object to be mapped
325 * @vm: address space in which the mapping is located
326 * @view: additional mapping requirements
327 *
328 * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
329 * the same @view characteristics. If a match is not found, one is created.
330 * Once created, the VMA is kept until either the object is freed, or the
331 * address space is closed.
332 *
333 * Returns the vma, or an error pointer.
334 */
335 struct i915_vma *
336 i915_vma_instance(struct drm_i915_gem_object *obj,
337 struct i915_address_space *vm,
338 const struct i915_ggtt_view *view)
339 {
340 struct i915_vma *vma;
341
342 GEM_BUG_ON(view && !i915_is_ggtt(vm));
343 GEM_BUG_ON(!atomic_read(&vm->open));
344
345 spin_lock(&obj->vma.lock);
346 vma = vma_lookup(obj, vm, view);
347 spin_unlock(&obj->vma.lock);
348
349 /* vma_create() will resolve the race if another creates the vma */
350 if (unlikely(!vma))
351 vma = vma_create(obj, vm, view);
352
353 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
354 return vma;
355 }
356
357 struct i915_vma_work {
358 struct dma_fence_work base;
359 struct i915_vma *vma;
360 struct drm_i915_gem_object *pinned;
361 enum i915_cache_level cache_level;
362 unsigned int flags;
363 };
364
365 static int __vma_bind(struct dma_fence_work *work)
366 {
367 struct i915_vma_work *vw = container_of(work, typeof(*vw), base);
368 struct i915_vma *vma = vw->vma;
369 int err;
370
371 err = vma->ops->bind_vma(vma, vw->cache_level, vw->flags);
372 if (err)
373 atomic_or(I915_VMA_ERROR, &vma->flags);
374
375 return err;
376 }
377
378 static void __vma_release(struct dma_fence_work *work)
379 {
380 struct i915_vma_work *vw = container_of(work, typeof(*vw), base);
381
382 if (vw->pinned)
383 __i915_gem_object_unpin_pages(vw->pinned);
384 }
385
386 static const struct dma_fence_work_ops bind_ops = {
387 .name = "bind",
388 .work = __vma_bind,
389 .release = __vma_release,
390 };
391
392 struct i915_vma_work *i915_vma_work(void)
393 {
394 struct i915_vma_work *vw;
395
396 vw = kzalloc(sizeof(*vw), GFP_KERNEL);
397 if (!vw)
398 return NULL;
399
400 dma_fence_work_init(&vw->base, &bind_ops);
401 vw->base.dma.error = -EAGAIN; /* disable the worker by default */
402
403 return vw;
404 }
405
406 /**
407 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
408 * @vma: VMA to map
409 * @cache_level: mapping cache level
410 * @flags: flags like global or local mapping
411 * @work: preallocated worker for allocating and binding the PTE
412 *
413 * DMA addresses are taken from the scatter-gather table of this object (or of
414 * this VMA in case of non-default GGTT views) and PTE entries set up.
415 * Note that DMA addresses are also the only part of the SG table we care about.
416 */
417 int i915_vma_bind(struct i915_vma *vma,
418 enum i915_cache_level cache_level,
419 u32 flags,
420 struct i915_vma_work *work)
421 {
422 u32 bind_flags;
423 u32 vma_flags;
424 int ret;
425
426 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
427 GEM_BUG_ON(vma->size > vma->node.size);
428
429 if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start,
430 vma->node.size,
431 vma->vm->total)))
432 return -ENODEV;
433
434 if (GEM_DEBUG_WARN_ON(!flags))
435 return -EINVAL;
436
437 bind_flags = flags;
438 bind_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
439
440 vma_flags = atomic_read(&vma->flags);
441 vma_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
442 if (flags & PIN_UPDATE)
443 bind_flags |= vma_flags;
444 else
445 bind_flags &= ~vma_flags;
446 if (bind_flags == 0)
447 return 0;
448
449 GEM_BUG_ON(!vma->pages);
450
451 trace_i915_vma_bind(vma, bind_flags);
452 if (work && (bind_flags & ~vma_flags) & vma->vm->bind_async_flags) {
453 work->vma = vma;
454 work->cache_level = cache_level;
455 work->flags = bind_flags | I915_VMA_ALLOC;
456
457 /*
458 * Note we only want to chain up to the migration fence on
459 * the pages (not the object itself). As we don't track that,
460 * yet, we have to use the exclusive fence instead.
461 *
462 * Also note that we do not want to track the async vma as
463 * part of the obj->resv->excl_fence as it only affects
464 * execution and not content or object's backing store lifetime.
465 */
466 GEM_BUG_ON(i915_active_has_exclusive(&vma->active));
467 i915_active_set_exclusive(&vma->active, &work->base.dma);
468 work->base.dma.error = 0; /* enable the queue_work() */
469
470 if (vma->obj) {
471 __i915_gem_object_pin_pages(vma->obj);
472 work->pinned = vma->obj;
473 }
474 } else {
475 GEM_BUG_ON((bind_flags & ~vma_flags) & vma->vm->bind_async_flags);
476 ret = vma->ops->bind_vma(vma, cache_level, bind_flags);
477 if (ret)
478 return ret;
479 }
480
481 atomic_or(bind_flags, &vma->flags);
482 return 0;
483 }
484
485 #ifdef __NetBSD__
486 # define __iomem __i915_vma_iomem
487 #endif
488
489 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
490 {
491 void __iomem *ptr;
492 int err;
493
494 if (GEM_WARN_ON(!i915_vma_is_map_and_fenceable(vma))) {
495 err = -ENODEV;
496 goto err;
497 }
498
499 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
500 GEM_BUG_ON(!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND));
501
502 ptr = READ_ONCE(vma->iomap);
503 if (ptr == NULL) {
504 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
505 vma->node.start,
506 vma->node.size);
507 if (ptr == NULL) {
508 err = -ENOMEM;
509 goto err;
510 }
511
512 if (unlikely(cmpxchg(&vma->iomap, NULL, ptr))) {
513 io_mapping_unmap(ptr);
514 ptr = vma->iomap;
515 }
516 }
517
518 __i915_vma_pin(vma);
519
520 err = i915_vma_pin_fence(vma);
521 if (err)
522 goto err_unpin;
523
524 i915_vma_set_ggtt_write(vma);
525
526 /* NB Access through the GTT requires the device to be awake. */
527 return ptr;
528
529 err_unpin:
530 __i915_vma_unpin(vma);
531 err:
532 return IO_ERR_PTR(err);
533 }
534
535 #ifdef __NetBSD__
536 # undef __iomem
537 #endif
538
539 void i915_vma_flush_writes(struct i915_vma *vma)
540 {
541 if (i915_vma_unset_ggtt_write(vma))
542 intel_gt_flush_ggtt_writes(vma->vm->gt);
543 }
544
545 void i915_vma_unpin_iomap(struct i915_vma *vma)
546 {
547 GEM_BUG_ON(vma->iomap == NULL);
548
549 i915_vma_flush_writes(vma);
550
551 i915_vma_unpin_fence(vma);
552 i915_vma_unpin(vma);
553 }
554
555 void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags)
556 {
557 struct i915_vma *vma;
558 struct drm_i915_gem_object *obj;
559
560 vma = fetch_and_zero(p_vma);
561 if (!vma)
562 return;
563
564 obj = vma->obj;
565 GEM_BUG_ON(!obj);
566
567 i915_vma_unpin(vma);
568 i915_vma_close(vma);
569
570 if (flags & I915_VMA_RELEASE_MAP)
571 i915_gem_object_unpin_map(obj);
572
573 i915_gem_object_put(obj);
574 }
575
576 bool i915_vma_misplaced(const struct i915_vma *vma,
577 u64 size, u64 alignment, u64 flags)
578 {
579 if (!drm_mm_node_allocated(&vma->node))
580 return false;
581
582 if (test_bit(I915_VMA_ERROR_BIT, __i915_vma_flags(vma)))
583 return true;
584
585 if (vma->node.size < size)
586 return true;
587
588 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
589 if (alignment && !IS_ALIGNED(vma->node.start, alignment))
590 return true;
591
592 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
593 return true;
594
595 if (flags & PIN_OFFSET_BIAS &&
596 vma->node.start < (flags & PIN_OFFSET_MASK))
597 return true;
598
599 if (flags & PIN_OFFSET_FIXED &&
600 vma->node.start != (flags & PIN_OFFSET_MASK))
601 return true;
602
603 return false;
604 }
605
606 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
607 {
608 bool mappable, fenceable;
609
610 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
611 GEM_BUG_ON(!vma->fence_size);
612
613 fenceable = (vma->node.size >= vma->fence_size &&
614 IS_ALIGNED(vma->node.start, vma->fence_alignment));
615
616 mappable = vma->node.start + vma->fence_size <= i915_vm_to_ggtt(vma->vm)->mappable_end;
617
618 if (mappable && fenceable)
619 set_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
620 else
621 clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
622 }
623
624 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long color)
625 {
626 struct drm_mm_node *node = &vma->node;
627 struct drm_mm_node *other;
628
629 /*
630 * On some machines we have to be careful when putting differing types
631 * of snoopable memory together to avoid the prefetcher crossing memory
632 * domains and dying. During vm initialisation, we decide whether or not
633 * these constraints apply and set the drm_mm.color_adjust
634 * appropriately.
635 */
636 if (!i915_vm_has_cache_coloring(vma->vm))
637 return true;
638
639 /* Only valid to be called on an already inserted vma */
640 GEM_BUG_ON(!drm_mm_node_allocated(node));
641 GEM_BUG_ON(list_empty(&node->node_list));
642
643 other = list_prev_entry(node, node_list);
644 if (i915_node_color_differs(other, color) &&
645 !drm_mm_hole_follows(other))
646 return false;
647
648 other = list_next_entry(node, node_list);
649 if (i915_node_color_differs(other, color) &&
650 !drm_mm_hole_follows(node))
651 return false;
652
653 return true;
654 }
655
656 static void assert_bind_count(const struct drm_i915_gem_object *obj)
657 {
658 /*
659 * Combine the assertion that the object is bound and that we have
660 * pinned its pages. But we should never have bound the object
661 * more than we have pinned its pages. (For complete accuracy, we
662 * assume that no else is pinning the pages, but as a rough assertion
663 * that we will not run into problems later, this will do!)
664 */
665 GEM_BUG_ON(atomic_read(&obj->mm.pages_pin_count) < atomic_read(&obj->bind_count));
666 }
667
668 /**
669 * i915_vma_insert - finds a slot for the vma in its address space
670 * @vma: the vma
671 * @size: requested size in bytes (can be larger than the VMA)
672 * @alignment: required alignment
673 * @flags: mask of PIN_* flags to use
674 *
675 * First we try to allocate some free space that meets the requirements for
676 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
677 * preferrably the oldest idle entry to make room for the new VMA.
678 *
679 * Returns:
680 * 0 on success, negative error code otherwise.
681 */
682 static int
683 i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
684 {
685 unsigned long color;
686 u64 start, end;
687 int ret;
688
689 GEM_BUG_ON(i915_vma_is_closed(vma));
690 GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
691 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
692
693 size = max(size, vma->size);
694 alignment = max(alignment, vma->display_alignment);
695 if (flags & PIN_MAPPABLE) {
696 size = max_t(typeof(size), size, vma->fence_size);
697 alignment = max_t(typeof(alignment),
698 alignment, vma->fence_alignment);
699 }
700
701 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
702 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
703 GEM_BUG_ON(!is_power_of_2(alignment));
704
705 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
706 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
707
708 end = vma->vm->total;
709 if (flags & PIN_MAPPABLE)
710 end = min_t(u64, end, i915_vm_to_ggtt(vma->vm)->mappable_end);
711 if (flags & PIN_ZONE_4G)
712 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
713 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
714
715 /* If binding the object/GGTT view requires more space than the entire
716 * aperture has, reject it early before evicting everything in a vain
717 * attempt to find space.
718 */
719 if (size > end) {
720 DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%"PRIu64" > %s aperture=%"PRIu64"\n",
721 size, flags & PIN_MAPPABLE ? "mappable" : "total",
722 end);
723 return -ENOSPC;
724 }
725
726 color = 0;
727 if (vma->obj && i915_vm_has_cache_coloring(vma->vm))
728 color = vma->obj->cache_level;
729
730 if (flags & PIN_OFFSET_FIXED) {
731 u64 offset = flags & PIN_OFFSET_MASK;
732 if (!IS_ALIGNED(offset, alignment) ||
733 range_overflows(offset, size, end))
734 return -EINVAL;
735
736 ret = i915_gem_gtt_reserve(vma->vm, &vma->node,
737 size, offset, color,
738 flags);
739 if (ret)
740 return ret;
741 } else {
742 /*
743 * We only support huge gtt pages through the 48b PPGTT,
744 * however we also don't want to force any alignment for
745 * objects which need to be tightly packed into the low 32bits.
746 *
747 * Note that we assume that GGTT are limited to 4GiB for the
748 * forseeable future. See also i915_ggtt_offset().
749 */
750 if (upper_32_bits(end - 1) &&
751 vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
752 /*
753 * We can't mix 64K and 4K PTEs in the same page-table
754 * (2M block), and so to avoid the ugliness and
755 * complexity of coloring we opt for just aligning 64K
756 * objects to 2M.
757 */
758 u64 page_alignment =
759 rounddown_pow_of_two(vma->page_sizes.sg |
760 I915_GTT_PAGE_SIZE_2M);
761
762 /*
763 * Check we don't expand for the limited Global GTT
764 * (mappable aperture is even more precious!). This
765 * also checks that we exclude the aliasing-ppgtt.
766 */
767 GEM_BUG_ON(i915_vma_is_ggtt(vma));
768
769 alignment = max(alignment, page_alignment);
770
771 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
772 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
773 }
774
775 ret = i915_gem_gtt_insert(vma->vm, &vma->node,
776 size, alignment, color,
777 start, end, flags);
778 if (ret)
779 return ret;
780
781 GEM_BUG_ON(vma->node.start < start);
782 GEM_BUG_ON(vma->node.start + vma->node.size > end);
783 }
784 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
785 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, color));
786
787 if (vma->obj) {
788 struct drm_i915_gem_object *obj = vma->obj;
789
790 atomic_inc(&obj->bind_count);
791 assert_bind_count(obj);
792 }
793 list_add_tail(&vma->vm_link, &vma->vm->bound_list);
794
795 return 0;
796 }
797
798 static void
799 i915_vma_detach(struct i915_vma *vma)
800 {
801 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
802 GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
803
804 /*
805 * And finally now the object is completely decoupled from this
806 * vma, we can drop its hold on the backing storage and allow
807 * it to be reaped by the shrinker.
808 */
809 list_del(&vma->vm_link);
810 if (vma->obj) {
811 struct drm_i915_gem_object *obj = vma->obj;
812
813 assert_bind_count(obj);
814 atomic_dec(&obj->bind_count);
815 }
816 }
817
818 static bool try_qad_pin(struct i915_vma *vma, unsigned int flags)
819 {
820 unsigned int bound;
821 bool pinned = true;
822
823 bound = atomic_read(&vma->flags);
824 do {
825 if (unlikely(flags & ~bound))
826 return false;
827
828 if (unlikely(bound & (I915_VMA_OVERFLOW | I915_VMA_ERROR)))
829 return false;
830
831 if (!(bound & I915_VMA_PIN_MASK))
832 goto unpinned;
833
834 GEM_BUG_ON(((bound + 1) & I915_VMA_PIN_MASK) == 0);
835 } while (!atomic_try_cmpxchg(&vma->flags, &bound, bound + 1));
836
837 return true;
838
839 unpinned:
840 /*
841 * If pin_count==0, but we are bound, check under the lock to avoid
842 * racing with a concurrent i915_vma_unbind().
843 */
844 mutex_lock(&vma->vm->mutex);
845 do {
846 if (unlikely(bound & (I915_VMA_OVERFLOW | I915_VMA_ERROR))) {
847 pinned = false;
848 break;
849 }
850
851 if (unlikely(flags & ~bound)) {
852 pinned = false;
853 break;
854 }
855 } while (!atomic_try_cmpxchg(&vma->flags, &bound, bound + 1));
856 mutex_unlock(&vma->vm->mutex);
857
858 return pinned;
859 }
860
861 static int vma_get_pages(struct i915_vma *vma)
862 {
863 int err = 0;
864
865 if (atomic_add_unless(&vma->pages_count, 1, 0))
866 return 0;
867
868 /* Allocations ahoy! */
869 if (mutex_lock_interruptible(&vma->pages_mutex))
870 return -EINTR;
871
872 if (!atomic_read(&vma->pages_count)) {
873 if (vma->obj) {
874 err = i915_gem_object_pin_pages(vma->obj);
875 if (err)
876 goto unlock;
877 }
878
879 err = vma->ops->set_pages(vma);
880 if (err) {
881 if (vma->obj)
882 i915_gem_object_unpin_pages(vma->obj);
883 goto unlock;
884 }
885 }
886 atomic_inc(&vma->pages_count);
887
888 unlock:
889 mutex_unlock(&vma->pages_mutex);
890
891 return err;
892 }
893
894 static void __vma_put_pages(struct i915_vma *vma, unsigned int count)
895 {
896 /* We allocate under vma_get_pages, so beware the shrinker */
897 mutex_lock_nested(&vma->pages_mutex, SINGLE_DEPTH_NESTING);
898 GEM_BUG_ON(atomic_read(&vma->pages_count) < count);
899 if (atomic_sub_return(count, &vma->pages_count) == 0) {
900 vma->ops->clear_pages(vma);
901 GEM_BUG_ON(vma->pages);
902 if (vma->obj)
903 i915_gem_object_unpin_pages(vma->obj);
904 }
905 mutex_unlock(&vma->pages_mutex);
906 }
907
908 static void vma_put_pages(struct i915_vma *vma)
909 {
910 if (atomic_add_unless(&vma->pages_count, -1, 1))
911 return;
912
913 __vma_put_pages(vma, 1);
914 }
915
916 static void vma_unbind_pages(struct i915_vma *vma)
917 {
918 unsigned int count;
919
920 lockdep_assert_held(&vma->vm->mutex);
921
922 /* The upper portion of pages_count is the number of bindings */
923 count = atomic_read(&vma->pages_count);
924 count >>= I915_VMA_PAGES_BIAS;
925 GEM_BUG_ON(!count);
926
927 __vma_put_pages(vma, count | count << I915_VMA_PAGES_BIAS);
928 }
929
930 int i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
931 {
932 struct i915_vma_work *work = NULL;
933 intel_wakeref_t wakeref = 0;
934 unsigned int bound;
935 int err;
936
937 BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
938 BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
939
940 GEM_BUG_ON(flags & PIN_UPDATE);
941 GEM_BUG_ON(!(flags & (PIN_USER | PIN_GLOBAL)));
942
943 /* First try and grab the pin without rebinding the vma */
944 if (try_qad_pin(vma, flags & I915_VMA_BIND_MASK))
945 return 0;
946
947 err = vma_get_pages(vma);
948 if (err)
949 return err;
950
951 if (flags & vma->vm->bind_async_flags) {
952 work = i915_vma_work();
953 if (!work) {
954 err = -ENOMEM;
955 goto err_pages;
956 }
957 }
958
959 if (flags & PIN_GLOBAL)
960 wakeref = intel_runtime_pm_get(&vma->vm->i915->runtime_pm);
961
962 /* No more allocations allowed once we hold vm->mutex */
963 err = mutex_lock_interruptible(&vma->vm->mutex);
964 if (err)
965 goto err_fence;
966
967 bound = atomic_read(&vma->flags);
968 if (unlikely(bound & I915_VMA_ERROR)) {
969 err = -ENOMEM;
970 goto err_unlock;
971 }
972
973 if (unlikely(!((bound + 1) & I915_VMA_PIN_MASK))) {
974 err = -EAGAIN; /* pins are meant to be fairly temporary */
975 goto err_unlock;
976 }
977
978 if (unlikely(!(flags & ~bound & I915_VMA_BIND_MASK))) {
979 __i915_vma_pin(vma);
980 goto err_unlock;
981 }
982
983 err = i915_active_acquire(&vma->active);
984 if (err)
985 goto err_unlock;
986
987 if (!(bound & I915_VMA_BIND_MASK)) {
988 err = i915_vma_insert(vma, size, alignment, flags);
989 if (err)
990 goto err_active;
991
992 if (i915_is_ggtt(vma->vm))
993 __i915_vma_set_map_and_fenceable(vma);
994 }
995
996 GEM_BUG_ON(!vma->pages);
997 err = i915_vma_bind(vma,
998 vma->obj ? vma->obj->cache_level : 0,
999 flags, work);
1000 if (err)
1001 goto err_remove;
1002
1003 /* There should only be at most 2 active bindings (user, global) */
1004 GEM_BUG_ON(bound + I915_VMA_PAGES_ACTIVE < bound);
1005 atomic_add(I915_VMA_PAGES_ACTIVE, &vma->pages_count);
1006 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
1007
1008 __i915_vma_pin(vma);
1009 GEM_BUG_ON(!i915_vma_is_pinned(vma));
1010 GEM_BUG_ON(!i915_vma_is_bound(vma, flags));
1011 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
1012
1013 err_remove:
1014 if (!i915_vma_is_bound(vma, I915_VMA_BIND_MASK)) {
1015 i915_vma_detach(vma);
1016 drm_mm_remove_node(&vma->node);
1017 }
1018 err_active:
1019 i915_active_release(&vma->active);
1020 err_unlock:
1021 mutex_unlock(&vma->vm->mutex);
1022 err_fence:
1023 if (work)
1024 dma_fence_work_commit(&work->base);
1025 if (wakeref)
1026 intel_runtime_pm_put(&vma->vm->i915->runtime_pm, wakeref);
1027 err_pages:
1028 vma_put_pages(vma);
1029 return err;
1030 }
1031
1032 static void flush_idle_contexts(struct intel_gt *gt)
1033 {
1034 struct intel_engine_cs *engine;
1035 enum intel_engine_id id;
1036
1037 for_each_engine(engine, gt, id)
1038 intel_engine_flush_barriers(engine);
1039
1040 intel_gt_wait_for_idle(gt, MAX_SCHEDULE_TIMEOUT);
1041 }
1042
1043 int i915_ggtt_pin(struct i915_vma *vma, u32 align, unsigned int flags)
1044 {
1045 struct i915_address_space *vm = vma->vm;
1046 int err;
1047
1048 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
1049
1050 do {
1051 err = i915_vma_pin(vma, 0, align, flags | PIN_GLOBAL);
1052 if (err != -ENOSPC)
1053 return err;
1054
1055 /* Unlike i915_vma_pin, we don't take no for an answer! */
1056 flush_idle_contexts(vm->gt);
1057 if (mutex_lock_interruptible(&vm->mutex) == 0) {
1058 i915_gem_evict_vm(vm);
1059 mutex_unlock(&vm->mutex);
1060 }
1061 } while (1);
1062 }
1063
1064 void i915_vma_close(struct i915_vma *vma)
1065 {
1066 struct intel_gt *gt = vma->vm->gt;
1067 unsigned long flags;
1068
1069 GEM_BUG_ON(i915_vma_is_closed(vma));
1070
1071 /*
1072 * We defer actually closing, unbinding and destroying the VMA until
1073 * the next idle point, or if the object is freed in the meantime. By
1074 * postponing the unbind, we allow for it to be resurrected by the
1075 * client, avoiding the work required to rebind the VMA. This is
1076 * advantageous for DRI, where the client/server pass objects
1077 * between themselves, temporarily opening a local VMA to the
1078 * object, and then closing it again. The same object is then reused
1079 * on the next frame (or two, depending on the depth of the swap queue)
1080 * causing us to rebind the VMA once more. This ends up being a lot
1081 * of wasted work for the steady state.
1082 */
1083 spin_lock_irqsave(>->closed_lock, flags);
1084 list_add(&vma->closed_link, >->closed_vma);
1085 spin_unlock_irqrestore(>->closed_lock, flags);
1086 }
1087
1088 static void __i915_vma_remove_closed(struct i915_vma *vma)
1089 {
1090 struct intel_gt *gt = vma->vm->gt;
1091
1092 spin_lock_irq(>->closed_lock);
1093 list_del_init(&vma->closed_link);
1094 spin_unlock_irq(>->closed_lock);
1095 }
1096
1097 void i915_vma_reopen(struct i915_vma *vma)
1098 {
1099 if (i915_vma_is_closed(vma))
1100 __i915_vma_remove_closed(vma);
1101 }
1102
1103 void i915_vma_release(struct kref *ref)
1104 {
1105 struct i915_vma *vma = container_of(ref, typeof(*vma), ref);
1106
1107 if (drm_mm_node_allocated(&vma->node)) {
1108 mutex_lock(&vma->vm->mutex);
1109 atomic_and(~I915_VMA_PIN_MASK, &vma->flags);
1110 WARN_ON(__i915_vma_unbind(vma));
1111 mutex_unlock(&vma->vm->mutex);
1112 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
1113 }
1114 GEM_BUG_ON(i915_vma_is_active(vma));
1115
1116 if (vma->obj) {
1117 struct drm_i915_gem_object *obj = vma->obj;
1118
1119 spin_lock(&obj->vma.lock);
1120 list_del(&vma->obj_link);
1121 rb_erase(&vma->obj_node, &obj->vma.tree);
1122 spin_unlock(&obj->vma.lock);
1123 }
1124
1125 __i915_vma_remove_closed(vma);
1126 i915_vm_put(vma->vm);
1127
1128 i915_active_fini(&vma->active);
1129 i915_vma_free(vma);
1130 }
1131
1132 void i915_vma_parked(struct intel_gt *gt)
1133 {
1134 struct i915_vma *vma, *next;
1135
1136 spin_lock_irq(>->closed_lock);
1137 list_for_each_entry_safe(vma, next, >->closed_vma, closed_link) {
1138 struct drm_i915_gem_object *obj = vma->obj;
1139 struct i915_address_space *vm = vma->vm;
1140
1141 /* XXX All to avoid keeping a reference on i915_vma itself */
1142
1143 if (!kref_get_unless_zero(&obj->base.refcount))
1144 continue;
1145
1146 if (i915_vm_tryopen(vm)) {
1147 list_del_init(&vma->closed_link);
1148 } else {
1149 i915_gem_object_put(obj);
1150 obj = NULL;
1151 }
1152
1153 spin_unlock_irq(>->closed_lock);
1154
1155 if (obj) {
1156 __i915_vma_put(vma);
1157 i915_gem_object_put(obj);
1158 }
1159
1160 i915_vm_close(vm);
1161
1162 /* Restart after dropping lock */
1163 spin_lock_irq(>->closed_lock);
1164 next = list_first_entry(>->closed_vma,
1165 typeof(*next), closed_link);
1166 }
1167 spin_unlock_irq(>->closed_lock);
1168 }
1169
1170 static void __i915_vma_iounmap(struct i915_vma *vma)
1171 {
1172 GEM_BUG_ON(i915_vma_is_pinned(vma));
1173
1174 if (vma->iomap == NULL)
1175 return;
1176
1177 #ifdef __NetBSD__
1178 io_mapping_unmap(&i915_vm_to_ggtt(vma->vm)->iomap, vma->iomap);
1179 #else
1180 io_mapping_unmap(vma->iomap);
1181 #endif
1182 vma->iomap = NULL;
1183 }
1184
1185 void i915_vma_revoke_mmap(struct i915_vma *vma)
1186 {
1187 struct drm_vma_offset_node *node;
1188 u64 vma_offset;
1189
1190 if (!i915_vma_has_userfault(vma))
1191 return;
1192
1193 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
1194 GEM_BUG_ON(!vma->obj->userfault_count);
1195
1196 #ifdef __NetBSD__
1197 __USE(vma_offset);
1198 __USE(node);
1199 struct drm_i915_private *i915 = to_i915(vma->obj->base.dev);
1200 paddr_t pa = i915->ggtt.gmadr.start + vma->node.start;
1201 vsize_t npgs = vma->size >> PAGE_SHIFT;
1202 while (npgs --> 0)
1203 pmap_pv_protect(pa = (npgs << PAGE_SHIFT), VM_PROT_NONE);
1204 #else
1205 node = &vma->mmo->vma_node;
1206 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
1207 unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
1208 drm_vma_node_offset_addr(node) + vma_offset,
1209 vma->size,
1210 1);
1211 #endif
1212
1213 i915_vma_unset_userfault(vma);
1214 if (!--vma->obj->userfault_count)
1215 list_del(&vma->obj->userfault_link);
1216 }
1217
1218 int __i915_vma_move_to_active(struct i915_vma *vma, struct i915_request *rq)
1219 {
1220 int err;
1221
1222 GEM_BUG_ON(!i915_vma_is_pinned(vma));
1223
1224 /* Wait for the vma to be bound before we start! */
1225 err = i915_request_await_active(rq, &vma->active);
1226 if (err)
1227 return err;
1228
1229 return i915_active_add_request(&vma->active, rq);
1230 }
1231
1232 int i915_vma_move_to_active(struct i915_vma *vma,
1233 struct i915_request *rq,
1234 unsigned int flags)
1235 {
1236 struct drm_i915_gem_object *obj = vma->obj;
1237 int err;
1238
1239 assert_object_held(obj);
1240
1241 err = __i915_vma_move_to_active(vma, rq);
1242 if (unlikely(err))
1243 return err;
1244
1245 if (flags & EXEC_OBJECT_WRITE) {
1246 struct intel_frontbuffer *front;
1247
1248 front = __intel_frontbuffer_get(obj);
1249 if (unlikely(front)) {
1250 if (intel_frontbuffer_invalidate(front, ORIGIN_CS))
1251 i915_active_add_request(&front->write, rq);
1252 intel_frontbuffer_put(front);
1253 }
1254
1255 dma_resv_add_excl_fence(vma->resv, &rq->fence);
1256 obj->write_domain = I915_GEM_DOMAIN_RENDER;
1257 obj->read_domains = 0;
1258 } else {
1259 err = dma_resv_reserve_shared(vma->resv, 1);
1260 if (unlikely(err))
1261 return err;
1262
1263 dma_resv_add_shared_fence(vma->resv, &rq->fence);
1264 obj->write_domain = 0;
1265 }
1266 obj->read_domains |= I915_GEM_GPU_DOMAINS;
1267 obj->mm.dirty = true;
1268
1269 GEM_BUG_ON(!i915_vma_is_active(vma));
1270 return 0;
1271 }
1272
1273 int __i915_vma_unbind(struct i915_vma *vma)
1274 {
1275 int ret;
1276
1277 lockdep_assert_held(&vma->vm->mutex);
1278
1279 /*
1280 * First wait upon any activity as retiring the request may
1281 * have side-effects such as unpinning or even unbinding this vma.
1282 *
1283 * XXX Actually waiting under the vm->mutex is a hinderance and
1284 * should be pipelined wherever possible. In cases where that is
1285 * unavoidable, we should lift the wait to before the mutex.
1286 */
1287 ret = i915_vma_sync(vma);
1288 if (ret)
1289 return ret;
1290
1291 if (i915_vma_is_pinned(vma)) {
1292 vma_print_allocator(vma, "is pinned");
1293 return -EAGAIN;
1294 }
1295
1296 /*
1297 * After confirming that no one else is pinning this vma, wait for
1298 * any laggards who may have crept in during the wait (through
1299 * a residual pin skipping the vm->mutex) to complete.
1300 */
1301 ret = i915_vma_sync(vma);
1302 if (ret)
1303 return ret;
1304
1305 if (!drm_mm_node_allocated(&vma->node))
1306 return 0;
1307
1308 GEM_BUG_ON(i915_vma_is_pinned(vma));
1309 GEM_BUG_ON(i915_vma_is_active(vma));
1310
1311 if (i915_vma_is_map_and_fenceable(vma)) {
1312 /*
1313 * Check that we have flushed all writes through the GGTT
1314 * before the unbind, other due to non-strict nature of those
1315 * indirect writes they may end up referencing the GGTT PTE
1316 * after the unbind.
1317 */
1318 i915_vma_flush_writes(vma);
1319 GEM_BUG_ON(i915_vma_has_ggtt_write(vma));
1320
1321 /* release the fence reg _after_ flushing */
1322 ret = i915_vma_revoke_fence(vma);
1323 if (ret)
1324 return ret;
1325
1326 /* Force a pagefault for domain tracking on next user access */
1327 i915_vma_revoke_mmap(vma);
1328
1329 __i915_vma_iounmap(vma);
1330 clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
1331 }
1332 GEM_BUG_ON(vma->fence);
1333 GEM_BUG_ON(i915_vma_has_userfault(vma));
1334
1335 if (likely(atomic_read(&vma->vm->open))) {
1336 trace_i915_vma_unbind(vma);
1337 vma->ops->unbind_vma(vma);
1338 }
1339 atomic_and(~(I915_VMA_BIND_MASK | I915_VMA_ERROR), &vma->flags);
1340
1341 i915_vma_detach(vma);
1342 vma_unbind_pages(vma);
1343
1344 drm_mm_remove_node(&vma->node); /* pairs with i915_vma_release() */
1345 return 0;
1346 }
1347
1348 int i915_vma_unbind(struct i915_vma *vma)
1349 {
1350 struct i915_address_space *vm = vma->vm;
1351 intel_wakeref_t wakeref = 0;
1352 int err;
1353
1354 if (!drm_mm_node_allocated(&vma->node))
1355 return 0;
1356
1357 if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
1358 /* XXX not always required: nop_clear_range */
1359 wakeref = intel_runtime_pm_get(&vm->i915->runtime_pm);
1360
1361 err = mutex_lock_interruptible(&vm->mutex);
1362 if (err)
1363 return err;
1364
1365 err = __i915_vma_unbind(vma);
1366 mutex_unlock(&vm->mutex);
1367
1368 if (wakeref)
1369 intel_runtime_pm_put(&vm->i915->runtime_pm, wakeref);
1370
1371 return err;
1372 }
1373
1374 struct i915_vma *i915_vma_make_unshrinkable(struct i915_vma *vma)
1375 {
1376 i915_gem_object_make_unshrinkable(vma->obj);
1377 return vma;
1378 }
1379
1380 void i915_vma_make_shrinkable(struct i915_vma *vma)
1381 {
1382 i915_gem_object_make_shrinkable(vma->obj);
1383 }
1384
1385 void i915_vma_make_purgeable(struct i915_vma *vma)
1386 {
1387 i915_gem_object_make_purgeable(vma->obj);
1388 }
1389
1390 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1391 #include "selftests/i915_vma.c"
1392 #endif
1393
1394 static void i915_global_vma_shrink(void)
1395 {
1396 kmem_cache_shrink(global.slab_vmas);
1397 }
1398
1399 static void i915_global_vma_exit(void)
1400 {
1401 kmem_cache_destroy(global.slab_vmas);
1402 }
1403
1404 static struct i915_global_vma global = { {
1405 .shrink = i915_global_vma_shrink,
1406 .exit = i915_global_vma_exit,
1407 } };
1408
1409 int __init i915_global_vma_init(void)
1410 {
1411 global.slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
1412 if (!global.slab_vmas)
1413 return -ENOMEM;
1414
1415 i915_global_register(&global.base);
1416 return 0;
1417 }
1418