xref: /linux/drivers/gpu/drm/xe/xe_bo.h (revision f5bd9d528ebac41a31919aa41f1a99eccb8917c8)
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 #define XE_BO_FLAG_GGTT0                BIT(18)
43 #define XE_BO_FLAG_GGTT1                BIT(19)
44 #define XE_BO_FLAG_GGTT2                BIT(20)
45 #define XE_BO_FLAG_GGTT3                BIT(21)
46 #define XE_BO_FLAG_GGTT_ALL             (XE_BO_FLAG_GGTT0 | \
47 					 XE_BO_FLAG_GGTT1 | \
48 					 XE_BO_FLAG_GGTT2 | \
49 					 XE_BO_FLAG_GGTT3)
50 
51 /* this one is trigger internally only */
52 #define XE_BO_FLAG_INTERNAL_TEST	BIT(30)
53 #define XE_BO_FLAG_INTERNAL_64K		BIT(31)
54 
55 #define XE_BO_FLAG_GGTTx(tile) \
56 	(XE_BO_FLAG_GGTT0 << (tile)->id)
57 
58 #define XE_PTE_SHIFT			12
59 #define XE_PAGE_SIZE			(1 << XE_PTE_SHIFT)
60 #define XE_PTE_MASK			(XE_PAGE_SIZE - 1)
61 #define XE_PDE_SHIFT			(XE_PTE_SHIFT - 3)
62 #define XE_PDES				(1 << XE_PDE_SHIFT)
63 #define XE_PDE_MASK			(XE_PDES - 1)
64 
65 #define XE_64K_PTE_SHIFT		16
66 #define XE_64K_PAGE_SIZE		(1 << XE_64K_PTE_SHIFT)
67 #define XE_64K_PTE_MASK			(XE_64K_PAGE_SIZE - 1)
68 #define XE_64K_PDE_MASK			(XE_PDE_MASK >> 4)
69 
70 #define XE_PL_SYSTEM		TTM_PL_SYSTEM
71 #define XE_PL_TT		TTM_PL_TT
72 #define XE_PL_VRAM0		TTM_PL_VRAM
73 #define XE_PL_VRAM1		(XE_PL_VRAM0 + 1)
74 #define XE_PL_STOLEN		(TTM_NUM_MEM_TYPES - 1)
75 
76 #define XE_BO_PROPS_INVALID	(-1)
77 
78 struct sg_table;
79 
80 struct xe_bo *xe_bo_alloc(void);
81 void xe_bo_free(struct xe_bo *bo);
82 
83 struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
84 				     struct xe_tile *tile, struct dma_resv *resv,
85 				     struct ttm_lru_bulk_move *bulk, size_t size,
86 				     u16 cpu_caching, enum ttm_bo_type type,
87 				     u32 flags);
88 struct xe_bo *
89 xe_bo_create_locked_range(struct xe_device *xe,
90 			  struct xe_tile *tile, struct xe_vm *vm,
91 			  size_t size, u64 start, u64 end,
92 			  enum ttm_bo_type type, u32 flags, u64 alignment);
93 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile,
94 				  struct xe_vm *vm, size_t size,
95 				  enum ttm_bo_type type, u32 flags);
96 struct xe_bo *xe_bo_create(struct xe_device *xe, struct xe_tile *tile,
97 			   struct xe_vm *vm, size_t size,
98 			   enum ttm_bo_type type, u32 flags);
99 struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_tile *tile,
100 				struct xe_vm *vm, size_t size,
101 				u16 cpu_caching,
102 				u32 flags);
103 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
104 				   struct xe_vm *vm, size_t size,
105 				   enum ttm_bo_type type, u32 flags);
106 struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile,
107 				      struct xe_vm *vm, size_t size, u64 offset,
108 				      enum ttm_bo_type type, u32 flags);
109 struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe,
110 					      struct xe_tile *tile,
111 					      struct xe_vm *vm,
112 					      size_t size, u64 offset,
113 					      enum ttm_bo_type type, u32 flags,
114 					      u64 alignment);
115 struct xe_bo *xe_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
116 				     const void *data, size_t size,
117 				     enum ttm_bo_type type, u32 flags);
118 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
119 					   size_t size, u32 flags);
120 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
121 					     const void *data, size_t size, u32 flags);
122 int xe_managed_bo_reinit_in_vram(struct xe_device *xe, struct xe_tile *tile, struct xe_bo **src);
123 
124 int xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
125 			      u32 bo_flags);
126 
127 static inline struct xe_bo *ttm_to_xe_bo(const struct ttm_buffer_object *bo)
128 {
129 	return container_of(bo, struct xe_bo, ttm);
130 }
131 
132 static inline struct xe_bo *gem_to_xe_bo(const struct drm_gem_object *obj)
133 {
134 	return container_of(obj, struct xe_bo, ttm.base);
135 }
136 
137 #define xe_bo_device(bo) ttm_to_xe_device((bo)->ttm.bdev)
138 
139 static inline struct xe_bo *xe_bo_get(struct xe_bo *bo)
140 {
141 	if (bo)
142 		drm_gem_object_get(&bo->ttm.base);
143 
144 	return bo;
145 }
146 
147 void xe_bo_put(struct xe_bo *bo);
148 
149 /*
150  * xe_bo_get_unless_zero() - Conditionally obtain a GEM object refcount on an
151  * xe bo
152  * @bo: The bo for which we want to obtain a refcount.
153  *
154  * There is a short window between where the bo's GEM object refcount reaches
155  * zero and where we put the final ttm_bo reference. Code in the eviction- and
156  * shrinking path should therefore attempt to grab a gem object reference before
157  * trying to use members outside of the base class ttm object. This function is
158  * intended for that purpose. On successful return, this function must be paired
159  * with an xe_bo_put().
160  *
161  * Return: @bo on success, NULL on failure.
162  */
163 static inline __must_check struct xe_bo *xe_bo_get_unless_zero(struct xe_bo *bo)
164 {
165 	if (!bo || !kref_get_unless_zero(&bo->ttm.base.refcount))
166 		return NULL;
167 
168 	return bo;
169 }
170 
171 static inline void __xe_bo_unset_bulk_move(struct xe_bo *bo)
172 {
173 	if (bo)
174 		ttm_bo_set_bulk_move(&bo->ttm, NULL);
175 }
176 
177 static inline void xe_bo_assert_held(struct xe_bo *bo)
178 {
179 	if (bo)
180 		dma_resv_assert_held((bo)->ttm.base.resv);
181 }
182 
183 int xe_bo_lock(struct xe_bo *bo, bool intr);
184 
185 void xe_bo_unlock(struct xe_bo *bo);
186 
187 static inline void xe_bo_unlock_vm_held(struct xe_bo *bo)
188 {
189 	if (bo) {
190 		XE_WARN_ON(bo->vm && bo->ttm.base.resv != xe_vm_resv(bo->vm));
191 		if (bo->vm)
192 			xe_vm_assert_held(bo->vm);
193 		else
194 			dma_resv_unlock(bo->ttm.base.resv);
195 	}
196 }
197 
198 int xe_bo_pin_external(struct xe_bo *bo);
199 int xe_bo_pin(struct xe_bo *bo);
200 void xe_bo_unpin_external(struct xe_bo *bo);
201 void xe_bo_unpin(struct xe_bo *bo);
202 int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict);
203 
204 static inline bool xe_bo_is_pinned(struct xe_bo *bo)
205 {
206 	return bo->ttm.pin_count;
207 }
208 
209 static inline void xe_bo_unpin_map_no_vm(struct xe_bo *bo)
210 {
211 	if (likely(bo)) {
212 		xe_bo_lock(bo, false);
213 		xe_bo_unpin(bo);
214 		xe_bo_unlock(bo);
215 
216 		xe_bo_put(bo);
217 	}
218 }
219 
220 bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo);
221 dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size);
222 dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size);
223 
224 static inline dma_addr_t
225 xe_bo_main_addr(struct xe_bo *bo, size_t page_size)
226 {
227 	return xe_bo_addr(bo, 0, page_size);
228 }
229 
230 static inline u32
231 __xe_bo_ggtt_addr(struct xe_bo *bo, u8 tile_id)
232 {
233 	struct xe_ggtt_node *ggtt_node = bo->ggtt_node[tile_id];
234 
235 	if (XE_WARN_ON(!ggtt_node))
236 		return 0;
237 
238 	XE_WARN_ON(ggtt_node->base.size > bo->size);
239 	XE_WARN_ON(ggtt_node->base.start + ggtt_node->base.size > (1ull << 32));
240 	return ggtt_node->base.start;
241 }
242 
243 static inline u32
244 xe_bo_ggtt_addr(struct xe_bo *bo)
245 {
246 	xe_assert(xe_bo_device(bo), bo->tile);
247 
248 	return __xe_bo_ggtt_addr(bo, bo->tile->id);
249 }
250 
251 int xe_bo_vmap(struct xe_bo *bo);
252 void xe_bo_vunmap(struct xe_bo *bo);
253 int xe_bo_read(struct xe_bo *bo, u64 offset, void *dst, int size);
254 
255 bool mem_type_is_vram(u32 mem_type);
256 bool xe_bo_is_vram(struct xe_bo *bo);
257 bool xe_bo_is_stolen(struct xe_bo *bo);
258 bool xe_bo_is_stolen_devmem(struct xe_bo *bo);
259 bool xe_bo_has_single_placement(struct xe_bo *bo);
260 uint64_t vram_region_gpu_offset(struct ttm_resource *res);
261 
262 bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type);
263 
264 int xe_bo_migrate(struct xe_bo *bo, u32 mem_type);
265 int xe_bo_evict(struct xe_bo *bo, bool force_alloc);
266 
267 int xe_bo_evict_pinned(struct xe_bo *bo);
268 int xe_bo_restore_pinned(struct xe_bo *bo);
269 
270 extern const struct ttm_device_funcs xe_ttm_funcs;
271 extern const char *const xe_mem_type_to_name[];
272 
273 int xe_gem_create_ioctl(struct drm_device *dev, void *data,
274 			struct drm_file *file);
275 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
276 			     struct drm_file *file);
277 void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo);
278 
279 int xe_bo_dumb_create(struct drm_file *file_priv,
280 		      struct drm_device *dev,
281 		      struct drm_mode_create_dumb *args);
282 
283 bool xe_bo_needs_ccs_pages(struct xe_bo *bo);
284 
285 static inline size_t xe_bo_ccs_pages_start(struct xe_bo *bo)
286 {
287 	return PAGE_ALIGN(bo->ttm.base.size);
288 }
289 
290 static inline bool xe_bo_has_pages(struct xe_bo *bo)
291 {
292 	if ((bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm)) ||
293 	    xe_bo_is_vram(bo))
294 		return true;
295 
296 	return false;
297 }
298 
299 void __xe_bo_release_dummy(struct kref *kref);
300 
301 /**
302  * xe_bo_put_deferred() - Put a buffer object with delayed final freeing
303  * @bo: The bo to put.
304  * @deferred: List to which to add the buffer object if we cannot put, or
305  * NULL if the function is to put unconditionally.
306  *
307  * Since the final freeing of an object includes both sleeping and (!)
308  * memory allocation in the dma_resv individualization, it's not ok
309  * to put an object from atomic context nor from within a held lock
310  * tainted by reclaim. In such situations we want to defer the final
311  * freeing until we've exited the restricting context, or in the worst
312  * case to a workqueue.
313  * This function either puts the object if possible without the refcount
314  * reaching zero, or adds it to the @deferred list if that was not possible.
315  * The caller needs to follow up with a call to xe_bo_put_commit() to actually
316  * put the bo iff this function returns true. It's safe to always
317  * follow up with a call to xe_bo_put_commit().
318  * TODO: It's TTM that is the villain here. Perhaps TTM should add an
319  * interface like this.
320  *
321  * Return: true if @bo was the first object put on the @freed list,
322  * false otherwise.
323  */
324 static inline bool
325 xe_bo_put_deferred(struct xe_bo *bo, struct llist_head *deferred)
326 {
327 	if (!deferred) {
328 		xe_bo_put(bo);
329 		return false;
330 	}
331 
332 	if (!kref_put(&bo->ttm.base.refcount, __xe_bo_release_dummy))
333 		return false;
334 
335 	return llist_add(&bo->freed, deferred);
336 }
337 
338 void xe_bo_put_commit(struct llist_head *deferred);
339 
340 struct sg_table *xe_bo_sg(struct xe_bo *bo);
341 
342 /*
343  * xe_sg_segment_size() - Provides upper limit for sg segment size.
344  * @dev: device pointer
345  *
346  * Returns the maximum segment size for the 'struct scatterlist'
347  * elements.
348  */
349 static inline unsigned int xe_sg_segment_size(struct device *dev)
350 {
351 	struct scatterlist __maybe_unused sg;
352 	size_t max = BIT_ULL(sizeof(sg.length) * 8) - 1;
353 
354 	max = min_t(size_t, max, dma_max_mapping_size(dev));
355 
356 	/*
357 	 * The iommu_dma_map_sg() function ensures iova allocation doesn't
358 	 * cross dma segment boundary. It does so by padding some sg elements.
359 	 * This can cause overflow, ending up with sg->length being set to 0.
360 	 * Avoid this by ensuring maximum segment size is half of 'max'
361 	 * rounded down to PAGE_SIZE.
362 	 */
363 	return round_down(max / 2, PAGE_SIZE);
364 }
365 
366 /**
367  * struct xe_bo_shrink_flags - flags governing the shrink behaviour.
368  * @purge: Only purging allowed. Don't shrink if bo not purgeable.
369  * @writeback: Attempt to immediately move content to swap.
370  */
371 struct xe_bo_shrink_flags {
372 	u32 purge : 1;
373 	u32 writeback : 1;
374 };
375 
376 long xe_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
377 		  const struct xe_bo_shrink_flags flags,
378 		  unsigned long *scanned);
379 
380 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
381 /**
382  * xe_bo_is_mem_type - Whether the bo currently resides in the given
383  * TTM memory type
384  * @bo: The bo to check.
385  * @mem_type: The TTM memory type.
386  *
387  * Return: true iff the bo resides in @mem_type, false otherwise.
388  */
389 static inline bool xe_bo_is_mem_type(struct xe_bo *bo, u32 mem_type)
390 {
391 	xe_bo_assert_held(bo);
392 	return bo->ttm.resource->mem_type == mem_type;
393 }
394 #endif
395 #endif
396