xref: /linux/drivers/gpu/drm/xe/xe_bo.h (revision 9d19ca5d0e8b4a3f4b2eaa14e86a25f1c93ff35b)
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_ggtt.h"
13 #include "xe_macros.h"
14 #include "xe_validation.h"
15 #include "xe_vm_types.h"
16 #include "xe_vm.h"
17 #include "xe_vram_types.h"
18 
19 #define XE_DEFAULT_GTT_SIZE_MB          3072ULL /* 3GB by default */
20 
21 #define XE_BO_FLAG_USER		BIT(0)
22 /* The bits below need to be contiguous, or things break */
23 #define XE_BO_FLAG_SYSTEM		BIT(1)
24 #define XE_BO_FLAG_VRAM0		BIT(2)
25 #define XE_BO_FLAG_VRAM1		BIT(3)
26 #define XE_BO_FLAG_VRAM_MASK		(XE_BO_FLAG_VRAM0 | XE_BO_FLAG_VRAM1)
27 /* -- */
28 #define XE_BO_FLAG_STOLEN		BIT(4)
29 #define XE_BO_FLAG_VRAM(vram)		(XE_BO_FLAG_VRAM0 << ((vram)->id))
30 #define XE_BO_FLAG_VRAM_IF_DGFX(tile)	(IS_DGFX(tile_to_xe(tile)) ? \
31 					 XE_BO_FLAG_VRAM((tile)->mem.vram) : \
32 					 XE_BO_FLAG_SYSTEM)
33 #define XE_BO_FLAG_GGTT			BIT(5)
34 #define XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE BIT(6)
35 #define XE_BO_FLAG_PINNED		BIT(7)
36 #define XE_BO_FLAG_NO_RESV_EVICT	BIT(8)
37 #define XE_BO_FLAG_DEFER_BACKING	BIT(9)
38 #define XE_BO_FLAG_FORCE_WC		BIT(10)
39 #define XE_BO_FLAG_FIXED_PLACEMENT	BIT(11)
40 #define XE_BO_FLAG_PAGETABLE		BIT(12)
41 #define XE_BO_FLAG_NEEDS_CPU_ACCESS	BIT(13)
42 #define XE_BO_FLAG_NEEDS_UC		BIT(14)
43 #define XE_BO_FLAG_NEEDS_64K		BIT(15)
44 #define XE_BO_FLAG_NEEDS_2M		BIT(16)
45 #define XE_BO_FLAG_GGTT_INVALIDATE	BIT(17)
46 #define XE_BO_FLAG_PINNED_NORESTORE	BIT(18)
47 #define XE_BO_FLAG_PINNED_LATE_RESTORE	BIT(19)
48 #define XE_BO_FLAG_GGTT0		BIT(20)
49 #define XE_BO_FLAG_GGTT1		BIT(21)
50 #define XE_BO_FLAG_GGTT2		BIT(22)
51 #define XE_BO_FLAG_GGTT3		BIT(23)
52 #define XE_BO_FLAG_CPU_ADDR_MIRROR	BIT(24)
53 #define XE_BO_FLAG_FORCE_USER_VRAM	BIT(25)
54 #define XE_BO_FLAG_NO_COMPRESSION	BIT(26)
55 
56 /* this one is trigger internally only */
57 #define XE_BO_FLAG_INTERNAL_TEST	BIT(30)
58 #define XE_BO_FLAG_INTERNAL_64K		BIT(31)
59 
60 #define XE_BO_FLAG_GGTT_ALL		(XE_BO_FLAG_GGTT0 | \
61 					 XE_BO_FLAG_GGTT1 | \
62 					 XE_BO_FLAG_GGTT2 | \
63 					 XE_BO_FLAG_GGTT3)
64 
65 #define XE_BO_FLAG_GGTTx(tile) \
66 	(XE_BO_FLAG_GGTT0 << (tile)->id)
67 
68 #define XE_PTE_SHIFT			12
69 #define XE_PAGE_SIZE			(1 << XE_PTE_SHIFT)
70 #define XE_PTE_MASK			(XE_PAGE_SIZE - 1)
71 #define XE_PDE_SHIFT			(XE_PTE_SHIFT - 3)
72 #define XE_PDES				(1 << XE_PDE_SHIFT)
73 #define XE_PDE_MASK			(XE_PDES - 1)
74 
75 #define XE_64K_PTE_SHIFT		16
76 #define XE_64K_PAGE_SIZE		(1 << XE_64K_PTE_SHIFT)
77 #define XE_64K_PTE_MASK			(XE_64K_PAGE_SIZE - 1)
78 #define XE_64K_PDE_MASK			(XE_PDE_MASK >> 4)
79 
80 #define XE_PL_SYSTEM		TTM_PL_SYSTEM
81 #define XE_PL_TT		TTM_PL_TT
82 #define XE_PL_VRAM0		TTM_PL_VRAM
83 #define XE_PL_VRAM1		(XE_PL_VRAM0 + 1)
84 #define XE_PL_STOLEN		(TTM_NUM_MEM_TYPES - 1)
85 
86 #define XE_BO_PROPS_INVALID	(-1)
87 
88 #define XE_PCI_BARRIER_MMAP_OFFSET	(0x50 << XE_PTE_SHIFT)
89 
90 /**
91  * enum xe_madv_purgeable_state - Buffer object purgeable state enumeration
92  *
93  * This enum defines the possible purgeable states for a buffer object,
94  * allowing userspace to provide memory usage hints to the kernel for
95  * better memory management under pressure.
96  *
97  * @XE_MADV_PURGEABLE_WILLNEED: The buffer object is needed and should not be purged.
98  * This is the default state.
99  * @XE_MADV_PURGEABLE_DONTNEED: The buffer object is not currently needed and can be
100  * purged by the kernel under memory pressure.
101  * @XE_MADV_PURGEABLE_PURGED: The buffer object has been purged by the kernel.
102  *
103  * Accessing a purged buffer will result in an error. Per i915 semantics,
104  * once purged, a BO remains permanently invalid and must be destroyed and recreated.
105  */
106 enum xe_madv_purgeable_state {
107 	XE_MADV_PURGEABLE_WILLNEED,
108 	XE_MADV_PURGEABLE_DONTNEED,
109 	XE_MADV_PURGEABLE_PURGED,
110 };
111 
112 struct sg_table;
113 
114 struct xe_bo *xe_bo_alloc(void);
115 void xe_bo_free(struct xe_bo *bo);
116 
117 struct xe_bo *xe_bo_init_locked(struct xe_device *xe, struct xe_bo *bo,
118 				struct xe_tile *tile, struct dma_resv *resv,
119 				struct ttm_lru_bulk_move *bulk, size_t size,
120 				u16 cpu_caching, enum ttm_bo_type type,
121 				u32 flags, struct dma_buf *dma_buf,
122 				struct drm_exec *exec);
123 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile,
124 				  struct xe_vm *vm, size_t size,
125 				  enum ttm_bo_type type, u32 flags,
126 				  struct drm_exec *exec);
127 struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_vm *vm, size_t size,
128 				u16 cpu_caching, u32 flags, struct drm_exec *exec);
129 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
130 				   struct xe_vm *vm, size_t size,
131 				   enum ttm_bo_type type, u32 flags,
132 				   struct drm_exec *exec);
133 struct xe_bo *xe_bo_create_pin_map_novm(struct xe_device *xe, struct xe_tile *tile,
134 					size_t size, enum ttm_bo_type type, u32 flags,
135 					bool intr);
136 struct xe_bo *xe_bo_create_pin_range_novm(struct xe_device *xe, struct xe_tile *tile,
137 					  size_t size, u64 start, u64 end,
138 					  enum ttm_bo_type type, u32 flags);
139 struct xe_bo *
140 xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile *tile,
141 			     size_t size, u64 offset, enum ttm_bo_type type,
142 			     u32 flags, u64 alignment, bool intr);
143 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
144 					   size_t size, u32 flags);
145 void xe_managed_bo_unpin_map_no_vm(struct xe_bo *bo);
146 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
147 					     const void *data, size_t size, u32 flags);
148 int xe_managed_bo_reinit_in_vram(struct xe_device *xe, struct xe_tile *tile, struct xe_bo **src);
149 
150 int xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
151 			      u32 bo_flags, enum ttm_bo_type type);
152 
153 static inline struct xe_bo *ttm_to_xe_bo(const struct ttm_buffer_object *bo)
154 {
155 	return container_of(bo, struct xe_bo, ttm);
156 }
157 
158 static inline struct xe_bo *gem_to_xe_bo(const struct drm_gem_object *obj)
159 {
160 	return container_of(obj, struct xe_bo, ttm.base);
161 }
162 
163 #define xe_bo_device(bo) ttm_to_xe_device((bo)->ttm.bdev)
164 
165 static inline struct xe_bo *xe_bo_get(struct xe_bo *bo)
166 {
167 	if (bo)
168 		drm_gem_object_get(&bo->ttm.base);
169 
170 	return bo;
171 }
172 
173 void xe_bo_put(struct xe_bo *bo);
174 
175 /*
176  * xe_bo_get_unless_zero() - Conditionally obtain a GEM object refcount on an
177  * xe bo
178  * @bo: The bo for which we want to obtain a refcount.
179  *
180  * There is a short window between where the bo's GEM object refcount reaches
181  * zero and where we put the final ttm_bo reference. Code in the eviction- and
182  * shrinking path should therefore attempt to grab a gem object reference before
183  * trying to use members outside of the base class ttm object. This function is
184  * intended for that purpose. On successful return, this function must be paired
185  * with an xe_bo_put().
186  *
187  * Return: @bo on success, NULL on failure.
188  */
189 static inline __must_check struct xe_bo *xe_bo_get_unless_zero(struct xe_bo *bo)
190 {
191 	if (!bo || !kref_get_unless_zero(&bo->ttm.base.refcount))
192 		return NULL;
193 
194 	return bo;
195 }
196 
197 static inline void __xe_bo_unset_bulk_move(struct xe_bo *bo)
198 {
199 	if (bo)
200 		ttm_bo_set_bulk_move(&bo->ttm, NULL);
201 }
202 
203 static inline void xe_bo_assert_held(struct xe_bo *bo)
204 {
205 	if (bo)
206 		dma_resv_assert_held((bo)->ttm.base.resv);
207 }
208 
209 int xe_bo_lock(struct xe_bo *bo, bool intr);
210 
211 void xe_bo_unlock(struct xe_bo *bo);
212 
213 static inline void xe_bo_unlock_vm_held(struct xe_bo *bo)
214 {
215 	if (bo) {
216 		XE_WARN_ON(bo->vm && bo->ttm.base.resv != xe_vm_resv(bo->vm));
217 		if (bo->vm)
218 			xe_vm_assert_held(bo->vm);
219 		else
220 			dma_resv_unlock(bo->ttm.base.resv);
221 	}
222 }
223 
224 int xe_bo_pin_external(struct xe_bo *bo, bool in_place, struct drm_exec *exec);
225 int xe_bo_pin(struct xe_bo *bo, struct drm_exec *exec);
226 void xe_bo_unpin_external(struct xe_bo *bo);
227 void xe_bo_unpin(struct xe_bo *bo);
228 int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict,
229 		   struct drm_exec *exec);
230 
231 static inline bool xe_bo_is_pinned(struct xe_bo *bo)
232 {
233 	return bo->ttm.pin_count;
234 }
235 
236 static inline bool xe_bo_is_protected(const struct xe_bo *bo)
237 {
238 	return bo->pxp_key_instance;
239 }
240 
241 /**
242  * xe_bo_is_purged() - Check if buffer object has been purged
243  * @bo: The buffer object to check
244  *
245  * Checks if the buffer object's backing store has been discarded by the
246  * kernel due to memory pressure after being marked as purgeable (DONTNEED).
247  * Once purged, the BO cannot be restored and any attempt to use it will fail.
248  *
249  * Context: Caller must hold the BO's dma-resv lock
250  * Return: true if the BO has been purged, false otherwise
251  */
252 static inline bool xe_bo_is_purged(struct xe_bo *bo)
253 {
254 	xe_bo_assert_held(bo);
255 	return bo->purgeable.state == XE_MADV_PURGEABLE_PURGED;
256 }
257 
258 /**
259  * xe_bo_madv_is_dontneed() - Check if BO is marked as DONTNEED
260  * @bo: The buffer object to check
261  *
262  * Checks if userspace has marked this BO as DONTNEED (i.e., its contents
263  * are not currently needed and can be discarded under memory pressure).
264  * This is used internally to decide whether a BO is eligible for purging.
265  *
266  * Context: Caller must hold the BO's dma-resv lock
267  * Return: true if the BO is marked DONTNEED, false otherwise
268  */
269 static inline bool xe_bo_madv_is_dontneed(struct xe_bo *bo)
270 {
271 	xe_bo_assert_held(bo);
272 	return bo->purgeable.state == XE_MADV_PURGEABLE_DONTNEED;
273 }
274 
275 void xe_bo_set_purgeable_state(struct xe_bo *bo, enum xe_madv_purgeable_state new_state);
276 
277 /**
278  * xe_bo_willneed_get_locked() - Acquire a WILLNEED holder on a BO
279  * @bo: Buffer object
280  *
281  * Increments willneed_count and, on a 0->1 transition, promotes the BO
282  * from DONTNEED to WILLNEED. PURGED is terminal and is never modified.
283  *
284  * Caller must hold the BO's dma-resv lock.
285  */
286 static inline void xe_bo_willneed_get_locked(struct xe_bo *bo)
287 {
288 	xe_bo_assert_held(bo);
289 
290 	/* Imported BOs are owned externally; do not track purgeability. */
291 	if (drm_gem_is_imported(&bo->ttm.base))
292 		return;
293 
294 	if (bo->purgeable.willneed_count++ == 0 && xe_bo_madv_is_dontneed(bo))
295 		xe_bo_set_purgeable_state(bo, XE_MADV_PURGEABLE_WILLNEED);
296 }
297 
298 /**
299  * xe_bo_willneed_put_locked() - Release a WILLNEED holder on a BO
300  * @bo: Buffer object
301  *
302  * Decrements willneed_count and, on a 1->0 transition, marks the BO
303  * DONTNEED only if it still has VMAs (implying all active VMAs are
304  * DONTNEED). If the last VMA is being removed, preserve the current BO
305  * state to match the previous VMA-walk semantics.
306  *
307  * PURGED is terminal and the BO state is never modified.
308  *
309  * Caller must hold the BO's dma-resv lock.
310  */
311 static inline void xe_bo_willneed_put_locked(struct xe_bo *bo)
312 {
313 	xe_bo_assert_held(bo);
314 
315 	if (drm_gem_is_imported(&bo->ttm.base))
316 		return;
317 
318 	xe_assert(xe_bo_device(bo), bo->purgeable.willneed_count > 0);
319 	if (--bo->purgeable.willneed_count == 0 && bo->purgeable.vma_count > 0 &&
320 	    !xe_bo_is_purged(bo))
321 		xe_bo_set_purgeable_state(bo, XE_MADV_PURGEABLE_DONTNEED);
322 }
323 
324 /**
325  * xe_bo_vma_count_inc_locked() - Account a new VMA on a BO
326  * @bo: Buffer object
327  *
328  * Increments vma_count.
329  *
330  * Caller must hold the BO's dma-resv lock.
331  */
332 static inline void xe_bo_vma_count_inc_locked(struct xe_bo *bo)
333 {
334 	xe_bo_assert_held(bo);
335 
336 	if (drm_gem_is_imported(&bo->ttm.base))
337 		return;
338 
339 	bo->purgeable.vma_count++;
340 }
341 
342 /**
343  * xe_bo_vma_count_dec_locked() - Account a VMA removal on a BO
344  * @bo: Buffer object
345  *
346  * Decrements vma_count.
347  *
348  * Caller must hold the BO's dma-resv lock.
349  */
350 static inline void xe_bo_vma_count_dec_locked(struct xe_bo *bo)
351 {
352 	xe_bo_assert_held(bo);
353 
354 	if (drm_gem_is_imported(&bo->ttm.base))
355 		return;
356 
357 	xe_assert(xe_bo_device(bo), bo->purgeable.vma_count > 0);
358 	bo->purgeable.vma_count--;
359 }
360 
361 static inline void xe_bo_unpin_map_no_vm(struct xe_bo *bo)
362 {
363 	if (likely(bo)) {
364 		xe_bo_lock(bo, false);
365 		xe_bo_unpin(bo);
366 		xe_bo_unlock(bo);
367 
368 		xe_bo_put(bo);
369 	}
370 }
371 
372 bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo);
373 dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size);
374 dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size);
375 
376 static inline dma_addr_t
377 xe_bo_main_addr(struct xe_bo *bo, size_t page_size)
378 {
379 	return xe_bo_addr(bo, 0, page_size);
380 }
381 
382 /**
383  * xe_bo_size() - Xe BO size
384  * @bo: The bo object.
385  *
386  * Simple helper to return Xe BO's size.
387  *
388  * Return: Xe BO's size
389  */
390 static inline size_t xe_bo_size(struct xe_bo *bo)
391 {
392 	return bo->ttm.base.size;
393 }
394 
395 static inline u32
396 __xe_bo_ggtt_addr(struct xe_bo *bo, u8 tile_id)
397 {
398 	struct xe_ggtt_node *ggtt_node = bo->ggtt_node[tile_id];
399 	u64 offset;
400 
401 	if (XE_WARN_ON(!ggtt_node))
402 		return 0;
403 
404 	offset = xe_ggtt_node_addr(ggtt_node);
405 	XE_WARN_ON(offset + xe_bo_size(bo) > (1ull << 32));
406 	return offset;
407 }
408 
409 static inline u32
410 xe_bo_ggtt_addr(struct xe_bo *bo)
411 {
412 	xe_assert(xe_bo_device(bo), bo->tile);
413 
414 	return __xe_bo_ggtt_addr(bo, bo->tile->id);
415 }
416 
417 int xe_bo_vmap(struct xe_bo *bo);
418 void xe_bo_vunmap(struct xe_bo *bo);
419 int xe_bo_read(struct xe_bo *bo, u64 offset, void *dst, int size);
420 
421 bool mem_type_is_vram(u32 mem_type);
422 bool xe_bo_is_vram(struct xe_bo *bo);
423 bool xe_bo_is_visible_vram(struct xe_bo *bo);
424 bool xe_bo_is_stolen(struct xe_bo *bo);
425 bool xe_bo_is_stolen_devmem(struct xe_bo *bo);
426 bool xe_bo_is_vm_bound(struct xe_bo *bo);
427 bool xe_bo_has_single_placement(struct xe_bo *bo);
428 uint64_t vram_region_gpu_offset(struct ttm_resource *res);
429 
430 bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type);
431 
432 int xe_bo_migrate(struct xe_bo *bo, u32 mem_type, struct ttm_operation_ctx *ctc,
433 		  struct drm_exec *exec);
434 int xe_bo_evict(struct xe_bo *bo, struct drm_exec *exec);
435 
436 int xe_bo_evict_pinned(struct xe_bo *bo);
437 int xe_bo_notifier_prepare_pinned(struct xe_bo *bo);
438 int xe_bo_notifier_unprepare_pinned(struct xe_bo *bo);
439 int xe_bo_restore_pinned(struct xe_bo *bo);
440 
441 int xe_bo_dma_unmap_pinned(struct xe_bo *bo);
442 
443 extern const struct ttm_device_funcs xe_ttm_funcs;
444 extern const char *const xe_mem_type_to_name[];
445 
446 int xe_gem_create_ioctl(struct drm_device *dev, void *data,
447 			struct drm_file *file);
448 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
449 			     struct drm_file *file);
450 void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo);
451 
452 int xe_bo_dumb_create(struct drm_file *file_priv,
453 		      struct drm_device *dev,
454 		      struct drm_mode_create_dumb *args);
455 
456 bool xe_bo_needs_ccs_pages(struct xe_bo *bo);
457 
458 int xe_bo_decompress(struct xe_bo *bo);
459 
460 static inline size_t xe_bo_ccs_pages_start(struct xe_bo *bo)
461 {
462 	return PAGE_ALIGN(xe_bo_size(bo));
463 }
464 
465 /**
466  * xe_bo_has_valid_ccs_bb - Check if CCS's BBs were setup for the BO.
467  * @bo: the &xe_bo to check
468  *
469  * The CCS's BBs should only be setup by the driver VF, but it is safe
470  * to call this function also by non-VF driver.
471  *
472  * Return: true iff the CCS's BBs are setup, false otherwise.
473  */
474 static inline bool xe_bo_has_valid_ccs_bb(struct xe_bo *bo)
475 {
476 	return bo->bb_ccs[XE_SRIOV_VF_CCS_READ_CTX] &&
477 	       bo->bb_ccs[XE_SRIOV_VF_CCS_WRITE_CTX];
478 }
479 
480 static inline bool xe_bo_has_pages(struct xe_bo *bo)
481 {
482 	if ((bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm)) ||
483 	    xe_bo_is_vram(bo))
484 		return true;
485 
486 	return false;
487 }
488 
489 void __xe_bo_release_dummy(struct kref *kref);
490 
491 /**
492  * xe_bo_put_deferred() - Put a buffer object with delayed final freeing
493  * @bo: The bo to put.
494  * @deferred: List to which to add the buffer object if we cannot put, or
495  * NULL if the function is to put unconditionally.
496  *
497  * Since the final freeing of an object includes both sleeping and (!)
498  * memory allocation in the dma_resv individualization, it's not ok
499  * to put an object from atomic context nor from within a held lock
500  * tainted by reclaim. In such situations we want to defer the final
501  * freeing until we've exited the restricting context, or in the worst
502  * case to a workqueue.
503  * This function either puts the object if possible without the refcount
504  * reaching zero, or adds it to the @deferred list if that was not possible.
505  * The caller needs to follow up with a call to xe_bo_put_commit() to actually
506  * put the bo iff this function returns true. It's safe to always
507  * follow up with a call to xe_bo_put_commit().
508  * TODO: It's TTM that is the villain here. Perhaps TTM should add an
509  * interface like this.
510  *
511  * Return: true if @bo was the first object put on the @freed list,
512  * false otherwise.
513  */
514 static inline bool
515 xe_bo_put_deferred(struct xe_bo *bo, struct llist_head *deferred)
516 {
517 	if (!deferred) {
518 		xe_bo_put(bo);
519 		return false;
520 	}
521 
522 	if (!kref_put(&bo->ttm.base.refcount, __xe_bo_release_dummy))
523 		return false;
524 
525 	return llist_add(&bo->freed, deferred);
526 }
527 
528 void xe_bo_put_commit(struct llist_head *deferred);
529 
530 /**
531  * xe_bo_put_async() - Put BO async
532  * @bo: The bo to put.
533  *
534  * Put BO async, the final put is deferred to a worker to exit an IRQ context.
535  */
536 static inline void
537 xe_bo_put_async(struct xe_bo *bo)
538 {
539 	struct xe_bo_dev *bo_device = &xe_bo_device(bo)->bo_device;
540 
541 	if (xe_bo_put_deferred(bo, &bo_device->async_list))
542 		schedule_work(&bo_device->async_free);
543 }
544 
545 void xe_bo_dev_init(struct xe_bo_dev *bo_device);
546 
547 void xe_bo_dev_fini(struct xe_bo_dev *bo_device);
548 
549 struct sg_table *xe_bo_sg(struct xe_bo *bo);
550 
551 /*
552  * xe_sg_segment_size() - Provides upper limit for sg segment size.
553  * @dev: device pointer
554  *
555  * Returns the maximum segment size for the 'struct scatterlist'
556  * elements.
557  */
558 static inline unsigned int xe_sg_segment_size(struct device *dev)
559 {
560 	struct scatterlist __maybe_unused sg;
561 	size_t max = BIT_ULL(sizeof(sg.length) * 8) - 1;
562 
563 	max = min_t(size_t, max, dma_max_mapping_size(dev));
564 
565 	/*
566 	 * The iommu_dma_map_sg() function ensures iova allocation doesn't
567 	 * cross dma segment boundary. It does so by padding some sg elements.
568 	 * This can cause overflow, ending up with sg->length being set to 0.
569 	 * Avoid this by ensuring maximum segment size is half of 'max'
570 	 * rounded down to PAGE_SIZE.
571 	 */
572 	return round_down(max / 2, PAGE_SIZE);
573 }
574 
575 /**
576  * struct xe_bo_shrink_flags - flags governing the shrink behaviour.
577  * @purge: Only purging allowed. Don't shrink if bo not purgeable.
578  * @writeback: Attempt to immediately move content to swap.
579  */
580 struct xe_bo_shrink_flags {
581 	u32 purge : 1;
582 	u32 writeback : 1;
583 };
584 
585 long xe_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
586 		  const struct xe_bo_shrink_flags flags,
587 		  unsigned long *scanned);
588 
589 /**
590  * xe_bo_is_mem_type - Whether the bo currently resides in the given
591  * TTM memory type
592  * @bo: The bo to check.
593  * @mem_type: The TTM memory type.
594  *
595  * Return: true iff the bo resides in @mem_type, false otherwise.
596  */
597 static inline bool xe_bo_is_mem_type(struct xe_bo *bo, u32 mem_type)
598 {
599 	xe_bo_assert_held(bo);
600 	return bo->ttm.resource->mem_type == mem_type;
601 }
602 #endif
603