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_validation.h"
14#include "xe_vm_types.h"
15#include "xe_vm.h"
16#include "xe_vram_types.h"
17
18#define XE_DEFAULT_GTT_SIZE_MB 3072ULL /* 3GB by default */
19
20#define XE_BO_FLAG_USER BIT(0)
21/* The bits below need to be contiguous, or things break */
22#define XE_BO_FLAG_SYSTEM BIT(1)
23#define XE_BO_FLAG_VRAM0 BIT(2)
24#define XE_BO_FLAG_VRAM1 BIT(3)
25#define XE_BO_FLAG_VRAM_MASK (XE_BO_FLAG_VRAM0 | XE_BO_FLAG_VRAM1)
26/* -- */
27#define XE_BO_FLAG_STOLEN BIT(4)
28#define XE_BO_FLAG_VRAM(vram) (XE_BO_FLAG_VRAM0 << ((vram)->id))
29#define XE_BO_FLAG_VRAM_IF_DGFX(tile) (IS_DGFX(tile_to_xe(tile)) ? \
30 XE_BO_FLAG_VRAM((tile)->mem.vram) : \
31 XE_BO_FLAG_SYSTEM)
32#define XE_BO_FLAG_GGTT BIT(5)
33#define XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE BIT(6)
34#define XE_BO_FLAG_PINNED BIT(7)
35#define XE_BO_FLAG_NO_RESV_EVICT BIT(8)
36#define XE_BO_FLAG_DEFER_BACKING BIT(9)
37#define XE_BO_FLAG_SCANOUT BIT(10)
38#define XE_BO_FLAG_FIXED_PLACEMENT BIT(11)
39#define XE_BO_FLAG_PAGETABLE BIT(12)
40#define XE_BO_FLAG_NEEDS_CPU_ACCESS BIT(13)
41#define XE_BO_FLAG_NEEDS_UC BIT(14)
42#define XE_BO_FLAG_NEEDS_64K BIT(15)
43#define XE_BO_FLAG_NEEDS_2M BIT(16)
44#define XE_BO_FLAG_GGTT_INVALIDATE BIT(17)
45#define XE_BO_FLAG_PINNED_NORESTORE BIT(18)
46#define XE_BO_FLAG_PINNED_LATE_RESTORE BIT(19)
47#define XE_BO_FLAG_GGTT0 BIT(20)
48#define XE_BO_FLAG_GGTT1 BIT(21)
49#define XE_BO_FLAG_GGTT2 BIT(22)
50#define XE_BO_FLAG_GGTT3 BIT(23)
51#define XE_BO_FLAG_CPU_ADDR_MIRROR BIT(24)
52#define XE_BO_FLAG_FORCE_USER_VRAM BIT(25)
53
54/* this one is trigger internally only */
55#define XE_BO_FLAG_INTERNAL_TEST BIT(30)
56#define XE_BO_FLAG_INTERNAL_64K BIT(31)
57
58#define XE_BO_FLAG_GGTT_ALL (XE_BO_FLAG_GGTT0 | \
59 XE_BO_FLAG_GGTT1 | \
60 XE_BO_FLAG_GGTT2 | \
61 XE_BO_FLAG_GGTT3)
62
63#define XE_BO_FLAG_GGTTx(tile) \
64 (XE_BO_FLAG_GGTT0 << (tile)->id)
65
66#define XE_PTE_SHIFT 12
67#define XE_PAGE_SIZE (1 << XE_PTE_SHIFT)
68#define XE_PTE_MASK (XE_PAGE_SIZE - 1)
69#define XE_PDE_SHIFT (XE_PTE_SHIFT - 3)
70#define XE_PDES (1 << XE_PDE_SHIFT)
71#define XE_PDE_MASK (XE_PDES - 1)
72
73#define XE_64K_PTE_SHIFT 16
74#define XE_64K_PAGE_SIZE (1 << XE_64K_PTE_SHIFT)
75#define XE_64K_PTE_MASK (XE_64K_PAGE_SIZE - 1)
76#define XE_64K_PDE_MASK (XE_PDE_MASK >> 4)
77
78#define XE_PL_SYSTEM TTM_PL_SYSTEM
79#define XE_PL_TT TTM_PL_TT
80#define XE_PL_VRAM0 TTM_PL_VRAM
81#define XE_PL_VRAM1 (XE_PL_VRAM0 + 1)
82#define XE_PL_STOLEN (TTM_NUM_MEM_TYPES - 1)
83
84#define XE_BO_PROPS_INVALID (-1)
85
86#define XE_PCI_BARRIER_MMAP_OFFSET (0x50 << XE_PTE_SHIFT)
87
88struct sg_table;
89
90struct xe_bo *xe_bo_alloc(void);
91void xe_bo_free(struct xe_bo *bo);
92
93struct xe_bo *xe_bo_init_locked(struct xe_device *xe, struct xe_bo *bo,
94 struct xe_tile *tile, struct dma_resv *resv,
95 struct ttm_lru_bulk_move *bulk, size_t size,
96 u16 cpu_caching, enum ttm_bo_type type,
97 u32 flags, struct drm_exec *exec);
98struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile,
99 struct xe_vm *vm, size_t size,
100 enum ttm_bo_type type, u32 flags,
101 struct drm_exec *exec);
102struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_vm *vm, size_t size,
103 u16 cpu_caching, u32 flags, struct drm_exec *exec);
104struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
105 struct xe_vm *vm, size_t size,
106 enum ttm_bo_type type, u32 flags,
107 struct drm_exec *exec);
108struct xe_bo *xe_bo_create_pin_map_novm(struct xe_device *xe, struct xe_tile *tile,
109 size_t size, enum ttm_bo_type type, u32 flags,
110 bool intr);
111struct xe_bo *xe_bo_create_pin_range_novm(struct xe_device *xe, struct xe_tile *tile,
112 size_t size, u64 start, u64 end,
113 enum ttm_bo_type type, u32 flags);
114struct xe_bo *
115xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile *tile,
116 size_t size, u64 offset, enum ttm_bo_type type,
117 u32 flags, u64 alignment, bool intr);
118struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile,
119 size_t size, u32 flags);
120void xe_managed_bo_unpin_map_no_vm(struct xe_bo *bo);
121struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile,
122 const void *data, size_t size, u32 flags);
123int xe_managed_bo_reinit_in_vram(struct xe_device *xe, struct xe_tile *tile, struct xe_bo **src);
124
125int xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo,
126 u32 bo_flags, enum ttm_bo_type type);
127
128static inline struct xe_bo *ttm_to_xe_bo(const struct ttm_buffer_object *bo)
129{
130 return container_of(bo, struct xe_bo, ttm);
131}
132
133static inline struct xe_bo *gem_to_xe_bo(const struct drm_gem_object *obj)
134{
135 return container_of(obj, struct xe_bo, ttm.base);
136}
137
138#define xe_bo_device(bo) ttm_to_xe_device((bo)->ttm.bdev)
139
140static inline struct xe_bo *xe_bo_get(struct xe_bo *bo)
141{
142 if (bo)
143 drm_gem_object_get(obj: &bo->ttm.base);
144
145 return bo;
146}
147
148void xe_bo_put(struct xe_bo *bo);
149
150/*
151 * xe_bo_get_unless_zero() - Conditionally obtain a GEM object refcount on an
152 * xe bo
153 * @bo: The bo for which we want to obtain a refcount.
154 *
155 * There is a short window between where the bo's GEM object refcount reaches
156 * zero and where we put the final ttm_bo reference. Code in the eviction- and
157 * shrinking path should therefore attempt to grab a gem object reference before
158 * trying to use members outside of the base class ttm object. This function is
159 * intended for that purpose. On successful return, this function must be paired
160 * with an xe_bo_put().
161 *
162 * Return: @bo on success, NULL on failure.
163 */
164static inline __must_check struct xe_bo *xe_bo_get_unless_zero(struct xe_bo *bo)
165{
166 if (!bo || !kref_get_unless_zero(kref: &bo->ttm.base.refcount))
167 return NULL;
168
169 return bo;
170}
171
172static inline void __xe_bo_unset_bulk_move(struct xe_bo *bo)
173{
174 if (bo)
175 ttm_bo_set_bulk_move(bo: &bo->ttm, NULL);
176}
177
178static inline void xe_bo_assert_held(struct xe_bo *bo)
179{
180 if (bo)
181 dma_resv_assert_held((bo)->ttm.base.resv);
182}
183
184int xe_bo_lock(struct xe_bo *bo, bool intr);
185
186void xe_bo_unlock(struct xe_bo *bo);
187
188static inline void xe_bo_unlock_vm_held(struct xe_bo *bo)
189{
190 if (bo) {
191 XE_WARN_ON(bo->vm && bo->ttm.base.resv != xe_vm_resv(bo->vm));
192 if (bo->vm)
193 xe_vm_assert_held(bo->vm);
194 else
195 dma_resv_unlock(obj: bo->ttm.base.resv);
196 }
197}
198
199int xe_bo_pin_external(struct xe_bo *bo, bool in_place, struct drm_exec *exec);
200int xe_bo_pin(struct xe_bo *bo, struct drm_exec *exec);
201void xe_bo_unpin_external(struct xe_bo *bo);
202void xe_bo_unpin(struct xe_bo *bo);
203int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict,
204 struct drm_exec *exec);
205
206static inline bool xe_bo_is_pinned(struct xe_bo *bo)
207{
208 return bo->ttm.pin_count;
209}
210
211static inline bool xe_bo_is_protected(const struct xe_bo *bo)
212{
213 return bo->pxp_key_instance;
214}
215
216static inline void xe_bo_unpin_map_no_vm(struct xe_bo *bo)
217{
218 if (likely(bo)) {
219 xe_bo_lock(bo, intr: false);
220 xe_bo_unpin(bo);
221 xe_bo_unlock(bo);
222
223 xe_bo_put(bo);
224 }
225}
226
227bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo);
228dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size);
229dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size);
230
231static inline dma_addr_t
232xe_bo_main_addr(struct xe_bo *bo, size_t page_size)
233{
234 return xe_bo_addr(bo, offset: 0, page_size);
235}
236
237/**
238 * xe_bo_size() - Xe BO size
239 * @bo: The bo object.
240 *
241 * Simple helper to return Xe BO's size.
242 *
243 * Return: Xe BO's size
244 */
245static inline size_t xe_bo_size(struct xe_bo *bo)
246{
247 return bo->ttm.base.size;
248}
249
250static inline u32
251__xe_bo_ggtt_addr(struct xe_bo *bo, u8 tile_id)
252{
253 struct xe_ggtt_node *ggtt_node = bo->ggtt_node[tile_id];
254
255 if (XE_WARN_ON(!ggtt_node))
256 return 0;
257
258 XE_WARN_ON(ggtt_node->base.size > xe_bo_size(bo));
259 XE_WARN_ON(ggtt_node->base.start + ggtt_node->base.size > (1ull << 32));
260 return ggtt_node->base.start;
261}
262
263static inline u32
264xe_bo_ggtt_addr(struct xe_bo *bo)
265{
266 xe_assert(xe_bo_device(bo), bo->tile);
267
268 return __xe_bo_ggtt_addr(bo, tile_id: bo->tile->id);
269}
270
271int xe_bo_vmap(struct xe_bo *bo);
272void xe_bo_vunmap(struct xe_bo *bo);
273int xe_bo_read(struct xe_bo *bo, u64 offset, void *dst, int size);
274
275bool mem_type_is_vram(u32 mem_type);
276bool xe_bo_is_vram(struct xe_bo *bo);
277bool xe_bo_is_visible_vram(struct xe_bo *bo);
278bool xe_bo_is_stolen(struct xe_bo *bo);
279bool xe_bo_is_stolen_devmem(struct xe_bo *bo);
280bool xe_bo_is_vm_bound(struct xe_bo *bo);
281bool xe_bo_has_single_placement(struct xe_bo *bo);
282uint64_t vram_region_gpu_offset(struct ttm_resource *res);
283
284bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type);
285
286int xe_bo_migrate(struct xe_bo *bo, u32 mem_type, struct ttm_operation_ctx *ctc,
287 struct drm_exec *exec);
288int xe_bo_evict(struct xe_bo *bo, struct drm_exec *exec);
289
290int xe_bo_evict_pinned(struct xe_bo *bo);
291int xe_bo_notifier_prepare_pinned(struct xe_bo *bo);
292int xe_bo_notifier_unprepare_pinned(struct xe_bo *bo);
293int xe_bo_restore_pinned(struct xe_bo *bo);
294
295int xe_bo_dma_unmap_pinned(struct xe_bo *bo);
296
297extern const struct ttm_device_funcs xe_ttm_funcs;
298extern const char *const xe_mem_type_to_name[];
299
300int xe_gem_create_ioctl(struct drm_device *dev, void *data,
301 struct drm_file *file);
302int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
303 struct drm_file *file);
304void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo);
305
306int xe_bo_dumb_create(struct drm_file *file_priv,
307 struct drm_device *dev,
308 struct drm_mode_create_dumb *args);
309
310bool xe_bo_needs_ccs_pages(struct xe_bo *bo);
311
312static inline size_t xe_bo_ccs_pages_start(struct xe_bo *bo)
313{
314 return PAGE_ALIGN(xe_bo_size(bo));
315}
316
317/**
318 * xe_bo_has_valid_ccs_bb - Check if CCS's BBs were setup for the BO.
319 * @bo: the &xe_bo to check
320 *
321 * The CCS's BBs should only be setup by the driver VF, but it is safe
322 * to call this function also by non-VF driver.
323 *
324 * Return: true iff the CCS's BBs are setup, false otherwise.
325 */
326static inline bool xe_bo_has_valid_ccs_bb(struct xe_bo *bo)
327{
328 return bo->bb_ccs[XE_SRIOV_VF_CCS_READ_CTX] &&
329 bo->bb_ccs[XE_SRIOV_VF_CCS_WRITE_CTX];
330}
331
332static inline bool xe_bo_has_pages(struct xe_bo *bo)
333{
334 if ((bo->ttm.ttm && ttm_tt_is_populated(tt: bo->ttm.ttm)) ||
335 xe_bo_is_vram(bo))
336 return true;
337
338 return false;
339}
340
341void __xe_bo_release_dummy(struct kref *kref);
342
343/**
344 * xe_bo_put_deferred() - Put a buffer object with delayed final freeing
345 * @bo: The bo to put.
346 * @deferred: List to which to add the buffer object if we cannot put, or
347 * NULL if the function is to put unconditionally.
348 *
349 * Since the final freeing of an object includes both sleeping and (!)
350 * memory allocation in the dma_resv individualization, it's not ok
351 * to put an object from atomic context nor from within a held lock
352 * tainted by reclaim. In such situations we want to defer the final
353 * freeing until we've exited the restricting context, or in the worst
354 * case to a workqueue.
355 * This function either puts the object if possible without the refcount
356 * reaching zero, or adds it to the @deferred list if that was not possible.
357 * The caller needs to follow up with a call to xe_bo_put_commit() to actually
358 * put the bo iff this function returns true. It's safe to always
359 * follow up with a call to xe_bo_put_commit().
360 * TODO: It's TTM that is the villain here. Perhaps TTM should add an
361 * interface like this.
362 *
363 * Return: true if @bo was the first object put on the @freed list,
364 * false otherwise.
365 */
366static inline bool
367xe_bo_put_deferred(struct xe_bo *bo, struct llist_head *deferred)
368{
369 if (!deferred) {
370 xe_bo_put(bo);
371 return false;
372 }
373
374 if (!kref_put(kref: &bo->ttm.base.refcount, release: __xe_bo_release_dummy))
375 return false;
376
377 return llist_add(new: &bo->freed, head: deferred);
378}
379
380void xe_bo_put_commit(struct llist_head *deferred);
381
382/**
383 * xe_bo_put_async() - Put BO async
384 * @bo: The bo to put.
385 *
386 * Put BO async, the final put is deferred to a worker to exit an IRQ context.
387 */
388static inline void
389xe_bo_put_async(struct xe_bo *bo)
390{
391 struct xe_bo_dev *bo_device = &xe_bo_device(bo)->bo_device;
392
393 if (xe_bo_put_deferred(bo, deferred: &bo_device->async_list))
394 schedule_work(work: &bo_device->async_free);
395}
396
397void xe_bo_dev_init(struct xe_bo_dev *bo_device);
398
399void xe_bo_dev_fini(struct xe_bo_dev *bo_device);
400
401struct sg_table *xe_bo_sg(struct xe_bo *bo);
402
403/*
404 * xe_sg_segment_size() - Provides upper limit for sg segment size.
405 * @dev: device pointer
406 *
407 * Returns the maximum segment size for the 'struct scatterlist'
408 * elements.
409 */
410static inline unsigned int xe_sg_segment_size(struct device *dev)
411{
412 struct scatterlist __maybe_unused sg;
413 size_t max = BIT_ULL(sizeof(sg.length) * 8) - 1;
414
415 max = min_t(size_t, max, dma_max_mapping_size(dev));
416
417 /*
418 * The iommu_dma_map_sg() function ensures iova allocation doesn't
419 * cross dma segment boundary. It does so by padding some sg elements.
420 * This can cause overflow, ending up with sg->length being set to 0.
421 * Avoid this by ensuring maximum segment size is half of 'max'
422 * rounded down to PAGE_SIZE.
423 */
424 return round_down(max / 2, PAGE_SIZE);
425}
426
427/**
428 * struct xe_bo_shrink_flags - flags governing the shrink behaviour.
429 * @purge: Only purging allowed. Don't shrink if bo not purgeable.
430 * @writeback: Attempt to immediately move content to swap.
431 */
432struct xe_bo_shrink_flags {
433 u32 purge : 1;
434 u32 writeback : 1;
435};
436
437long xe_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
438 const struct xe_bo_shrink_flags flags,
439 unsigned long *scanned);
440
441/**
442 * xe_bo_is_mem_type - Whether the bo currently resides in the given
443 * TTM memory type
444 * @bo: The bo to check.
445 * @mem_type: The TTM memory type.
446 *
447 * Return: true iff the bo resides in @mem_type, false otherwise.
448 */
449static inline bool xe_bo_is_mem_type(struct xe_bo *bo, u32 mem_type)
450{
451 xe_bo_assert_held(bo);
452 return bo->ttm.resource->mem_type == mem_type;
453}
454#endif
455

source code of linux/drivers/gpu/drm/xe/xe_bo.h