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"
15b3f450d9SMatthew Auld #include "intel_memory_region.h"
1698932149SChris Wilson #include "i915_gem_object_types.h"
17f0e4a063SChris Wilson #include "i915_gem_gtt.h"
185c43ec5dSThomas Hellström #include "i915_gem_ww.h"
192c86e55dSMatthew Auld #include "i915_vma_types.h"
20f0e4a063SChris Wilson 
21b6e913e1SThomas Hellström enum intel_region_id;
22b6e913e1SThomas Hellström 
235e352e32SFei Yang #define obj_to_i915(obj__) to_i915((obj__)->base.dev)
245e352e32SFei Yang 
i915_gem_object_size_2big(u64 size)25ae2fb480SMatthew Auld static inline bool i915_gem_object_size_2big(u64 size)
26ae2fb480SMatthew Auld {
27ae2fb480SMatthew Auld 	struct drm_i915_gem_object *obj;
28ae2fb480SMatthew Auld 
29ae2fb480SMatthew Auld 	if (overflows_type(size, obj->base.size))
30ae2fb480SMatthew Auld 		return true;
31ae2fb480SMatthew Auld 
32ae2fb480SMatthew Auld 	return false;
33ae2fb480SMatthew Auld }
34ae2fb480SMatthew Auld 
355e352e32SFei Yang unsigned int i915_gem_get_pat_index(struct drm_i915_private *i915,
365e352e32SFei Yang 				    enum i915_cache_level level);
379275277dSFei Yang bool i915_gem_object_has_cache_level(const struct drm_i915_gem_object *obj,
389275277dSFei Yang 				     enum i915_cache_level lvl);
398475355fSChris Wilson void i915_gem_init__objects(struct drm_i915_private *i915);
408475355fSChris Wilson 
41c8ad09afSDaniel Vetter void i915_objects_module_exit(void);
42c8ad09afSDaniel Vetter int i915_objects_module_init(void);
43c8ad09afSDaniel Vetter 
4498932149SChris Wilson struct drm_i915_gem_object *i915_gem_object_alloc(void);
4598932149SChris Wilson void i915_gem_object_free(struct drm_i915_gem_object *obj);
4698932149SChris Wilson 
478475355fSChris Wilson void i915_gem_object_init(struct drm_i915_gem_object *obj,
487867d709SChris Wilson 			  const struct drm_i915_gem_object_ops *ops,
49c471748dSMaarten Lankhorst 			  struct lock_class_key *key,
50c471748dSMaarten Lankhorst 			  unsigned alloc_flags);
51068396bbSThomas Hellström 
52068396bbSThomas Hellström void __i915_gem_object_fini(struct drm_i915_gem_object *obj);
53068396bbSThomas Hellström 
548475355fSChris Wilson struct drm_i915_gem_object *
55da1184cdSMatthew Auld i915_gem_object_create_shmem(struct drm_i915_private *i915,
56da1184cdSMatthew Auld 			     resource_size_t size);
578475355fSChris Wilson struct drm_i915_gem_object *
588475355fSChris Wilson i915_gem_object_create_shmem_from_data(struct drm_i915_private *i915,
59da1184cdSMatthew Auld 				       const void *data, resource_size_t size);
60bf947c98SJason Ekstrand struct drm_i915_gem_object *
61bf947c98SJason Ekstrand __i915_gem_object_create_user(struct drm_i915_private *i915, u64 size,
62bf947c98SJason Ekstrand 			      struct intel_memory_region **placements,
63bf947c98SJason Ekstrand 			      unsigned int n_placements);
648475355fSChris Wilson 
658475355fSChris Wilson extern const struct drm_i915_gem_object_ops i915_gem_shmem_ops;
66ed29c269SMaarten Lankhorst 
678475355fSChris Wilson void __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
688475355fSChris Wilson 				     struct sg_table *pages,
698475355fSChris Wilson 				     bool needs_clflush);
708475355fSChris Wilson 
71a6117097SMaarten Lankhorst int i915_gem_object_pwrite_phys(struct drm_i915_gem_object *obj,
72a6117097SMaarten Lankhorst 				const struct drm_i915_gem_pwrite *args);
73a6117097SMaarten Lankhorst int i915_gem_object_pread_phys(struct drm_i915_gem_object *obj,
74a6117097SMaarten Lankhorst 			       const struct drm_i915_gem_pread *args);
75a6117097SMaarten Lankhorst 
76f033428dSChris Wilson int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align);
77a85fffe3SMaarten Lankhorst void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj,
78a85fffe3SMaarten Lankhorst 				     struct sg_table *pages);
79a6117097SMaarten Lankhorst void i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
80a6117097SMaarten Lankhorst 				    struct sg_table *pages);
81a6117097SMaarten Lankhorst 
828475355fSChris Wilson void i915_gem_flush_free_objects(struct drm_i915_private *i915);
838475355fSChris Wilson 
84f033428dSChris Wilson struct sg_table *
85f033428dSChris Wilson __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj);
86f033428dSChris Wilson 
8798932149SChris Wilson /**
8898932149SChris Wilson  * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
89faa19ce8SJani Nikula  * @file: DRM file private date
9098932149SChris Wilson  * @handle: userspace handle
9198932149SChris Wilson  *
9298932149SChris Wilson  * Returns:
9398932149SChris Wilson  *
9498932149SChris Wilson  * A pointer to the object named by the handle if such exists on @filp, NULL
9598932149SChris Wilson  * otherwise. This object is only valid whilst under the RCU read lock, and
9698932149SChris Wilson  * note carefully the object may be in the process of being destroyed.
9798932149SChris Wilson  */
9898932149SChris Wilson static inline struct drm_i915_gem_object *
i915_gem_object_lookup_rcu(struct drm_file * file,u32 handle)9998932149SChris Wilson i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
10098932149SChris Wilson {
10198932149SChris Wilson #ifdef CONFIG_LOCKDEP
10298932149SChris Wilson 	WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
10398932149SChris Wilson #endif
10498932149SChris Wilson 	return idr_find(&file->object_idr, handle);
10598932149SChris Wilson }
10698932149SChris Wilson 
10798932149SChris Wilson static inline struct drm_i915_gem_object *
i915_gem_object_get_rcu(struct drm_i915_gem_object * obj)108280d14a6SChris Wilson i915_gem_object_get_rcu(struct drm_i915_gem_object *obj)
109280d14a6SChris Wilson {
110280d14a6SChris Wilson 	if (obj && !kref_get_unless_zero(&obj->base.refcount))
111280d14a6SChris Wilson 		obj = NULL;
112280d14a6SChris Wilson 
113280d14a6SChris Wilson 	return obj;
114280d14a6SChris Wilson }
115280d14a6SChris Wilson 
116280d14a6SChris Wilson static inline struct drm_i915_gem_object *
i915_gem_object_lookup(struct drm_file * file,u32 handle)11798932149SChris Wilson i915_gem_object_lookup(struct drm_file *file, u32 handle)
11898932149SChris Wilson {
11998932149SChris Wilson 	struct drm_i915_gem_object *obj;
12098932149SChris Wilson 
12198932149SChris Wilson 	rcu_read_lock();
12298932149SChris Wilson 	obj = i915_gem_object_lookup_rcu(file, handle);
123280d14a6SChris Wilson 	obj = i915_gem_object_get_rcu(obj);
12498932149SChris Wilson 	rcu_read_unlock();
12598932149SChris Wilson 
12698932149SChris Wilson 	return obj;
12798932149SChris Wilson }
12898932149SChris Wilson 
12998932149SChris Wilson __deprecated
130b5893ffcSJanusz Krzysztofik struct drm_gem_object *
13198932149SChris Wilson drm_gem_object_lookup(struct drm_file *file, u32 handle);
13298932149SChris Wilson 
13398932149SChris Wilson __attribute__((nonnull))
13498932149SChris Wilson static inline struct drm_i915_gem_object *
i915_gem_object_get(struct drm_i915_gem_object * obj)13598932149SChris Wilson i915_gem_object_get(struct drm_i915_gem_object *obj)
13698932149SChris Wilson {
13798932149SChris Wilson 	drm_gem_object_get(&obj->base);
13898932149SChris Wilson 	return obj;
13998932149SChris Wilson }
14098932149SChris Wilson 
14198932149SChris Wilson __attribute__((nonnull))
14298932149SChris Wilson static inline void
i915_gem_object_put(struct drm_i915_gem_object * obj)14398932149SChris Wilson i915_gem_object_put(struct drm_i915_gem_object *obj)
14498932149SChris Wilson {
14598932149SChris Wilson 	__drm_gem_object_put(&obj->base);
14698932149SChris Wilson }
14798932149SChris Wilson 
14852791eeeSChristian König #define assert_object_held(obj) dma_resv_assert_held((obj)->base.resv)
1496951e589SChris Wilson 
150a3258dbdSThomas Hellström /*
151a3258dbdSThomas Hellström  * If more than one potential simultaneous locker, assert held.
152a3258dbdSThomas Hellström  */
assert_object_held_shared(const struct drm_i915_gem_object * obj)1530ff37575SThomas Hellström static inline void assert_object_held_shared(const struct drm_i915_gem_object *obj)
154a3258dbdSThomas Hellström {
155a3258dbdSThomas Hellström 	/*
156a3258dbdSThomas Hellström 	 * Note mm list lookup is protected by
157a3258dbdSThomas Hellström 	 * kref_get_unless_zero().
158a3258dbdSThomas Hellström 	 */
159a3258dbdSThomas Hellström 	if (IS_ENABLED(CONFIG_LOCKDEP) &&
160a3258dbdSThomas Hellström 	    kref_read(&obj->base.refcount) > 0)
161cf41a8f1SMaarten Lankhorst 		assert_object_held(obj);
162a3258dbdSThomas Hellström }
163a3258dbdSThomas Hellström 
__i915_gem_object_lock(struct drm_i915_gem_object * obj,struct i915_gem_ww_ctx * ww,bool intr)16480f0b679SMaarten Lankhorst static inline int __i915_gem_object_lock(struct drm_i915_gem_object *obj,
16580f0b679SMaarten Lankhorst 					 struct i915_gem_ww_ctx *ww,
16680f0b679SMaarten Lankhorst 					 bool intr)
16798932149SChris Wilson {
16880f0b679SMaarten Lankhorst 	int ret;
16980f0b679SMaarten Lankhorst 
17080f0b679SMaarten Lankhorst 	if (intr)
17180f0b679SMaarten Lankhorst 		ret = dma_resv_lock_interruptible(obj->base.resv, ww ? &ww->ctx : NULL);
17280f0b679SMaarten Lankhorst 	else
17380f0b679SMaarten Lankhorst 		ret = dma_resv_lock(obj->base.resv, ww ? &ww->ctx : NULL);
17480f0b679SMaarten Lankhorst 
1751c4dbe05SThomas Hellström 	if (!ret && ww) {
1761c4dbe05SThomas Hellström 		i915_gem_object_get(obj);
17780f0b679SMaarten Lankhorst 		list_add_tail(&obj->obj_link, &ww->obj_list);
1781c4dbe05SThomas Hellström 	}
17980f0b679SMaarten Lankhorst 	if (ret == -EALREADY)
18080f0b679SMaarten Lankhorst 		ret = 0;
18180f0b679SMaarten Lankhorst 
1821c4dbe05SThomas Hellström 	if (ret == -EDEADLK) {
1831c4dbe05SThomas Hellström 		i915_gem_object_get(obj);
18480f0b679SMaarten Lankhorst 		ww->contended = obj;
1851c4dbe05SThomas Hellström 	}
18680f0b679SMaarten Lankhorst 
18780f0b679SMaarten Lankhorst 	return ret;
18880f0b679SMaarten Lankhorst }
18980f0b679SMaarten Lankhorst 
i915_gem_object_lock(struct drm_i915_gem_object * obj,struct i915_gem_ww_ctx * ww)19080f0b679SMaarten Lankhorst static inline int i915_gem_object_lock(struct drm_i915_gem_object *obj,
19180f0b679SMaarten Lankhorst 				       struct i915_gem_ww_ctx *ww)
19280f0b679SMaarten Lankhorst {
19380f0b679SMaarten Lankhorst 	return __i915_gem_object_lock(obj, ww, ww && ww->intr);
19480f0b679SMaarten Lankhorst }
19580f0b679SMaarten Lankhorst 
i915_gem_object_lock_interruptible(struct drm_i915_gem_object * obj,struct i915_gem_ww_ctx * ww)19680f0b679SMaarten Lankhorst static inline int i915_gem_object_lock_interruptible(struct drm_i915_gem_object *obj,
19780f0b679SMaarten Lankhorst 						     struct i915_gem_ww_ctx *ww)
19880f0b679SMaarten Lankhorst {
19980f0b679SMaarten Lankhorst 	WARN_ON(ww && !ww->intr);
20080f0b679SMaarten Lankhorst 	return __i915_gem_object_lock(obj, ww, true);
20198932149SChris Wilson }
20298932149SChris Wilson 
i915_gem_object_trylock(struct drm_i915_gem_object * obj,struct i915_gem_ww_ctx * ww)203d8be1357SMaarten Lankhorst static inline bool i915_gem_object_trylock(struct drm_i915_gem_object *obj,
204d8be1357SMaarten Lankhorst 					   struct i915_gem_ww_ctx *ww)
2052850748eSChris Wilson {
206d8be1357SMaarten Lankhorst 	if (!ww)
2072850748eSChris Wilson 		return dma_resv_trylock(obj->base.resv);
208d8be1357SMaarten Lankhorst 	else
209d8be1357SMaarten Lankhorst 		return ww_mutex_trylock(&obj->base.resv->lock, &ww->ctx);
2102850748eSChris Wilson }
2112850748eSChris Wilson 
i915_gem_object_unlock(struct drm_i915_gem_object * obj)21298932149SChris Wilson static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj)
21398932149SChris Wilson {
214213d5092SThomas Hellström 	if (obj->ops->adjust_lru)
215213d5092SThomas Hellström 		obj->ops->adjust_lru(obj);
216213d5092SThomas Hellström 
21752791eeeSChristian König 	dma_resv_unlock(obj->base.resv);
21898932149SChris Wilson }
21998932149SChris Wilson 
22098932149SChris Wilson static inline void
i915_gem_object_set_readonly(struct drm_i915_gem_object * obj)22198932149SChris Wilson i915_gem_object_set_readonly(struct drm_i915_gem_object *obj)
22298932149SChris Wilson {
223cc662126SAbdiel Janulgue 	obj->flags |= I915_BO_READONLY;
22498932149SChris Wilson }
22598932149SChris Wilson 
22698932149SChris Wilson static inline bool
i915_gem_object_is_readonly(const struct drm_i915_gem_object * obj)22798932149SChris Wilson i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj)
22898932149SChris Wilson {
229cc662126SAbdiel Janulgue 	return obj->flags & I915_BO_READONLY;
23098932149SChris Wilson }
23198932149SChris Wilson 
23298932149SChris Wilson static inline bool
i915_gem_object_is_contiguous(const struct drm_i915_gem_object * obj)2332f0b97caSMatthew Auld i915_gem_object_is_contiguous(const struct drm_i915_gem_object *obj)
2342f0b97caSMatthew Auld {
2352f0b97caSMatthew Auld 	return obj->flags & I915_BO_ALLOC_CONTIGUOUS;
2362f0b97caSMatthew Auld }
2372f0b97caSMatthew Auld 
2382f0b97caSMatthew Auld static inline bool
i915_gem_object_is_volatile(const struct drm_i915_gem_object * obj)2397c98501aSMatthew Auld i915_gem_object_is_volatile(const struct drm_i915_gem_object *obj)
2407c98501aSMatthew Auld {
2417c98501aSMatthew Auld 	return obj->flags & I915_BO_ALLOC_VOLATILE;
2427c98501aSMatthew Auld }
2437c98501aSMatthew Auld 
2447c98501aSMatthew Auld static inline void
i915_gem_object_set_volatile(struct drm_i915_gem_object * obj)2457c98501aSMatthew Auld i915_gem_object_set_volatile(struct drm_i915_gem_object *obj)
2467c98501aSMatthew Auld {
2477c98501aSMatthew Auld 	obj->flags |= I915_BO_ALLOC_VOLATILE;
2487c98501aSMatthew Auld }
2497c98501aSMatthew Auld 
2507c98501aSMatthew Auld static inline bool
i915_gem_object_has_tiling_quirk(struct drm_i915_gem_object * obj)2510175969eSChris Wilson i915_gem_object_has_tiling_quirk(struct drm_i915_gem_object *obj)
2520175969eSChris Wilson {
2530175969eSChris Wilson 	return test_bit(I915_TILING_QUIRK_BIT, &obj->flags);
2540175969eSChris Wilson }
2550175969eSChris Wilson 
2560175969eSChris Wilson static inline void
i915_gem_object_set_tiling_quirk(struct drm_i915_gem_object * obj)2570175969eSChris Wilson i915_gem_object_set_tiling_quirk(struct drm_i915_gem_object *obj)
2580175969eSChris Wilson {
2590175969eSChris Wilson 	set_bit(I915_TILING_QUIRK_BIT, &obj->flags);
2600175969eSChris Wilson }
2610175969eSChris Wilson 
2620175969eSChris Wilson static inline void
i915_gem_object_clear_tiling_quirk(struct drm_i915_gem_object * obj)2630175969eSChris Wilson i915_gem_object_clear_tiling_quirk(struct drm_i915_gem_object *obj)
2640175969eSChris Wilson {
2650175969eSChris Wilson 	clear_bit(I915_TILING_QUIRK_BIT, &obj->flags);
2660175969eSChris Wilson }
2670175969eSChris Wilson 
2680175969eSChris Wilson static inline bool
i915_gem_object_is_protected(const struct drm_i915_gem_object * obj)269d3ac8d42SDaniele Ceraolo Spurio i915_gem_object_is_protected(const struct drm_i915_gem_object *obj)
270d3ac8d42SDaniele Ceraolo Spurio {
271d3ac8d42SDaniele Ceraolo Spurio 	return obj->flags & I915_BO_PROTECTED;
272d3ac8d42SDaniele Ceraolo Spurio }
273d3ac8d42SDaniele Ceraolo Spurio 
274d3ac8d42SDaniele Ceraolo Spurio static inline bool
i915_gem_object_type_has(const struct drm_i915_gem_object * obj,unsigned long flags)2753cbad5d7SChris Wilson i915_gem_object_type_has(const struct drm_i915_gem_object *obj,
2763cbad5d7SChris Wilson 			 unsigned long flags)
2773cbad5d7SChris Wilson {
2783cbad5d7SChris Wilson 	return obj->ops->flags & flags;
2793cbad5d7SChris Wilson }
2803cbad5d7SChris Wilson 
2810ff37575SThomas Hellström bool i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj);
28298932149SChris Wilson 
2830ff37575SThomas Hellström bool i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj);
2845fbc2c2bSImre Deak 
2855fbc2c2bSImre Deak static inline bool
i915_gem_object_is_shrinkable(const struct drm_i915_gem_object * obj)28698932149SChris Wilson i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
28798932149SChris Wilson {
2887a9883beSVidya Srinivas 	/* TODO: make DPT shrinkable when it has no bound vmas */
2897a9883beSVidya Srinivas 	return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_SHRINKABLE) &&
2907a9883beSVidya Srinivas 		!obj->is_dpt;
29198932149SChris Wilson }
29298932149SChris Wilson 
29398932149SChris Wilson static inline bool
i915_gem_object_has_self_managed_shrink_list(const struct drm_i915_gem_object * obj)294ebd4a8ecSMatthew Auld i915_gem_object_has_self_managed_shrink_list(const struct drm_i915_gem_object *obj)
295ebd4a8ecSMatthew Auld {
296ebd4a8ecSMatthew Auld 	return i915_gem_object_type_has(obj, I915_GEM_OBJECT_SELF_MANAGED_SHRINK_LIST);
297ebd4a8ecSMatthew Auld }
298ebd4a8ecSMatthew Auld 
299ebd4a8ecSMatthew Auld static inline bool
i915_gem_object_is_proxy(const struct drm_i915_gem_object * obj)30098932149SChris Wilson i915_gem_object_is_proxy(const struct drm_i915_gem_object *obj)
30198932149SChris Wilson {
3023cbad5d7SChris Wilson 	return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_PROXY);
30398932149SChris Wilson }
30498932149SChris Wilson 
30598932149SChris Wilson static inline bool
i915_gem_object_never_mmap(const struct drm_i915_gem_object * obj)306f6c26b55SJanusz Krzysztofik i915_gem_object_never_mmap(const struct drm_i915_gem_object *obj)
307a4311745SChris Wilson {
308f6c26b55SJanusz Krzysztofik 	return i915_gem_object_type_has(obj, I915_GEM_OBJECT_NO_MMAP);
309a4311745SChris Wilson }
310a4311745SChris Wilson 
311a4311745SChris Wilson static inline bool
i915_gem_object_is_framebuffer(const struct drm_i915_gem_object * obj)31298932149SChris Wilson i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj)
31398932149SChris Wilson {
314779cb5baSVille Syrjälä 	return READ_ONCE(obj->frontbuffer) || obj->is_dpt;
31598932149SChris Wilson }
31698932149SChris Wilson 
31798932149SChris Wilson static inline unsigned int
i915_gem_object_get_tiling(const struct drm_i915_gem_object * obj)31898932149SChris Wilson i915_gem_object_get_tiling(const struct drm_i915_gem_object *obj)
31998932149SChris Wilson {
32098932149SChris Wilson 	return obj->tiling_and_stride & TILING_MASK;
32198932149SChris Wilson }
32298932149SChris Wilson 
32398932149SChris Wilson static inline bool
i915_gem_object_is_tiled(const struct drm_i915_gem_object * obj)32498932149SChris Wilson i915_gem_object_is_tiled(const struct drm_i915_gem_object *obj)
32598932149SChris Wilson {
32698932149SChris Wilson 	return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
32798932149SChris Wilson }
32898932149SChris Wilson 
32998932149SChris Wilson static inline unsigned int
i915_gem_object_get_stride(const struct drm_i915_gem_object * obj)33098932149SChris Wilson i915_gem_object_get_stride(const struct drm_i915_gem_object *obj)
33198932149SChris Wilson {
33298932149SChris Wilson 	return obj->tiling_and_stride & STRIDE_MASK;
33398932149SChris Wilson }
33498932149SChris Wilson 
33598932149SChris Wilson static inline unsigned int
i915_gem_tile_height(unsigned int tiling)33698932149SChris Wilson i915_gem_tile_height(unsigned int tiling)
33798932149SChris Wilson {
33898932149SChris Wilson 	GEM_BUG_ON(!tiling);
33998932149SChris Wilson 	return tiling == I915_TILING_Y ? 32 : 8;
34098932149SChris Wilson }
34198932149SChris Wilson 
34298932149SChris Wilson static inline unsigned int
i915_gem_object_get_tile_height(const struct drm_i915_gem_object * obj)34398932149SChris Wilson i915_gem_object_get_tile_height(const struct drm_i915_gem_object *obj)
34498932149SChris Wilson {
34598932149SChris Wilson 	return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
34698932149SChris Wilson }
34798932149SChris Wilson 
34898932149SChris Wilson static inline unsigned int
i915_gem_object_get_tile_row_size(const struct drm_i915_gem_object * obj)34998932149SChris Wilson i915_gem_object_get_tile_row_size(const struct drm_i915_gem_object *obj)
35098932149SChris Wilson {
35198932149SChris Wilson 	return (i915_gem_object_get_stride(obj) *
35298932149SChris Wilson 		i915_gem_object_get_tile_height(obj));
35398932149SChris Wilson }
35498932149SChris Wilson 
35598932149SChris Wilson int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
35698932149SChris Wilson 			       unsigned int tiling, unsigned int stride);
35798932149SChris Wilson 
358f47e6306SChris Wilson /**
359f47e6306SChris Wilson  * __i915_gem_object_page_iter_get_sg - helper to find the target scatterlist
360f47e6306SChris Wilson  * pointer and the target page position using pgoff_t n input argument and
361f47e6306SChris Wilson  * i915_gem_object_page_iter
362f47e6306SChris Wilson  * @obj: i915 GEM buffer object
363f47e6306SChris Wilson  * @iter: i915 GEM buffer object page iterator
364f47e6306SChris Wilson  * @n: page offset
365f47e6306SChris Wilson  * @offset: searched physical offset,
366f47e6306SChris Wilson  *          it will be used for returning physical page offset value
367f47e6306SChris Wilson  *
368f47e6306SChris Wilson  * Context: Takes and releases the mutex lock of the i915_gem_object_page_iter.
369f47e6306SChris Wilson  *          Takes and releases the RCU lock to search the radix_tree of
370f47e6306SChris Wilson  *          i915_gem_object_page_iter.
371f47e6306SChris Wilson  *
372f47e6306SChris Wilson  * Returns:
373f47e6306SChris Wilson  * The target scatterlist pointer and the target page position.
374f47e6306SChris Wilson  *
375f47e6306SChris Wilson  * Recommended to use wrapper macro: i915_gem_object_page_iter_get_sg()
376f47e6306SChris Wilson  */
37798932149SChris Wilson struct scatterlist *
378f47e6306SChris Wilson __i915_gem_object_page_iter_get_sg(struct drm_i915_gem_object *obj,
379934941edSTvrtko Ursulin 				   struct i915_gem_object_page_iter *iter,
380f47e6306SChris Wilson 				   pgoff_t  n,
381f47e6306SChris Wilson 				   unsigned int *offset);
382934941edSTvrtko Ursulin 
383f47e6306SChris Wilson /**
384f47e6306SChris Wilson  * i915_gem_object_page_iter_get_sg - wrapper macro for
385f47e6306SChris Wilson  * __i915_gem_object_page_iter_get_sg()
386f47e6306SChris Wilson  * @obj: i915 GEM buffer object
387f47e6306SChris Wilson  * @it: i915 GEM buffer object page iterator
388f47e6306SChris Wilson  * @n: page offset
389f47e6306SChris Wilson  * @offset: searched physical offset,
390f47e6306SChris Wilson  *          it will be used for returning physical page offset value
391f47e6306SChris Wilson  *
392f47e6306SChris Wilson  * Context: Takes and releases the mutex lock of the i915_gem_object_page_iter.
393f47e6306SChris Wilson  *          Takes and releases the RCU lock to search the radix_tree of
394f47e6306SChris Wilson  *          i915_gem_object_page_iter.
395f47e6306SChris Wilson  *
396f47e6306SChris Wilson  * Returns:
397f47e6306SChris Wilson  * The target scatterlist pointer and the target page position.
398f47e6306SChris Wilson  *
399f47e6306SChris Wilson  * In order to avoid the truncation of the input parameter, it checks the page
400f47e6306SChris Wilson  * offset n's type from the input parameter before calling
401f47e6306SChris Wilson  * __i915_gem_object_page_iter_get_sg().
402f47e6306SChris Wilson  */
403f47e6306SChris Wilson #define i915_gem_object_page_iter_get_sg(obj, it, n, offset) ({	\
404f47e6306SChris Wilson 	static_assert(castable_to_type(n, pgoff_t));		\
405f47e6306SChris Wilson 	__i915_gem_object_page_iter_get_sg(obj, it, n, offset);	\
406f47e6306SChris Wilson })
407f47e6306SChris Wilson 
408f47e6306SChris Wilson /**
409f47e6306SChris Wilson  * __i915_gem_object_get_sg - helper to find the target scatterlist
410f47e6306SChris Wilson  * pointer and the target page position using pgoff_t n input argument and
411f47e6306SChris Wilson  * drm_i915_gem_object. It uses an internal shmem scatterlist lookup function.
412f47e6306SChris Wilson  * @obj: i915 GEM buffer object
413f47e6306SChris Wilson  * @n: page offset
414f47e6306SChris Wilson  * @offset: searched physical offset,
415f47e6306SChris Wilson  *          it will be used for returning physical page offset value
416f47e6306SChris Wilson  *
417f47e6306SChris Wilson  * It uses drm_i915_gem_object's internal shmem scatterlist lookup function as
418f47e6306SChris Wilson  * i915_gem_object_page_iter and calls __i915_gem_object_page_iter_get_sg().
419f47e6306SChris Wilson  *
420f47e6306SChris Wilson  * Returns:
421f47e6306SChris Wilson  * The target scatterlist pointer and the target page position.
422f47e6306SChris Wilson  *
423f47e6306SChris Wilson  * Recommended to use wrapper macro: i915_gem_object_get_sg()
424f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg()
425f47e6306SChris Wilson  */
426934941edSTvrtko Ursulin static inline struct scatterlist *
__i915_gem_object_get_sg(struct drm_i915_gem_object * obj,pgoff_t n,unsigned int * offset)427f47e6306SChris Wilson __i915_gem_object_get_sg(struct drm_i915_gem_object *obj, pgoff_t n,
4287d6a276eSJason Ekstrand 			 unsigned int *offset)
429934941edSTvrtko Ursulin {
430f47e6306SChris Wilson 	return __i915_gem_object_page_iter_get_sg(obj, &obj->mm.get_page, n, offset);
431934941edSTvrtko Ursulin }
432934941edSTvrtko Ursulin 
433f47e6306SChris Wilson /**
434f47e6306SChris Wilson  * i915_gem_object_get_sg - wrapper macro for __i915_gem_object_get_sg()
435f47e6306SChris Wilson  * @obj: i915 GEM buffer object
436f47e6306SChris Wilson  * @n: page offset
437f47e6306SChris Wilson  * @offset: searched physical offset,
438f47e6306SChris Wilson  *          it will be used for returning physical page offset value
439f47e6306SChris Wilson  *
440f47e6306SChris Wilson  * Returns:
441f47e6306SChris Wilson  * The target scatterlist pointer and the target page position.
442f47e6306SChris Wilson  *
443f47e6306SChris Wilson  * In order to avoid the truncation of the input parameter, it checks the page
444f47e6306SChris Wilson  * offset n's type from the input parameter before calling
445f47e6306SChris Wilson  * __i915_gem_object_get_sg().
446f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg()
447f47e6306SChris Wilson  */
448f47e6306SChris Wilson #define i915_gem_object_get_sg(obj, n, offset) ({	\
449f47e6306SChris Wilson 	static_assert(castable_to_type(n, pgoff_t));	\
450f47e6306SChris Wilson 	__i915_gem_object_get_sg(obj, n, offset);	\
451f47e6306SChris Wilson })
452f47e6306SChris Wilson 
453f47e6306SChris Wilson /**
454f47e6306SChris Wilson  * __i915_gem_object_get_sg_dma - helper to find the target scatterlist
455f47e6306SChris Wilson  * pointer and the target page position using pgoff_t n input argument and
456f47e6306SChris Wilson  * drm_i915_gem_object. It uses an internal DMA mapped scatterlist lookup function
457f47e6306SChris Wilson  * @obj: i915 GEM buffer object
458f47e6306SChris Wilson  * @n: page offset
459f47e6306SChris Wilson  * @offset: searched physical offset,
460f47e6306SChris Wilson  *          it will be used for returning physical page offset value
461f47e6306SChris Wilson  *
462f47e6306SChris Wilson  * It uses drm_i915_gem_object's internal DMA mapped scatterlist lookup function
463f47e6306SChris Wilson  * as i915_gem_object_page_iter and calls __i915_gem_object_page_iter_get_sg().
464f47e6306SChris Wilson  *
465f47e6306SChris Wilson  * Returns:
466f47e6306SChris Wilson  * The target scatterlist pointer and the target page position.
467f47e6306SChris Wilson  *
468f47e6306SChris Wilson  * Recommended to use wrapper macro: i915_gem_object_get_sg_dma()
469f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg()
470f47e6306SChris Wilson  */
471934941edSTvrtko Ursulin static inline struct scatterlist *
__i915_gem_object_get_sg_dma(struct drm_i915_gem_object * obj,pgoff_t n,unsigned int * offset)472f47e6306SChris Wilson __i915_gem_object_get_sg_dma(struct drm_i915_gem_object *obj, pgoff_t n,
4737d6a276eSJason Ekstrand 			     unsigned int *offset)
474934941edSTvrtko Ursulin {
475f47e6306SChris Wilson 	return __i915_gem_object_page_iter_get_sg(obj, &obj->mm.get_dma_page, n, offset);
476934941edSTvrtko Ursulin }
47798932149SChris Wilson 
478f47e6306SChris Wilson /**
479f47e6306SChris Wilson  * i915_gem_object_get_sg_dma - wrapper macro for __i915_gem_object_get_sg_dma()
480f47e6306SChris Wilson  * @obj: i915 GEM buffer object
481f47e6306SChris Wilson  * @n: page offset
482f47e6306SChris Wilson  * @offset: searched physical offset,
483f47e6306SChris Wilson  *          it will be used for returning physical page offset value
484f47e6306SChris Wilson  *
485f47e6306SChris Wilson  * Returns:
486f47e6306SChris Wilson  * The target scatterlist pointer and the target page position.
487f47e6306SChris Wilson  *
488f47e6306SChris Wilson  * In order to avoid the truncation of the input parameter, it checks the page
489f47e6306SChris Wilson  * offset n's type from the input parameter before calling
490f47e6306SChris Wilson  * __i915_gem_object_get_sg_dma().
491f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg()
492f47e6306SChris Wilson  */
493f47e6306SChris Wilson #define i915_gem_object_get_sg_dma(obj, n, offset) ({	\
494f47e6306SChris Wilson 	static_assert(castable_to_type(n, pgoff_t));	\
495f47e6306SChris Wilson 	__i915_gem_object_get_sg_dma(obj, n, offset);	\
496f47e6306SChris Wilson })
49798932149SChris Wilson 
498f47e6306SChris Wilson /**
499f47e6306SChris Wilson  * __i915_gem_object_get_page - helper to find the target page with a page offset
500f47e6306SChris Wilson  * @obj: i915 GEM buffer object
501f47e6306SChris Wilson  * @n: page offset
502f47e6306SChris Wilson  *
503f47e6306SChris Wilson  * It uses drm_i915_gem_object's internal shmem scatterlist lookup function as
504f47e6306SChris Wilson  * i915_gem_object_page_iter and calls __i915_gem_object_page_iter_get_sg()
505f47e6306SChris Wilson  * internally.
506f47e6306SChris Wilson  *
507f47e6306SChris Wilson  * Returns:
508f47e6306SChris Wilson  * The target page pointer.
509f47e6306SChris Wilson  *
510f47e6306SChris Wilson  * Recommended to use wrapper macro: i915_gem_object_get_page()
511f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg()
512f47e6306SChris Wilson  */
5134993a8a3SDave Airlie struct page *
514f47e6306SChris Wilson __i915_gem_object_get_page(struct drm_i915_gem_object *obj, pgoff_t n);
5154993a8a3SDave Airlie 
516f47e6306SChris Wilson /**
517f47e6306SChris Wilson  * i915_gem_object_get_page - wrapper macro for __i915_gem_object_get_page
518f47e6306SChris Wilson  * @obj: i915 GEM buffer object
519f47e6306SChris Wilson  * @n: page offset
520f47e6306SChris Wilson  *
521f47e6306SChris Wilson  * Returns:
522f47e6306SChris Wilson  * The target page pointer.
523f47e6306SChris Wilson  *
524f47e6306SChris Wilson  * In order to avoid the truncation of the input parameter, it checks the page
525f47e6306SChris Wilson  * offset n's type from the input parameter before calling
526f47e6306SChris Wilson  * __i915_gem_object_get_page().
527f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg()
528f47e6306SChris Wilson  */
529f47e6306SChris Wilson #define i915_gem_object_get_page(obj, n) ({		\
530f47e6306SChris Wilson 	static_assert(castable_to_type(n, pgoff_t));	\
531f47e6306SChris Wilson 	__i915_gem_object_get_page(obj, n);		\
532f47e6306SChris Wilson })
533f47e6306SChris Wilson 
534f47e6306SChris Wilson /**
535f47e6306SChris Wilson  * __i915_gem_object_get_dirty_page - helper to find the target page with a page
536f47e6306SChris Wilson  * offset
537f47e6306SChris Wilson  * @obj: i915 GEM buffer object
538f47e6306SChris Wilson  * @n: page offset
539f47e6306SChris Wilson  *
540f47e6306SChris Wilson  * It works like i915_gem_object_get_page(), but it marks the returned page dirty.
541f47e6306SChris Wilson  *
542f47e6306SChris Wilson  * Returns:
543f47e6306SChris Wilson  * The target page pointer.
544f47e6306SChris Wilson  *
545f47e6306SChris Wilson  * Recommended to use wrapper macro: i915_gem_object_get_dirty_page()
546f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg() and __i915_gem_object_get_page()
547f47e6306SChris Wilson  */
548f47e6306SChris Wilson struct page *
549f47e6306SChris Wilson __i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, pgoff_t n);
550f47e6306SChris Wilson 
551f47e6306SChris Wilson /**
552f47e6306SChris Wilson  * i915_gem_object_get_dirty_page - wrapper macro for __i915_gem_object_get_dirty_page
553f47e6306SChris Wilson  * @obj: i915 GEM buffer object
554f47e6306SChris Wilson  * @n: page offset
555f47e6306SChris Wilson  *
556f47e6306SChris Wilson  * Returns:
557f47e6306SChris Wilson  * The target page pointer.
558f47e6306SChris Wilson  *
559f47e6306SChris Wilson  * In order to avoid the truncation of the input parameter, it checks the page
560f47e6306SChris Wilson  * offset n's type from the input parameter before calling
561f47e6306SChris Wilson  * __i915_gem_object_get_dirty_page().
562f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg() and __i915_gem_object_get_page()
563f47e6306SChris Wilson  */
564f47e6306SChris Wilson #define i915_gem_object_get_dirty_page(obj, n) ({	\
565f47e6306SChris Wilson 	static_assert(castable_to_type(n, pgoff_t));	\
566f47e6306SChris Wilson 	__i915_gem_object_get_dirty_page(obj, n);	\
567f47e6306SChris Wilson })
568f47e6306SChris Wilson 
569f47e6306SChris Wilson /**
570f47e6306SChris Wilson  * __i915_gem_object_get_dma_address_len - helper to get bus addresses of
571f47e6306SChris Wilson  * targeted DMA mapped scatterlist from i915 GEM buffer object and it's length
572f47e6306SChris Wilson  * @obj: i915 GEM buffer object
573f47e6306SChris Wilson  * @n: page offset
574f47e6306SChris Wilson  * @len: DMA mapped scatterlist's DMA bus addresses length to return
575f47e6306SChris Wilson  *
576f47e6306SChris Wilson  * Returns:
577f47e6306SChris Wilson  * Bus addresses of targeted DMA mapped scatterlist
578f47e6306SChris Wilson  *
579f47e6306SChris Wilson  * Recommended to use wrapper macro: i915_gem_object_get_dma_address_len()
580f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg() and __i915_gem_object_get_sg_dma()
581f47e6306SChris Wilson  */
58298932149SChris Wilson dma_addr_t
583f47e6306SChris Wilson __i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj, pgoff_t n,
58498932149SChris Wilson 				      unsigned int *len);
58598932149SChris Wilson 
586f47e6306SChris Wilson /**
587f47e6306SChris Wilson  * i915_gem_object_get_dma_address_len - wrapper macro for
588f47e6306SChris Wilson  * __i915_gem_object_get_dma_address_len
589f47e6306SChris Wilson  * @obj: i915 GEM buffer object
590f47e6306SChris Wilson  * @n: page offset
591f47e6306SChris Wilson  * @len: DMA mapped scatterlist's DMA bus addresses length to return
592f47e6306SChris Wilson  *
593f47e6306SChris Wilson  * Returns:
594f47e6306SChris Wilson  * Bus addresses of targeted DMA mapped scatterlist
595f47e6306SChris Wilson  *
596f47e6306SChris Wilson  * In order to avoid the truncation of the input parameter, it checks the page
597f47e6306SChris Wilson  * offset n's type from the input parameter before calling
598f47e6306SChris Wilson  * __i915_gem_object_get_dma_address_len().
599f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg() and
600f47e6306SChris Wilson  * __i915_gem_object_get_dma_address_len()
601f47e6306SChris Wilson  */
602f47e6306SChris Wilson #define i915_gem_object_get_dma_address_len(obj, n, len) ({	\
603f47e6306SChris Wilson 	static_assert(castable_to_type(n, pgoff_t));		\
604f47e6306SChris Wilson 	__i915_gem_object_get_dma_address_len(obj, n, len);	\
605f47e6306SChris Wilson })
606f47e6306SChris Wilson 
607f47e6306SChris Wilson /**
608f47e6306SChris Wilson  * __i915_gem_object_get_dma_address - helper to get bus addresses of
609f47e6306SChris Wilson  * targeted DMA mapped scatterlist from i915 GEM buffer object
610f47e6306SChris Wilson  * @obj: i915 GEM buffer object
611f47e6306SChris Wilson  * @n: page offset
612f47e6306SChris Wilson  *
613f47e6306SChris Wilson  * Returns:
614f47e6306SChris Wilson  * Bus addresses of targeted DMA mapped scatterlis
615f47e6306SChris Wilson  *
616f47e6306SChris Wilson  * Recommended to use wrapper macro: i915_gem_object_get_dma_address()
617f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg() and __i915_gem_object_get_sg_dma()
618f47e6306SChris Wilson  */
61998932149SChris Wilson dma_addr_t
620f47e6306SChris Wilson __i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, pgoff_t n);
621f47e6306SChris Wilson 
622f47e6306SChris Wilson /**
623f47e6306SChris Wilson  * i915_gem_object_get_dma_address - wrapper macro for
624f47e6306SChris Wilson  * __i915_gem_object_get_dma_address
625f47e6306SChris Wilson  * @obj: i915 GEM buffer object
626f47e6306SChris Wilson  * @n: page offset
627f47e6306SChris Wilson  *
628f47e6306SChris Wilson  * Returns:
629f47e6306SChris Wilson  * Bus addresses of targeted DMA mapped scatterlist
630f47e6306SChris Wilson  *
631f47e6306SChris Wilson  * In order to avoid the truncation of the input parameter, it checks the page
632f47e6306SChris Wilson  * offset n's type from the input parameter before calling
633f47e6306SChris Wilson  * __i915_gem_object_get_dma_address().
634f47e6306SChris Wilson  * See also __i915_gem_object_page_iter_get_sg() and
635f47e6306SChris Wilson  * __i915_gem_object_get_dma_address()
636f47e6306SChris Wilson  */
637f47e6306SChris Wilson #define i915_gem_object_get_dma_address(obj, n) ({	\
638f47e6306SChris Wilson 	static_assert(castable_to_type(n, pgoff_t));	\
639f47e6306SChris Wilson 	__i915_gem_object_get_dma_address(obj, n);	\
640f47e6306SChris Wilson })
64198932149SChris Wilson 
64298932149SChris Wilson void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
6438c949515SMatthew Auld 				 struct sg_table *pages);
644f033428dSChris Wilson 
645f033428dSChris Wilson int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
64698932149SChris Wilson int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
64798932149SChris Wilson 
64898932149SChris Wilson static inline int __must_check
i915_gem_object_pin_pages(struct drm_i915_gem_object * obj)64998932149SChris Wilson i915_gem_object_pin_pages(struct drm_i915_gem_object *obj)
65098932149SChris Wilson {
651cf41a8f1SMaarten Lankhorst 	assert_object_held(obj);
65298932149SChris Wilson 
65398932149SChris Wilson 	if (atomic_inc_not_zero(&obj->mm.pages_pin_count))
65498932149SChris Wilson 		return 0;
65598932149SChris Wilson 
65698932149SChris Wilson 	return __i915_gem_object_get_pages(obj);
65798932149SChris Wilson }
65898932149SChris Wilson 
659c858ffa1SMaarten Lankhorst int i915_gem_object_pin_pages_unlocked(struct drm_i915_gem_object *obj);
660c858ffa1SMaarten Lankhorst 
66198932149SChris Wilson static inline bool
i915_gem_object_has_pages(struct drm_i915_gem_object * obj)66298932149SChris Wilson i915_gem_object_has_pages(struct drm_i915_gem_object *obj)
66398932149SChris Wilson {
66498932149SChris Wilson 	return !IS_ERR_OR_NULL(READ_ONCE(obj->mm.pages));
66598932149SChris Wilson }
66698932149SChris Wilson 
66798932149SChris Wilson static inline void
__i915_gem_object_pin_pages(struct drm_i915_gem_object * obj)66898932149SChris Wilson __i915_gem_object_pin_pages(struct drm_i915_gem_object *obj)
66998932149SChris Wilson {
67098932149SChris Wilson 	GEM_BUG_ON(!i915_gem_object_has_pages(obj));
67198932149SChris Wilson 
67298932149SChris Wilson 	atomic_inc(&obj->mm.pages_pin_count);
67398932149SChris Wilson }
67498932149SChris Wilson 
67598932149SChris Wilson static inline bool
i915_gem_object_has_pinned_pages(struct drm_i915_gem_object * obj)67698932149SChris Wilson i915_gem_object_has_pinned_pages(struct drm_i915_gem_object *obj)
67798932149SChris Wilson {
67898932149SChris Wilson 	return atomic_read(&obj->mm.pages_pin_count);
67998932149SChris Wilson }
68098932149SChris Wilson 
68198932149SChris Wilson static inline void
__i915_gem_object_unpin_pages(struct drm_i915_gem_object * obj)68298932149SChris Wilson __i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj)
68398932149SChris Wilson {
68498932149SChris Wilson 	GEM_BUG_ON(!i915_gem_object_has_pages(obj));
68598932149SChris Wilson 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
68698932149SChris Wilson 
68798932149SChris Wilson 	atomic_dec(&obj->mm.pages_pin_count);
68898932149SChris Wilson }
68998932149SChris Wilson 
69098932149SChris Wilson static inline void
i915_gem_object_unpin_pages(struct drm_i915_gem_object * obj)69198932149SChris Wilson i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj)
69298932149SChris Wilson {
69398932149SChris Wilson 	__i915_gem_object_unpin_pages(obj);
69498932149SChris Wilson }
69598932149SChris Wilson 
696f86dbacbSDaniel Vetter int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj);
6977ae03459SMatthew Auld int i915_gem_object_truncate(struct drm_i915_gem_object *obj);
69898932149SChris Wilson 
69998932149SChris Wilson /**
70098932149SChris Wilson  * i915_gem_object_pin_map - return a contiguous mapping of the entire object
70198932149SChris Wilson  * @obj: the object to map into kernel address space
70298932149SChris Wilson  * @type: the type of mapping, used to select pgprot_t
70398932149SChris Wilson  *
70498932149SChris Wilson  * Calls i915_gem_object_pin_pages() to prevent reaping of the object's
70598932149SChris Wilson  * pages and then returns a contiguous mapping of the backing storage into
70698932149SChris Wilson  * the kernel address space. Based on the @type of mapping, the PTE will be
70798932149SChris Wilson  * set to either WriteBack or WriteCombine (via pgprot_t).
70898932149SChris Wilson  *
70998932149SChris Wilson  * The caller is responsible for calling i915_gem_object_unpin_map() when the
71098932149SChris Wilson  * mapping is no longer required.
71198932149SChris Wilson  *
71298932149SChris Wilson  * Returns the pointer through which to access the mapped object, or an
71398932149SChris Wilson  * ERR_PTR() on error.
71498932149SChris Wilson  */
71598932149SChris Wilson void *__must_check i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
71698932149SChris Wilson 					   enum i915_map_type type);
71798932149SChris Wilson 
71874827b53SMaarten Lankhorst void *__must_check i915_gem_object_pin_map_unlocked(struct drm_i915_gem_object *obj,
71974827b53SMaarten Lankhorst 						    enum i915_map_type type);
72074827b53SMaarten Lankhorst 
72198932149SChris Wilson void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
72298932149SChris Wilson 				 unsigned long offset,
72398932149SChris Wilson 				 unsigned long size);
i915_gem_object_flush_map(struct drm_i915_gem_object * obj)72498932149SChris Wilson static inline void i915_gem_object_flush_map(struct drm_i915_gem_object *obj)
72598932149SChris Wilson {
72698932149SChris Wilson 	__i915_gem_object_flush_map(obj, 0, obj->base.size);
72798932149SChris Wilson }
72898932149SChris Wilson 
72998932149SChris Wilson /**
73098932149SChris Wilson  * i915_gem_object_unpin_map - releases an earlier mapping
73198932149SChris Wilson  * @obj: the object to unmap
73298932149SChris Wilson  *
73398932149SChris Wilson  * After pinning the object and mapping its pages, once you are finished
73498932149SChris Wilson  * with your access, call i915_gem_object_unpin_map() to release the pin
73598932149SChris Wilson  * upon the mapping. Once the pin count reaches zero, that mapping may be
73698932149SChris Wilson  * removed.
73798932149SChris Wilson  */
i915_gem_object_unpin_map(struct drm_i915_gem_object * obj)73898932149SChris Wilson static inline void i915_gem_object_unpin_map(struct drm_i915_gem_object *obj)
73998932149SChris Wilson {
74098932149SChris Wilson 	i915_gem_object_unpin_pages(obj);
74198932149SChris Wilson }
74298932149SChris Wilson 
74389d19b2bSChris Wilson void __i915_gem_object_release_map(struct drm_i915_gem_object *obj);
74489d19b2bSChris Wilson 
745f0e4a063SChris Wilson int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj,
746f0e4a063SChris Wilson 				 unsigned int *needs_clflush);
747f0e4a063SChris Wilson int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj,
748f0e4a063SChris Wilson 				  unsigned int *needs_clflush);
749f0e4a063SChris Wilson #define CLFLUSH_BEFORE	BIT(0)
750f0e4a063SChris Wilson #define CLFLUSH_AFTER	BIT(1)
751f0e4a063SChris Wilson #define CLFLUSH_FLAGS	(CLFLUSH_BEFORE | CLFLUSH_AFTER)
752f0e4a063SChris Wilson 
753f0e4a063SChris Wilson static inline void
i915_gem_object_finish_access(struct drm_i915_gem_object * obj)754f0e4a063SChris Wilson i915_gem_object_finish_access(struct drm_i915_gem_object *obj)
755f0e4a063SChris Wilson {
756f0e4a063SChris Wilson 	i915_gem_object_unpin_pages(obj);
757f0e4a063SChris Wilson }
758f0e4a063SChris Wilson 
7591d7f5e6cSChristian König int i915_gem_object_get_moving_fence(struct drm_i915_gem_object *obj,
7601d7f5e6cSChristian König 				     struct dma_fence **fence);
761f6c466b8SMaarten Lankhorst int i915_gem_object_wait_moving_fence(struct drm_i915_gem_object *obj,
762f6c466b8SMaarten Lankhorst 				      bool intr);
763bfe53be2SMatthew Auld bool i915_gem_object_has_unknown_state(struct drm_i915_gem_object *obj);
764f6c466b8SMaarten Lankhorst 
76598932149SChris Wilson void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
76698932149SChris Wilson 					 unsigned int cache_level);
7679275277dSFei Yang void i915_gem_object_set_pat_index(struct drm_i915_gem_object *obj,
7689275277dSFei Yang 				   unsigned int pat_index);
76930f1dccdSMatthew Auld bool i915_gem_object_can_bypass_llc(struct drm_i915_gem_object *obj);
77098932149SChris Wilson void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj);
771c1793ba8SMaarten Lankhorst void i915_gem_object_flush_if_display_locked(struct drm_i915_gem_object *obj);
7722ea6ec76SMatthew Auld bool i915_gem_cpu_write_needs_clflush(struct drm_i915_gem_object *obj);
77398932149SChris Wilson 
774f0e4a063SChris Wilson int __must_check
775f0e4a063SChris Wilson i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write);
776f0e4a063SChris Wilson int __must_check
777f0e4a063SChris Wilson i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write);
778f0e4a063SChris Wilson int __must_check
779f0e4a063SChris Wilson i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write);
780f0e4a063SChris Wilson struct i915_vma * __must_check
781f0e4a063SChris Wilson i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
7821b321026SMaarten Lankhorst 				     struct i915_gem_ww_ctx *ww,
783f0e4a063SChris Wilson 				     u32 alignment,
7843bb6a442SNiranjana Vishwanathapura 				     const struct i915_gtt_view *view,
785f0e4a063SChris Wilson 				     unsigned int flags);
786f0e4a063SChris Wilson 
7871aff1903SChris Wilson void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj);
7881aff1903SChris Wilson void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj);
789ebd4a8ecSMatthew Auld void __i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj);
790ebd4a8ecSMatthew Auld void __i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj);
7911aff1903SChris Wilson void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj);
7921aff1903SChris Wilson 
__start_cpu_write(struct drm_i915_gem_object * obj)7938475355fSChris Wilson static inline void __start_cpu_write(struct drm_i915_gem_object *obj)
7948475355fSChris Wilson {
7958475355fSChris Wilson 	obj->read_domains = I915_GEM_DOMAIN_CPU;
7968475355fSChris Wilson 	obj->write_domain = I915_GEM_DOMAIN_CPU;
7972ea6ec76SMatthew Auld 	if (i915_gem_cpu_write_needs_clflush(obj))
7988475355fSChris Wilson 		obj->cache_dirty = true;
7998475355fSChris Wilson }
80098932149SChris Wilson 
801f99e67f1SChris Wilson void i915_gem_fence_wait_priority(struct dma_fence *fence,
802f99e67f1SChris Wilson 				  const struct i915_sched_attr *attr);
803f99e67f1SChris Wilson 
804d45a1a53SChris Wilson int i915_gem_object_wait(struct drm_i915_gem_object *obj,
805d45a1a53SChris Wilson 			 unsigned int flags,
806d45a1a53SChris Wilson 			 long timeout);
807d45a1a53SChris Wilson int i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
808d45a1a53SChris Wilson 				  unsigned int flags,
809d45a1a53SChris Wilson 				  const struct i915_sched_attr *attr);
810d45a1a53SChris Wilson 
811da42104fSChris Wilson void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
812da42104fSChris Wilson 					 enum fb_op_origin origin);
813da42104fSChris Wilson void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
814da42104fSChris Wilson 					      enum fb_op_origin origin);
815da42104fSChris Wilson 
816da42104fSChris Wilson static inline void
i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object * obj,enum fb_op_origin origin)817da42104fSChris Wilson i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
818da42104fSChris Wilson 				  enum fb_op_origin origin)
819da42104fSChris Wilson {
820da42104fSChris Wilson 	if (unlikely(rcu_access_pointer(obj->frontbuffer)))
821da42104fSChris Wilson 		__i915_gem_object_flush_frontbuffer(obj, origin);
822da42104fSChris Wilson }
823da42104fSChris Wilson 
824da42104fSChris Wilson static inline void
i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object * obj,enum fb_op_origin origin)825da42104fSChris Wilson i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
826da42104fSChris Wilson 				       enum fb_op_origin origin)
827da42104fSChris Wilson {
828da42104fSChris Wilson 	if (unlikely(rcu_access_pointer(obj->frontbuffer)))
829da42104fSChris Wilson 		__i915_gem_object_invalidate_frontbuffer(obj, origin);
830da42104fSChris Wilson }
831da42104fSChris Wilson 
8325fbc2c2bSImre Deak int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size);
8335fbc2c2bSImre Deak 
83441a9c75dSChris Wilson bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj);
83541a9c75dSChris Wilson 
836213d5092SThomas Hellström void __i915_gem_free_object_rcu(struct rcu_head *head);
837213d5092SThomas Hellström 
838068396bbSThomas Hellström void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj);
839068396bbSThomas Hellström 
840213d5092SThomas Hellström void __i915_gem_free_object(struct drm_i915_gem_object *obj);
841213d5092SThomas Hellström 
842213d5092SThomas Hellström bool i915_gem_object_evictable(struct drm_i915_gem_object *obj);
843213d5092SThomas Hellström 
8442e53d7c1SThomas Hellström bool i915_gem_object_migratable(struct drm_i915_gem_object *obj);
8452e53d7c1SThomas Hellström 
846b6e913e1SThomas Hellström int i915_gem_object_migrate(struct drm_i915_gem_object *obj,
847b6e913e1SThomas Hellström 			    struct i915_gem_ww_ctx *ww,
848b6e913e1SThomas Hellström 			    enum intel_region_id id);
849999f4562SMatthew Auld int __i915_gem_object_migrate(struct drm_i915_gem_object *obj,
850999f4562SMatthew Auld 			      struct i915_gem_ww_ctx *ww,
851999f4562SMatthew Auld 			      enum intel_region_id id,
852999f4562SMatthew Auld 			      unsigned int flags);
853b6e913e1SThomas Hellström 
854b6e913e1SThomas Hellström bool i915_gem_object_can_migrate(struct drm_i915_gem_object *obj,
855b6e913e1SThomas Hellström 				 enum intel_region_id id);
856b6e913e1SThomas Hellström 
857b6e913e1SThomas Hellström int i915_gem_object_wait_migration(struct drm_i915_gem_object *obj,
858b6e913e1SThomas Hellström 				   unsigned int flags);
859b6e913e1SThomas Hellström 
860b3f450d9SMatthew Auld bool i915_gem_object_placement_possible(struct drm_i915_gem_object *obj,
861b3f450d9SMatthew Auld 					enum intel_memory_type type);
862b3f450d9SMatthew Auld 
863efeb3cafSMatthew Auld bool i915_gem_object_needs_ccs_pages(struct drm_i915_gem_object *obj);
864efeb3cafSMatthew Auld 
865cad7109aSThomas Hellström int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
8667ae03459SMatthew Auld 			 size_t size, struct intel_memory_region *mr,
8677ae03459SMatthew Auld 			 struct address_space *mapping,
8687ae03459SMatthew Auld 			 unsigned int max_segment);
869cad7109aSThomas Hellström void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping,
8707ae03459SMatthew Auld 			 bool dirty, bool backup);
8717ae03459SMatthew Auld void __shmem_writeback(size_t size, struct address_space *mapping);
8727ae03459SMatthew Auld 
873ed29c269SMaarten Lankhorst #ifdef CONFIG_MMU_NOTIFIER
874ed29c269SMaarten Lankhorst static inline bool
i915_gem_object_is_userptr(struct drm_i915_gem_object * obj)875ed29c269SMaarten Lankhorst i915_gem_object_is_userptr(struct drm_i915_gem_object *obj)
876ed29c269SMaarten Lankhorst {
877ed29c269SMaarten Lankhorst 	return obj->userptr.notifier.mm;
878ed29c269SMaarten Lankhorst }
879ed29c269SMaarten Lankhorst 
880ed29c269SMaarten Lankhorst int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj);
881ed29c269SMaarten Lankhorst int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj);
882ed29c269SMaarten Lankhorst int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj);
883ed29c269SMaarten Lankhorst #else
i915_gem_object_is_userptr(struct drm_i915_gem_object * obj)884ed29c269SMaarten Lankhorst static inline bool i915_gem_object_is_userptr(struct drm_i915_gem_object *obj) { return false; }
885ed29c269SMaarten Lankhorst 
i915_gem_object_userptr_submit_init(struct drm_i915_gem_object * obj)886ed29c269SMaarten Lankhorst static inline int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }
i915_gem_object_userptr_submit_done(struct drm_i915_gem_object * obj)887ed29c269SMaarten Lankhorst static inline int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }
i915_gem_object_userptr_validate(struct drm_i915_gem_object * obj)888ed29c269SMaarten Lankhorst static inline int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }
889ed29c269SMaarten Lankhorst 
890ed29c269SMaarten Lankhorst #endif
891ed29c269SMaarten Lankhorst 
8927b574550SJouni Högander /**
8937b574550SJouni Högander  * i915_gem_object_get_frontbuffer - Get the object's frontbuffer
8947b574550SJouni Högander  * @obj: The object whose frontbuffer to get.
8957b574550SJouni Högander  *
8967b574550SJouni Högander  * Get pointer to object's frontbuffer if such exists. Please note that RCU
8977b574550SJouni Högander  * mechanism is used to handle e.g. ongoing removal of frontbuffer pointer.
8987b574550SJouni Högander  *
8997b574550SJouni Högander  * Return: pointer to object's frontbuffer is such exists or NULL
9007b574550SJouni Högander  */
9017b574550SJouni Högander static inline struct intel_frontbuffer *
i915_gem_object_get_frontbuffer(const struct drm_i915_gem_object * obj)9027b574550SJouni Högander i915_gem_object_get_frontbuffer(const struct drm_i915_gem_object *obj)
9037b574550SJouni Högander {
9047b574550SJouni Högander 	struct intel_frontbuffer *front;
9057b574550SJouni Högander 
9067b574550SJouni Högander 	if (likely(!rcu_access_pointer(obj->frontbuffer)))
9077b574550SJouni Högander 		return NULL;
9087b574550SJouni Högander 
9097b574550SJouni Högander 	rcu_read_lock();
9107b574550SJouni Högander 	do {
9117b574550SJouni Högander 		front = rcu_dereference(obj->frontbuffer);
9127b574550SJouni Högander 		if (!front)
9137b574550SJouni Högander 			break;
9147b574550SJouni Högander 
9157b574550SJouni Högander 		if (unlikely(!kref_get_unless_zero(&front->ref)))
9167b574550SJouni Högander 			continue;
9177b574550SJouni Högander 
9187b574550SJouni Högander 		if (likely(front == rcu_access_pointer(obj->frontbuffer)))
9197b574550SJouni Högander 			break;
9207b574550SJouni Högander 
9217b574550SJouni Högander 		intel_frontbuffer_put(front);
9227b574550SJouni Högander 	} while (1);
9237b574550SJouni Högander 	rcu_read_unlock();
9247b574550SJouni Högander 
9257b574550SJouni Högander 	return front;
9267b574550SJouni Högander }
9277b574550SJouni Högander 
9287b574550SJouni Högander /**
9297b574550SJouni Högander  * i915_gem_object_set_frontbuffer - Set the object's frontbuffer
9307b574550SJouni Högander  * @obj: The object whose frontbuffer to set.
9317b574550SJouni Högander  * @front: The frontbuffer to set
9327b574550SJouni Högander  *
9337b574550SJouni Högander  * Set object's frontbuffer pointer. If frontbuffer is already set for the
9347b574550SJouni Högander  * object keep it and return it's pointer to the caller. Please note that RCU
9357b574550SJouni Högander  * mechanism is used to handle e.g. ongoing removal of frontbuffer pointer. This
9367b574550SJouni Högander  * function is protected by i915->display.fb_tracking.lock
9377b574550SJouni Högander  *
9387b574550SJouni Högander  * Return: pointer to frontbuffer which was set.
9397b574550SJouni Högander  */
9407b574550SJouni Högander static inline struct intel_frontbuffer *
i915_gem_object_set_frontbuffer(struct drm_i915_gem_object * obj,struct intel_frontbuffer * front)9417b574550SJouni Högander i915_gem_object_set_frontbuffer(struct drm_i915_gem_object *obj,
9427b574550SJouni Högander 				struct intel_frontbuffer *front)
9437b574550SJouni Högander {
9447b574550SJouni Högander 	struct intel_frontbuffer *cur = front;
9457b574550SJouni Högander 
9467b574550SJouni Högander 	if (!front) {
9477b574550SJouni Högander 		RCU_INIT_POINTER(obj->frontbuffer, NULL);
9487b574550SJouni Högander 	} else if (rcu_access_pointer(obj->frontbuffer)) {
9497b574550SJouni Högander 		cur = rcu_dereference_protected(obj->frontbuffer, true);
9507b574550SJouni Högander 		kref_get(&cur->ref);
9517b574550SJouni Högander 	} else {
9527b574550SJouni Högander 		drm_gem_object_get(intel_bo_to_drm_bo(obj));
9537b574550SJouni Högander 		rcu_assign_pointer(obj->frontbuffer, front);
9547b574550SJouni Högander 	}
9557b574550SJouni Högander 
9567b574550SJouni Högander 	return cur;
9577b574550SJouni Högander }
9587b574550SJouni Högander 
95998932149SChris Wilson #endif
960