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 <linux/sched/mm.h>
26 #include <linux/dma-fence-array.h>
27 #include <drm/drm_gem.h>
28
29 #include "display/intel_display.h"
30 #include "display/intel_frontbuffer.h"
31 #include "gem/i915_gem_lmem.h"
32 #include "gem/i915_gem_tiling.h"
33 #include "gt/intel_engine.h"
34 #include "gt/intel_engine_heartbeat.h"
35 #include "gt/intel_gt.h"
36 #include "gt/intel_gt_pm.h"
37 #include "gt/intel_gt_requests.h"
38 #include "gt/intel_tlb.h"
39
40 #include "i915_drv.h"
41 #include "i915_gem_evict.h"
42 #include "i915_sw_fence_work.h"
43 #include "i915_trace.h"
44 #include "i915_vma.h"
45 #include "i915_vma_resource.h"
46
assert_vma_held_evict(const struct i915_vma * vma)47 static inline void assert_vma_held_evict(const struct i915_vma *vma)
48 {
49 /*
50 * We may be forced to unbind when the vm is dead, to clean it up.
51 * This is the only exception to the requirement of the object lock
52 * being held.
53 */
54 if (kref_read(&vma->vm->ref))
55 assert_object_held_shared(vma->obj);
56 }
57
58 static struct pool slab_vmas;
59
i915_vma_alloc(void)60 static struct i915_vma *i915_vma_alloc(void)
61 {
62 #ifdef __linux__
63 return kmem_cache_zalloc(slab_vmas, GFP_KERNEL);
64 #else
65 return pool_get(&slab_vmas, PR_WAITOK | PR_ZERO);
66 #endif
67 }
68
i915_vma_free(struct i915_vma * vma)69 static void i915_vma_free(struct i915_vma *vma)
70 {
71 #ifdef __linux__
72 return kmem_cache_free(slab_vmas, vma);
73 #else
74 pool_put(&slab_vmas, vma);
75 #endif
76 }
77
78 #if IS_ENABLED(CONFIG_DRM_I915_ERRLOG_GEM) && IS_ENABLED(CONFIG_DRM_DEBUG_MM)
79
80 #include <linux/stackdepot.h>
81
vma_print_allocator(struct i915_vma * vma,const char * reason)82 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
83 {
84 char buf[512];
85
86 if (!vma->node.stack) {
87 drm_dbg(vma->obj->base.dev,
88 "vma.node [%08llx + %08llx] %s: unknown owner\n",
89 vma->node.start, vma->node.size, reason);
90 return;
91 }
92
93 stack_depot_snprint(vma->node.stack, buf, sizeof(buf), 0);
94 drm_dbg(vma->obj->base.dev,
95 "vma.node [%08llx + %08llx] %s: inserted at %s\n",
96 vma->node.start, vma->node.size, reason, buf);
97 }
98
99 #else
100
vma_print_allocator(struct i915_vma * vma,const char * reason)101 static void vma_print_allocator(struct i915_vma *vma, const char *reason)
102 {
103 }
104
105 #endif
106
active_to_vma(struct i915_active * ref)107 static inline struct i915_vma *active_to_vma(struct i915_active *ref)
108 {
109 return container_of(ref, typeof(struct i915_vma), active);
110 }
111
__i915_vma_active(struct i915_active * ref)112 static int __i915_vma_active(struct i915_active *ref)
113 {
114 struct i915_vma *vma = active_to_vma(ref);
115
116 if (!i915_vma_tryget(vma))
117 return -ENOENT;
118
119 /*
120 * Exclude global GTT VMA from holding a GT wakeref
121 * while active, otherwise GPU never goes idle.
122 */
123 if (!i915_vma_is_ggtt(vma))
124 intel_gt_pm_get(vma->vm->gt);
125
126 return 0;
127 }
128
__i915_vma_retire(struct i915_active * ref)129 static void __i915_vma_retire(struct i915_active *ref)
130 {
131 struct i915_vma *vma = active_to_vma(ref);
132
133 if (!i915_vma_is_ggtt(vma)) {
134 /*
135 * Since we can be called from atomic contexts,
136 * use an async variant of intel_gt_pm_put().
137 */
138 intel_gt_pm_put_async(vma->vm->gt);
139 }
140
141 i915_vma_put(vma);
142 }
143
144 static struct i915_vma *
vma_create(struct drm_i915_gem_object * obj,struct i915_address_space * vm,const struct i915_gtt_view * view)145 vma_create(struct drm_i915_gem_object *obj,
146 struct i915_address_space *vm,
147 const struct i915_gtt_view *view)
148 {
149 struct i915_vma *pos = ERR_PTR(-E2BIG);
150 struct i915_vma *vma;
151 struct rb_node *rb, **p;
152 int err;
153
154 /* The aliasing_ppgtt should never be used directly! */
155 GEM_BUG_ON(vm == &vm->gt->ggtt->alias->vm);
156
157 vma = i915_vma_alloc();
158 if (vma == NULL)
159 return ERR_PTR(-ENOMEM);
160
161 vma->ops = &vm->vma_ops;
162 vma->obj = obj;
163 vma->size = obj->base.size;
164 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
165
166 i915_active_init(&vma->active, __i915_vma_active, __i915_vma_retire, 0);
167
168 #ifdef notyet
169 /* Declare ourselves safe for use inside shrinkers */
170 if (IS_ENABLED(CONFIG_LOCKDEP)) {
171 fs_reclaim_acquire(GFP_KERNEL);
172 might_lock(&vma->active.mutex);
173 fs_reclaim_release(GFP_KERNEL);
174 }
175 #endif
176
177 INIT_LIST_HEAD(&vma->closed_link);
178 INIT_LIST_HEAD(&vma->obj_link);
179 RB_CLEAR_NODE(&vma->obj_node);
180
181 if (view && view->type != I915_GTT_VIEW_NORMAL) {
182 vma->gtt_view = *view;
183 if (view->type == I915_GTT_VIEW_PARTIAL) {
184 GEM_BUG_ON(range_overflows_t(u64,
185 view->partial.offset,
186 view->partial.size,
187 obj->base.size >> PAGE_SHIFT));
188 vma->size = view->partial.size;
189 vma->size <<= PAGE_SHIFT;
190 GEM_BUG_ON(vma->size > obj->base.size);
191 } else if (view->type == I915_GTT_VIEW_ROTATED) {
192 vma->size = intel_rotation_info_size(&view->rotated);
193 vma->size <<= PAGE_SHIFT;
194 } else if (view->type == I915_GTT_VIEW_REMAPPED) {
195 vma->size = intel_remapped_info_size(&view->remapped);
196 vma->size <<= PAGE_SHIFT;
197 }
198 }
199
200 if (unlikely(vma->size > vm->total))
201 goto err_vma;
202
203 GEM_BUG_ON(!IS_ALIGNED(vma->size, I915_GTT_PAGE_SIZE));
204
205 err = mutex_lock_interruptible(&vm->mutex);
206 if (err) {
207 pos = ERR_PTR(err);
208 goto err_vma;
209 }
210
211 vma->vm = vm;
212 list_add_tail(&vma->vm_link, &vm->unbound_list);
213
214 spin_lock(&obj->vma.lock);
215 if (i915_is_ggtt(vm)) {
216 if (unlikely(overflows_type(vma->size, u32)))
217 goto err_unlock;
218
219 vma->fence_size = i915_gem_fence_size(vm->i915, vma->size,
220 i915_gem_object_get_tiling(obj),
221 i915_gem_object_get_stride(obj));
222 if (unlikely(vma->fence_size < vma->size || /* overflow */
223 vma->fence_size > vm->total))
224 goto err_unlock;
225
226 GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I915_GTT_MIN_ALIGNMENT));
227
228 vma->fence_alignment = i915_gem_fence_alignment(vm->i915, vma->size,
229 i915_gem_object_get_tiling(obj),
230 i915_gem_object_get_stride(obj));
231 GEM_BUG_ON(!is_power_of_2(vma->fence_alignment));
232
233 __set_bit(I915_VMA_GGTT_BIT, __i915_vma_flags(vma));
234 }
235
236 rb = NULL;
237 p = &obj->vma.tree.rb_node;
238 while (*p) {
239 long cmp;
240
241 rb = *p;
242 pos = rb_entry(rb, struct i915_vma, obj_node);
243
244 /*
245 * If the view already exists in the tree, another thread
246 * already created a matching vma, so return the older instance
247 * and dispose of ours.
248 */
249 cmp = i915_vma_compare(pos, vm, view);
250 if (cmp < 0)
251 p = &rb->rb_right;
252 else if (cmp > 0)
253 p = &rb->rb_left;
254 else
255 goto err_unlock;
256 }
257 rb_link_node(&vma->obj_node, rb, p);
258 rb_insert_color(&vma->obj_node, &obj->vma.tree);
259
260 if (i915_vma_is_ggtt(vma))
261 /*
262 * We put the GGTT vma at the start of the vma-list, followed
263 * by the ppGGTT vma. This allows us to break early when
264 * iterating over only the GGTT vma for an object, see
265 * for_each_ggtt_vma()
266 */
267 list_add(&vma->obj_link, &obj->vma.list);
268 else
269 list_add_tail(&vma->obj_link, &obj->vma.list);
270
271 spin_unlock(&obj->vma.lock);
272 mutex_unlock(&vm->mutex);
273
274 return vma;
275
276 err_unlock:
277 spin_unlock(&obj->vma.lock);
278 list_del_init(&vma->vm_link);
279 mutex_unlock(&vm->mutex);
280 err_vma:
281 i915_vma_free(vma);
282 return pos;
283 }
284
285 static struct i915_vma *
i915_vma_lookup(struct drm_i915_gem_object * obj,struct i915_address_space * vm,const struct i915_gtt_view * view)286 i915_vma_lookup(struct drm_i915_gem_object *obj,
287 struct i915_address_space *vm,
288 const struct i915_gtt_view *view)
289 {
290 struct rb_node *rb;
291
292 rb = obj->vma.tree.rb_node;
293 while (rb) {
294 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
295 long cmp;
296
297 cmp = i915_vma_compare(vma, vm, view);
298 if (cmp == 0)
299 return vma;
300
301 if (cmp < 0)
302 rb = rb->rb_right;
303 else
304 rb = rb->rb_left;
305 }
306
307 return NULL;
308 }
309
310 /**
311 * i915_vma_instance - return the singleton instance of the VMA
312 * @obj: parent &struct drm_i915_gem_object to be mapped
313 * @vm: address space in which the mapping is located
314 * @view: additional mapping requirements
315 *
316 * i915_vma_instance() looks up an existing VMA of the @obj in the @vm with
317 * the same @view characteristics. If a match is not found, one is created.
318 * Once created, the VMA is kept until either the object is freed, or the
319 * address space is closed.
320 *
321 * Returns the vma, or an error pointer.
322 */
323 struct i915_vma *
i915_vma_instance(struct drm_i915_gem_object * obj,struct i915_address_space * vm,const struct i915_gtt_view * view)324 i915_vma_instance(struct drm_i915_gem_object *obj,
325 struct i915_address_space *vm,
326 const struct i915_gtt_view *view)
327 {
328 struct i915_vma *vma;
329
330 GEM_BUG_ON(view && !i915_is_ggtt_or_dpt(vm));
331 GEM_BUG_ON(!kref_read(&vm->ref));
332
333 spin_lock(&obj->vma.lock);
334 vma = i915_vma_lookup(obj, vm, view);
335 spin_unlock(&obj->vma.lock);
336
337 /* vma_create() will resolve the race if another creates the vma */
338 if (unlikely(!vma))
339 vma = vma_create(obj, vm, view);
340
341 GEM_BUG_ON(!IS_ERR(vma) && i915_vma_compare(vma, vm, view));
342 return vma;
343 }
344
345 struct i915_vma_work {
346 struct dma_fence_work base;
347 struct i915_address_space *vm;
348 struct i915_vm_pt_stash stash;
349 struct i915_vma_resource *vma_res;
350 struct drm_i915_gem_object *obj;
351 struct i915_sw_dma_fence_cb cb;
352 unsigned int pat_index;
353 unsigned int flags;
354 };
355
__vma_bind(struct dma_fence_work * work)356 static void __vma_bind(struct dma_fence_work *work)
357 {
358 struct i915_vma_work *vw = container_of(work, typeof(*vw), base);
359 struct i915_vma_resource *vma_res = vw->vma_res;
360
361 /*
362 * We are about the bind the object, which must mean we have already
363 * signaled the work to potentially clear/move the pages underneath. If
364 * something went wrong at that stage then the object should have
365 * unknown_state set, in which case we need to skip the bind.
366 */
367 if (i915_gem_object_has_unknown_state(vw->obj))
368 return;
369
370 vma_res->ops->bind_vma(vma_res->vm, &vw->stash,
371 vma_res, vw->pat_index, vw->flags);
372 }
373
__vma_release(struct dma_fence_work * work)374 static void __vma_release(struct dma_fence_work *work)
375 {
376 struct i915_vma_work *vw = container_of(work, typeof(*vw), base);
377
378 if (vw->obj)
379 i915_gem_object_put(vw->obj);
380
381 i915_vm_free_pt_stash(vw->vm, &vw->stash);
382 if (vw->vma_res)
383 i915_vma_resource_put(vw->vma_res);
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
i915_vma_work(void)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
i915_vma_wait_for_bind(struct i915_vma * vma)406 int i915_vma_wait_for_bind(struct i915_vma *vma)
407 {
408 int err = 0;
409
410 if (rcu_access_pointer(vma->active.excl.fence)) {
411 struct dma_fence *fence;
412
413 rcu_read_lock();
414 fence = dma_fence_get_rcu_safe(&vma->active.excl.fence);
415 rcu_read_unlock();
416 if (fence) {
417 err = dma_fence_wait(fence, true);
418 dma_fence_put(fence);
419 }
420 }
421
422 return err;
423 }
424
425 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
i915_vma_verify_bind_complete(struct i915_vma * vma)426 static int i915_vma_verify_bind_complete(struct i915_vma *vma)
427 {
428 struct dma_fence *fence = i915_active_fence_get(&vma->active.excl);
429 int err;
430
431 if (!fence)
432 return 0;
433
434 if (dma_fence_is_signaled(fence))
435 err = fence->error;
436 else
437 err = -EBUSY;
438
439 dma_fence_put(fence);
440
441 return err;
442 }
443 #else
444 #define i915_vma_verify_bind_complete(_vma) 0
445 #endif
446
447 I915_SELFTEST_EXPORT void
i915_vma_resource_init_from_vma(struct i915_vma_resource * vma_res,struct i915_vma * vma)448 i915_vma_resource_init_from_vma(struct i915_vma_resource *vma_res,
449 struct i915_vma *vma)
450 {
451 struct drm_i915_gem_object *obj = vma->obj;
452
453 i915_vma_resource_init(vma_res, vma->vm, vma->pages, &vma->page_sizes,
454 obj->mm.rsgt, i915_gem_object_is_readonly(obj),
455 i915_gem_object_is_lmem(obj), obj->mm.region,
456 vma->ops, vma->private, __i915_vma_offset(vma),
457 __i915_vma_size(vma), vma->size, vma->guard);
458 }
459
460 /**
461 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
462 * @vma: VMA to map
463 * @pat_index: PAT index to set in PTE
464 * @flags: flags like global or local mapping
465 * @work: preallocated worker for allocating and binding the PTE
466 * @vma_res: pointer to a preallocated vma resource. The resource is either
467 * consumed or freed.
468 *
469 * DMA addresses are taken from the scatter-gather table of this object (or of
470 * this VMA in case of non-default GGTT views) and PTE entries set up.
471 * Note that DMA addresses are also the only part of the SG table we care about.
472 */
i915_vma_bind(struct i915_vma * vma,unsigned int pat_index,u32 flags,struct i915_vma_work * work,struct i915_vma_resource * vma_res)473 int i915_vma_bind(struct i915_vma *vma,
474 unsigned int pat_index,
475 u32 flags,
476 struct i915_vma_work *work,
477 struct i915_vma_resource *vma_res)
478 {
479 u32 bind_flags;
480 u32 vma_flags;
481 int ret;
482
483 lockdep_assert_held(&vma->vm->mutex);
484 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
485 GEM_BUG_ON(vma->size > i915_vma_size(vma));
486
487 if (GEM_DEBUG_WARN_ON(range_overflows(vma->node.start,
488 vma->node.size,
489 vma->vm->total))) {
490 i915_vma_resource_free(vma_res);
491 return -ENODEV;
492 }
493
494 if (GEM_DEBUG_WARN_ON(!flags)) {
495 i915_vma_resource_free(vma_res);
496 return -EINVAL;
497 }
498
499 bind_flags = flags;
500 bind_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
501
502 vma_flags = atomic_read(&vma->flags);
503 vma_flags &= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
504
505 bind_flags &= ~vma_flags;
506 if (bind_flags == 0) {
507 i915_vma_resource_free(vma_res);
508 return 0;
509 }
510
511 GEM_BUG_ON(!atomic_read(&vma->pages_count));
512
513 /* Wait for or await async unbinds touching our range */
514 if (work && bind_flags & vma->vm->bind_async_flags)
515 ret = i915_vma_resource_bind_dep_await(vma->vm,
516 &work->base.chain,
517 vma->node.start,
518 vma->node.size,
519 true,
520 GFP_NOWAIT |
521 __GFP_RETRY_MAYFAIL |
522 __GFP_NOWARN);
523 else
524 ret = i915_vma_resource_bind_dep_sync(vma->vm, vma->node.start,
525 vma->node.size, true);
526 if (ret) {
527 i915_vma_resource_free(vma_res);
528 return ret;
529 }
530
531 if (vma->resource || !vma_res) {
532 /* Rebinding with an additional I915_VMA_*_BIND */
533 GEM_WARN_ON(!vma_flags);
534 i915_vma_resource_free(vma_res);
535 } else {
536 i915_vma_resource_init_from_vma(vma_res, vma);
537 vma->resource = vma_res;
538 }
539 trace_i915_vma_bind(vma, bind_flags);
540 if (work && bind_flags & vma->vm->bind_async_flags) {
541 struct dma_fence *prev;
542
543 work->vma_res = i915_vma_resource_get(vma->resource);
544 work->pat_index = pat_index;
545 work->flags = bind_flags;
546
547 /*
548 * Note we only want to chain up to the migration fence on
549 * the pages (not the object itself). As we don't track that,
550 * yet, we have to use the exclusive fence instead.
551 *
552 * Also note that we do not want to track the async vma as
553 * part of the obj->resv->excl_fence as it only affects
554 * execution and not content or object's backing store lifetime.
555 */
556 prev = i915_active_set_exclusive(&vma->active, &work->base.dma);
557 if (prev) {
558 __i915_sw_fence_await_dma_fence(&work->base.chain,
559 prev,
560 &work->cb);
561 dma_fence_put(prev);
562 }
563
564 work->base.dma.error = 0; /* enable the queue_work() */
565 work->obj = i915_gem_object_get(vma->obj);
566 } else {
567 ret = i915_gem_object_wait_moving_fence(vma->obj, true);
568 if (ret) {
569 i915_vma_resource_free(vma->resource);
570 vma->resource = NULL;
571
572 return ret;
573 }
574 vma->ops->bind_vma(vma->vm, NULL, vma->resource, pat_index,
575 bind_flags);
576 }
577
578 atomic_or(bind_flags, &vma->flags);
579 return 0;
580 }
581
i915_vma_pin_iomap(struct i915_vma * vma)582 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
583 {
584 void __iomem *ptr;
585 int err;
586
587 if (WARN_ON_ONCE(vma->obj->flags & I915_BO_ALLOC_GPU_ONLY))
588 return IOMEM_ERR_PTR(-EINVAL);
589
590 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
591 GEM_BUG_ON(!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND));
592 GEM_BUG_ON(i915_vma_verify_bind_complete(vma));
593
594 ptr = READ_ONCE(vma->iomap);
595 if (ptr == NULL) {
596 /*
597 * TODO: consider just using i915_gem_object_pin_map() for lmem
598 * instead, which already supports mapping non-contiguous chunks
599 * of pages, that way we can also drop the
600 * I915_BO_ALLOC_CONTIGUOUS when allocating the object.
601 */
602 if (i915_gem_object_is_lmem(vma->obj)) {
603 ptr = i915_gem_object_lmem_io_map(vma->obj, 0,
604 vma->obj->base.size);
605 } else if (i915_vma_is_map_and_fenceable(vma)) {
606 ptr = io_mapping_map_wc(&i915_vm_to_ggtt(vma->vm)->iomap,
607 i915_vma_offset(vma),
608 i915_vma_size(vma));
609 } else {
610 ptr = (void __iomem *)
611 i915_gem_object_pin_map(vma->obj, I915_MAP_WC);
612 if (IS_ERR(ptr)) {
613 err = PTR_ERR(ptr);
614 goto err;
615 }
616 ptr = page_pack_bits(ptr, 1);
617 }
618
619 if (ptr == NULL) {
620 err = -ENOMEM;
621 goto err;
622 }
623
624 if (unlikely(cmpxchg(&vma->iomap, NULL, ptr))) {
625 if (page_unmask_bits(ptr))
626 __i915_gem_object_release_map(vma->obj);
627 else
628 io_mapping_unmap(ptr);
629 ptr = vma->iomap;
630 }
631 }
632
633 __i915_vma_pin(vma);
634
635 err = i915_vma_pin_fence(vma);
636 if (err)
637 goto err_unpin;
638
639 i915_vma_set_ggtt_write(vma);
640
641 /* NB Access through the GTT requires the device to be awake. */
642 return page_mask_bits(ptr);
643
644 err_unpin:
645 __i915_vma_unpin(vma);
646 err:
647 return IOMEM_ERR_PTR(err);
648 }
649
i915_vma_flush_writes(struct i915_vma * vma)650 void i915_vma_flush_writes(struct i915_vma *vma)
651 {
652 if (i915_vma_unset_ggtt_write(vma))
653 intel_gt_flush_ggtt_writes(vma->vm->gt);
654 }
655
i915_vma_unpin_iomap(struct i915_vma * vma)656 void i915_vma_unpin_iomap(struct i915_vma *vma)
657 {
658 GEM_BUG_ON(vma->iomap == NULL);
659
660 /* XXX We keep the mapping until __i915_vma_unbind()/evict() */
661
662 i915_vma_flush_writes(vma);
663
664 i915_vma_unpin_fence(vma);
665 i915_vma_unpin(vma);
666 }
667
i915_vma_unpin_and_release(struct i915_vma ** p_vma,unsigned int flags)668 void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags)
669 {
670 struct i915_vma *vma;
671 struct drm_i915_gem_object *obj;
672
673 vma = fetch_and_zero(p_vma);
674 if (!vma)
675 return;
676
677 obj = vma->obj;
678 GEM_BUG_ON(!obj);
679
680 i915_vma_unpin(vma);
681
682 if (flags & I915_VMA_RELEASE_MAP)
683 i915_gem_object_unpin_map(obj);
684
685 i915_gem_object_put(obj);
686 }
687
i915_vma_misplaced(const struct i915_vma * vma,u64 size,u64 alignment,u64 flags)688 bool i915_vma_misplaced(const struct i915_vma *vma,
689 u64 size, u64 alignment, u64 flags)
690 {
691 if (!drm_mm_node_allocated(&vma->node))
692 return false;
693
694 if (test_bit(I915_VMA_ERROR_BIT, __i915_vma_flags(vma)))
695 return true;
696
697 if (i915_vma_size(vma) < size)
698 return true;
699
700 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
701 if (alignment && !IS_ALIGNED(i915_vma_offset(vma), alignment))
702 return true;
703
704 if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
705 return true;
706
707 if (flags & PIN_OFFSET_BIAS &&
708 i915_vma_offset(vma) < (flags & PIN_OFFSET_MASK))
709 return true;
710
711 if (flags & PIN_OFFSET_FIXED &&
712 i915_vma_offset(vma) != (flags & PIN_OFFSET_MASK))
713 return true;
714
715 if (flags & PIN_OFFSET_GUARD &&
716 vma->guard < (flags & PIN_OFFSET_MASK))
717 return true;
718
719 return false;
720 }
721
__i915_vma_set_map_and_fenceable(struct i915_vma * vma)722 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
723 {
724 bool mappable, fenceable;
725
726 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
727 GEM_BUG_ON(!vma->fence_size);
728
729 fenceable = (i915_vma_size(vma) >= vma->fence_size &&
730 IS_ALIGNED(i915_vma_offset(vma), vma->fence_alignment));
731
732 mappable = i915_ggtt_offset(vma) + vma->fence_size <=
733 i915_vm_to_ggtt(vma->vm)->mappable_end;
734
735 if (mappable && fenceable)
736 set_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
737 else
738 clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
739 }
740
i915_gem_valid_gtt_space(struct i915_vma * vma,unsigned long color)741 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long color)
742 {
743 struct drm_mm_node *node = &vma->node;
744 struct drm_mm_node *other;
745
746 /*
747 * On some machines we have to be careful when putting differing types
748 * of snoopable memory together to avoid the prefetcher crossing memory
749 * domains and dying. During vm initialisation, we decide whether or not
750 * these constraints apply and set the drm_mm.color_adjust
751 * appropriately.
752 */
753 if (!i915_vm_has_cache_coloring(vma->vm))
754 return true;
755
756 /* Only valid to be called on an already inserted vma */
757 GEM_BUG_ON(!drm_mm_node_allocated(node));
758 GEM_BUG_ON(list_empty(&node->node_list));
759
760 other = list_prev_entry(node, node_list);
761 if (i915_node_color_differs(other, color) &&
762 !drm_mm_hole_follows(other))
763 return false;
764
765 other = list_next_entry(node, node_list);
766 if (i915_node_color_differs(other, color) &&
767 !drm_mm_hole_follows(node))
768 return false;
769
770 return true;
771 }
772
773 /**
774 * i915_vma_insert - finds a slot for the vma in its address space
775 * @vma: the vma
776 * @ww: An optional struct i915_gem_ww_ctx
777 * @size: requested size in bytes (can be larger than the VMA)
778 * @alignment: required alignment
779 * @flags: mask of PIN_* flags to use
780 *
781 * First we try to allocate some free space that meets the requirements for
782 * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
783 * preferrably the oldest idle entry to make room for the new VMA.
784 *
785 * Returns:
786 * 0 on success, negative error code otherwise.
787 */
788 static int
i915_vma_insert(struct i915_vma * vma,struct i915_gem_ww_ctx * ww,u64 size,u64 alignment,u64 flags)789 i915_vma_insert(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
790 u64 size, u64 alignment, u64 flags)
791 {
792 unsigned long color, guard;
793 u64 start, end;
794 int ret;
795
796 GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
797 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
798 GEM_BUG_ON(hweight64(flags & (PIN_OFFSET_GUARD | PIN_OFFSET_FIXED | PIN_OFFSET_BIAS)) > 1);
799
800 size = max(size, vma->size);
801 alignment = max_t(typeof(alignment), alignment, vma->display_alignment);
802 if (flags & PIN_MAPPABLE) {
803 size = max_t(typeof(size), size, vma->fence_size);
804 alignment = max_t(typeof(alignment),
805 alignment, vma->fence_alignment);
806 }
807
808 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
809 GEM_BUG_ON(!IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
810 GEM_BUG_ON(!is_power_of_2(alignment));
811
812 guard = vma->guard; /* retain guard across rebinds */
813 if (flags & PIN_OFFSET_GUARD) {
814 GEM_BUG_ON(overflows_type(flags & PIN_OFFSET_MASK, u32));
815 guard = max_t(u32, guard, flags & PIN_OFFSET_MASK);
816 }
817 /*
818 * As we align the node upon insertion, but the hardware gets
819 * node.start + guard, the easiest way to make that work is
820 * to make the guard a multiple of the alignment size.
821 */
822 guard = ALIGN(guard, alignment);
823
824 start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
825 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
826
827 end = vma->vm->total;
828 if (flags & PIN_MAPPABLE)
829 end = min_t(u64, end, i915_vm_to_ggtt(vma->vm)->mappable_end);
830 if (flags & PIN_ZONE_4G)
831 end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
832 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
833
834 alignment = max(alignment, i915_vm_obj_min_alignment(vma->vm, vma->obj));
835
836 /*
837 * If binding the object/GGTT view requires more space than the entire
838 * aperture has, reject it early before evicting everything in a vain
839 * attempt to find space.
840 */
841 if (size > end - 2 * guard) {
842 drm_dbg(vma->obj->base.dev,
843 "Attempting to bind an object larger than the aperture: request=%llu > %s aperture=%llu\n",
844 size, flags & PIN_MAPPABLE ? "mappable" : "total", end);
845 return -ENOSPC;
846 }
847
848 color = 0;
849
850 if (i915_vm_has_cache_coloring(vma->vm))
851 color = vma->obj->pat_index;
852
853 if (flags & PIN_OFFSET_FIXED) {
854 u64 offset = flags & PIN_OFFSET_MASK;
855 if (!IS_ALIGNED(offset, alignment) ||
856 range_overflows(offset, size, end))
857 return -EINVAL;
858 /*
859 * The caller knows not of the guard added by others and
860 * requests for the offset of the start of its buffer
861 * to be fixed, which may not be the same as the position
862 * of the vma->node due to the guard pages.
863 */
864 if (offset < guard || offset + size > end - guard)
865 return -ENOSPC;
866
867 ret = i915_gem_gtt_reserve(vma->vm, ww, &vma->node,
868 size + 2 * guard,
869 offset - guard,
870 color, flags);
871 if (ret)
872 return ret;
873 } else {
874 size += 2 * guard;
875 /*
876 * We only support huge gtt pages through the 48b PPGTT,
877 * however we also don't want to force any alignment for
878 * objects which need to be tightly packed into the low 32bits.
879 *
880 * Note that we assume that GGTT are limited to 4GiB for the
881 * forseeable future. See also i915_ggtt_offset().
882 */
883 if (upper_32_bits(end - 1) &&
884 vma->page_sizes.sg > I915_GTT_PAGE_SIZE &&
885 !HAS_64K_PAGES(vma->vm->i915)) {
886 /*
887 * We can't mix 64K and 4K PTEs in the same page-table
888 * (2M block), and so to avoid the ugliness and
889 * complexity of coloring we opt for just aligning 64K
890 * objects to 2M.
891 */
892 u64 page_alignment =
893 rounddown_pow_of_two(vma->page_sizes.sg |
894 I915_GTT_PAGE_SIZE_2M);
895
896 /*
897 * Check we don't expand for the limited Global GTT
898 * (mappable aperture is even more precious!). This
899 * also checks that we exclude the aliasing-ppgtt.
900 */
901 GEM_BUG_ON(i915_vma_is_ggtt(vma));
902
903 alignment = max(alignment, page_alignment);
904
905 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K)
906 size = round_up(size, I915_GTT_PAGE_SIZE_2M);
907 }
908
909 ret = i915_gem_gtt_insert(vma->vm, ww, &vma->node,
910 size, alignment, color,
911 start, end, flags);
912 if (ret)
913 return ret;
914
915 GEM_BUG_ON(vma->node.start < start);
916 GEM_BUG_ON(vma->node.start + vma->node.size > end);
917 }
918 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
919 GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, color));
920
921 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
922 vma->guard = guard;
923
924 return 0;
925 }
926
927 static void
i915_vma_detach(struct i915_vma * vma)928 i915_vma_detach(struct i915_vma *vma)
929 {
930 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
931 GEM_BUG_ON(i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
932
933 /*
934 * And finally now the object is completely decoupled from this
935 * vma, we can drop its hold on the backing storage and allow
936 * it to be reaped by the shrinker.
937 */
938 list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
939 }
940
try_qad_pin(struct i915_vma * vma,unsigned int flags)941 static bool try_qad_pin(struct i915_vma *vma, unsigned int flags)
942 {
943 unsigned int bound;
944
945 bound = atomic_read(&vma->flags);
946
947 if (flags & PIN_VALIDATE) {
948 flags &= I915_VMA_BIND_MASK;
949
950 return (flags & bound) == flags;
951 }
952
953 /* with the lock mandatory for unbind, we don't race here */
954 flags &= I915_VMA_BIND_MASK;
955 do {
956 if (unlikely(flags & ~bound))
957 return false;
958
959 if (unlikely(bound & (I915_VMA_OVERFLOW | I915_VMA_ERROR)))
960 return false;
961
962 GEM_BUG_ON(((bound + 1) & I915_VMA_PIN_MASK) == 0);
963 } while (!atomic_try_cmpxchg(&vma->flags, &bound, bound + 1));
964
965 return true;
966 }
967
968 static struct scatterlist *
rotate_pages(struct drm_i915_gem_object * obj,unsigned int offset,unsigned int width,unsigned int height,unsigned int src_stride,unsigned int dst_stride,struct sg_table * st,struct scatterlist * sg)969 rotate_pages(struct drm_i915_gem_object *obj, unsigned int offset,
970 unsigned int width, unsigned int height,
971 unsigned int src_stride, unsigned int dst_stride,
972 struct sg_table *st, struct scatterlist *sg)
973 {
974 unsigned int column, row;
975 pgoff_t src_idx;
976
977 for (column = 0; column < width; column++) {
978 unsigned int left;
979
980 src_idx = src_stride * (height - 1) + column + offset;
981 for (row = 0; row < height; row++) {
982 st->nents++;
983 /*
984 * We don't need the pages, but need to initialize
985 * the entries so the sg list can be happily traversed.
986 * The only thing we need are DMA addresses.
987 */
988 sg_set_page(sg, NULL, I915_GTT_PAGE_SIZE, 0);
989 sg_dma_address(sg) =
990 i915_gem_object_get_dma_address(obj, src_idx);
991 sg_dma_len(sg) = I915_GTT_PAGE_SIZE;
992 sg = sg_next(sg);
993 src_idx -= src_stride;
994 }
995
996 left = (dst_stride - height) * I915_GTT_PAGE_SIZE;
997
998 if (!left)
999 continue;
1000
1001 st->nents++;
1002
1003 /*
1004 * The DE ignores the PTEs for the padding tiles, the sg entry
1005 * here is just a conenience to indicate how many padding PTEs
1006 * to insert at this spot.
1007 */
1008 sg_set_page(sg, NULL, left, 0);
1009 sg_dma_address(sg) = 0;
1010 sg_dma_len(sg) = left;
1011 sg = sg_next(sg);
1012 }
1013
1014 return sg;
1015 }
1016
1017 static noinline struct sg_table *
intel_rotate_pages(struct intel_rotation_info * rot_info,struct drm_i915_gem_object * obj)1018 intel_rotate_pages(struct intel_rotation_info *rot_info,
1019 struct drm_i915_gem_object *obj)
1020 {
1021 unsigned int size = intel_rotation_info_size(rot_info);
1022 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1023 struct sg_table *st;
1024 struct scatterlist *sg;
1025 int ret = -ENOMEM;
1026 int i;
1027
1028 /* Allocate target SG list. */
1029 st = kmalloc(sizeof(*st), GFP_KERNEL);
1030 if (!st)
1031 goto err_st_alloc;
1032
1033 ret = sg_alloc_table(st, size, GFP_KERNEL);
1034 if (ret)
1035 goto err_sg_alloc;
1036
1037 st->nents = 0;
1038 sg = st->sgl;
1039
1040 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
1041 sg = rotate_pages(obj, rot_info->plane[i].offset,
1042 rot_info->plane[i].width, rot_info->plane[i].height,
1043 rot_info->plane[i].src_stride,
1044 rot_info->plane[i].dst_stride,
1045 st, sg);
1046
1047 return st;
1048
1049 err_sg_alloc:
1050 kfree(st);
1051 err_st_alloc:
1052
1053 drm_dbg(&i915->drm, "Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
1054 obj->base.size, rot_info->plane[0].width,
1055 rot_info->plane[0].height, size);
1056
1057 return ERR_PTR(ret);
1058 }
1059
1060 static struct scatterlist *
add_padding_pages(unsigned int count,struct sg_table * st,struct scatterlist * sg)1061 add_padding_pages(unsigned int count,
1062 struct sg_table *st, struct scatterlist *sg)
1063 {
1064 st->nents++;
1065
1066 /*
1067 * The DE ignores the PTEs for the padding tiles, the sg entry
1068 * here is just a convenience to indicate how many padding PTEs
1069 * to insert at this spot.
1070 */
1071 sg_set_page(sg, NULL, count * I915_GTT_PAGE_SIZE, 0);
1072 sg_dma_address(sg) = 0;
1073 sg_dma_len(sg) = count * I915_GTT_PAGE_SIZE;
1074 sg = sg_next(sg);
1075
1076 return sg;
1077 }
1078
1079 static struct scatterlist *
remap_tiled_color_plane_pages(struct drm_i915_gem_object * obj,unsigned long offset,unsigned int alignment_pad,unsigned int width,unsigned int height,unsigned int src_stride,unsigned int dst_stride,struct sg_table * st,struct scatterlist * sg,unsigned int * gtt_offset)1080 remap_tiled_color_plane_pages(struct drm_i915_gem_object *obj,
1081 unsigned long offset, unsigned int alignment_pad,
1082 unsigned int width, unsigned int height,
1083 unsigned int src_stride, unsigned int dst_stride,
1084 struct sg_table *st, struct scatterlist *sg,
1085 unsigned int *gtt_offset)
1086 {
1087 unsigned int row;
1088
1089 if (!width || !height)
1090 return sg;
1091
1092 if (alignment_pad)
1093 sg = add_padding_pages(alignment_pad, st, sg);
1094
1095 for (row = 0; row < height; row++) {
1096 unsigned int left = width * I915_GTT_PAGE_SIZE;
1097
1098 while (left) {
1099 dma_addr_t addr;
1100 unsigned int length;
1101
1102 /*
1103 * We don't need the pages, but need to initialize
1104 * the entries so the sg list can be happily traversed.
1105 * The only thing we need are DMA addresses.
1106 */
1107
1108 addr = i915_gem_object_get_dma_address_len(obj, offset, &length);
1109
1110 length = min(left, length);
1111
1112 st->nents++;
1113
1114 sg_set_page(sg, NULL, length, 0);
1115 sg_dma_address(sg) = addr;
1116 sg_dma_len(sg) = length;
1117 sg = sg_next(sg);
1118
1119 offset += length / I915_GTT_PAGE_SIZE;
1120 left -= length;
1121 }
1122
1123 offset += src_stride - width;
1124
1125 left = (dst_stride - width) * I915_GTT_PAGE_SIZE;
1126
1127 if (!left)
1128 continue;
1129
1130 sg = add_padding_pages(left >> PAGE_SHIFT, st, sg);
1131 }
1132
1133 *gtt_offset += alignment_pad + dst_stride * height;
1134
1135 return sg;
1136 }
1137
1138 static struct scatterlist *
remap_contiguous_pages(struct drm_i915_gem_object * obj,pgoff_t obj_offset,unsigned int count,struct sg_table * st,struct scatterlist * sg)1139 remap_contiguous_pages(struct drm_i915_gem_object *obj,
1140 pgoff_t obj_offset,
1141 unsigned int count,
1142 struct sg_table *st, struct scatterlist *sg)
1143 {
1144 struct scatterlist *iter;
1145 unsigned int offset;
1146
1147 iter = i915_gem_object_get_sg_dma(obj, obj_offset, &offset);
1148 GEM_BUG_ON(!iter);
1149
1150 do {
1151 unsigned int len;
1152
1153 len = min(sg_dma_len(iter) - (offset << PAGE_SHIFT),
1154 count << PAGE_SHIFT);
1155 sg_set_page(sg, NULL, len, 0);
1156 sg_dma_address(sg) =
1157 sg_dma_address(iter) + (offset << PAGE_SHIFT);
1158 sg_dma_len(sg) = len;
1159
1160 st->nents++;
1161 count -= len >> PAGE_SHIFT;
1162 if (count == 0)
1163 return sg;
1164
1165 sg = __sg_next(sg);
1166 iter = __sg_next(iter);
1167 offset = 0;
1168 } while (1);
1169 }
1170
1171 static struct scatterlist *
remap_linear_color_plane_pages(struct drm_i915_gem_object * obj,pgoff_t obj_offset,unsigned int alignment_pad,unsigned int size,struct sg_table * st,struct scatterlist * sg,unsigned int * gtt_offset)1172 remap_linear_color_plane_pages(struct drm_i915_gem_object *obj,
1173 pgoff_t obj_offset, unsigned int alignment_pad,
1174 unsigned int size,
1175 struct sg_table *st, struct scatterlist *sg,
1176 unsigned int *gtt_offset)
1177 {
1178 if (!size)
1179 return sg;
1180
1181 if (alignment_pad)
1182 sg = add_padding_pages(alignment_pad, st, sg);
1183
1184 sg = remap_contiguous_pages(obj, obj_offset, size, st, sg);
1185 sg = sg_next(sg);
1186
1187 *gtt_offset += alignment_pad + size;
1188
1189 return sg;
1190 }
1191
1192 static struct scatterlist *
remap_color_plane_pages(const struct intel_remapped_info * rem_info,struct drm_i915_gem_object * obj,int color_plane,struct sg_table * st,struct scatterlist * sg,unsigned int * gtt_offset)1193 remap_color_plane_pages(const struct intel_remapped_info *rem_info,
1194 struct drm_i915_gem_object *obj,
1195 int color_plane,
1196 struct sg_table *st, struct scatterlist *sg,
1197 unsigned int *gtt_offset)
1198 {
1199 unsigned int alignment_pad = 0;
1200
1201 if (rem_info->plane_alignment)
1202 alignment_pad = ALIGN(*gtt_offset, rem_info->plane_alignment) - *gtt_offset;
1203
1204 if (rem_info->plane[color_plane].linear)
1205 sg = remap_linear_color_plane_pages(obj,
1206 rem_info->plane[color_plane].offset,
1207 alignment_pad,
1208 rem_info->plane[color_plane].size,
1209 st, sg,
1210 gtt_offset);
1211
1212 else
1213 sg = remap_tiled_color_plane_pages(obj,
1214 rem_info->plane[color_plane].offset,
1215 alignment_pad,
1216 rem_info->plane[color_plane].width,
1217 rem_info->plane[color_plane].height,
1218 rem_info->plane[color_plane].src_stride,
1219 rem_info->plane[color_plane].dst_stride,
1220 st, sg,
1221 gtt_offset);
1222
1223 return sg;
1224 }
1225
1226 static noinline struct sg_table *
intel_remap_pages(struct intel_remapped_info * rem_info,struct drm_i915_gem_object * obj)1227 intel_remap_pages(struct intel_remapped_info *rem_info,
1228 struct drm_i915_gem_object *obj)
1229 {
1230 unsigned int size = intel_remapped_info_size(rem_info);
1231 struct drm_i915_private *i915 = to_i915(obj->base.dev);
1232 struct sg_table *st;
1233 struct scatterlist *sg;
1234 unsigned int gtt_offset = 0;
1235 int ret = -ENOMEM;
1236 int i;
1237
1238 /* Allocate target SG list. */
1239 st = kmalloc(sizeof(*st), GFP_KERNEL);
1240 if (!st)
1241 goto err_st_alloc;
1242
1243 ret = sg_alloc_table(st, size, GFP_KERNEL);
1244 if (ret)
1245 goto err_sg_alloc;
1246
1247 st->nents = 0;
1248 sg = st->sgl;
1249
1250 for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
1251 sg = remap_color_plane_pages(rem_info, obj, i, st, sg, >t_offset);
1252
1253 i915_sg_trim(st);
1254
1255 return st;
1256
1257 err_sg_alloc:
1258 kfree(st);
1259 err_st_alloc:
1260
1261 drm_dbg(&i915->drm, "Failed to create remapped mapping for object size %zu! (%ux%u tiles, %u pages)\n",
1262 obj->base.size, rem_info->plane[0].width,
1263 rem_info->plane[0].height, size);
1264
1265 return ERR_PTR(ret);
1266 }
1267
1268 static noinline struct sg_table *
intel_partial_pages(const struct i915_gtt_view * view,struct drm_i915_gem_object * obj)1269 intel_partial_pages(const struct i915_gtt_view *view,
1270 struct drm_i915_gem_object *obj)
1271 {
1272 struct sg_table *st;
1273 struct scatterlist *sg;
1274 unsigned int count = view->partial.size;
1275 int ret = -ENOMEM;
1276
1277 st = kmalloc(sizeof(*st), GFP_KERNEL);
1278 if (!st)
1279 goto err_st_alloc;
1280
1281 ret = sg_alloc_table(st, count, GFP_KERNEL);
1282 if (ret)
1283 goto err_sg_alloc;
1284
1285 st->nents = 0;
1286
1287 sg = remap_contiguous_pages(obj, view->partial.offset, count, st, st->sgl);
1288
1289 sg_mark_end(sg);
1290 i915_sg_trim(st); /* Drop any unused tail entries. */
1291
1292 return st;
1293
1294 err_sg_alloc:
1295 kfree(st);
1296 err_st_alloc:
1297 return ERR_PTR(ret);
1298 }
1299
1300 static int
__i915_vma_get_pages(struct i915_vma * vma)1301 __i915_vma_get_pages(struct i915_vma *vma)
1302 {
1303 struct sg_table *pages;
1304
1305 /*
1306 * The vma->pages are only valid within the lifespan of the borrowed
1307 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
1308 * must be the vma->pages. A simple rule is that vma->pages must only
1309 * be accessed when the obj->mm.pages are pinned.
1310 */
1311 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
1312
1313 switch (vma->gtt_view.type) {
1314 default:
1315 GEM_BUG_ON(vma->gtt_view.type);
1316 fallthrough;
1317 case I915_GTT_VIEW_NORMAL:
1318 pages = vma->obj->mm.pages;
1319 break;
1320
1321 case I915_GTT_VIEW_ROTATED:
1322 pages =
1323 intel_rotate_pages(&vma->gtt_view.rotated, vma->obj);
1324 break;
1325
1326 case I915_GTT_VIEW_REMAPPED:
1327 pages =
1328 intel_remap_pages(&vma->gtt_view.remapped, vma->obj);
1329 break;
1330
1331 case I915_GTT_VIEW_PARTIAL:
1332 pages = intel_partial_pages(&vma->gtt_view, vma->obj);
1333 break;
1334 }
1335
1336 if (IS_ERR(pages)) {
1337 drm_err(&vma->vm->i915->drm,
1338 "Failed to get pages for VMA view type %u (%ld)!\n",
1339 vma->gtt_view.type, PTR_ERR(pages));
1340 return PTR_ERR(pages);
1341 }
1342
1343 vma->pages = pages;
1344
1345 return 0;
1346 }
1347
i915_vma_get_pages(struct i915_vma * vma)1348 I915_SELFTEST_EXPORT int i915_vma_get_pages(struct i915_vma *vma)
1349 {
1350 int err;
1351
1352 if (atomic_add_unless(&vma->pages_count, 1, 0))
1353 return 0;
1354
1355 err = i915_gem_object_pin_pages(vma->obj);
1356 if (err)
1357 return err;
1358
1359 err = __i915_vma_get_pages(vma);
1360 if (err)
1361 goto err_unpin;
1362
1363 vma->page_sizes = vma->obj->mm.page_sizes;
1364 atomic_inc(&vma->pages_count);
1365
1366 return 0;
1367
1368 err_unpin:
1369 __i915_gem_object_unpin_pages(vma->obj);
1370
1371 return err;
1372 }
1373
vma_invalidate_tlb(struct i915_address_space * vm,u32 * tlb)1374 void vma_invalidate_tlb(struct i915_address_space *vm, u32 *tlb)
1375 {
1376 struct intel_gt *gt;
1377 int id;
1378
1379 if (!tlb)
1380 return;
1381
1382 /*
1383 * Before we release the pages that were bound by this vma, we
1384 * must invalidate all the TLBs that may still have a reference
1385 * back to our physical address. It only needs to be done once,
1386 * so after updating the PTE to point away from the pages, record
1387 * the most recent TLB invalidation seqno, and if we have not yet
1388 * flushed the TLBs upon release, perform a full invalidation.
1389 */
1390 for_each_gt(gt, vm->i915, id)
1391 WRITE_ONCE(tlb[id],
1392 intel_gt_next_invalidate_tlb_full(gt));
1393 }
1394
__vma_put_pages(struct i915_vma * vma,unsigned int count)1395 static void __vma_put_pages(struct i915_vma *vma, unsigned int count)
1396 {
1397 /* We allocate under vma_get_pages, so beware the shrinker */
1398 GEM_BUG_ON(atomic_read(&vma->pages_count) < count);
1399
1400 if (atomic_sub_return(count, &vma->pages_count) == 0) {
1401 if (vma->pages != vma->obj->mm.pages) {
1402 sg_free_table(vma->pages);
1403 kfree(vma->pages);
1404 }
1405 vma->pages = NULL;
1406
1407 i915_gem_object_unpin_pages(vma->obj);
1408 }
1409 }
1410
i915_vma_put_pages(struct i915_vma * vma)1411 I915_SELFTEST_EXPORT void i915_vma_put_pages(struct i915_vma *vma)
1412 {
1413 if (atomic_add_unless(&vma->pages_count, -1, 1))
1414 return;
1415
1416 __vma_put_pages(vma, 1);
1417 }
1418
vma_unbind_pages(struct i915_vma * vma)1419 static void vma_unbind_pages(struct i915_vma *vma)
1420 {
1421 unsigned int count;
1422
1423 lockdep_assert_held(&vma->vm->mutex);
1424
1425 /* The upper portion of pages_count is the number of bindings */
1426 count = atomic_read(&vma->pages_count);
1427 count >>= I915_VMA_PAGES_BIAS;
1428 GEM_BUG_ON(!count);
1429
1430 __vma_put_pages(vma, count | count << I915_VMA_PAGES_BIAS);
1431 }
1432
i915_vma_pin_ww(struct i915_vma * vma,struct i915_gem_ww_ctx * ww,u64 size,u64 alignment,u64 flags)1433 int i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
1434 u64 size, u64 alignment, u64 flags)
1435 {
1436 struct i915_vma_work *work = NULL;
1437 struct dma_fence *moving = NULL;
1438 struct i915_vma_resource *vma_res = NULL;
1439 intel_wakeref_t wakeref;
1440 unsigned int bound;
1441 int err;
1442
1443 assert_vma_held(vma);
1444 GEM_BUG_ON(!ww);
1445
1446 BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
1447 BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
1448
1449 GEM_BUG_ON(!(flags & (PIN_USER | PIN_GLOBAL)));
1450
1451 /* First try and grab the pin without rebinding the vma */
1452 if (try_qad_pin(vma, flags))
1453 return 0;
1454
1455 err = i915_vma_get_pages(vma);
1456 if (err)
1457 return err;
1458
1459 /*
1460 * In case of a global GTT, we must hold a runtime-pm wakeref
1461 * while global PTEs are updated. In other cases, we hold
1462 * the rpm reference while the VMA is active. Since runtime
1463 * resume may require allocations, which are forbidden inside
1464 * vm->mutex, get the first rpm wakeref outside of the mutex.
1465 */
1466 wakeref = intel_runtime_pm_get(&vma->vm->i915->runtime_pm);
1467
1468 if (flags & vma->vm->bind_async_flags) {
1469 /* lock VM */
1470 err = i915_vm_lock_objects(vma->vm, ww);
1471 if (err)
1472 goto err_rpm;
1473
1474 work = i915_vma_work();
1475 if (!work) {
1476 err = -ENOMEM;
1477 goto err_rpm;
1478 }
1479
1480 work->vm = vma->vm;
1481
1482 err = i915_gem_object_get_moving_fence(vma->obj, &moving);
1483 if (err)
1484 goto err_rpm;
1485
1486 dma_fence_work_chain(&work->base, moving);
1487
1488 /* Allocate enough page directories to used PTE */
1489 if (vma->vm->allocate_va_range) {
1490 err = i915_vm_alloc_pt_stash(vma->vm,
1491 &work->stash,
1492 vma->size);
1493 if (err)
1494 goto err_fence;
1495
1496 err = i915_vm_map_pt_stash(vma->vm, &work->stash);
1497 if (err)
1498 goto err_fence;
1499 }
1500 }
1501
1502 vma_res = i915_vma_resource_alloc();
1503 if (IS_ERR(vma_res)) {
1504 err = PTR_ERR(vma_res);
1505 goto err_fence;
1506 }
1507
1508 /*
1509 * Differentiate between user/kernel vma inside the aliasing-ppgtt.
1510 *
1511 * We conflate the Global GTT with the user's vma when using the
1512 * aliasing-ppgtt, but it is still vitally important to try and
1513 * keep the use cases distinct. For example, userptr objects are
1514 * not allowed inside the Global GTT as that will cause lock
1515 * inversions when we have to evict them the mmu_notifier callbacks -
1516 * but they are allowed to be part of the user ppGTT which can never
1517 * be mapped. As such we try to give the distinct users of the same
1518 * mutex, distinct lockclasses [equivalent to how we keep i915_ggtt
1519 * and i915_ppgtt separate].
1520 *
1521 * NB this may cause us to mask real lock inversions -- while the
1522 * code is safe today, lockdep may not be able to spot future
1523 * transgressions.
1524 */
1525 err = mutex_lock_interruptible_nested(&vma->vm->mutex,
1526 !(flags & PIN_GLOBAL));
1527 if (err)
1528 goto err_vma_res;
1529
1530 /* No more allocations allowed now we hold vm->mutex */
1531
1532 if (unlikely(i915_vma_is_closed(vma))) {
1533 err = -ENOENT;
1534 goto err_unlock;
1535 }
1536
1537 bound = atomic_read(&vma->flags);
1538 if (unlikely(bound & I915_VMA_ERROR)) {
1539 err = -ENOMEM;
1540 goto err_unlock;
1541 }
1542
1543 if (unlikely(!((bound + 1) & I915_VMA_PIN_MASK))) {
1544 err = -EAGAIN; /* pins are meant to be fairly temporary */
1545 goto err_unlock;
1546 }
1547
1548 if (unlikely(!(flags & ~bound & I915_VMA_BIND_MASK))) {
1549 if (!(flags & PIN_VALIDATE))
1550 __i915_vma_pin(vma);
1551 goto err_unlock;
1552 }
1553
1554 err = i915_active_acquire(&vma->active);
1555 if (err)
1556 goto err_unlock;
1557
1558 if (!(bound & I915_VMA_BIND_MASK)) {
1559 err = i915_vma_insert(vma, ww, size, alignment, flags);
1560 if (err)
1561 goto err_active;
1562
1563 if (i915_is_ggtt(vma->vm))
1564 __i915_vma_set_map_and_fenceable(vma);
1565 }
1566
1567 GEM_BUG_ON(!vma->pages);
1568 err = i915_vma_bind(vma,
1569 vma->obj->pat_index,
1570 flags, work, vma_res);
1571 vma_res = NULL;
1572 if (err)
1573 goto err_remove;
1574
1575 /* There should only be at most 2 active bindings (user, global) */
1576 GEM_BUG_ON(bound + I915_VMA_PAGES_ACTIVE < bound);
1577 atomic_add(I915_VMA_PAGES_ACTIVE, &vma->pages_count);
1578 list_move_tail(&vma->vm_link, &vma->vm->bound_list);
1579
1580 if (!(flags & PIN_VALIDATE)) {
1581 __i915_vma_pin(vma);
1582 GEM_BUG_ON(!i915_vma_is_pinned(vma));
1583 }
1584 GEM_BUG_ON(!i915_vma_is_bound(vma, flags));
1585 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
1586
1587 err_remove:
1588 if (!i915_vma_is_bound(vma, I915_VMA_BIND_MASK)) {
1589 i915_vma_detach(vma);
1590 drm_mm_remove_node(&vma->node);
1591 }
1592 err_active:
1593 i915_active_release(&vma->active);
1594 err_unlock:
1595 mutex_unlock(&vma->vm->mutex);
1596 err_vma_res:
1597 i915_vma_resource_free(vma_res);
1598 err_fence:
1599 if (work)
1600 dma_fence_work_commit_imm(&work->base);
1601 err_rpm:
1602 intel_runtime_pm_put(&vma->vm->i915->runtime_pm, wakeref);
1603
1604 if (moving)
1605 dma_fence_put(moving);
1606
1607 i915_vma_put_pages(vma);
1608 return err;
1609 }
1610
flush_idle_contexts(struct intel_gt * gt)1611 static void flush_idle_contexts(struct intel_gt *gt)
1612 {
1613 struct intel_engine_cs *engine;
1614 enum intel_engine_id id;
1615
1616 for_each_engine(engine, gt, id)
1617 intel_engine_flush_barriers(engine);
1618
1619 intel_gt_wait_for_idle(gt, MAX_SCHEDULE_TIMEOUT);
1620 }
1621
__i915_ggtt_pin(struct i915_vma * vma,struct i915_gem_ww_ctx * ww,u32 align,unsigned int flags)1622 static int __i915_ggtt_pin(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
1623 u32 align, unsigned int flags)
1624 {
1625 struct i915_address_space *vm = vma->vm;
1626 struct intel_gt *gt;
1627 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
1628 int err;
1629
1630 do {
1631 err = i915_vma_pin_ww(vma, ww, 0, align, flags | PIN_GLOBAL);
1632
1633 if (err != -ENOSPC) {
1634 if (!err) {
1635 err = i915_vma_wait_for_bind(vma);
1636 if (err)
1637 i915_vma_unpin(vma);
1638 }
1639 return err;
1640 }
1641
1642 /* Unlike i915_vma_pin, we don't take no for an answer! */
1643 list_for_each_entry(gt, &ggtt->gt_list, ggtt_link)
1644 flush_idle_contexts(gt);
1645 if (mutex_lock_interruptible(&vm->mutex) == 0) {
1646 /*
1647 * We pass NULL ww here, as we don't want to unbind
1648 * locked objects when called from execbuf when pinning
1649 * is removed. This would probably regress badly.
1650 */
1651 i915_gem_evict_vm(vm, NULL, NULL);
1652 mutex_unlock(&vm->mutex);
1653 }
1654 } while (1);
1655 }
1656
i915_ggtt_pin(struct i915_vma * vma,struct i915_gem_ww_ctx * ww,u32 align,unsigned int flags)1657 int i915_ggtt_pin(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
1658 u32 align, unsigned int flags)
1659 {
1660 struct i915_gem_ww_ctx _ww;
1661 int err;
1662
1663 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
1664
1665 if (ww)
1666 return __i915_ggtt_pin(vma, ww, align, flags);
1667
1668 lockdep_assert_not_held(&vma->obj->base.resv->lock.base);
1669
1670 for_i915_gem_ww(&_ww, err, true) {
1671 err = i915_gem_object_lock(vma->obj, &_ww);
1672 if (!err)
1673 err = __i915_ggtt_pin(vma, &_ww, align, flags);
1674 }
1675
1676 return err;
1677 }
1678
1679 /**
1680 * i915_ggtt_clear_scanout - Clear scanout flag for all objects ggtt vmas
1681 * @obj: i915 GEM object
1682 * This function clears scanout flags for objects ggtt vmas. These flags are set
1683 * when object is pinned for display use and this function to clear them all is
1684 * targeted to be called by frontbuffer tracking code when the frontbuffer is
1685 * about to be released.
1686 */
i915_ggtt_clear_scanout(struct drm_i915_gem_object * obj)1687 void i915_ggtt_clear_scanout(struct drm_i915_gem_object *obj)
1688 {
1689 struct i915_vma *vma;
1690
1691 spin_lock(&obj->vma.lock);
1692 for_each_ggtt_vma(vma, obj) {
1693 i915_vma_clear_scanout(vma);
1694 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
1695 }
1696 spin_unlock(&obj->vma.lock);
1697 }
1698
__vma_close(struct i915_vma * vma,struct intel_gt * gt)1699 static void __vma_close(struct i915_vma *vma, struct intel_gt *gt)
1700 {
1701 /*
1702 * We defer actually closing, unbinding and destroying the VMA until
1703 * the next idle point, or if the object is freed in the meantime. By
1704 * postponing the unbind, we allow for it to be resurrected by the
1705 * client, avoiding the work required to rebind the VMA. This is
1706 * advantageous for DRI, where the client/server pass objects
1707 * between themselves, temporarily opening a local VMA to the
1708 * object, and then closing it again. The same object is then reused
1709 * on the next frame (or two, depending on the depth of the swap queue)
1710 * causing us to rebind the VMA once more. This ends up being a lot
1711 * of wasted work for the steady state.
1712 */
1713 GEM_BUG_ON(i915_vma_is_closed(vma));
1714 list_add(&vma->closed_link, >->closed_vma);
1715 }
1716
i915_vma_close(struct i915_vma * vma)1717 void i915_vma_close(struct i915_vma *vma)
1718 {
1719 struct intel_gt *gt = vma->vm->gt;
1720 unsigned long flags;
1721
1722 if (i915_vma_is_ggtt(vma))
1723 return;
1724
1725 GEM_BUG_ON(!atomic_read(&vma->open_count));
1726 if (atomic_dec_and_lock_irqsave(&vma->open_count,
1727 >->closed_lock,
1728 flags)) {
1729 __vma_close(vma, gt);
1730 spin_unlock_irqrestore(>->closed_lock, flags);
1731 }
1732 }
1733
__i915_vma_remove_closed(struct i915_vma * vma)1734 static void __i915_vma_remove_closed(struct i915_vma *vma)
1735 {
1736 list_del_init(&vma->closed_link);
1737 }
1738
i915_vma_reopen(struct i915_vma * vma)1739 void i915_vma_reopen(struct i915_vma *vma)
1740 {
1741 struct intel_gt *gt = vma->vm->gt;
1742
1743 spin_lock_irq(>->closed_lock);
1744 if (i915_vma_is_closed(vma))
1745 __i915_vma_remove_closed(vma);
1746 spin_unlock_irq(>->closed_lock);
1747 }
1748
force_unbind(struct i915_vma * vma)1749 static void force_unbind(struct i915_vma *vma)
1750 {
1751 if (!drm_mm_node_allocated(&vma->node))
1752 return;
1753
1754 atomic_and(~I915_VMA_PIN_MASK, &vma->flags);
1755 WARN_ON(__i915_vma_unbind(vma));
1756 GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
1757 }
1758
release_references(struct i915_vma * vma,struct intel_gt * gt,bool vm_ddestroy)1759 static void release_references(struct i915_vma *vma, struct intel_gt *gt,
1760 bool vm_ddestroy)
1761 {
1762 struct drm_i915_gem_object *obj = vma->obj;
1763
1764 GEM_BUG_ON(i915_vma_is_active(vma));
1765
1766 spin_lock(&obj->vma.lock);
1767 list_del(&vma->obj_link);
1768 if (!RB_EMPTY_NODE(&vma->obj_node))
1769 rb_erase(&vma->obj_node, &obj->vma.tree);
1770
1771 spin_unlock(&obj->vma.lock);
1772
1773 spin_lock_irq(>->closed_lock);
1774 __i915_vma_remove_closed(vma);
1775 spin_unlock_irq(>->closed_lock);
1776
1777 if (vm_ddestroy)
1778 i915_vm_resv_put(vma->vm);
1779
1780 /* Wait for async active retire */
1781 i915_active_wait(&vma->active);
1782 i915_active_fini(&vma->active);
1783 GEM_WARN_ON(vma->resource);
1784 i915_vma_free(vma);
1785 }
1786
1787 /*
1788 * i915_vma_destroy_locked - Remove all weak reference to the vma and put
1789 * the initial reference.
1790 *
1791 * This function should be called when it's decided the vma isn't needed
1792 * anymore. The caller must assure that it doesn't race with another lookup
1793 * plus destroy, typically by taking an appropriate reference.
1794 *
1795 * Current callsites are
1796 * - __i915_gem_object_pages_fini()
1797 * - __i915_vm_close() - Blocks the above function by taking a reference on
1798 * the object.
1799 * - __i915_vma_parked() - Blocks the above functions by taking a reference
1800 * on the vm and a reference on the object. Also takes the object lock so
1801 * destruction from __i915_vma_parked() can be blocked by holding the
1802 * object lock. Since the object lock is only allowed from within i915 with
1803 * an object refcount, holding the object lock also implicitly blocks the
1804 * vma freeing from __i915_gem_object_pages_fini().
1805 *
1806 * Because of locks taken during destruction, a vma is also guaranteed to
1807 * stay alive while the following locks are held if it was looked up while
1808 * holding one of the locks:
1809 * - vm->mutex
1810 * - obj->vma.lock
1811 * - gt->closed_lock
1812 */
i915_vma_destroy_locked(struct i915_vma * vma)1813 void i915_vma_destroy_locked(struct i915_vma *vma)
1814 {
1815 lockdep_assert_held(&vma->vm->mutex);
1816
1817 force_unbind(vma);
1818 list_del_init(&vma->vm_link);
1819 release_references(vma, vma->vm->gt, false);
1820 }
1821
i915_vma_destroy(struct i915_vma * vma)1822 void i915_vma_destroy(struct i915_vma *vma)
1823 {
1824 struct intel_gt *gt;
1825 bool vm_ddestroy;
1826
1827 mutex_lock(&vma->vm->mutex);
1828 force_unbind(vma);
1829 list_del_init(&vma->vm_link);
1830 vm_ddestroy = vma->vm_ddestroy;
1831 vma->vm_ddestroy = false;
1832
1833 /* vma->vm may be freed when releasing vma->vm->mutex. */
1834 gt = vma->vm->gt;
1835 mutex_unlock(&vma->vm->mutex);
1836 release_references(vma, gt, vm_ddestroy);
1837 }
1838
i915_vma_parked(struct intel_gt * gt)1839 void i915_vma_parked(struct intel_gt *gt)
1840 {
1841 struct i915_vma *vma, *next;
1842 DRM_LIST_HEAD(closed);
1843
1844 spin_lock_irq(>->closed_lock);
1845 list_for_each_entry_safe(vma, next, >->closed_vma, closed_link) {
1846 struct drm_i915_gem_object *obj = vma->obj;
1847 struct i915_address_space *vm = vma->vm;
1848
1849 /* XXX All to avoid keeping a reference on i915_vma itself */
1850
1851 if (!kref_get_unless_zero(&obj->base.refcount))
1852 continue;
1853
1854 if (!i915_vm_tryget(vm)) {
1855 i915_gem_object_put(obj);
1856 continue;
1857 }
1858
1859 list_move(&vma->closed_link, &closed);
1860 }
1861 spin_unlock_irq(>->closed_lock);
1862
1863 /* As the GT is held idle, no vma can be reopened as we destroy them */
1864 list_for_each_entry_safe(vma, next, &closed, closed_link) {
1865 struct drm_i915_gem_object *obj = vma->obj;
1866 struct i915_address_space *vm = vma->vm;
1867
1868 if (i915_gem_object_trylock(obj, NULL)) {
1869 INIT_LIST_HEAD(&vma->closed_link);
1870 i915_vma_destroy(vma);
1871 i915_gem_object_unlock(obj);
1872 } else {
1873 /* back you go.. */
1874 spin_lock_irq(>->closed_lock);
1875 list_add(&vma->closed_link, >->closed_vma);
1876 spin_unlock_irq(>->closed_lock);
1877 }
1878
1879 i915_gem_object_put(obj);
1880 i915_vm_put(vm);
1881 }
1882 }
1883
__i915_vma_iounmap(struct i915_vma * vma)1884 static void __i915_vma_iounmap(struct i915_vma *vma)
1885 {
1886 GEM_BUG_ON(i915_vma_is_pinned(vma));
1887
1888 if (vma->iomap == NULL)
1889 return;
1890
1891 if (page_unmask_bits(vma->iomap))
1892 __i915_gem_object_release_map(vma->obj);
1893 else
1894 io_mapping_unmap(vma->iomap);
1895 vma->iomap = NULL;
1896 }
1897
i915_vma_revoke_mmap(struct i915_vma * vma)1898 void i915_vma_revoke_mmap(struct i915_vma *vma)
1899 {
1900 struct drm_vma_offset_node *node;
1901 u64 vma_offset;
1902
1903 if (!i915_vma_has_userfault(vma))
1904 return;
1905
1906 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
1907 GEM_BUG_ON(!vma->obj->userfault_count);
1908
1909 node = &vma->mmo->vma_node;
1910 vma_offset = vma->gtt_view.partial.offset << PAGE_SHIFT;
1911 #ifdef __linux__
1912 unmap_mapping_range(vma->vm->i915->drm.anon_inode->i_mapping,
1913 drm_vma_node_offset_addr(node) + vma_offset,
1914 vma->size,
1915 1);
1916 #else
1917 struct drm_i915_private *dev_priv = vma->obj->base.dev->dev_private;
1918 struct vm_page *pg;
1919
1920 for (pg = &dev_priv->pgs[atop(vma->node.start)];
1921 pg != &dev_priv->pgs[atop(vma->node.start + vma->size)];
1922 pg++)
1923 pmap_page_protect(pg, PROT_NONE);
1924 #endif
1925
1926 i915_vma_unset_userfault(vma);
1927 if (!--vma->obj->userfault_count)
1928 list_del(&vma->obj->userfault_link);
1929 }
1930
1931 static int
__i915_request_await_bind(struct i915_request * rq,struct i915_vma * vma)1932 __i915_request_await_bind(struct i915_request *rq, struct i915_vma *vma)
1933 {
1934 return __i915_request_await_exclusive(rq, &vma->active);
1935 }
1936
__i915_vma_move_to_active(struct i915_vma * vma,struct i915_request * rq)1937 static int __i915_vma_move_to_active(struct i915_vma *vma, struct i915_request *rq)
1938 {
1939 int err;
1940
1941 /* Wait for the vma to be bound before we start! */
1942 err = __i915_request_await_bind(rq, vma);
1943 if (err)
1944 return err;
1945
1946 return i915_active_add_request(&vma->active, rq);
1947 }
1948
_i915_vma_move_to_active(struct i915_vma * vma,struct i915_request * rq,struct dma_fence * fence,unsigned int flags)1949 int _i915_vma_move_to_active(struct i915_vma *vma,
1950 struct i915_request *rq,
1951 struct dma_fence *fence,
1952 unsigned int flags)
1953 {
1954 struct drm_i915_gem_object *obj = vma->obj;
1955 int err;
1956
1957 assert_object_held(obj);
1958
1959 GEM_BUG_ON(!vma->pages);
1960
1961 if (!(flags & __EXEC_OBJECT_NO_REQUEST_AWAIT)) {
1962 err = i915_request_await_object(rq, vma->obj, flags & EXEC_OBJECT_WRITE);
1963 if (unlikely(err))
1964 return err;
1965 }
1966 err = __i915_vma_move_to_active(vma, rq);
1967 if (unlikely(err))
1968 return err;
1969
1970 /*
1971 * Reserve fences slot early to prevent an allocation after preparing
1972 * the workload and associating fences with dma_resv.
1973 */
1974 if (fence && !(flags & __EXEC_OBJECT_NO_RESERVE)) {
1975 struct dma_fence *curr;
1976 int idx;
1977
1978 dma_fence_array_for_each(curr, idx, fence)
1979 ;
1980 err = dma_resv_reserve_fences(vma->obj->base.resv, idx);
1981 if (unlikely(err))
1982 return err;
1983 }
1984
1985 if (flags & EXEC_OBJECT_WRITE) {
1986 struct intel_frontbuffer *front;
1987
1988 front = i915_gem_object_get_frontbuffer(obj);
1989 if (unlikely(front)) {
1990 if (intel_frontbuffer_invalidate(front, ORIGIN_CS))
1991 i915_active_add_request(&front->write, rq);
1992 intel_frontbuffer_put(front);
1993 }
1994 }
1995
1996 if (fence) {
1997 struct dma_fence *curr;
1998 enum dma_resv_usage usage;
1999 int idx;
2000
2001 if (flags & EXEC_OBJECT_WRITE) {
2002 usage = DMA_RESV_USAGE_WRITE;
2003 obj->write_domain = I915_GEM_DOMAIN_RENDER;
2004 obj->read_domains = 0;
2005 } else {
2006 usage = DMA_RESV_USAGE_READ;
2007 obj->write_domain = 0;
2008 }
2009
2010 dma_fence_array_for_each(curr, idx, fence)
2011 dma_resv_add_fence(vma->obj->base.resv, curr, usage);
2012 }
2013
2014 if (flags & EXEC_OBJECT_NEEDS_FENCE && vma->fence)
2015 i915_active_add_request(&vma->fence->active, rq);
2016
2017 obj->read_domains |= I915_GEM_GPU_DOMAINS;
2018 obj->mm.dirty = true;
2019
2020 GEM_BUG_ON(!i915_vma_is_active(vma));
2021 return 0;
2022 }
2023
__i915_vma_evict(struct i915_vma * vma,bool async)2024 struct dma_fence *__i915_vma_evict(struct i915_vma *vma, bool async)
2025 {
2026 struct i915_vma_resource *vma_res = vma->resource;
2027 struct dma_fence *unbind_fence;
2028
2029 GEM_BUG_ON(i915_vma_is_pinned(vma));
2030 assert_vma_held_evict(vma);
2031
2032 if (i915_vma_is_map_and_fenceable(vma)) {
2033 /* Force a pagefault for domain tracking on next user access */
2034 i915_vma_revoke_mmap(vma);
2035
2036 /*
2037 * Check that we have flushed all writes through the GGTT
2038 * before the unbind, other due to non-strict nature of those
2039 * indirect writes they may end up referencing the GGTT PTE
2040 * after the unbind.
2041 *
2042 * Note that we may be concurrently poking at the GGTT_WRITE
2043 * bit from set-domain, as we mark all GGTT vma associated
2044 * with an object. We know this is for another vma, as we
2045 * are currently unbinding this one -- so if this vma will be
2046 * reused, it will be refaulted and have its dirty bit set
2047 * before the next write.
2048 */
2049 i915_vma_flush_writes(vma);
2050
2051 /* release the fence reg _after_ flushing */
2052 i915_vma_revoke_fence(vma);
2053
2054 clear_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
2055 }
2056
2057 __i915_vma_iounmap(vma);
2058
2059 GEM_BUG_ON(vma->fence);
2060 GEM_BUG_ON(i915_vma_has_userfault(vma));
2061
2062 /* Object backend must be async capable. */
2063 GEM_WARN_ON(async && !vma->resource->bi.pages_rsgt);
2064
2065 /* If vm is not open, unbind is a nop. */
2066 vma_res->needs_wakeref = i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND) &&
2067 kref_read(&vma->vm->ref);
2068 vma_res->skip_pte_rewrite = !kref_read(&vma->vm->ref) ||
2069 vma->vm->skip_pte_rewrite;
2070 trace_i915_vma_unbind(vma);
2071
2072 if (async)
2073 unbind_fence = i915_vma_resource_unbind(vma_res,
2074 vma->obj->mm.tlb);
2075 else
2076 unbind_fence = i915_vma_resource_unbind(vma_res, NULL);
2077
2078 vma->resource = NULL;
2079
2080 atomic_and(~(I915_VMA_BIND_MASK | I915_VMA_ERROR | I915_VMA_GGTT_WRITE),
2081 &vma->flags);
2082
2083 i915_vma_detach(vma);
2084
2085 if (!async) {
2086 if (unbind_fence) {
2087 dma_fence_wait(unbind_fence, false);
2088 dma_fence_put(unbind_fence);
2089 unbind_fence = NULL;
2090 }
2091 vma_invalidate_tlb(vma->vm, vma->obj->mm.tlb);
2092 }
2093
2094 /*
2095 * Binding itself may not have completed until the unbind fence signals,
2096 * so don't drop the pages until that happens, unless the resource is
2097 * async_capable.
2098 */
2099
2100 vma_unbind_pages(vma);
2101 return unbind_fence;
2102 }
2103
__i915_vma_unbind(struct i915_vma * vma)2104 int __i915_vma_unbind(struct i915_vma *vma)
2105 {
2106 int ret;
2107
2108 lockdep_assert_held(&vma->vm->mutex);
2109 assert_vma_held_evict(vma);
2110
2111 if (!drm_mm_node_allocated(&vma->node))
2112 return 0;
2113
2114 if (i915_vma_is_pinned(vma)) {
2115 vma_print_allocator(vma, "is pinned");
2116 return -EAGAIN;
2117 }
2118
2119 /*
2120 * After confirming that no one else is pinning this vma, wait for
2121 * any laggards who may have crept in during the wait (through
2122 * a residual pin skipping the vm->mutex) to complete.
2123 */
2124 ret = i915_vma_sync(vma);
2125 if (ret)
2126 return ret;
2127
2128 GEM_BUG_ON(i915_vma_is_active(vma));
2129 __i915_vma_evict(vma, false);
2130
2131 drm_mm_remove_node(&vma->node); /* pairs with i915_vma_release() */
2132 return 0;
2133 }
2134
__i915_vma_unbind_async(struct i915_vma * vma)2135 static struct dma_fence *__i915_vma_unbind_async(struct i915_vma *vma)
2136 {
2137 struct dma_fence *fence;
2138
2139 lockdep_assert_held(&vma->vm->mutex);
2140
2141 if (!drm_mm_node_allocated(&vma->node))
2142 return NULL;
2143
2144 if (i915_vma_is_pinned(vma) ||
2145 &vma->obj->mm.rsgt->table != vma->resource->bi.pages)
2146 return ERR_PTR(-EAGAIN);
2147
2148 /*
2149 * We probably need to replace this with awaiting the fences of the
2150 * object's dma_resv when the vma active goes away. When doing that
2151 * we need to be careful to not add the vma_resource unbind fence
2152 * immediately to the object's dma_resv, because then unbinding
2153 * the next vma from the object, in case there are many, will
2154 * actually await the unbinding of the previous vmas, which is
2155 * undesirable.
2156 */
2157 if (i915_sw_fence_await_active(&vma->resource->chain, &vma->active,
2158 I915_ACTIVE_AWAIT_EXCL |
2159 I915_ACTIVE_AWAIT_ACTIVE) < 0) {
2160 return ERR_PTR(-EBUSY);
2161 }
2162
2163 fence = __i915_vma_evict(vma, true);
2164
2165 drm_mm_remove_node(&vma->node); /* pairs with i915_vma_release() */
2166
2167 return fence;
2168 }
2169
i915_vma_unbind(struct i915_vma * vma)2170 int i915_vma_unbind(struct i915_vma *vma)
2171 {
2172 struct i915_address_space *vm = vma->vm;
2173 intel_wakeref_t wakeref = 0;
2174 int err;
2175
2176 assert_object_held_shared(vma->obj);
2177
2178 /* Optimistic wait before taking the mutex */
2179 err = i915_vma_sync(vma);
2180 if (err)
2181 return err;
2182
2183 if (!drm_mm_node_allocated(&vma->node))
2184 return 0;
2185
2186 if (i915_vma_is_pinned(vma)) {
2187 vma_print_allocator(vma, "is pinned");
2188 return -EAGAIN;
2189 }
2190
2191 if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
2192 /* XXX not always required: nop_clear_range */
2193 wakeref = intel_runtime_pm_get(&vm->i915->runtime_pm);
2194
2195 err = mutex_lock_interruptible_nested(&vma->vm->mutex, !wakeref);
2196 if (err)
2197 goto out_rpm;
2198
2199 err = __i915_vma_unbind(vma);
2200 mutex_unlock(&vm->mutex);
2201
2202 out_rpm:
2203 if (wakeref)
2204 intel_runtime_pm_put(&vm->i915->runtime_pm, wakeref);
2205 return err;
2206 }
2207
i915_vma_unbind_async(struct i915_vma * vma,bool trylock_vm)2208 int i915_vma_unbind_async(struct i915_vma *vma, bool trylock_vm)
2209 {
2210 struct drm_i915_gem_object *obj = vma->obj;
2211 struct i915_address_space *vm = vma->vm;
2212 intel_wakeref_t wakeref = 0;
2213 struct dma_fence *fence;
2214 int err;
2215
2216 /*
2217 * We need the dma-resv lock since we add the
2218 * unbind fence to the dma-resv object.
2219 */
2220 assert_object_held(obj);
2221
2222 if (!drm_mm_node_allocated(&vma->node))
2223 return 0;
2224
2225 if (i915_vma_is_pinned(vma)) {
2226 vma_print_allocator(vma, "is pinned");
2227 return -EAGAIN;
2228 }
2229
2230 if (!obj->mm.rsgt)
2231 return -EBUSY;
2232
2233 err = dma_resv_reserve_fences(obj->base.resv, 2);
2234 if (err)
2235 return -EBUSY;
2236
2237 /*
2238 * It would be great if we could grab this wakeref from the
2239 * async unbind work if needed, but we can't because it uses
2240 * kmalloc and it's in the dma-fence signalling critical path.
2241 */
2242 if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
2243 wakeref = intel_runtime_pm_get(&vm->i915->runtime_pm);
2244
2245 if (trylock_vm && !mutex_trylock(&vm->mutex)) {
2246 err = -EBUSY;
2247 goto out_rpm;
2248 } else if (!trylock_vm) {
2249 err = mutex_lock_interruptible_nested(&vm->mutex, !wakeref);
2250 if (err)
2251 goto out_rpm;
2252 }
2253
2254 fence = __i915_vma_unbind_async(vma);
2255 mutex_unlock(&vm->mutex);
2256 if (IS_ERR_OR_NULL(fence)) {
2257 err = PTR_ERR_OR_ZERO(fence);
2258 goto out_rpm;
2259 }
2260
2261 dma_resv_add_fence(obj->base.resv, fence, DMA_RESV_USAGE_READ);
2262 dma_fence_put(fence);
2263
2264 out_rpm:
2265 if (wakeref)
2266 intel_runtime_pm_put(&vm->i915->runtime_pm, wakeref);
2267 return err;
2268 }
2269
i915_vma_unbind_unlocked(struct i915_vma * vma)2270 int i915_vma_unbind_unlocked(struct i915_vma *vma)
2271 {
2272 int err;
2273
2274 i915_gem_object_lock(vma->obj, NULL);
2275 err = i915_vma_unbind(vma);
2276 i915_gem_object_unlock(vma->obj);
2277
2278 return err;
2279 }
2280
i915_vma_make_unshrinkable(struct i915_vma * vma)2281 struct i915_vma *i915_vma_make_unshrinkable(struct i915_vma *vma)
2282 {
2283 i915_gem_object_make_unshrinkable(vma->obj);
2284 return vma;
2285 }
2286
i915_vma_make_shrinkable(struct i915_vma * vma)2287 void i915_vma_make_shrinkable(struct i915_vma *vma)
2288 {
2289 i915_gem_object_make_shrinkable(vma->obj);
2290 }
2291
i915_vma_make_purgeable(struct i915_vma * vma)2292 void i915_vma_make_purgeable(struct i915_vma *vma)
2293 {
2294 i915_gem_object_make_purgeable(vma->obj);
2295 }
2296
2297 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
2298 #include "selftests/i915_vma.c"
2299 #endif
2300
i915_vma_module_exit(void)2301 void i915_vma_module_exit(void)
2302 {
2303 #ifdef __linux__
2304 kmem_cache_destroy(slab_vmas);
2305 #else
2306 pool_destroy(&slab_vmas);
2307 #endif
2308 }
2309
i915_vma_module_init(void)2310 int __init i915_vma_module_init(void)
2311 {
2312 #ifdef __linux__
2313 slab_vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
2314 if (!slab_vmas)
2315 return -ENOMEM;
2316 #else
2317 pool_init(&slab_vmas, sizeof(struct i915_vma),
2318 CACHELINESIZE, IPL_NONE, 0, "drmvma", NULL);
2319 #endif
2320
2321 return 0;
2322 }
2323