2 * Copyright © 2017 Intel Corporation
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:
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
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
25 #include <linux/highmem.h>
26 #include <linux/sched/mm.h>
28 #include <drm/drm_cache.h>
30 #include "display/intel_frontbuffer.h"
31 #include "pxp/intel_pxp.h"
34 #include "i915_file_private.h"
35 #include "i915_gem_clflush.h"
36 #include "i915_gem_context.h"
37 #include "i915_gem_dmabuf.h"
38 #include "i915_gem_mman.h"
39 #include "i915_gem_object.h"
40 #include "i915_gem_ttm.h"
41 #include "i915_memcpy.h"
42 #include "i915_trace.h"
44 static struct kmem_cache *slab_objects;
46 static const struct drm_gem_object_funcs i915_gem_object_funcs;
48 struct drm_i915_gem_object *i915_gem_object_alloc(void)
50 struct drm_i915_gem_object *obj;
52 obj = kmem_cache_zalloc(slab_objects, GFP_KERNEL);
55 obj->base.funcs = &i915_gem_object_funcs;
60 void i915_gem_object_free(struct drm_i915_gem_object *obj)
62 return kmem_cache_free(slab_objects, obj);
65 void i915_gem_object_init(struct drm_i915_gem_object *obj,
66 const struct drm_i915_gem_object_ops *ops,
67 struct lock_class_key *key, unsigned flags)
70 * A gem object is embedded both in a struct ttm_buffer_object :/ and
71 * in a drm_i915_gem_object. Make sure they are aliased.
73 BUILD_BUG_ON(offsetof(typeof(*obj), base) !=
74 offsetof(typeof(*obj), __do_not_access.base));
76 spin_lock_init(&obj->vma.lock);
77 INIT_LIST_HEAD(&obj->vma.list);
79 INIT_LIST_HEAD(&obj->mm.link);
81 INIT_LIST_HEAD(&obj->lut_list);
82 spin_lock_init(&obj->lut_lock);
84 spin_lock_init(&obj->mmo.lock);
85 obj->mmo.offsets = RB_ROOT;
87 init_rcu_head(&obj->rcu);
90 GEM_BUG_ON(flags & ~I915_BO_ALLOC_FLAGS);
93 obj->mm.madv = I915_MADV_WILLNEED;
94 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
95 mutex_init(&obj->mm.get_page.lock);
96 INIT_RADIX_TREE(&obj->mm.get_dma_page.radix, GFP_KERNEL | __GFP_NOWARN);
97 mutex_init(&obj->mm.get_dma_page.lock);
101 * __i915_gem_object_fini - Clean up a GEM object initialization
102 * @obj: The gem object to cleanup
104 * This function cleans up gem object fields that are set up by
105 * drm_gem_private_object_init() and i915_gem_object_init().
106 * It's primarily intended as a helper for backends that need to
107 * clean up the gem object in separate steps.
109 void __i915_gem_object_fini(struct drm_i915_gem_object *obj)
111 mutex_destroy(&obj->mm.get_page.lock);
112 mutex_destroy(&obj->mm.get_dma_page.lock);
113 dma_resv_fini(&obj->base._resv);
117 * i915_gem_object_set_cache_coherency - Mark up the object's coherency levels
118 * for a given cache_level
119 * @obj: #drm_i915_gem_object
120 * @cache_level: cache level
122 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
123 unsigned int cache_level)
125 struct drm_i915_private *i915 = to_i915(obj->base.dev);
127 obj->cache_level = cache_level;
129 if (cache_level != I915_CACHE_NONE)
130 obj->cache_coherent = (I915_BO_CACHE_COHERENT_FOR_READ |
131 I915_BO_CACHE_COHERENT_FOR_WRITE);
132 else if (HAS_LLC(i915))
133 obj->cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ;
135 obj->cache_coherent = 0;
138 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE) &&
142 bool i915_gem_object_can_bypass_llc(struct drm_i915_gem_object *obj)
144 struct drm_i915_private *i915 = to_i915(obj->base.dev);
147 * This is purely from a security perspective, so we simply don't care
148 * about non-userspace objects being able to bypass the LLC.
150 if (!(obj->flags & I915_BO_ALLOC_USER))
154 * EHL and JSL add the 'Bypass LLC' MOCS entry, which should make it
155 * possible for userspace to bypass the GTT caching bits set by the
156 * kernel, as per the given object cache_level. This is troublesome
157 * since the heavy flush we apply when first gathering the pages is
158 * skipped if the kernel thinks the object is coherent with the GPU. As
159 * a result it might be possible to bypass the cache and read the
160 * contents of the page directly, which could be stale data. If it's
161 * just a case of userspace shooting themselves in the foot then so be
162 * it, but since i915 takes the stance of always zeroing memory before
163 * handing it to userspace, we need to prevent this.
165 return IS_JSL_EHL(i915);
168 static void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
170 struct drm_i915_gem_object *obj = to_intel_bo(gem);
171 struct drm_i915_file_private *fpriv = file->driver_priv;
172 struct i915_lut_handle bookmark = {};
173 struct i915_mmap_offset *mmo, *mn;
174 struct i915_lut_handle *lut, *ln;
177 spin_lock(&obj->lut_lock);
178 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
179 struct i915_gem_context *ctx = lut->ctx;
181 if (ctx && ctx->file_priv == fpriv) {
182 i915_gem_context_get(ctx);
183 list_move(&lut->obj_link, &close);
186 /* Break long locks, and carefully continue on from this spot */
187 if (&ln->obj_link != &obj->lut_list) {
188 list_add_tail(&bookmark.obj_link, &ln->obj_link);
189 if (cond_resched_lock(&obj->lut_lock))
190 list_safe_reset_next(&bookmark, ln, obj_link);
191 __list_del_entry(&bookmark.obj_link);
194 spin_unlock(&obj->lut_lock);
196 spin_lock(&obj->mmo.lock);
197 rbtree_postorder_for_each_entry_safe(mmo, mn, &obj->mmo.offsets, offset)
198 drm_vma_node_revoke(&mmo->vma_node, file);
199 spin_unlock(&obj->mmo.lock);
201 list_for_each_entry_safe(lut, ln, &close, obj_link) {
202 struct i915_gem_context *ctx = lut->ctx;
203 struct i915_vma *vma;
206 * We allow the process to have multiple handles to the same
207 * vma, in the same fd namespace, by virtue of flink/open.
210 mutex_lock(&ctx->lut_mutex);
211 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
213 GEM_BUG_ON(vma->obj != obj);
214 GEM_BUG_ON(!atomic_read(&vma->open_count));
217 mutex_unlock(&ctx->lut_mutex);
219 i915_gem_context_put(lut->ctx);
220 i915_lut_handle_free(lut);
221 i915_gem_object_put(obj);
225 void __i915_gem_free_object_rcu(struct rcu_head *head)
227 struct drm_i915_gem_object *obj =
228 container_of(head, typeof(*obj), rcu);
229 struct drm_i915_private *i915 = to_i915(obj->base.dev);
231 i915_gem_object_free(obj);
233 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
234 atomic_dec(&i915->mm.free_count);
237 static void __i915_gem_object_free_mmaps(struct drm_i915_gem_object *obj)
239 /* Skip serialisation and waking the device if known to be not used. */
241 if (obj->userfault_count)
242 i915_gem_object_release_mmap_gtt(obj);
244 if (!RB_EMPTY_ROOT(&obj->mmo.offsets)) {
245 struct i915_mmap_offset *mmo, *mn;
247 i915_gem_object_release_mmap_offset(obj);
249 rbtree_postorder_for_each_entry_safe(mmo, mn,
252 drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
256 obj->mmo.offsets = RB_ROOT;
261 * __i915_gem_object_pages_fini - Clean up pages use of a gem object
262 * @obj: The gem object to clean up
264 * This function cleans up usage of the object mm.pages member. It
265 * is intended for backends that need to clean up a gem object in
266 * separate steps and needs to be called when the object is idle before
267 * the object's backing memory is freed.
269 void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj)
271 assert_object_held_shared(obj);
273 if (!list_empty(&obj->vma.list)) {
274 struct i915_vma *vma;
276 spin_lock(&obj->vma.lock);
277 while ((vma = list_first_entry_or_null(&obj->vma.list,
280 GEM_BUG_ON(vma->obj != obj);
281 spin_unlock(&obj->vma.lock);
283 i915_vma_destroy(vma);
285 spin_lock(&obj->vma.lock);
287 spin_unlock(&obj->vma.lock);
290 __i915_gem_object_free_mmaps(obj);
292 atomic_set(&obj->mm.pages_pin_count, 0);
293 __i915_gem_object_put_pages(obj);
294 GEM_BUG_ON(i915_gem_object_has_pages(obj));
297 void __i915_gem_free_object(struct drm_i915_gem_object *obj)
299 trace_i915_gem_object_destroy(obj);
301 GEM_BUG_ON(!list_empty(&obj->lut_list));
303 bitmap_free(obj->bit_17);
305 if (obj->base.import_attach)
306 drm_prime_gem_destroy(&obj->base, NULL);
308 drm_gem_free_mmap_offset(&obj->base);
310 if (obj->ops->release)
311 obj->ops->release(obj);
313 if (obj->mm.n_placements > 1)
314 kfree(obj->mm.placements);
316 if (obj->shares_resv_from)
317 i915_vm_resv_put(obj->shares_resv_from);
319 __i915_gem_object_fini(obj);
322 static void __i915_gem_free_objects(struct drm_i915_private *i915,
323 struct llist_node *freed)
325 struct drm_i915_gem_object *obj, *on;
327 llist_for_each_entry_safe(obj, on, freed, freed) {
329 if (obj->ops->delayed_free) {
330 obj->ops->delayed_free(obj);
334 __i915_gem_object_pages_fini(obj);
335 __i915_gem_free_object(obj);
337 /* But keep the pointer alive for RCU-protected lookups */
338 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
343 void i915_gem_flush_free_objects(struct drm_i915_private *i915)
345 struct llist_node *freed = llist_del_all(&i915->mm.free_list);
348 __i915_gem_free_objects(i915, freed);
351 static void __i915_gem_free_work(struct work_struct *work)
353 struct drm_i915_private *i915 =
354 container_of(work, struct drm_i915_private, mm.free_work);
356 i915_gem_flush_free_objects(i915);
359 static void i915_gem_free_object(struct drm_gem_object *gem_obj)
361 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
362 struct drm_i915_private *i915 = to_i915(obj->base.dev);
364 GEM_BUG_ON(i915_gem_object_is_framebuffer(obj));
367 * Before we free the object, make sure any pure RCU-only
368 * read-side critical sections are complete, e.g.
369 * i915_gem_busy_ioctl(). For the corresponding synchronized
370 * lookup see i915_gem_object_lookup_rcu().
372 atomic_inc(&i915->mm.free_count);
375 * Since we require blocking on struct_mutex to unbind the freed
376 * object from the GPU before releasing resources back to the
377 * system, we can not do that directly from the RCU callback (which may
378 * be a softirq context), but must instead then defer that work onto a
379 * kthread. We use the RCU callback rather than move the freed object
380 * directly onto the work queue so that we can mix between using the
381 * worker and performing frees directly from subsequent allocations for
382 * crude but effective memory throttling.
385 if (llist_add(&obj->freed, &i915->mm.free_list))
386 queue_work(i915->wq, &i915->mm.free_work);
389 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
390 enum fb_op_origin origin)
392 struct intel_frontbuffer *front;
394 front = __intel_frontbuffer_get(obj);
396 intel_frontbuffer_flush(front, origin);
397 intel_frontbuffer_put(front);
401 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
402 enum fb_op_origin origin)
404 struct intel_frontbuffer *front;
406 front = __intel_frontbuffer_get(obj);
408 intel_frontbuffer_invalidate(front, origin);
409 intel_frontbuffer_put(front);
414 i915_gem_object_read_from_page_kmap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
419 src_map = kmap_atomic(i915_gem_object_get_page(obj, offset >> PAGE_SHIFT));
421 src_ptr = src_map + offset_in_page(offset);
422 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
423 drm_clflush_virt_range(src_ptr, size);
424 memcpy(dst, src_ptr, size);
426 kunmap_atomic(src_map);
430 i915_gem_object_read_from_page_iomap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
432 void __iomem *src_map;
433 void __iomem *src_ptr;
434 dma_addr_t dma = i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT);
436 src_map = io_mapping_map_wc(&obj->mm.region->iomap,
437 dma - obj->mm.region->region.start,
440 src_ptr = src_map + offset_in_page(offset);
441 if (!i915_memcpy_from_wc(dst, (void __force *)src_ptr, size))
442 memcpy_fromio(dst, src_ptr, size);
444 io_mapping_unmap(src_map);
448 * i915_gem_object_read_from_page - read data from the page of a GEM object
449 * @obj: GEM object to read from
450 * @offset: offset within the object
451 * @dst: buffer to store the read data
452 * @size: size to read
454 * Reads data from @obj at the specified offset. The requested region to read
455 * from can't cross a page boundary. The caller must ensure that @obj pages
456 * are pinned and that @obj is synced wrt. any related writes.
458 * Return: %0 on success or -ENODEV if the type of @obj's backing store is
461 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
463 GEM_BUG_ON(offset >= obj->base.size);
464 GEM_BUG_ON(offset_in_page(offset) > PAGE_SIZE - size);
465 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
467 if (i915_gem_object_has_struct_page(obj))
468 i915_gem_object_read_from_page_kmap(obj, offset, dst, size);
469 else if (i915_gem_object_has_iomem(obj))
470 i915_gem_object_read_from_page_iomap(obj, offset, dst, size);
478 * i915_gem_object_evictable - Whether object is likely evictable after unbind.
479 * @obj: The object to check
481 * This function checks whether the object is likely unvictable after unbind.
482 * If the object is not locked when checking, the result is only advisory.
483 * If the object is locked when checking, and the function returns true,
484 * then an eviction should indeed be possible. But since unlocked vma
485 * unpinning and unbinding is currently possible, the object can actually
486 * become evictable even if this function returns false.
488 * Return: true if the object may be evictable. False otherwise.
490 bool i915_gem_object_evictable(struct drm_i915_gem_object *obj)
492 struct i915_vma *vma;
493 int pin_count = atomic_read(&obj->mm.pages_pin_count);
498 spin_lock(&obj->vma.lock);
499 list_for_each_entry(vma, &obj->vma.list, obj_link) {
500 if (i915_vma_is_pinned(vma)) {
501 spin_unlock(&obj->vma.lock);
504 if (atomic_read(&vma->pages_count))
507 spin_unlock(&obj->vma.lock);
508 GEM_WARN_ON(pin_count < 0);
510 return pin_count == 0;
514 * i915_gem_object_migratable - Whether the object is migratable out of the
516 * @obj: Pointer to the object.
518 * Return: Whether the object is allowed to be resident in other
519 * regions than the current while pages are present.
521 bool i915_gem_object_migratable(struct drm_i915_gem_object *obj)
523 struct intel_memory_region *mr = READ_ONCE(obj->mm.region);
528 return obj->mm.n_placements > 1;
532 * i915_gem_object_has_struct_page - Whether the object is page-backed
533 * @obj: The object to query.
535 * This function should only be called while the object is locked or pinned,
536 * otherwise the page backing may change under the caller.
538 * Return: True if page-backed, false otherwise.
540 bool i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
542 #ifdef CONFIG_LOCKDEP
543 if (IS_DGFX(to_i915(obj->base.dev)) &&
544 i915_gem_object_evictable((void __force *)obj))
545 assert_object_held_shared(obj);
547 return obj->mem_flags & I915_BO_FLAG_STRUCT_PAGE;
551 * i915_gem_object_has_iomem - Whether the object is iomem-backed
552 * @obj: The object to query.
554 * This function should only be called while the object is locked or pinned,
555 * otherwise the iomem backing may change under the caller.
557 * Return: True if iomem-backed, false otherwise.
559 bool i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj)
561 #ifdef CONFIG_LOCKDEP
562 if (IS_DGFX(to_i915(obj->base.dev)) &&
563 i915_gem_object_evictable((void __force *)obj))
564 assert_object_held_shared(obj);
566 return obj->mem_flags & I915_BO_FLAG_IOMEM;
570 * i915_gem_object_can_migrate - Whether an object likely can be migrated
572 * @obj: The object to migrate
573 * @id: The region intended to migrate to
575 * Check whether the object backend supports migration to the
576 * given region. Note that pinning may affect the ability to migrate as
577 * returned by this function.
579 * This function is primarily intended as a helper for checking the
580 * possibility to migrate objects and might be slightly less permissive
581 * than i915_gem_object_migrate() when it comes to objects with the
582 * I915_BO_ALLOC_USER flag set.
584 * Return: true if migration is possible, false otherwise.
586 bool i915_gem_object_can_migrate(struct drm_i915_gem_object *obj,
587 enum intel_region_id id)
589 struct drm_i915_private *i915 = to_i915(obj->base.dev);
590 unsigned int num_allowed = obj->mm.n_placements;
591 struct intel_memory_region *mr;
594 GEM_BUG_ON(id >= INTEL_REGION_UNKNOWN);
595 GEM_BUG_ON(obj->mm.madv != I915_MADV_WILLNEED);
597 mr = i915->mm.regions[id];
601 if (!IS_ALIGNED(obj->base.size, mr->min_page_size))
604 if (obj->mm.region == mr)
607 if (!i915_gem_object_evictable(obj))
610 if (!obj->ops->migrate)
613 if (!(obj->flags & I915_BO_ALLOC_USER))
616 if (num_allowed == 0)
619 for (i = 0; i < num_allowed; ++i) {
620 if (mr == obj->mm.placements[i])
628 * i915_gem_object_migrate - Migrate an object to the desired region id
629 * @obj: The object to migrate.
630 * @ww: An optional struct i915_gem_ww_ctx. If NULL, the backend may
631 * not be successful in evicting other objects to make room for this object.
632 * @id: The region id to migrate to.
634 * Attempt to migrate the object to the desired memory region. The
635 * object backend must support migration and the object may not be
636 * pinned, (explicitly pinned pages or pinned vmas). The object must
638 * On successful completion, the object will have pages pointing to
639 * memory in the new region, but an async migration task may not have
640 * completed yet, and to accomplish that, i915_gem_object_wait_migration()
643 * Note: the @ww parameter is not used yet, but included to make sure
644 * callers put some effort into obtaining a valid ww ctx if one is
647 * Return: 0 on success. Negative error code on failure. In particular may
648 * return -ENXIO on lack of region space, -EDEADLK for deadlock avoidance
649 * if @ww is set, -EINTR or -ERESTARTSYS if signal pending, and
650 * -EBUSY if the object is pinned.
652 int i915_gem_object_migrate(struct drm_i915_gem_object *obj,
653 struct i915_gem_ww_ctx *ww,
654 enum intel_region_id id)
656 struct drm_i915_private *i915 = to_i915(obj->base.dev);
657 struct intel_memory_region *mr;
659 GEM_BUG_ON(id >= INTEL_REGION_UNKNOWN);
660 GEM_BUG_ON(obj->mm.madv != I915_MADV_WILLNEED);
661 assert_object_held(obj);
663 mr = i915->mm.regions[id];
666 if (!i915_gem_object_can_migrate(obj, id))
669 if (!obj->ops->migrate) {
670 if (GEM_WARN_ON(obj->mm.region != mr))
675 return obj->ops->migrate(obj, mr);
679 * i915_gem_object_placement_possible - Check whether the object can be
680 * placed at certain memory type
681 * @obj: Pointer to the object
682 * @type: The memory type to check
684 * Return: True if the object can be placed in @type. False otherwise.
686 bool i915_gem_object_placement_possible(struct drm_i915_gem_object *obj,
687 enum intel_memory_type type)
691 if (!obj->mm.n_placements) {
693 case INTEL_MEMORY_LOCAL:
694 return i915_gem_object_has_iomem(obj);
695 case INTEL_MEMORY_SYSTEM:
696 return i915_gem_object_has_pages(obj);
698 /* Ignore stolen for now */
704 for (i = 0; i < obj->mm.n_placements; i++) {
705 if (obj->mm.placements[i]->type == type)
713 * i915_gem_object_needs_ccs_pages - Check whether the object requires extra
714 * pages when placed in system-memory, in order to save and later restore the
715 * flat-CCS aux state when the object is moved between local-memory and
717 * @obj: Pointer to the object
719 * Return: True if the object needs extra ccs pages. False otherwise.
721 bool i915_gem_object_needs_ccs_pages(struct drm_i915_gem_object *obj)
723 bool lmem_placement = false;
726 for (i = 0; i < obj->mm.n_placements; i++) {
727 /* Compression is not allowed for the objects with smem placement */
728 if (obj->mm.placements[i]->type == INTEL_MEMORY_SYSTEM)
730 if (!lmem_placement &&
731 obj->mm.placements[i]->type == INTEL_MEMORY_LOCAL)
732 lmem_placement = true;
735 return lmem_placement;
738 void i915_gem_init__objects(struct drm_i915_private *i915)
740 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
743 void i915_objects_module_exit(void)
745 kmem_cache_destroy(slab_objects);
748 int __init i915_objects_module_init(void)
750 slab_objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
757 static const struct drm_gem_object_funcs i915_gem_object_funcs = {
758 .free = i915_gem_free_object,
759 .close = i915_gem_close_object,
760 .export = i915_gem_prime_export,
764 * i915_gem_object_get_moving_fence - Get the object's moving fence if any
765 * @obj: The object whose moving fence to get.
766 * @fence: The resulting fence
768 * A non-signaled moving fence means that there is an async operation
769 * pending on the object that needs to be waited on before setting up
770 * any GPU- or CPU PTEs to the object's pages.
772 * Return: Negative error code or 0 for success.
774 int i915_gem_object_get_moving_fence(struct drm_i915_gem_object *obj,
775 struct dma_fence **fence)
777 return dma_resv_get_singleton(obj->base.resv, DMA_RESV_USAGE_KERNEL,
782 * i915_gem_object_wait_moving_fence - Wait for the object's moving fence if any
783 * @obj: The object whose moving fence to wait for.
784 * @intr: Whether to wait interruptible.
786 * If the moving fence signaled without an error, it is detached from the
789 * Return: 0 if successful, -ERESTARTSYS if the wait was interrupted,
790 * negative error code if the async operation represented by the
791 * moving fence failed.
793 int i915_gem_object_wait_moving_fence(struct drm_i915_gem_object *obj,
798 assert_object_held(obj);
800 ret = dma_resv_wait_timeout(obj->base. resv, DMA_RESV_USAGE_KERNEL,
801 intr, MAX_SCHEDULE_TIMEOUT);
804 else if (ret > 0 && i915_gem_object_has_unknown_state(obj))
807 return ret < 0 ? ret : 0;
811 * i915_gem_object_has_unknown_state - Return true if the object backing pages are
812 * in an unknown_state. This means that userspace must NEVER be allowed to touch
813 * the pages, with either the GPU or CPU.
815 * ONLY valid to be called after ensuring that all kernel fences have signalled
816 * (in particular the fence for moving/clearing the object).
818 bool i915_gem_object_has_unknown_state(struct drm_i915_gem_object *obj)
821 * The below barrier pairs with the dma_fence_signal() in
822 * __memcpy_work(). We should only sample the unknown_state after all
823 * the kernel fences have signalled.
826 return obj->mm.unknown_state;
829 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
830 #include "selftests/huge_gem_object.c"
831 #include "selftests/huge_pages.c"
832 #include "selftests/i915_gem_migrate.c"
833 #include "selftests/i915_gem_object.c"
834 #include "selftests/i915_gem_coherency.c"