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_object_frontbuffer.h"
41 #include "i915_gem_ttm.h"
42 #include "i915_memcpy.h"
43 #include "i915_trace.h"
45 static struct kmem_cache *slab_objects;
47 static const struct drm_gem_object_funcs i915_gem_object_funcs;
49 unsigned int i915_gem_get_pat_index(struct drm_i915_private *i915,
50 enum i915_cache_level level)
52 if (drm_WARN_ON(&i915->drm, level >= I915_MAX_CACHE_LEVEL))
55 return INTEL_INFO(i915)->cachelevel_to_pat[level];
58 bool i915_gem_object_has_cache_level(const struct drm_i915_gem_object *obj,
59 enum i915_cache_level lvl)
62 * In case the pat_index is set by user space, this kernel mode
63 * driver should leave the coherency to be managed by user space,
64 * simply return true here.
66 if (obj->pat_set_by_user)
70 * Otherwise the pat_index should have been converted from cache_level
71 * so that the following comparison is valid.
73 return obj->pat_index == i915_gem_get_pat_index(obj_to_i915(obj), lvl);
76 struct drm_i915_gem_object *i915_gem_object_alloc(void)
78 struct drm_i915_gem_object *obj;
80 obj = kmem_cache_zalloc(slab_objects, GFP_KERNEL);
83 obj->base.funcs = &i915_gem_object_funcs;
88 void i915_gem_object_free(struct drm_i915_gem_object *obj)
90 return kmem_cache_free(slab_objects, obj);
93 void i915_gem_object_init(struct drm_i915_gem_object *obj,
94 const struct drm_i915_gem_object_ops *ops,
95 struct lock_class_key *key, unsigned flags)
98 * A gem object is embedded both in a struct ttm_buffer_object :/ and
99 * in a drm_i915_gem_object. Make sure they are aliased.
101 BUILD_BUG_ON(offsetof(typeof(*obj), base) !=
102 offsetof(typeof(*obj), __do_not_access.base));
104 spin_lock_init(&obj->vma.lock);
105 INIT_LIST_HEAD(&obj->vma.list);
107 INIT_LIST_HEAD(&obj->mm.link);
109 INIT_LIST_HEAD(&obj->lut_list);
110 spin_lock_init(&obj->lut_lock);
112 spin_lock_init(&obj->mmo.lock);
113 obj->mmo.offsets = RB_ROOT;
115 init_rcu_head(&obj->rcu);
118 GEM_BUG_ON(flags & ~I915_BO_ALLOC_FLAGS);
121 obj->mm.madv = I915_MADV_WILLNEED;
122 INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
123 mutex_init(&obj->mm.get_page.lock);
124 INIT_RADIX_TREE(&obj->mm.get_dma_page.radix, GFP_KERNEL | __GFP_NOWARN);
125 mutex_init(&obj->mm.get_dma_page.lock);
129 * __i915_gem_object_fini - Clean up a GEM object initialization
130 * @obj: The gem object to cleanup
132 * This function cleans up gem object fields that are set up by
133 * drm_gem_private_object_init() and i915_gem_object_init().
134 * It's primarily intended as a helper for backends that need to
135 * clean up the gem object in separate steps.
137 void __i915_gem_object_fini(struct drm_i915_gem_object *obj)
139 mutex_destroy(&obj->mm.get_page.lock);
140 mutex_destroy(&obj->mm.get_dma_page.lock);
141 dma_resv_fini(&obj->base._resv);
145 * i915_gem_object_set_cache_coherency - Mark up the object's coherency levels
146 * for a given cache_level
147 * @obj: #drm_i915_gem_object
148 * @cache_level: cache level
150 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
151 unsigned int cache_level)
153 struct drm_i915_private *i915 = to_i915(obj->base.dev);
155 obj->pat_index = i915_gem_get_pat_index(i915, cache_level);
157 if (cache_level != I915_CACHE_NONE)
158 obj->cache_coherent = (I915_BO_CACHE_COHERENT_FOR_READ |
159 I915_BO_CACHE_COHERENT_FOR_WRITE);
160 else if (HAS_LLC(i915))
161 obj->cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ;
163 obj->cache_coherent = 0;
166 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE) &&
171 * i915_gem_object_set_pat_index - set PAT index to be used in PTE encode
172 * @obj: #drm_i915_gem_object
173 * @pat_index: PAT index
175 * This is a clone of i915_gem_object_set_cache_coherency taking pat index
176 * instead of cache_level as its second argument.
178 void i915_gem_object_set_pat_index(struct drm_i915_gem_object *obj,
179 unsigned int pat_index)
181 struct drm_i915_private *i915 = to_i915(obj->base.dev);
183 if (obj->pat_index == pat_index)
186 obj->pat_index = pat_index;
188 if (pat_index != i915_gem_get_pat_index(i915, I915_CACHE_NONE))
189 obj->cache_coherent = (I915_BO_CACHE_COHERENT_FOR_READ |
190 I915_BO_CACHE_COHERENT_FOR_WRITE);
191 else if (HAS_LLC(i915))
192 obj->cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ;
194 obj->cache_coherent = 0;
197 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE) &&
201 bool i915_gem_object_can_bypass_llc(struct drm_i915_gem_object *obj)
203 struct drm_i915_private *i915 = to_i915(obj->base.dev);
206 * This is purely from a security perspective, so we simply don't care
207 * about non-userspace objects being able to bypass the LLC.
209 if (!(obj->flags & I915_BO_ALLOC_USER))
213 * Always flush cache for UMD objects at creation time.
215 if (obj->pat_set_by_user)
219 * EHL and JSL add the 'Bypass LLC' MOCS entry, which should make it
220 * possible for userspace to bypass the GTT caching bits set by the
221 * kernel, as per the given object cache_level. This is troublesome
222 * since the heavy flush we apply when first gathering the pages is
223 * skipped if the kernel thinks the object is coherent with the GPU. As
224 * a result it might be possible to bypass the cache and read the
225 * contents of the page directly, which could be stale data. If it's
226 * just a case of userspace shooting themselves in the foot then so be
227 * it, but since i915 takes the stance of always zeroing memory before
228 * handing it to userspace, we need to prevent this.
230 return (IS_JASPERLAKE(i915) || IS_ELKHARTLAKE(i915));
233 static void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
235 struct drm_i915_gem_object *obj = to_intel_bo(gem);
236 struct drm_i915_file_private *fpriv = file->driver_priv;
237 struct i915_lut_handle bookmark = {};
238 struct i915_mmap_offset *mmo, *mn;
239 struct i915_lut_handle *lut, *ln;
242 spin_lock(&obj->lut_lock);
243 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
244 struct i915_gem_context *ctx = lut->ctx;
246 if (ctx && ctx->file_priv == fpriv) {
247 i915_gem_context_get(ctx);
248 list_move(&lut->obj_link, &close);
251 /* Break long locks, and carefully continue on from this spot */
252 if (&ln->obj_link != &obj->lut_list) {
253 list_add_tail(&bookmark.obj_link, &ln->obj_link);
254 if (cond_resched_lock(&obj->lut_lock))
255 list_safe_reset_next(&bookmark, ln, obj_link);
256 __list_del_entry(&bookmark.obj_link);
259 spin_unlock(&obj->lut_lock);
261 spin_lock(&obj->mmo.lock);
262 rbtree_postorder_for_each_entry_safe(mmo, mn, &obj->mmo.offsets, offset)
263 drm_vma_node_revoke(&mmo->vma_node, file);
264 spin_unlock(&obj->mmo.lock);
266 list_for_each_entry_safe(lut, ln, &close, obj_link) {
267 struct i915_gem_context *ctx = lut->ctx;
268 struct i915_vma *vma;
271 * We allow the process to have multiple handles to the same
272 * vma, in the same fd namespace, by virtue of flink/open.
275 mutex_lock(&ctx->lut_mutex);
276 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
278 GEM_BUG_ON(vma->obj != obj);
279 GEM_BUG_ON(!atomic_read(&vma->open_count));
282 mutex_unlock(&ctx->lut_mutex);
284 i915_gem_context_put(lut->ctx);
285 i915_lut_handle_free(lut);
286 i915_gem_object_put(obj);
290 void __i915_gem_free_object_rcu(struct rcu_head *head)
292 struct drm_i915_gem_object *obj =
293 container_of(head, typeof(*obj), rcu);
294 struct drm_i915_private *i915 = to_i915(obj->base.dev);
296 i915_gem_object_free(obj);
298 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
299 atomic_dec(&i915->mm.free_count);
302 static void __i915_gem_object_free_mmaps(struct drm_i915_gem_object *obj)
304 /* Skip serialisation and waking the device if known to be not used. */
306 if (obj->userfault_count && !IS_DGFX(to_i915(obj->base.dev)))
307 i915_gem_object_release_mmap_gtt(obj);
309 if (!RB_EMPTY_ROOT(&obj->mmo.offsets)) {
310 struct i915_mmap_offset *mmo, *mn;
312 i915_gem_object_release_mmap_offset(obj);
314 rbtree_postorder_for_each_entry_safe(mmo, mn,
317 drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
321 obj->mmo.offsets = RB_ROOT;
326 * __i915_gem_object_pages_fini - Clean up pages use of a gem object
327 * @obj: The gem object to clean up
329 * This function cleans up usage of the object mm.pages member. It
330 * is intended for backends that need to clean up a gem object in
331 * separate steps and needs to be called when the object is idle before
332 * the object's backing memory is freed.
334 void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj)
336 assert_object_held_shared(obj);
338 if (!list_empty(&obj->vma.list)) {
339 struct i915_vma *vma;
341 spin_lock(&obj->vma.lock);
342 while ((vma = list_first_entry_or_null(&obj->vma.list,
345 GEM_BUG_ON(vma->obj != obj);
346 spin_unlock(&obj->vma.lock);
348 i915_vma_destroy(vma);
350 spin_lock(&obj->vma.lock);
352 spin_unlock(&obj->vma.lock);
355 __i915_gem_object_free_mmaps(obj);
357 atomic_set(&obj->mm.pages_pin_count, 0);
360 * dma_buf_unmap_attachment() requires reservation to be
361 * locked. The imported GEM shouldn't share reservation lock
362 * and ttm_bo_cleanup_memtype_use() shouldn't be invoked for
363 * dma-buf, so it's safe to take the lock.
365 if (obj->base.import_attach)
366 i915_gem_object_lock(obj, NULL);
368 __i915_gem_object_put_pages(obj);
370 if (obj->base.import_attach)
371 i915_gem_object_unlock(obj);
373 GEM_BUG_ON(i915_gem_object_has_pages(obj));
376 void __i915_gem_free_object(struct drm_i915_gem_object *obj)
378 trace_i915_gem_object_destroy(obj);
380 GEM_BUG_ON(!list_empty(&obj->lut_list));
382 bitmap_free(obj->bit_17);
384 if (obj->base.import_attach)
385 drm_prime_gem_destroy(&obj->base, NULL);
387 drm_gem_free_mmap_offset(&obj->base);
389 if (obj->ops->release)
390 obj->ops->release(obj);
392 if (obj->mm.n_placements > 1)
393 kfree(obj->mm.placements);
395 if (obj->shares_resv_from)
396 i915_vm_resv_put(obj->shares_resv_from);
398 __i915_gem_object_fini(obj);
401 static void __i915_gem_free_objects(struct drm_i915_private *i915,
402 struct llist_node *freed)
404 struct drm_i915_gem_object *obj, *on;
406 llist_for_each_entry_safe(obj, on, freed, freed) {
408 if (obj->ops->delayed_free) {
409 obj->ops->delayed_free(obj);
413 __i915_gem_object_pages_fini(obj);
414 __i915_gem_free_object(obj);
416 /* But keep the pointer alive for RCU-protected lookups */
417 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
422 void i915_gem_flush_free_objects(struct drm_i915_private *i915)
424 struct llist_node *freed = llist_del_all(&i915->mm.free_list);
427 __i915_gem_free_objects(i915, freed);
430 static void __i915_gem_free_work(struct work_struct *work)
432 struct drm_i915_private *i915 =
433 container_of(work, struct drm_i915_private, mm.free_work);
435 i915_gem_flush_free_objects(i915);
438 static void i915_gem_free_object(struct drm_gem_object *gem_obj)
440 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
441 struct drm_i915_private *i915 = to_i915(obj->base.dev);
443 GEM_BUG_ON(i915_gem_object_is_framebuffer(obj));
446 * Before we free the object, make sure any pure RCU-only
447 * read-side critical sections are complete, e.g.
448 * i915_gem_busy_ioctl(). For the corresponding synchronized
449 * lookup see i915_gem_object_lookup_rcu().
451 atomic_inc(&i915->mm.free_count);
454 * Since we require blocking on struct_mutex to unbind the freed
455 * object from the GPU before releasing resources back to the
456 * system, we can not do that directly from the RCU callback (which may
457 * be a softirq context), but must instead then defer that work onto a
458 * kthread. We use the RCU callback rather than move the freed object
459 * directly onto the work queue so that we can mix between using the
460 * worker and performing frees directly from subsequent allocations for
461 * crude but effective memory throttling.
464 if (llist_add(&obj->freed, &i915->mm.free_list))
465 queue_work(i915->wq, &i915->mm.free_work);
468 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
469 enum fb_op_origin origin)
471 struct intel_frontbuffer *front;
473 front = i915_gem_object_get_frontbuffer(obj);
475 intel_frontbuffer_flush(front, origin);
476 intel_frontbuffer_put(front);
480 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
481 enum fb_op_origin origin)
483 struct intel_frontbuffer *front;
485 front = i915_gem_object_get_frontbuffer(obj);
487 intel_frontbuffer_invalidate(front, origin);
488 intel_frontbuffer_put(front);
493 i915_gem_object_read_from_page_kmap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
495 pgoff_t idx = offset >> PAGE_SHIFT;
499 src_map = kmap_atomic(i915_gem_object_get_page(obj, idx));
501 src_ptr = src_map + offset_in_page(offset);
502 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
503 drm_clflush_virt_range(src_ptr, size);
504 memcpy(dst, src_ptr, size);
506 kunmap_atomic(src_map);
510 i915_gem_object_read_from_page_iomap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
512 pgoff_t idx = offset >> PAGE_SHIFT;
513 dma_addr_t dma = i915_gem_object_get_dma_address(obj, idx);
514 void __iomem *src_map;
515 void __iomem *src_ptr;
517 src_map = io_mapping_map_wc(&obj->mm.region->iomap,
518 dma - obj->mm.region->region.start,
521 src_ptr = src_map + offset_in_page(offset);
522 if (!i915_memcpy_from_wc(dst, (void __force *)src_ptr, size))
523 memcpy_fromio(dst, src_ptr, size);
525 io_mapping_unmap(src_map);
528 static bool object_has_mappable_iomem(struct drm_i915_gem_object *obj)
530 GEM_BUG_ON(!i915_gem_object_has_iomem(obj));
532 if (IS_DGFX(to_i915(obj->base.dev)))
533 return i915_ttm_resource_mappable(i915_gem_to_ttm(obj)->resource);
539 * i915_gem_object_read_from_page - read data from the page of a GEM object
540 * @obj: GEM object to read from
541 * @offset: offset within the object
542 * @dst: buffer to store the read data
543 * @size: size to read
545 * Reads data from @obj at the specified offset. The requested region to read
546 * from can't cross a page boundary. The caller must ensure that @obj pages
547 * are pinned and that @obj is synced wrt. any related writes.
549 * Return: %0 on success or -ENODEV if the type of @obj's backing store is
552 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
554 GEM_BUG_ON(overflows_type(offset >> PAGE_SHIFT, pgoff_t));
555 GEM_BUG_ON(offset >= obj->base.size);
556 GEM_BUG_ON(offset_in_page(offset) > PAGE_SIZE - size);
557 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
559 if (i915_gem_object_has_struct_page(obj))
560 i915_gem_object_read_from_page_kmap(obj, offset, dst, size);
561 else if (i915_gem_object_has_iomem(obj) && object_has_mappable_iomem(obj))
562 i915_gem_object_read_from_page_iomap(obj, offset, dst, size);
570 * i915_gem_object_evictable - Whether object is likely evictable after unbind.
571 * @obj: The object to check
573 * This function checks whether the object is likely unvictable after unbind.
574 * If the object is not locked when checking, the result is only advisory.
575 * If the object is locked when checking, and the function returns true,
576 * then an eviction should indeed be possible. But since unlocked vma
577 * unpinning and unbinding is currently possible, the object can actually
578 * become evictable even if this function returns false.
580 * Return: true if the object may be evictable. False otherwise.
582 bool i915_gem_object_evictable(struct drm_i915_gem_object *obj)
584 struct i915_vma *vma;
585 int pin_count = atomic_read(&obj->mm.pages_pin_count);
590 spin_lock(&obj->vma.lock);
591 list_for_each_entry(vma, &obj->vma.list, obj_link) {
592 if (i915_vma_is_pinned(vma)) {
593 spin_unlock(&obj->vma.lock);
596 if (atomic_read(&vma->pages_count))
599 spin_unlock(&obj->vma.lock);
600 GEM_WARN_ON(pin_count < 0);
602 return pin_count == 0;
606 * i915_gem_object_migratable - Whether the object is migratable out of the
608 * @obj: Pointer to the object.
610 * Return: Whether the object is allowed to be resident in other
611 * regions than the current while pages are present.
613 bool i915_gem_object_migratable(struct drm_i915_gem_object *obj)
615 struct intel_memory_region *mr = READ_ONCE(obj->mm.region);
620 return obj->mm.n_placements > 1;
624 * i915_gem_object_has_struct_page - Whether the object is page-backed
625 * @obj: The object to query.
627 * This function should only be called while the object is locked or pinned,
628 * otherwise the page backing may change under the caller.
630 * Return: True if page-backed, false otherwise.
632 bool i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
634 #ifdef CONFIG_LOCKDEP
635 if (IS_DGFX(to_i915(obj->base.dev)) &&
636 i915_gem_object_evictable((void __force *)obj))
637 assert_object_held_shared(obj);
639 return obj->mem_flags & I915_BO_FLAG_STRUCT_PAGE;
643 * i915_gem_object_has_iomem - Whether the object is iomem-backed
644 * @obj: The object to query.
646 * This function should only be called while the object is locked or pinned,
647 * otherwise the iomem backing may change under the caller.
649 * Return: True if iomem-backed, false otherwise.
651 bool i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj)
653 #ifdef CONFIG_LOCKDEP
654 if (IS_DGFX(to_i915(obj->base.dev)) &&
655 i915_gem_object_evictable((void __force *)obj))
656 assert_object_held_shared(obj);
658 return obj->mem_flags & I915_BO_FLAG_IOMEM;
662 * i915_gem_object_can_migrate - Whether an object likely can be migrated
664 * @obj: The object to migrate
665 * @id: The region intended to migrate to
667 * Check whether the object backend supports migration to the
668 * given region. Note that pinning may affect the ability to migrate as
669 * returned by this function.
671 * This function is primarily intended as a helper for checking the
672 * possibility to migrate objects and might be slightly less permissive
673 * than i915_gem_object_migrate() when it comes to objects with the
674 * I915_BO_ALLOC_USER flag set.
676 * Return: true if migration is possible, false otherwise.
678 bool i915_gem_object_can_migrate(struct drm_i915_gem_object *obj,
679 enum intel_region_id id)
681 struct drm_i915_private *i915 = to_i915(obj->base.dev);
682 unsigned int num_allowed = obj->mm.n_placements;
683 struct intel_memory_region *mr;
686 GEM_BUG_ON(id >= INTEL_REGION_UNKNOWN);
687 GEM_BUG_ON(obj->mm.madv != I915_MADV_WILLNEED);
689 mr = i915->mm.regions[id];
693 if (!IS_ALIGNED(obj->base.size, mr->min_page_size))
696 if (obj->mm.region == mr)
699 if (!i915_gem_object_evictable(obj))
702 if (!obj->ops->migrate)
705 if (!(obj->flags & I915_BO_ALLOC_USER))
708 if (num_allowed == 0)
711 for (i = 0; i < num_allowed; ++i) {
712 if (mr == obj->mm.placements[i])
720 * i915_gem_object_migrate - Migrate an object to the desired region id
721 * @obj: The object to migrate.
722 * @ww: An optional struct i915_gem_ww_ctx. If NULL, the backend may
723 * not be successful in evicting other objects to make room for this object.
724 * @id: The region id to migrate to.
726 * Attempt to migrate the object to the desired memory region. The
727 * object backend must support migration and the object may not be
728 * pinned, (explicitly pinned pages or pinned vmas). The object must
730 * On successful completion, the object will have pages pointing to
731 * memory in the new region, but an async migration task may not have
732 * completed yet, and to accomplish that, i915_gem_object_wait_migration()
735 * Note: the @ww parameter is not used yet, but included to make sure
736 * callers put some effort into obtaining a valid ww ctx if one is
739 * Return: 0 on success. Negative error code on failure. In particular may
740 * return -ENXIO on lack of region space, -EDEADLK for deadlock avoidance
741 * if @ww is set, -EINTR or -ERESTARTSYS if signal pending, and
742 * -EBUSY if the object is pinned.
744 int i915_gem_object_migrate(struct drm_i915_gem_object *obj,
745 struct i915_gem_ww_ctx *ww,
746 enum intel_region_id id)
748 return __i915_gem_object_migrate(obj, ww, id, obj->flags);
752 * __i915_gem_object_migrate - Migrate an object to the desired region id, with
753 * control of the extra flags
754 * @obj: The object to migrate.
755 * @ww: An optional struct i915_gem_ww_ctx. If NULL, the backend may
756 * not be successful in evicting other objects to make room for this object.
757 * @id: The region id to migrate to.
758 * @flags: The object flags. Normally just obj->flags.
760 * Attempt to migrate the object to the desired memory region. The
761 * object backend must support migration and the object may not be
762 * pinned, (explicitly pinned pages or pinned vmas). The object must
764 * On successful completion, the object will have pages pointing to
765 * memory in the new region, but an async migration task may not have
766 * completed yet, and to accomplish that, i915_gem_object_wait_migration()
769 * Note: the @ww parameter is not used yet, but included to make sure
770 * callers put some effort into obtaining a valid ww ctx if one is
773 * Return: 0 on success. Negative error code on failure. In particular may
774 * return -ENXIO on lack of region space, -EDEADLK for deadlock avoidance
775 * if @ww is set, -EINTR or -ERESTARTSYS if signal pending, and
776 * -EBUSY if the object is pinned.
778 int __i915_gem_object_migrate(struct drm_i915_gem_object *obj,
779 struct i915_gem_ww_ctx *ww,
780 enum intel_region_id id,
783 struct drm_i915_private *i915 = to_i915(obj->base.dev);
784 struct intel_memory_region *mr;
786 GEM_BUG_ON(id >= INTEL_REGION_UNKNOWN);
787 GEM_BUG_ON(obj->mm.madv != I915_MADV_WILLNEED);
788 assert_object_held(obj);
790 mr = i915->mm.regions[id];
793 if (!i915_gem_object_can_migrate(obj, id))
796 if (!obj->ops->migrate) {
797 if (GEM_WARN_ON(obj->mm.region != mr))
802 return obj->ops->migrate(obj, mr, flags);
806 * i915_gem_object_placement_possible - Check whether the object can be
807 * placed at certain memory type
808 * @obj: Pointer to the object
809 * @type: The memory type to check
811 * Return: True if the object can be placed in @type. False otherwise.
813 bool i915_gem_object_placement_possible(struct drm_i915_gem_object *obj,
814 enum intel_memory_type type)
818 if (!obj->mm.n_placements) {
820 case INTEL_MEMORY_LOCAL:
821 return i915_gem_object_has_iomem(obj);
822 case INTEL_MEMORY_SYSTEM:
823 return i915_gem_object_has_pages(obj);
825 /* Ignore stolen for now */
831 for (i = 0; i < obj->mm.n_placements; i++) {
832 if (obj->mm.placements[i]->type == type)
840 * i915_gem_object_needs_ccs_pages - Check whether the object requires extra
841 * pages when placed in system-memory, in order to save and later restore the
842 * flat-CCS aux state when the object is moved between local-memory and
844 * @obj: Pointer to the object
846 * Return: True if the object needs extra ccs pages. False otherwise.
848 bool i915_gem_object_needs_ccs_pages(struct drm_i915_gem_object *obj)
850 bool lmem_placement = false;
853 if (!HAS_FLAT_CCS(to_i915(obj->base.dev)))
856 if (obj->flags & I915_BO_ALLOC_CCS_AUX)
859 for (i = 0; i < obj->mm.n_placements; i++) {
860 /* Compression is not allowed for the objects with smem placement */
861 if (obj->mm.placements[i]->type == INTEL_MEMORY_SYSTEM)
863 if (!lmem_placement &&
864 obj->mm.placements[i]->type == INTEL_MEMORY_LOCAL)
865 lmem_placement = true;
868 return lmem_placement;
871 void i915_gem_init__objects(struct drm_i915_private *i915)
873 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
876 void i915_objects_module_exit(void)
878 kmem_cache_destroy(slab_objects);
881 int __init i915_objects_module_init(void)
883 slab_objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
890 static const struct drm_gem_object_funcs i915_gem_object_funcs = {
891 .free = i915_gem_free_object,
892 .close = i915_gem_close_object,
893 .export = i915_gem_prime_export,
897 * i915_gem_object_get_moving_fence - Get the object's moving fence if any
898 * @obj: The object whose moving fence to get.
899 * @fence: The resulting fence
901 * A non-signaled moving fence means that there is an async operation
902 * pending on the object that needs to be waited on before setting up
903 * any GPU- or CPU PTEs to the object's pages.
905 * Return: Negative error code or 0 for success.
907 int i915_gem_object_get_moving_fence(struct drm_i915_gem_object *obj,
908 struct dma_fence **fence)
910 return dma_resv_get_singleton(obj->base.resv, DMA_RESV_USAGE_KERNEL,
915 * i915_gem_object_wait_moving_fence - Wait for the object's moving fence if any
916 * @obj: The object whose moving fence to wait for.
917 * @intr: Whether to wait interruptible.
919 * If the moving fence signaled without an error, it is detached from the
922 * Return: 0 if successful, -ERESTARTSYS if the wait was interrupted,
923 * negative error code if the async operation represented by the
924 * moving fence failed.
926 int i915_gem_object_wait_moving_fence(struct drm_i915_gem_object *obj,
931 assert_object_held(obj);
933 ret = dma_resv_wait_timeout(obj->base. resv, DMA_RESV_USAGE_KERNEL,
934 intr, MAX_SCHEDULE_TIMEOUT);
937 else if (ret > 0 && i915_gem_object_has_unknown_state(obj))
940 return ret < 0 ? ret : 0;
944 * i915_gem_object_has_unknown_state - Return true if the object backing pages are
945 * in an unknown_state. This means that userspace must NEVER be allowed to touch
946 * the pages, with either the GPU or CPU.
948 * ONLY valid to be called after ensuring that all kernel fences have signalled
949 * (in particular the fence for moving/clearing the object).
951 bool i915_gem_object_has_unknown_state(struct drm_i915_gem_object *obj)
954 * The below barrier pairs with the dma_fence_signal() in
955 * __memcpy_work(). We should only sample the unknown_state after all
956 * the kernel fences have signalled.
959 return obj->mm.unknown_state;
962 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
963 #include "selftests/huge_gem_object.c"
964 #include "selftests/huge_pages.c"
965 #include "selftests/i915_gem_migrate.c"
966 #include "selftests/i915_gem_object.c"
967 #include "selftests/i915_gem_coherency.c"