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