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