198932149SChris Wilson /* 298932149SChris Wilson * SPDX-License-Identifier: MIT 398932149SChris Wilson * 498932149SChris Wilson * Copyright © 2016 Intel Corporation 598932149SChris Wilson */ 698932149SChris Wilson 798932149SChris Wilson #ifndef __I915_GEM_OBJECT_H__ 898932149SChris Wilson #define __I915_GEM_OBJECT_H__ 998932149SChris Wilson 1098932149SChris Wilson #include <drm/drm_gem.h> 1198932149SChris Wilson #include <drm/drm_file.h> 1298932149SChris Wilson #include <drm/drm_device.h> 1398932149SChris Wilson 14da42104fSChris Wilson #include "display/intel_frontbuffer.h" 1598932149SChris Wilson #include "i915_gem_object_types.h" 16f0e4a063SChris Wilson #include "i915_gem_gtt.h" 172c86e55dSMatthew Auld #include "i915_vma_types.h" 18f0e4a063SChris Wilson 198475355fSChris Wilson void i915_gem_init__objects(struct drm_i915_private *i915); 208475355fSChris Wilson 2198932149SChris Wilson struct drm_i915_gem_object *i915_gem_object_alloc(void); 2298932149SChris Wilson void i915_gem_object_free(struct drm_i915_gem_object *obj); 2398932149SChris Wilson 248475355fSChris Wilson void i915_gem_object_init(struct drm_i915_gem_object *obj, 257867d709SChris Wilson const struct drm_i915_gem_object_ops *ops, 267867d709SChris Wilson struct lock_class_key *key); 278475355fSChris Wilson struct drm_i915_gem_object * 28da1184cdSMatthew Auld i915_gem_object_create_shmem(struct drm_i915_private *i915, 29da1184cdSMatthew Auld resource_size_t size); 308475355fSChris Wilson struct drm_i915_gem_object * 318475355fSChris Wilson i915_gem_object_create_shmem_from_data(struct drm_i915_private *i915, 32da1184cdSMatthew Auld const void *data, resource_size_t size); 338475355fSChris Wilson 348475355fSChris Wilson extern const struct drm_i915_gem_object_ops i915_gem_shmem_ops; 358475355fSChris Wilson void __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, 368475355fSChris Wilson struct sg_table *pages, 378475355fSChris Wilson bool needs_clflush); 388475355fSChris Wilson 39f033428dSChris Wilson int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align); 40f033428dSChris Wilson 418475355fSChris Wilson void i915_gem_flush_free_objects(struct drm_i915_private *i915); 428475355fSChris Wilson 43f033428dSChris Wilson struct sg_table * 44f033428dSChris Wilson __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj); 45f033428dSChris Wilson void i915_gem_object_truncate(struct drm_i915_gem_object *obj); 46f033428dSChris Wilson 4798932149SChris Wilson /** 4898932149SChris Wilson * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle 4998932149SChris Wilson * @filp: DRM file private date 5098932149SChris Wilson * @handle: userspace handle 5198932149SChris Wilson * 5298932149SChris Wilson * Returns: 5398932149SChris Wilson * 5498932149SChris Wilson * A pointer to the object named by the handle if such exists on @filp, NULL 5598932149SChris Wilson * otherwise. This object is only valid whilst under the RCU read lock, and 5698932149SChris Wilson * note carefully the object may be in the process of being destroyed. 5798932149SChris Wilson */ 5898932149SChris Wilson static inline struct drm_i915_gem_object * 5998932149SChris Wilson i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle) 6098932149SChris Wilson { 6198932149SChris Wilson #ifdef CONFIG_LOCKDEP 6298932149SChris Wilson WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map)); 6398932149SChris Wilson #endif 6498932149SChris Wilson return idr_find(&file->object_idr, handle); 6598932149SChris Wilson } 6698932149SChris Wilson 6798932149SChris Wilson static inline struct drm_i915_gem_object * 68280d14a6SChris Wilson i915_gem_object_get_rcu(struct drm_i915_gem_object *obj) 69280d14a6SChris Wilson { 70280d14a6SChris Wilson if (obj && !kref_get_unless_zero(&obj->base.refcount)) 71280d14a6SChris Wilson obj = NULL; 72280d14a6SChris Wilson 73280d14a6SChris Wilson return obj; 74280d14a6SChris Wilson } 75280d14a6SChris Wilson 76280d14a6SChris Wilson static inline struct drm_i915_gem_object * 7798932149SChris Wilson i915_gem_object_lookup(struct drm_file *file, u32 handle) 7898932149SChris Wilson { 7998932149SChris Wilson struct drm_i915_gem_object *obj; 8098932149SChris Wilson 8198932149SChris Wilson rcu_read_lock(); 8298932149SChris Wilson obj = i915_gem_object_lookup_rcu(file, handle); 83280d14a6SChris Wilson obj = i915_gem_object_get_rcu(obj); 8498932149SChris Wilson rcu_read_unlock(); 8598932149SChris Wilson 8698932149SChris Wilson return obj; 8798932149SChris Wilson } 8898932149SChris Wilson 8998932149SChris Wilson __deprecated 90b5893ffcSJanusz Krzysztofik struct drm_gem_object * 9198932149SChris Wilson drm_gem_object_lookup(struct drm_file *file, u32 handle); 9298932149SChris Wilson 9398932149SChris Wilson __attribute__((nonnull)) 9498932149SChris Wilson static inline struct drm_i915_gem_object * 9598932149SChris Wilson i915_gem_object_get(struct drm_i915_gem_object *obj) 9698932149SChris Wilson { 9798932149SChris Wilson drm_gem_object_get(&obj->base); 9898932149SChris Wilson return obj; 9998932149SChris Wilson } 10098932149SChris Wilson 10198932149SChris Wilson __attribute__((nonnull)) 10298932149SChris Wilson static inline void 10398932149SChris Wilson i915_gem_object_put(struct drm_i915_gem_object *obj) 10498932149SChris Wilson { 10598932149SChris Wilson __drm_gem_object_put(&obj->base); 10698932149SChris Wilson } 10798932149SChris Wilson 10852791eeeSChristian König #define assert_object_held(obj) dma_resv_assert_held((obj)->base.resv) 1096951e589SChris Wilson 11080f0b679SMaarten Lankhorst static inline int __i915_gem_object_lock(struct drm_i915_gem_object *obj, 11180f0b679SMaarten Lankhorst struct i915_gem_ww_ctx *ww, 11280f0b679SMaarten Lankhorst bool intr) 11398932149SChris Wilson { 11480f0b679SMaarten Lankhorst int ret; 11580f0b679SMaarten Lankhorst 11680f0b679SMaarten Lankhorst if (intr) 11780f0b679SMaarten Lankhorst ret = dma_resv_lock_interruptible(obj->base.resv, ww ? &ww->ctx : NULL); 11880f0b679SMaarten Lankhorst else 11980f0b679SMaarten Lankhorst ret = dma_resv_lock(obj->base.resv, ww ? &ww->ctx : NULL); 12080f0b679SMaarten Lankhorst 12180f0b679SMaarten Lankhorst if (!ret && ww) 12280f0b679SMaarten Lankhorst list_add_tail(&obj->obj_link, &ww->obj_list); 12380f0b679SMaarten Lankhorst if (ret == -EALREADY) 12480f0b679SMaarten Lankhorst ret = 0; 12580f0b679SMaarten Lankhorst 12680f0b679SMaarten Lankhorst if (ret == -EDEADLK) 12780f0b679SMaarten Lankhorst ww->contended = obj; 12880f0b679SMaarten Lankhorst 12980f0b679SMaarten Lankhorst return ret; 13080f0b679SMaarten Lankhorst } 13180f0b679SMaarten Lankhorst 13280f0b679SMaarten Lankhorst static inline int i915_gem_object_lock(struct drm_i915_gem_object *obj, 13380f0b679SMaarten Lankhorst struct i915_gem_ww_ctx *ww) 13480f0b679SMaarten Lankhorst { 13580f0b679SMaarten Lankhorst return __i915_gem_object_lock(obj, ww, ww && ww->intr); 13680f0b679SMaarten Lankhorst } 13780f0b679SMaarten Lankhorst 13880f0b679SMaarten Lankhorst static inline int i915_gem_object_lock_interruptible(struct drm_i915_gem_object *obj, 13980f0b679SMaarten Lankhorst struct i915_gem_ww_ctx *ww) 14080f0b679SMaarten Lankhorst { 14180f0b679SMaarten Lankhorst WARN_ON(ww && !ww->intr); 14280f0b679SMaarten Lankhorst return __i915_gem_object_lock(obj, ww, true); 14398932149SChris Wilson } 14498932149SChris Wilson 1452850748eSChris Wilson static inline bool i915_gem_object_trylock(struct drm_i915_gem_object *obj) 1462850748eSChris Wilson { 1472850748eSChris Wilson return dma_resv_trylock(obj->base.resv); 1482850748eSChris Wilson } 1492850748eSChris Wilson 15098932149SChris Wilson static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj) 15198932149SChris Wilson { 15252791eeeSChristian König dma_resv_unlock(obj->base.resv); 15398932149SChris Wilson } 15498932149SChris Wilson 1556951e589SChris Wilson struct dma_fence * 1566951e589SChris Wilson i915_gem_object_lock_fence(struct drm_i915_gem_object *obj); 1576951e589SChris Wilson void i915_gem_object_unlock_fence(struct drm_i915_gem_object *obj, 1586951e589SChris Wilson struct dma_fence *fence); 1596951e589SChris Wilson 16098932149SChris Wilson static inline void 16198932149SChris Wilson i915_gem_object_set_readonly(struct drm_i915_gem_object *obj) 16298932149SChris Wilson { 163cc662126SAbdiel Janulgue obj->flags |= I915_BO_READONLY; 16498932149SChris Wilson } 16598932149SChris Wilson 16698932149SChris Wilson static inline bool 16798932149SChris Wilson i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj) 16898932149SChris Wilson { 169cc662126SAbdiel Janulgue return obj->flags & I915_BO_READONLY; 17098932149SChris Wilson } 17198932149SChris Wilson 17298932149SChris Wilson static inline bool 1732f0b97caSMatthew Auld i915_gem_object_is_contiguous(const struct drm_i915_gem_object *obj) 1742f0b97caSMatthew Auld { 1752f0b97caSMatthew Auld return obj->flags & I915_BO_ALLOC_CONTIGUOUS; 1762f0b97caSMatthew Auld } 1772f0b97caSMatthew Auld 1782f0b97caSMatthew Auld static inline bool 1797c98501aSMatthew Auld i915_gem_object_is_volatile(const struct drm_i915_gem_object *obj) 1807c98501aSMatthew Auld { 1817c98501aSMatthew Auld return obj->flags & I915_BO_ALLOC_VOLATILE; 1827c98501aSMatthew Auld } 1837c98501aSMatthew Auld 1847c98501aSMatthew Auld static inline void 1857c98501aSMatthew Auld i915_gem_object_set_volatile(struct drm_i915_gem_object *obj) 1867c98501aSMatthew Auld { 1877c98501aSMatthew Auld obj->flags |= I915_BO_ALLOC_VOLATILE; 1887c98501aSMatthew Auld } 1897c98501aSMatthew Auld 1907c98501aSMatthew Auld static inline bool 1910175969eSChris Wilson i915_gem_object_has_tiling_quirk(struct drm_i915_gem_object *obj) 1920175969eSChris Wilson { 1930175969eSChris Wilson return test_bit(I915_TILING_QUIRK_BIT, &obj->flags); 1940175969eSChris Wilson } 1950175969eSChris Wilson 1960175969eSChris Wilson static inline void 1970175969eSChris Wilson i915_gem_object_set_tiling_quirk(struct drm_i915_gem_object *obj) 1980175969eSChris Wilson { 1990175969eSChris Wilson set_bit(I915_TILING_QUIRK_BIT, &obj->flags); 2000175969eSChris Wilson } 2010175969eSChris Wilson 2020175969eSChris Wilson static inline void 2030175969eSChris Wilson i915_gem_object_clear_tiling_quirk(struct drm_i915_gem_object *obj) 2040175969eSChris Wilson { 2050175969eSChris Wilson clear_bit(I915_TILING_QUIRK_BIT, &obj->flags); 2060175969eSChris Wilson } 2070175969eSChris Wilson 2080175969eSChris Wilson static inline bool 2093cbad5d7SChris Wilson i915_gem_object_type_has(const struct drm_i915_gem_object *obj, 2103cbad5d7SChris Wilson unsigned long flags) 2113cbad5d7SChris Wilson { 2123cbad5d7SChris Wilson return obj->ops->flags & flags; 2133cbad5d7SChris Wilson } 2143cbad5d7SChris Wilson 2153cbad5d7SChris Wilson static inline bool 21698932149SChris Wilson i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj) 21798932149SChris Wilson { 2183cbad5d7SChris Wilson return i915_gem_object_type_has(obj, I915_GEM_OBJECT_HAS_STRUCT_PAGE); 21998932149SChris Wilson } 22098932149SChris Wilson 22198932149SChris Wilson static inline bool 2225fbc2c2bSImre Deak i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj) 2235fbc2c2bSImre Deak { 2245fbc2c2bSImre Deak return i915_gem_object_type_has(obj, I915_GEM_OBJECT_HAS_IOMEM); 2255fbc2c2bSImre Deak } 2265fbc2c2bSImre Deak 2275fbc2c2bSImre Deak static inline bool 22898932149SChris Wilson i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj) 22998932149SChris Wilson { 2303cbad5d7SChris Wilson return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_SHRINKABLE); 23198932149SChris Wilson } 23298932149SChris Wilson 23398932149SChris Wilson static inline bool 23498932149SChris Wilson i915_gem_object_is_proxy(const struct drm_i915_gem_object *obj) 23598932149SChris Wilson { 2363cbad5d7SChris Wilson return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_PROXY); 23798932149SChris Wilson } 23898932149SChris Wilson 23998932149SChris Wilson static inline bool 240f6c26b55SJanusz Krzysztofik i915_gem_object_never_mmap(const struct drm_i915_gem_object *obj) 241a4311745SChris Wilson { 242f6c26b55SJanusz Krzysztofik return i915_gem_object_type_has(obj, I915_GEM_OBJECT_NO_MMAP); 243a4311745SChris Wilson } 244a4311745SChris Wilson 245a4311745SChris Wilson static inline bool 24698932149SChris Wilson i915_gem_object_needs_async_cancel(const struct drm_i915_gem_object *obj) 24798932149SChris Wilson { 2483cbad5d7SChris Wilson return i915_gem_object_type_has(obj, I915_GEM_OBJECT_ASYNC_CANCEL); 24998932149SChris Wilson } 25098932149SChris Wilson 25198932149SChris Wilson static inline bool 25298932149SChris Wilson i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj) 25398932149SChris Wilson { 2548e7cb179SChris Wilson return READ_ONCE(obj->frontbuffer); 25598932149SChris Wilson } 25698932149SChris Wilson 25798932149SChris Wilson static inline unsigned int 25898932149SChris Wilson i915_gem_object_get_tiling(const struct drm_i915_gem_object *obj) 25998932149SChris Wilson { 26098932149SChris Wilson return obj->tiling_and_stride & TILING_MASK; 26198932149SChris Wilson } 26298932149SChris Wilson 26398932149SChris Wilson static inline bool 26498932149SChris Wilson i915_gem_object_is_tiled(const struct drm_i915_gem_object *obj) 26598932149SChris Wilson { 26698932149SChris Wilson return i915_gem_object_get_tiling(obj) != I915_TILING_NONE; 26798932149SChris Wilson } 26898932149SChris Wilson 26998932149SChris Wilson static inline unsigned int 27098932149SChris Wilson i915_gem_object_get_stride(const struct drm_i915_gem_object *obj) 27198932149SChris Wilson { 27298932149SChris Wilson return obj->tiling_and_stride & STRIDE_MASK; 27398932149SChris Wilson } 27498932149SChris Wilson 27598932149SChris Wilson static inline unsigned int 27698932149SChris Wilson i915_gem_tile_height(unsigned int tiling) 27798932149SChris Wilson { 27898932149SChris Wilson GEM_BUG_ON(!tiling); 27998932149SChris Wilson return tiling == I915_TILING_Y ? 32 : 8; 28098932149SChris Wilson } 28198932149SChris Wilson 28298932149SChris Wilson static inline unsigned int 28398932149SChris Wilson i915_gem_object_get_tile_height(const struct drm_i915_gem_object *obj) 28498932149SChris Wilson { 28598932149SChris Wilson return i915_gem_tile_height(i915_gem_object_get_tiling(obj)); 28698932149SChris Wilson } 28798932149SChris Wilson 28898932149SChris Wilson static inline unsigned int 28998932149SChris Wilson i915_gem_object_get_tile_row_size(const struct drm_i915_gem_object *obj) 29098932149SChris Wilson { 29198932149SChris Wilson return (i915_gem_object_get_stride(obj) * 29298932149SChris Wilson i915_gem_object_get_tile_height(obj)); 29398932149SChris Wilson } 29498932149SChris Wilson 29598932149SChris Wilson int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj, 29698932149SChris Wilson unsigned int tiling, unsigned int stride); 29798932149SChris Wilson 29898932149SChris Wilson struct scatterlist * 299934941edSTvrtko Ursulin __i915_gem_object_get_sg(struct drm_i915_gem_object *obj, 300934941edSTvrtko Ursulin struct i915_gem_object_page_iter *iter, 301934941edSTvrtko Ursulin unsigned int n, 302*0edbb9baSMaarten Lankhorst unsigned int *offset, bool allow_alloc); 303934941edSTvrtko Ursulin 304934941edSTvrtko Ursulin static inline struct scatterlist * 30598932149SChris Wilson i915_gem_object_get_sg(struct drm_i915_gem_object *obj, 306934941edSTvrtko Ursulin unsigned int n, 307*0edbb9baSMaarten Lankhorst unsigned int *offset, bool allow_alloc) 308934941edSTvrtko Ursulin { 309*0edbb9baSMaarten Lankhorst return __i915_gem_object_get_sg(obj, &obj->mm.get_page, n, offset, allow_alloc); 310934941edSTvrtko Ursulin } 311934941edSTvrtko Ursulin 312934941edSTvrtko Ursulin static inline struct scatterlist * 313934941edSTvrtko Ursulin i915_gem_object_get_sg_dma(struct drm_i915_gem_object *obj, 314934941edSTvrtko Ursulin unsigned int n, 315*0edbb9baSMaarten Lankhorst unsigned int *offset, bool allow_alloc) 316934941edSTvrtko Ursulin { 317*0edbb9baSMaarten Lankhorst return __i915_gem_object_get_sg(obj, &obj->mm.get_dma_page, n, offset, allow_alloc); 318934941edSTvrtko Ursulin } 31998932149SChris Wilson 32098932149SChris Wilson struct page * 32198932149SChris Wilson i915_gem_object_get_page(struct drm_i915_gem_object *obj, 32298932149SChris Wilson unsigned int n); 32398932149SChris Wilson 3244993a8a3SDave Airlie struct page * 3254993a8a3SDave Airlie i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, 3264993a8a3SDave Airlie unsigned int n); 3274993a8a3SDave Airlie 32898932149SChris Wilson dma_addr_t 32998932149SChris Wilson i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj, 33098932149SChris Wilson unsigned long n, 33198932149SChris Wilson unsigned int *len); 33298932149SChris Wilson 33398932149SChris Wilson dma_addr_t 33498932149SChris Wilson i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, 33598932149SChris Wilson unsigned long n); 33698932149SChris Wilson 33798932149SChris Wilson void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, 33898932149SChris Wilson struct sg_table *pages, 33998932149SChris Wilson unsigned int sg_page_sizes); 340f033428dSChris Wilson 341f033428dSChris Wilson int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj); 34298932149SChris Wilson int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj); 34398932149SChris Wilson 34474ceefd1SDaniel Vetter enum i915_mm_subclass { /* lockdep subclass for obj->mm.lock/struct_mutex */ 34574ceefd1SDaniel Vetter I915_MM_NORMAL = 0, 34674ceefd1SDaniel Vetter /* 34774ceefd1SDaniel Vetter * Only used by struct_mutex, when called "recursively" from 34874ceefd1SDaniel Vetter * direct-reclaim-esque. Safe because there is only every one 34974ceefd1SDaniel Vetter * struct_mutex in the entire system. 35074ceefd1SDaniel Vetter */ 35174ceefd1SDaniel Vetter I915_MM_SHRINKER = 1, 35274ceefd1SDaniel Vetter /* 35374ceefd1SDaniel Vetter * Used for obj->mm.lock when allocating pages. Safe because the object 35474ceefd1SDaniel Vetter * isn't yet on any LRU, and therefore the shrinker can't deadlock on 35574ceefd1SDaniel Vetter * it. As soon as the object has pages, obj->mm.lock nests within 35674ceefd1SDaniel Vetter * fs_reclaim. 35774ceefd1SDaniel Vetter */ 35874ceefd1SDaniel Vetter I915_MM_GET_PAGES = 1, 35974ceefd1SDaniel Vetter }; 36074ceefd1SDaniel Vetter 36198932149SChris Wilson static inline int __must_check 36298932149SChris Wilson i915_gem_object_pin_pages(struct drm_i915_gem_object *obj) 36398932149SChris Wilson { 36474ceefd1SDaniel Vetter might_lock_nested(&obj->mm.lock, I915_MM_GET_PAGES); 36598932149SChris Wilson 36698932149SChris Wilson if (atomic_inc_not_zero(&obj->mm.pages_pin_count)) 36798932149SChris Wilson return 0; 36898932149SChris Wilson 36998932149SChris Wilson return __i915_gem_object_get_pages(obj); 37098932149SChris Wilson } 37198932149SChris Wilson 37298932149SChris Wilson static inline bool 37398932149SChris Wilson i915_gem_object_has_pages(struct drm_i915_gem_object *obj) 37498932149SChris Wilson { 37598932149SChris Wilson return !IS_ERR_OR_NULL(READ_ONCE(obj->mm.pages)); 37698932149SChris Wilson } 37798932149SChris Wilson 37898932149SChris Wilson static inline void 37998932149SChris Wilson __i915_gem_object_pin_pages(struct drm_i915_gem_object *obj) 38098932149SChris Wilson { 38198932149SChris Wilson GEM_BUG_ON(!i915_gem_object_has_pages(obj)); 38298932149SChris Wilson 38398932149SChris Wilson atomic_inc(&obj->mm.pages_pin_count); 38498932149SChris Wilson } 38598932149SChris Wilson 38698932149SChris Wilson static inline bool 38798932149SChris Wilson i915_gem_object_has_pinned_pages(struct drm_i915_gem_object *obj) 38898932149SChris Wilson { 38998932149SChris Wilson return atomic_read(&obj->mm.pages_pin_count); 39098932149SChris Wilson } 39198932149SChris Wilson 39298932149SChris Wilson static inline void 39398932149SChris Wilson __i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj) 39498932149SChris Wilson { 39598932149SChris Wilson GEM_BUG_ON(!i915_gem_object_has_pages(obj)); 39698932149SChris Wilson GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); 39798932149SChris Wilson 39898932149SChris Wilson atomic_dec(&obj->mm.pages_pin_count); 39998932149SChris Wilson } 40098932149SChris Wilson 40198932149SChris Wilson static inline void 40298932149SChris Wilson i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj) 40398932149SChris Wilson { 40498932149SChris Wilson __i915_gem_object_unpin_pages(obj); 40598932149SChris Wilson } 40698932149SChris Wilson 407f86dbacbSDaniel Vetter int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj); 408f033428dSChris Wilson void i915_gem_object_truncate(struct drm_i915_gem_object *obj); 409f033428dSChris Wilson void i915_gem_object_writeback(struct drm_i915_gem_object *obj); 41098932149SChris Wilson 41198932149SChris Wilson /** 41298932149SChris Wilson * i915_gem_object_pin_map - return a contiguous mapping of the entire object 41398932149SChris Wilson * @obj: the object to map into kernel address space 41498932149SChris Wilson * @type: the type of mapping, used to select pgprot_t 41598932149SChris Wilson * 41698932149SChris Wilson * Calls i915_gem_object_pin_pages() to prevent reaping of the object's 41798932149SChris Wilson * pages and then returns a contiguous mapping of the backing storage into 41898932149SChris Wilson * the kernel address space. Based on the @type of mapping, the PTE will be 41998932149SChris Wilson * set to either WriteBack or WriteCombine (via pgprot_t). 42098932149SChris Wilson * 42198932149SChris Wilson * The caller is responsible for calling i915_gem_object_unpin_map() when the 42298932149SChris Wilson * mapping is no longer required. 42398932149SChris Wilson * 42498932149SChris Wilson * Returns the pointer through which to access the mapped object, or an 42598932149SChris Wilson * ERR_PTR() on error. 42698932149SChris Wilson */ 42798932149SChris Wilson void *__must_check i915_gem_object_pin_map(struct drm_i915_gem_object *obj, 42898932149SChris Wilson enum i915_map_type type); 42998932149SChris Wilson 43098932149SChris Wilson void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj, 43198932149SChris Wilson unsigned long offset, 43298932149SChris Wilson unsigned long size); 43398932149SChris Wilson static inline void i915_gem_object_flush_map(struct drm_i915_gem_object *obj) 43498932149SChris Wilson { 43598932149SChris Wilson __i915_gem_object_flush_map(obj, 0, obj->base.size); 43698932149SChris Wilson } 43798932149SChris Wilson 43898932149SChris Wilson /** 43998932149SChris Wilson * i915_gem_object_unpin_map - releases an earlier mapping 44098932149SChris Wilson * @obj: the object to unmap 44198932149SChris Wilson * 44298932149SChris Wilson * After pinning the object and mapping its pages, once you are finished 44398932149SChris Wilson * with your access, call i915_gem_object_unpin_map() to release the pin 44498932149SChris Wilson * upon the mapping. Once the pin count reaches zero, that mapping may be 44598932149SChris Wilson * removed. 44698932149SChris Wilson */ 44798932149SChris Wilson static inline void i915_gem_object_unpin_map(struct drm_i915_gem_object *obj) 44898932149SChris Wilson { 44998932149SChris Wilson i915_gem_object_unpin_pages(obj); 45098932149SChris Wilson } 45198932149SChris Wilson 45289d19b2bSChris Wilson void __i915_gem_object_release_map(struct drm_i915_gem_object *obj); 45389d19b2bSChris Wilson 454f0e4a063SChris Wilson int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj, 455f0e4a063SChris Wilson unsigned int *needs_clflush); 456f0e4a063SChris Wilson int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj, 457f0e4a063SChris Wilson unsigned int *needs_clflush); 458f0e4a063SChris Wilson #define CLFLUSH_BEFORE BIT(0) 459f0e4a063SChris Wilson #define CLFLUSH_AFTER BIT(1) 460f0e4a063SChris Wilson #define CLFLUSH_FLAGS (CLFLUSH_BEFORE | CLFLUSH_AFTER) 461f0e4a063SChris Wilson 462f0e4a063SChris Wilson static inline void 463f0e4a063SChris Wilson i915_gem_object_finish_access(struct drm_i915_gem_object *obj) 464f0e4a063SChris Wilson { 465f0e4a063SChris Wilson i915_gem_object_unpin_pages(obj); 466f0e4a063SChris Wilson } 467f0e4a063SChris Wilson 46898932149SChris Wilson static inline struct intel_engine_cs * 46998932149SChris Wilson i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj) 47098932149SChris Wilson { 47198932149SChris Wilson struct intel_engine_cs *engine = NULL; 47298932149SChris Wilson struct dma_fence *fence; 47398932149SChris Wilson 47498932149SChris Wilson rcu_read_lock(); 47552791eeeSChristian König fence = dma_resv_get_excl_rcu(obj->base.resv); 47698932149SChris Wilson rcu_read_unlock(); 47798932149SChris Wilson 47898932149SChris Wilson if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence)) 47998932149SChris Wilson engine = to_request(fence)->engine; 48098932149SChris Wilson dma_fence_put(fence); 48198932149SChris Wilson 48298932149SChris Wilson return engine; 48398932149SChris Wilson } 48498932149SChris Wilson 48598932149SChris Wilson void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj, 48698932149SChris Wilson unsigned int cache_level); 48798932149SChris Wilson void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj); 488c1793ba8SMaarten Lankhorst void i915_gem_object_flush_if_display_locked(struct drm_i915_gem_object *obj); 48998932149SChris Wilson 490f0e4a063SChris Wilson int __must_check 491f0e4a063SChris Wilson i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write); 492f0e4a063SChris Wilson int __must_check 493f0e4a063SChris Wilson i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write); 494f0e4a063SChris Wilson int __must_check 495f0e4a063SChris Wilson i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write); 496f0e4a063SChris Wilson struct i915_vma * __must_check 497f0e4a063SChris Wilson i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, 498f0e4a063SChris Wilson u32 alignment, 499f0e4a063SChris Wilson const struct i915_ggtt_view *view, 500f0e4a063SChris Wilson unsigned int flags); 501f0e4a063SChris Wilson 5021aff1903SChris Wilson void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj); 5031aff1903SChris Wilson void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj); 5041aff1903SChris Wilson void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj); 5051aff1903SChris Wilson 5068475355fSChris Wilson static inline bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) 5078475355fSChris Wilson { 5088475355fSChris Wilson if (obj->cache_dirty) 5098475355fSChris Wilson return false; 5108475355fSChris Wilson 5118475355fSChris Wilson if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) 5128475355fSChris Wilson return true; 5138475355fSChris Wilson 5145a90606dSChris Wilson /* Currently in use by HW (display engine)? Keep flushed. */ 5155a90606dSChris Wilson return i915_gem_object_is_framebuffer(obj); 5168475355fSChris Wilson } 5178475355fSChris Wilson 5188475355fSChris Wilson static inline void __start_cpu_write(struct drm_i915_gem_object *obj) 5198475355fSChris Wilson { 5208475355fSChris Wilson obj->read_domains = I915_GEM_DOMAIN_CPU; 5218475355fSChris Wilson obj->write_domain = I915_GEM_DOMAIN_CPU; 5228475355fSChris Wilson if (cpu_write_needs_clflush(obj)) 5238475355fSChris Wilson obj->cache_dirty = true; 5248475355fSChris Wilson } 52598932149SChris Wilson 526f99e67f1SChris Wilson void i915_gem_fence_wait_priority(struct dma_fence *fence, 527f99e67f1SChris Wilson const struct i915_sched_attr *attr); 528f99e67f1SChris Wilson 529d45a1a53SChris Wilson int i915_gem_object_wait(struct drm_i915_gem_object *obj, 530d45a1a53SChris Wilson unsigned int flags, 531d45a1a53SChris Wilson long timeout); 532d45a1a53SChris Wilson int i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, 533d45a1a53SChris Wilson unsigned int flags, 534d45a1a53SChris Wilson const struct i915_sched_attr *attr); 535d45a1a53SChris Wilson 536da42104fSChris Wilson void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj, 537da42104fSChris Wilson enum fb_op_origin origin); 538da42104fSChris Wilson void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj, 539da42104fSChris Wilson enum fb_op_origin origin); 540da42104fSChris Wilson 541da42104fSChris Wilson static inline void 542da42104fSChris Wilson i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj, 543da42104fSChris Wilson enum fb_op_origin origin) 544da42104fSChris Wilson { 545da42104fSChris Wilson if (unlikely(rcu_access_pointer(obj->frontbuffer))) 546da42104fSChris Wilson __i915_gem_object_flush_frontbuffer(obj, origin); 547da42104fSChris Wilson } 548da42104fSChris Wilson 549da42104fSChris Wilson static inline void 550da42104fSChris Wilson i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj, 551da42104fSChris Wilson enum fb_op_origin origin) 552da42104fSChris Wilson { 553da42104fSChris Wilson if (unlikely(rcu_access_pointer(obj->frontbuffer))) 554da42104fSChris Wilson __i915_gem_object_invalidate_frontbuffer(obj, origin); 555da42104fSChris Wilson } 556da42104fSChris Wilson 5575fbc2c2bSImre Deak int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size); 5585fbc2c2bSImre Deak 55941a9c75dSChris Wilson bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj); 56041a9c75dSChris Wilson 56198932149SChris Wilson #endif 562