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