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