bool i915_gem_object_has_cache_level(conststruct drm_i915_gem_object *obj, enum i915_cache_level lvl)
{ /* * In case the pat_index is set by user space, this kernel mode * driver should leave the coherency to be managed by user space, * simply return true here.
*/ if (obj->pat_set_by_user) returntrue;
/* * Otherwise the pat_index should have been converted from cache_level * so that the following comparison is valid.
*/ return obj->pat_index == i915_gem_get_pat_index(obj_to_i915(obj), lvl);
}
void i915_gem_object_init(struct drm_i915_gem_object *obj, conststruct drm_i915_gem_object_ops *ops, struct lock_class_key *key, unsigned flags)
{ /* * A gem object is embedded both in a struct ttm_buffer_object :/ and * in a drm_i915_gem_object. Make sure they are aliased.
*/
BUILD_BUG_ON(offsetof(typeof(*obj), base) !=
offsetof(typeof(*obj), __do_not_access.base));
/** * __i915_gem_object_fini - Clean up a GEM object initialization * @obj: The gem object to cleanup * * This function cleans up gem object fields that are set up by * drm_gem_private_object_init() and i915_gem_object_init(). * It's primarily intended as a helper for backends that need to * clean up the gem object in separate steps.
*/ void __i915_gem_object_fini(struct drm_i915_gem_object *obj)
{
mutex_destroy(&obj->mm.get_page.lock);
mutex_destroy(&obj->mm.get_dma_page.lock);
dma_resv_fini(&obj->base._resv);
}
/** * i915_gem_object_set_cache_coherency - Mark up the object's coherency levels * for a given cache_level * @obj: #drm_i915_gem_object * @cache_level: cache level
*/ void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj, unsignedint cache_level)
{ struct drm_i915_private *i915 = to_i915(obj->base.dev);
/** * i915_gem_object_set_pat_index - set PAT index to be used in PTE encode * @obj: #drm_i915_gem_object * @pat_index: PAT index * * This is a clone of i915_gem_object_set_cache_coherency taking pat index * instead of cache_level as its second argument.
*/ void i915_gem_object_set_pat_index(struct drm_i915_gem_object *obj, unsignedint pat_index)
{ struct drm_i915_private *i915 = to_i915(obj->base.dev);
/* * This is purely from a security perspective, so we simply don't care * about non-userspace objects being able to bypass the LLC.
*/ if (!(obj->flags & I915_BO_ALLOC_USER)) returnfalse;
/* * Always flush cache for UMD objects at creation time.
*/ if (obj->pat_set_by_user) returntrue;
/* * EHL and JSL add the 'Bypass LLC' MOCS entry, which should make it * possible for userspace to bypass the GTT caching bits set by the * kernel, as per the given object cache_level. This is troublesome * since the heavy flush we apply when first gathering the pages is * skipped if the kernel thinks the object is coherent with the GPU. As * a result it might be possible to bypass the cache and read the * contents of the page directly, which could be stale data. If it's * just a case of userspace shooting themselves in the foot then so be * it, but since i915 takes the stance of always zeroing memory before * handing it to userspace, we need to prevent this.
*/ return (IS_JASPERLAKE(i915) || IS_ELKHARTLAKE(i915));
}
/* Break long locks, and carefully continue on from this spot */ if (&ln->obj_link != &obj->lut_list) {
list_add_tail(&bookmark.obj_link, &ln->obj_link); if (cond_resched_lock(&obj->lut_lock))
list_safe_reset_next(&bookmark, ln, obj_link);
__list_del_entry(&bookmark.obj_link);
}
}
spin_unlock(&obj->lut_lock);
/** * __i915_gem_object_pages_fini - Clean up pages use of a gem object * @obj: The gem object to clean up * * This function cleans up usage of the object mm.pages member. It * is intended for backends that need to clean up a gem object in * separate steps and needs to be called when the object is idle before * the object's backing memory is freed.
*/ void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj)
{
assert_object_held_shared(obj);
if (!list_empty(&obj->vma.list)) { struct i915_vma *vma;
/* * dma_buf_unmap_attachment() requires reservation to be * locked. The imported GEM shouldn't share reservation lock * and ttm_bo_cleanup_memtype_use() shouldn't be invoked for * dma-buf, so it's safe to take the lock.
*/ if (obj->base.import_attach)
i915_gem_object_lock(obj, NULL);
__i915_gem_object_put_pages(obj);
if (obj->base.import_attach)
i915_gem_object_unlock(obj);
/* * Before we free the object, make sure any pure RCU-only * read-side critical sections are complete, e.g. * i915_gem_busy_ioctl(). For the corresponding synchronized * lookup see i915_gem_object_lookup_rcu().
*/
atomic_inc(&i915->mm.free_count);
/* * Since we require blocking on struct_mutex to unbind the freed * object from the GPU before releasing resources back to the * system, we can not do that directly from the RCU callback (which may * be a softirq context), but must instead then defer that work onto a * kthread. We use the RCU callback rather than move the freed object * directly onto the work queue so that we can mix between using the * worker and performing frees directly from subsequent allocations for * crude but effective memory throttling.
*/
if (llist_add(&obj->freed, &i915->mm.free_list))
queue_work(i915->wq, &i915->mm.free_work);
}
if (IS_DGFX(to_i915(obj->base.dev))) return i915_ttm_resource_mappable(i915_gem_to_ttm(obj)->resource);
returntrue;
}
/** * i915_gem_object_read_from_page - read data from the page of a GEM object * @obj: GEM object to read from * @offset: offset within the object * @dst: buffer to store the read data * @size: size to read * * Reads data from @obj at the specified offset. The requested region to read * from can't cross a page boundary. The caller must ensure that @obj pages * are pinned and that @obj is synced wrt. any related writes. * * Return: %0 on success or -ENODEV if the type of @obj's backing store is * unsupported.
*/ int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
{
GEM_BUG_ON(overflows_type(offset >> PAGE_SHIFT, pgoff_t));
GEM_BUG_ON(offset >= obj->base.size);
GEM_BUG_ON(offset_in_page(offset) > PAGE_SIZE - size);
GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
/** * i915_gem_object_evictable - Whether object is likely evictable after unbind. * @obj: The object to check * * This function checks whether the object is likely unvictable after unbind. * If the object is not locked when checking, the result is only advisory. * If the object is locked when checking, and the function returns true, * then an eviction should indeed be possible. But since unlocked vma * unpinning and unbinding is currently possible, the object can actually * become evictable even if this function returns false. * * Return: true if the object may be evictable. False otherwise.
*/ bool i915_gem_object_evictable(struct drm_i915_gem_object *obj)
{ struct i915_vma *vma; int pin_count = atomic_read(&obj->mm.pages_pin_count);
if (!pin_count) returntrue;
spin_lock(&obj->vma.lock);
list_for_each_entry(vma, &obj->vma.list, obj_link) { if (i915_vma_is_pinned(vma)) {
spin_unlock(&obj->vma.lock); returnfalse;
} if (atomic_read(&vma->pages_count))
pin_count--;
}
spin_unlock(&obj->vma.lock);
GEM_WARN_ON(pin_count < 0);
return pin_count == 0;
}
/** * i915_gem_object_migratable - Whether the object is migratable out of the * current region. * @obj: Pointer to the object. * * Return: Whether the object is allowed to be resident in other * regions than the current while pages are present.
*/ bool i915_gem_object_migratable(struct drm_i915_gem_object *obj)
{ struct intel_memory_region *mr = READ_ONCE(obj->mm.region);
if (!mr) returnfalse;
return obj->mm.n_placements > 1;
}
/** * i915_gem_object_has_struct_page - Whether the object is page-backed * @obj: The object to query. * * This function should only be called while the object is locked or pinned, * otherwise the page backing may change under the caller. * * Return: True if page-backed, false otherwise.
*/ bool i915_gem_object_has_struct_page(conststruct drm_i915_gem_object *obj)
{ #ifdef CONFIG_LOCKDEP if (IS_DGFX(to_i915(obj->base.dev)) &&
i915_gem_object_evictable((void __force *)obj))
assert_object_held_shared(obj); #endif return obj->mem_flags & I915_BO_FLAG_STRUCT_PAGE;
}
/** * i915_gem_object_has_iomem - Whether the object is iomem-backed * @obj: The object to query. * * This function should only be called while the object is locked or pinned, * otherwise the iomem backing may change under the caller. * * Return: True if iomem-backed, false otherwise.
*/ bool i915_gem_object_has_iomem(conststruct drm_i915_gem_object *obj)
{ #ifdef CONFIG_LOCKDEP if (IS_DGFX(to_i915(obj->base.dev)) &&
i915_gem_object_evictable((void __force *)obj))
assert_object_held_shared(obj); #endif return obj->mem_flags & I915_BO_FLAG_IOMEM;
}
/** * i915_gem_object_can_migrate - Whether an object likely can be migrated * * @obj: The object to migrate * @id: The region intended to migrate to * * Check whether the object backend supports migration to the * given region. Note that pinning may affect the ability to migrate as * returned by this function. * * This function is primarily intended as a helper for checking the * possibility to migrate objects and might be slightly less permissive * than i915_gem_object_migrate() when it comes to objects with the * I915_BO_ALLOC_USER flag set. * * Return: true if migration is possible, false otherwise.
*/ bool i915_gem_object_can_migrate(struct drm_i915_gem_object *obj, enum intel_region_id id)
{ struct drm_i915_private *i915 = to_i915(obj->base.dev); unsignedint num_allowed = obj->mm.n_placements; struct intel_memory_region *mr; unsignedint i;
if (!IS_ALIGNED(obj->base.size, mr->min_page_size)) returnfalse;
if (obj->mm.region == mr) returntrue;
if (!i915_gem_object_evictable(obj)) returnfalse;
if (!obj->ops->migrate) returnfalse;
if (!(obj->flags & I915_BO_ALLOC_USER)) returntrue;
if (num_allowed == 0) returnfalse;
for (i = 0; i < num_allowed; ++i) { if (mr == obj->mm.placements[i]) returntrue;
}
returnfalse;
}
/** * i915_gem_object_migrate - Migrate an object to the desired region id * @obj: The object to migrate. * @ww: An optional struct i915_gem_ww_ctx. If NULL, the backend may * not be successful in evicting other objects to make room for this object. * @id: The region id to migrate to. * * Attempt to migrate the object to the desired memory region. The * object backend must support migration and the object may not be * pinned, (explicitly pinned pages or pinned vmas). The object must * be locked. * On successful completion, the object will have pages pointing to * memory in the new region, but an async migration task may not have * completed yet, and to accomplish that, i915_gem_object_wait_migration() * must be called. * * Note: the @ww parameter is not used yet, but included to make sure * callers put some effort into obtaining a valid ww ctx if one is * available. * * Return: 0 on success. Negative error code on failure. In particular may * return -ENXIO on lack of region space, -EDEADLK for deadlock avoidance * if @ww is set, -EINTR or -ERESTARTSYS if signal pending, and * -EBUSY if the object is pinned.
*/ int i915_gem_object_migrate(struct drm_i915_gem_object *obj, struct i915_gem_ww_ctx *ww, enum intel_region_id id)
{ return __i915_gem_object_migrate(obj, ww, id, obj->flags);
}
/** * __i915_gem_object_migrate - Migrate an object to the desired region id, with * control of the extra flags * @obj: The object to migrate. * @ww: An optional struct i915_gem_ww_ctx. If NULL, the backend may * not be successful in evicting other objects to make room for this object. * @id: The region id to migrate to. * @flags: The object flags. Normally just obj->flags. * * Attempt to migrate the object to the desired memory region. The * object backend must support migration and the object may not be * pinned, (explicitly pinned pages or pinned vmas). The object must * be locked. * On successful completion, the object will have pages pointing to * memory in the new region, but an async migration task may not have * completed yet, and to accomplish that, i915_gem_object_wait_migration() * must be called. * * Note: the @ww parameter is not used yet, but included to make sure * callers put some effort into obtaining a valid ww ctx if one is * available. * * Return: 0 on success. Negative error code on failure. In particular may * return -ENXIO on lack of region space, -EDEADLK for deadlock avoidance * if @ww is set, -EINTR or -ERESTARTSYS if signal pending, and * -EBUSY if the object is pinned.
*/ int __i915_gem_object_migrate(struct drm_i915_gem_object *obj, struct i915_gem_ww_ctx *ww, enum intel_region_id id, unsignedint flags)
{ struct drm_i915_private *i915 = to_i915(obj->base.dev); struct intel_memory_region *mr;
if (!i915_gem_object_can_migrate(obj, id)) return -EINVAL;
if (!obj->ops->migrate) { if (GEM_WARN_ON(obj->mm.region != mr)) return -EINVAL; return 0;
}
return obj->ops->migrate(obj, mr, flags);
}
/** * i915_gem_object_placement_possible - Check whether the object can be * placed at certain memory type * @obj: Pointer to the object * @type: The memory type to check * * Return: True if the object can be placed in @type. False otherwise.
*/ bool i915_gem_object_placement_possible(struct drm_i915_gem_object *obj, enum intel_memory_type type)
{ unsignedint i;
if (!obj->mm.n_placements) { switch (type) { case INTEL_MEMORY_LOCAL: return i915_gem_object_has_iomem(obj); case INTEL_MEMORY_SYSTEM: return i915_gem_object_has_pages(obj); default: /* Ignore stolen for now */
GEM_BUG_ON(1); returnfalse;
}
}
for (i = 0; i < obj->mm.n_placements; i++) { if (obj->mm.placements[i]->type == type) returntrue;
}
returnfalse;
}
/** * i915_gem_object_needs_ccs_pages - Check whether the object requires extra * pages when placed in system-memory, in order to save and later restore the * flat-CCS aux state when the object is moved between local-memory and * system-memory * @obj: Pointer to the object * * Return: True if the object needs extra ccs pages. False otherwise.
*/ bool i915_gem_object_needs_ccs_pages(struct drm_i915_gem_object *obj)
{ bool lmem_placement = false; int i;
if (!HAS_FLAT_CCS(to_i915(obj->base.dev))) returnfalse;
if (obj->flags & I915_BO_ALLOC_CCS_AUX) returntrue;
for (i = 0; i < obj->mm.n_placements; i++) { /* Compression is not allowed for the objects with smem placement */ if (obj->mm.placements[i]->type == INTEL_MEMORY_SYSTEM) returnfalse; if (!lmem_placement &&
obj->mm.placements[i]->type == INTEL_MEMORY_LOCAL)
lmem_placement = true;
}
/** * i915_gem_object_get_moving_fence - Get the object's moving fence if any * @obj: The object whose moving fence to get. * @fence: The resulting fence * * A non-signaled moving fence means that there is an async operation * pending on the object that needs to be waited on before setting up * any GPU- or CPU PTEs to the object's pages. * * Return: Negative error code or 0 for success.
*/ int i915_gem_object_get_moving_fence(struct drm_i915_gem_object *obj, struct dma_fence **fence)
{ return dma_resv_get_singleton(obj->base.resv, DMA_RESV_USAGE_KERNEL,
fence);
}
/** * i915_gem_object_wait_moving_fence - Wait for the object's moving fence if any * @obj: The object whose moving fence to wait for. * @intr: Whether to wait interruptible. * * If the moving fence signaled without an error, it is detached from the * object and put. * * Return: 0 if successful, -ERESTARTSYS if the wait was interrupted, * negative error code if the async operation represented by the * moving fence failed.
*/ int i915_gem_object_wait_moving_fence(struct drm_i915_gem_object *obj, bool intr)
{ long ret;
assert_object_held(obj);
ret = dma_resv_wait_timeout(obj->base. resv, DMA_RESV_USAGE_KERNEL,
intr, MAX_SCHEDULE_TIMEOUT); if (!ret)
ret = -ETIME; elseif (ret > 0 && i915_gem_object_has_unknown_state(obj))
ret = -EIO;
return ret < 0 ? ret : 0;
}
/* * i915_gem_object_has_unknown_state - Return true if the object backing pages are * in an unknown_state. This means that userspace must NEVER be allowed to touch * the pages, with either the GPU or CPU. * * ONLY valid to be called after ensuring that all kernel fences have signalled * (in particular the fence for moving/clearing the object).
*/ bool i915_gem_object_has_unknown_state(struct drm_i915_gem_object *obj)
{ /* * The below barrier pairs with the dma_fence_signal() in * __memcpy_work(). We should only sample the unknown_state after all * the kernel fences have signalled.
*/
smp_rmb(); return obj->mm.unknown_state;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.