xref: /linux/drivers/gpu/drm/xe/xe_bo.h (revision de848da12f752170c2ebe114804a985314fd5a6a)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #ifndef _XE_BO_H_
7 #define _XE_BO_H_
8 
9 #include <drm/ttm/ttm_tt.h>
10 
11 #include "xe_bo_types.h"
12 #include "xe_macros.h"
13 #include "xe_vm_types.h"
14 #include "xe_vm.h"
15 
16 #define XE_DEFAULT_GTT_SIZE_MB          3072ULL /* 3GB by default */
17 
18 #define XE_BO_FLAG_USER		BIT(0)
19 /* The bits below need to be contiguous, or things break */
20 #define XE_BO_FLAG_SYSTEM		BIT(1)
21 #define XE_BO_FLAG_VRAM0		BIT(2)
22 #define XE_BO_FLAG_VRAM1		BIT(3)
23 #define XE_BO_FLAG_VRAM_MASK		(XE_BO_FLAG_VRAM0 | XE_BO_FLAG_VRAM1)
24 /* -- */
25 #define XE_BO_FLAG_STOLEN		BIT(4)
26 #define XE_BO_FLAG_VRAM_IF_DGFX(tile)	(IS_DGFX(tile_to_xe(tile)) ? \
27 					 XE_BO_FLAG_VRAM0 << (tile)->id : \
28 					 XE_BO_FLAG_SYSTEM)
29 #define XE_BO_FLAG_GGTT			BIT(5)
30 #define XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE BIT(6)
31 #define XE_BO_FLAG_PINNED		BIT(7)
32 #define XE_BO_FLAG_NO_RESV_EVICT	BIT(8)
33 #define XE_BO_FLAG_DEFER_BACKING	BIT(9)
34 #define XE_BO_FLAG_SCANOUT		BIT(10)
35 #define XE_BO_FLAG_FIXED_PLACEMENT	BIT(11)
36 #define XE_BO_FLAG_PAGETABLE		BIT(12)
37 #define XE_BO_FLAG_NEEDS_CPU_ACCESS	BIT(13)
38 #define XE_BO_FLAG_NEEDS_UC		BIT(14)
39 #define XE_BO_FLAG_NEEDS_64K		BIT(15)
40 #define XE_BO_FLAG_NEEDS_2M		BIT(16)
41 #define XE_BO_FLAG_GGTT_INVALIDATE	BIT(17)
42 /* this one is trigger internally only */
43 #define XE_BO_FLAG_INTERNAL_TEST	BIT(30)
44 #define XE_BO_FLAG_INTERNAL_64K		BIT(31)
45 
46 #define XE_PTE_SHIFT			12
47 #define XE_PAGE_SIZE			(1 << XE_PTE_SHIFT)
48 #define XE_PTE_MASK			(XE_PAGE_SIZE - 1)
49 #define XE_PDE_SHIFT			(XE_PTE_SHIFT - 3)
50 #define XE_PDES				(1 << XE_PDE_SHIFT)
51 #define XE_PDE_MASK			(XE_PDES - 1)
52 
53 #define XE_64K_PTE_SHIFT		16
54 #define XE_64K_PAGE_SIZE		(1 << XE_64K_PTE_SHIFT)
55 #define XE_64K_PTE_MASK			(XE_64K_PAGE_SIZE - 1)
56 #define XE_64K_PDE_MASK			(XE_PDE_MASK >> 4)
57 
58 #define XE_PL_SYSTEM		TTM_PL_SYSTEM
59 #define XE_PL_TT		TTM_PL_TT
60 #define XE_PL_VRAM0		TTM_PL_VRAM
61 #define XE_PL_VRAM1		(XE_PL_VRAM0 + 1)
62 #define XE_PL_STOLEN		(TTM_NUM_MEM_TYPES - 1)
63 
64 #define XE_BO_PROPS_INVALID	(-1)
65 
66 struct sg_table;
67 
68 struct xe_bo *xe_bo_alloc(void);
69 void xe_bo_free(struct xe_bo *bo);
70 
71 struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
72 				     struct xe_tile *tile, struct dma_resv *resv,
73 				     struct ttm_lru_bulk_move *bulk, size_t size,
74 				     u16 cpu_caching, enum ttm_bo_type type,
75 				     u32 flags);
76 struct xe_bo *
77 xe_bo_create_locked_range(struct xe_device *xe,
78 			  struct xe_tile *tile, struct xe_vm *vm,
79 			  size_t size, u64 start, u64 end,
80 			  enum ttm_bo_type type, u32 flags);
81 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile,
82 				  struct xe_vm *vm, size_t size,
83 				  enum ttm_bo_type type, u32 flags);
84 struct xe_bo *xe_bo_create(struct xe_device *xe, struct xe_tile *tile,
85 			   struct xe_vm *vm, size_t size,
86 			   enum ttm_bo_type type, u32 flags);
87 struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_tile *tile,
88 				struct xe_vm *vm, size_t size,
89 				u16 cpu_caching,
90 				u32 flags);
91 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
92 				   struct xe_vm *vm, size_t size,
93 				   enum ttm_bo_type type, u32 flags);
94 struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,
95 				      struct xe_vm *vm, size_t size, u64 offset,
96 				      enum ttm_bo_type type, u32 flags);
97 struct xe_bo *xe_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
98 				     const void *data, size_t size,
99 				     enum ttm_bo_type type, u32 flags);
100 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
101 					   size_t size, u32 flags);
102 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
103 					     const void *data, size_t size, u32 flags);
104 int xe_managed_bo_reinit_in_vram(struct xe_device *xe, struct xe_tile *tile, struct xe_bo **src);
105 
106 int xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
107 			      u32 bo_flags);
108 
109 static inline struct xe_bo *ttm_to_xe_bo(const struct ttm_buffer_object *bo)
110 {
111 	return container_of(bo, struct xe_bo, ttm);
112 }
113 
114 static inline struct xe_bo *gem_to_xe_bo(const struct drm_gem_object *obj)
115 {
116 	return container_of(obj, struct xe_bo, ttm.base);
117 }
118 
119 #define xe_bo_device(bo) ttm_to_xe_device((bo)->ttm.bdev)
120 
121 static inline struct xe_bo *xe_bo_get(struct xe_bo *bo)
122 {
123 	if (bo)
124 		drm_gem_object_get(&bo->ttm.base);
125 
126 	return bo;
127 }
128 
129 static inline void xe_bo_put(struct xe_bo *bo)
130 {
131 	if (bo)
132 		drm_gem_object_put(&bo->ttm.base);
133 }
134 
135 static inline void __xe_bo_unset_bulk_move(struct xe_bo *bo)
136 {
137 	if (bo)
138 		ttm_bo_set_bulk_move(&bo->ttm, NULL);
139 }
140 
141 static inline void xe_bo_assert_held(struct xe_bo *bo)
142 {
143 	if (bo)
144 		dma_resv_assert_held((bo)->ttm.base.resv);
145 }
146 
147 int xe_bo_lock(struct xe_bo *bo, bool intr);
148 
149 void xe_bo_unlock(struct xe_bo *bo);
150 
151 static inline void xe_bo_unlock_vm_held(struct xe_bo *bo)
152 {
153 	if (bo) {
154 		XE_WARN_ON(bo->vm && bo->ttm.base.resv != xe_vm_resv(bo->vm));
155 		if (bo->vm)
156 			xe_vm_assert_held(bo->vm);
157 		else
158 			dma_resv_unlock(bo->ttm.base.resv);
159 	}
160 }
161 
162 int xe_bo_pin_external(struct xe_bo *bo);
163 int xe_bo_pin(struct xe_bo *bo);
164 void xe_bo_unpin_external(struct xe_bo *bo);
165 void xe_bo_unpin(struct xe_bo *bo);
166 int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict);
167 
168 static inline bool xe_bo_is_pinned(struct xe_bo *bo)
169 {
170 	return bo->ttm.pin_count;
171 }
172 
173 static inline void xe_bo_unpin_map_no_vm(struct xe_bo *bo)
174 {
175 	if (likely(bo)) {
176 		xe_bo_lock(bo, false);
177 		xe_bo_unpin(bo);
178 		xe_bo_unlock(bo);
179 
180 		xe_bo_put(bo);
181 	}
182 }
183 
184 bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo);
185 dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size);
186 dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size);
187 
188 static inline dma_addr_t
189 xe_bo_main_addr(struct xe_bo *bo, size_t page_size)
190 {
191 	return xe_bo_addr(bo, 0, page_size);
192 }
193 
194 static inline u32
195 xe_bo_ggtt_addr(struct xe_bo *bo)
196 {
197 	if (XE_WARN_ON(!bo->ggtt_node))
198 		return 0;
199 
200 	XE_WARN_ON(bo->ggtt_node->base.size > bo->size);
201 	XE_WARN_ON(bo->ggtt_node->base.start + bo->ggtt_node->base.size > (1ull << 32));
202 	return bo->ggtt_node->base.start;
203 }
204 
205 int xe_bo_vmap(struct xe_bo *bo);
206 void xe_bo_vunmap(struct xe_bo *bo);
207 
208 bool mem_type_is_vram(u32 mem_type);
209 bool xe_bo_is_vram(struct xe_bo *bo);
210 bool xe_bo_is_stolen(struct xe_bo *bo);
211 bool xe_bo_is_stolen_devmem(struct xe_bo *bo);
212 bool xe_bo_has_single_placement(struct xe_bo *bo);
213 uint64_t vram_region_gpu_offset(struct ttm_resource *res);
214 
215 bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type);
216 
217 int xe_bo_migrate(struct xe_bo *bo, u32 mem_type);
218 int xe_bo_evict(struct xe_bo *bo, bool force_alloc);
219 
220 int xe_bo_evict_pinned(struct xe_bo *bo);
221 int xe_bo_restore_pinned(struct xe_bo *bo);
222 
223 extern const struct ttm_device_funcs xe_ttm_funcs;
224 extern const char *const xe_mem_type_to_name[];
225 
226 int xe_gem_create_ioctl(struct drm_device *dev, void *data,
227 			struct drm_file *file);
228 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
229 			     struct drm_file *file);
230 void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo);
231 
232 int xe_bo_dumb_create(struct drm_file *file_priv,
233 		      struct drm_device *dev,
234 		      struct drm_mode_create_dumb *args);
235 
236 bool xe_bo_needs_ccs_pages(struct xe_bo *bo);
237 
238 static inline size_t xe_bo_ccs_pages_start(struct xe_bo *bo)
239 {
240 	return PAGE_ALIGN(bo->ttm.base.size);
241 }
242 
243 static inline bool xe_bo_has_pages(struct xe_bo *bo)
244 {
245 	if ((bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm)) ||
246 	    xe_bo_is_vram(bo))
247 		return true;
248 
249 	return false;
250 }
251 
252 void __xe_bo_release_dummy(struct kref *kref);
253 
254 /**
255  * xe_bo_put_deferred() - Put a buffer object with delayed final freeing
256  * @bo: The bo to put.
257  * @deferred: List to which to add the buffer object if we cannot put, or
258  * NULL if the function is to put unconditionally.
259  *
260  * Since the final freeing of an object includes both sleeping and (!)
261  * memory allocation in the dma_resv individualization, it's not ok
262  * to put an object from atomic context nor from within a held lock
263  * tainted by reclaim. In such situations we want to defer the final
264  * freeing until we've exited the restricting context, or in the worst
265  * case to a workqueue.
266  * This function either puts the object if possible without the refcount
267  * reaching zero, or adds it to the @deferred list if that was not possible.
268  * The caller needs to follow up with a call to xe_bo_put_commit() to actually
269  * put the bo iff this function returns true. It's safe to always
270  * follow up with a call to xe_bo_put_commit().
271  * TODO: It's TTM that is the villain here. Perhaps TTM should add an
272  * interface like this.
273  *
274  * Return: true if @bo was the first object put on the @freed list,
275  * false otherwise.
276  */
277 static inline bool
278 xe_bo_put_deferred(struct xe_bo *bo, struct llist_head *deferred)
279 {
280 	if (!deferred) {
281 		xe_bo_put(bo);
282 		return false;
283 	}
284 
285 	if (!kref_put(&bo->ttm.base.refcount, __xe_bo_release_dummy))
286 		return false;
287 
288 	return llist_add(&bo->freed, deferred);
289 }
290 
291 void xe_bo_put_commit(struct llist_head *deferred);
292 
293 struct sg_table *xe_bo_sg(struct xe_bo *bo);
294 
295 /*
296  * xe_sg_segment_size() - Provides upper limit for sg segment size.
297  * @dev: device pointer
298  *
299  * Returns the maximum segment size for the 'struct scatterlist'
300  * elements.
301  */
302 static inline unsigned int xe_sg_segment_size(struct device *dev)
303 {
304 	struct scatterlist __maybe_unused sg;
305 	size_t max = BIT_ULL(sizeof(sg.length) * 8) - 1;
306 
307 	max = min_t(size_t, max, dma_max_mapping_size(dev));
308 
309 	/*
310 	 * The iommu_dma_map_sg() function ensures iova allocation doesn't
311 	 * cross dma segment boundary. It does so by padding some sg elements.
312 	 * This can cause overflow, ending up with sg->length being set to 0.
313 	 * Avoid this by ensuring maximum segment size is half of 'max'
314 	 * rounded down to PAGE_SIZE.
315 	 */
316 	return round_down(max / 2, PAGE_SIZE);
317 }
318 
319 #define i915_gem_object_flush_if_display(obj)		((void)(obj))
320 
321 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
322 /**
323  * xe_bo_is_mem_type - Whether the bo currently resides in the given
324  * TTM memory type
325  * @bo: The bo to check.
326  * @mem_type: The TTM memory type.
327  *
328  * Return: true iff the bo resides in @mem_type, false otherwise.
329  */
330 static inline bool xe_bo_is_mem_type(struct xe_bo *bo, u32 mem_type)
331 {
332 	xe_bo_assert_held(bo);
333 	return bo->ttm.resource->mem_type == mem_type;
334 }
335 #endif
336 #endif
337