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