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