1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2021 Intel Corporation 4 */ 5 6 #include "xe_bo.h" 7 8 #include <linux/dma-buf.h> 9 #include <linux/nospec.h> 10 11 #include <drm/drm_drv.h> 12 #include <drm/drm_dumb_buffers.h> 13 #include <drm/drm_gem_ttm_helper.h> 14 #include <drm/drm_managed.h> 15 #include <drm/ttm/ttm_backup.h> 16 #include <drm/ttm/ttm_device.h> 17 #include <drm/ttm/ttm_placement.h> 18 #include <drm/ttm/ttm_tt.h> 19 #include <uapi/drm/xe_drm.h> 20 21 #include <kunit/static_stub.h> 22 23 #include <trace/events/gpu_mem.h> 24 25 #include "xe_device.h" 26 #include "xe_dma_buf.h" 27 #include "xe_drm_client.h" 28 #include "xe_ggtt.h" 29 #include "xe_map.h" 30 #include "xe_migrate.h" 31 #include "xe_pat.h" 32 #include "xe_pm.h" 33 #include "xe_preempt_fence.h" 34 #include "xe_pxp.h" 35 #include "xe_res_cursor.h" 36 #include "xe_shrinker.h" 37 #include "xe_sriov_vf_ccs.h" 38 #include "xe_tile.h" 39 #include "xe_trace_bo.h" 40 #include "xe_ttm_stolen_mgr.h" 41 #include "xe_vm.h" 42 #include "xe_vram_types.h" 43 44 const char *const xe_mem_type_to_name[TTM_NUM_MEM_TYPES] = { 45 [XE_PL_SYSTEM] = "system", 46 [XE_PL_TT] = "gtt", 47 [XE_PL_VRAM0] = "vram0", 48 [XE_PL_VRAM1] = "vram1", 49 [XE_PL_STOLEN] = "stolen" 50 }; 51 52 static const struct ttm_place sys_placement_flags = { 53 .fpfn = 0, 54 .lpfn = 0, 55 .mem_type = XE_PL_SYSTEM, 56 .flags = 0, 57 }; 58 59 static struct ttm_placement sys_placement = { 60 .num_placement = 1, 61 .placement = &sys_placement_flags, 62 }; 63 64 static struct ttm_placement purge_placement; 65 66 static const struct ttm_place tt_placement_flags[] = { 67 { 68 .fpfn = 0, 69 .lpfn = 0, 70 .mem_type = XE_PL_TT, 71 .flags = TTM_PL_FLAG_DESIRED, 72 }, 73 { 74 .fpfn = 0, 75 .lpfn = 0, 76 .mem_type = XE_PL_SYSTEM, 77 .flags = TTM_PL_FLAG_FALLBACK, 78 } 79 }; 80 81 static struct ttm_placement tt_placement = { 82 .num_placement = 2, 83 .placement = tt_placement_flags, 84 }; 85 86 #define for_each_set_bo_vram_flag(bit__, bo_flags__) \ 87 for (unsigned int __bit_tmp = BIT(0); __bit_tmp <= XE_BO_FLAG_VRAM_MASK; __bit_tmp <<= 1) \ 88 for_each_if(((bit__) = __bit_tmp) & (bo_flags__) & XE_BO_FLAG_VRAM_MASK) 89 90 bool mem_type_is_vram(u32 mem_type) 91 { 92 return mem_type >= XE_PL_VRAM0 && mem_type != XE_PL_STOLEN; 93 } 94 95 static bool resource_is_stolen_vram(struct xe_device *xe, struct ttm_resource *res) 96 { 97 return res->mem_type == XE_PL_STOLEN && IS_DGFX(xe); 98 } 99 100 static bool resource_is_vram(struct ttm_resource *res) 101 { 102 return mem_type_is_vram(res->mem_type); 103 } 104 105 bool xe_bo_is_vram(struct xe_bo *bo) 106 { 107 return resource_is_vram(bo->ttm.resource) || 108 resource_is_stolen_vram(xe_bo_device(bo), bo->ttm.resource); 109 } 110 111 bool xe_bo_is_stolen(struct xe_bo *bo) 112 { 113 return bo->ttm.resource->mem_type == XE_PL_STOLEN; 114 } 115 116 /** 117 * xe_bo_has_single_placement - check if BO is placed only in one memory location 118 * @bo: The BO 119 * 120 * This function checks whether a given BO is placed in only one memory location. 121 * 122 * Returns: true if the BO is placed in a single memory location, false otherwise. 123 * 124 */ 125 bool xe_bo_has_single_placement(struct xe_bo *bo) 126 { 127 return bo->placement.num_placement == 1; 128 } 129 130 /** 131 * xe_bo_is_stolen_devmem - check if BO is of stolen type accessed via PCI BAR 132 * @bo: The BO 133 * 134 * The stolen memory is accessed through the PCI BAR for both DGFX and some 135 * integrated platforms that have a dedicated bit in the PTE for devmem (DM). 136 * 137 * Returns: true if it's stolen memory accessed via PCI BAR, false otherwise. 138 */ 139 bool xe_bo_is_stolen_devmem(struct xe_bo *bo) 140 { 141 return xe_bo_is_stolen(bo) && 142 GRAPHICS_VERx100(xe_bo_device(bo)) >= 1270; 143 } 144 145 /** 146 * xe_bo_is_vm_bound - check if BO has any mappings through VM_BIND 147 * @bo: The BO 148 * 149 * Check if a given bo is bound through VM_BIND. This requires the 150 * reservation lock for the BO to be held. 151 * 152 * Returns: boolean 153 */ 154 bool xe_bo_is_vm_bound(struct xe_bo *bo) 155 { 156 xe_bo_assert_held(bo); 157 158 return !list_empty(&bo->ttm.base.gpuva.list); 159 } 160 161 static bool xe_bo_is_user(struct xe_bo *bo) 162 { 163 return bo->flags & XE_BO_FLAG_USER; 164 } 165 166 static struct xe_migrate * 167 mem_type_to_migrate(struct xe_device *xe, u32 mem_type) 168 { 169 struct xe_tile *tile; 170 171 xe_assert(xe, mem_type == XE_PL_STOLEN || mem_type_is_vram(mem_type)); 172 tile = &xe->tiles[mem_type == XE_PL_STOLEN ? 0 : (mem_type - XE_PL_VRAM0)]; 173 return tile->migrate; 174 } 175 176 static void try_add_system(struct xe_device *xe, struct xe_bo *bo, 177 u32 bo_flags, u32 *c) 178 { 179 if (bo_flags & XE_BO_FLAG_SYSTEM) { 180 xe_assert(xe, *c < ARRAY_SIZE(bo->placements)); 181 182 bo->placements[*c] = (struct ttm_place) { 183 .mem_type = XE_PL_TT, 184 .flags = (bo_flags & XE_BO_FLAG_VRAM_MASK) ? 185 TTM_PL_FLAG_FALLBACK : 0, 186 }; 187 *c += 1; 188 } 189 } 190 191 static bool force_contiguous(u32 bo_flags) 192 { 193 if (bo_flags & XE_BO_FLAG_STOLEN) 194 return true; /* users expect this */ 195 else if (bo_flags & XE_BO_FLAG_PINNED && 196 !(bo_flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) 197 return true; /* needs vmap */ 198 else if (bo_flags & XE_BO_FLAG_CPU_ADDR_MIRROR) 199 return true; 200 201 /* 202 * For eviction / restore on suspend / resume objects pinned in VRAM 203 * must be contiguous, also only contiguous BOs support xe_bo_vmap. 204 */ 205 return bo_flags & XE_BO_FLAG_NEEDS_CPU_ACCESS && 206 bo_flags & XE_BO_FLAG_PINNED; 207 } 208 209 static u8 vram_bo_flag_to_tile_id(struct xe_device *xe, u32 vram_bo_flag) 210 { 211 xe_assert(xe, vram_bo_flag & XE_BO_FLAG_VRAM_MASK); 212 xe_assert(xe, (vram_bo_flag & (vram_bo_flag - 1)) == 0); 213 214 return __ffs(vram_bo_flag >> (__ffs(XE_BO_FLAG_VRAM0) - 1)) - 1; 215 } 216 217 static u32 bo_vram_flags_to_vram_placement(struct xe_device *xe, u32 bo_flags, u32 vram_flag, 218 enum ttm_bo_type type) 219 { 220 u8 tile_id = vram_bo_flag_to_tile_id(xe, vram_flag); 221 222 xe_assert(xe, tile_id < xe->info.tile_count); 223 224 if (type == ttm_bo_type_kernel && !(bo_flags & XE_BO_FLAG_FORCE_USER_VRAM)) 225 return xe->tiles[tile_id].mem.kernel_vram->placement; 226 else 227 return xe->tiles[tile_id].mem.vram->placement; 228 } 229 230 static void add_vram(struct xe_device *xe, struct xe_bo *bo, 231 struct ttm_place *places, u32 bo_flags, u32 mem_type, u32 *c) 232 { 233 struct ttm_place place = { .mem_type = mem_type }; 234 struct ttm_resource_manager *mgr = ttm_manager_type(&xe->ttm, mem_type); 235 struct xe_ttm_vram_mgr *vram_mgr = to_xe_ttm_vram_mgr(mgr); 236 237 struct xe_vram_region *vram; 238 u64 io_size; 239 240 xe_assert(xe, *c < ARRAY_SIZE(bo->placements)); 241 242 vram = container_of(vram_mgr, struct xe_vram_region, ttm); 243 xe_assert(xe, vram && vram->usable_size); 244 io_size = vram->io_size; 245 246 if (force_contiguous(bo_flags)) 247 place.flags |= TTM_PL_FLAG_CONTIGUOUS; 248 249 if (io_size < vram->usable_size) { 250 if (bo_flags & XE_BO_FLAG_NEEDS_CPU_ACCESS) { 251 place.fpfn = 0; 252 place.lpfn = io_size >> PAGE_SHIFT; 253 } else { 254 place.flags |= TTM_PL_FLAG_TOPDOWN; 255 } 256 } 257 places[*c] = place; 258 *c += 1; 259 } 260 261 static void try_add_vram(struct xe_device *xe, struct xe_bo *bo, 262 u32 bo_flags, enum ttm_bo_type type, u32 *c) 263 { 264 u32 vram_flag; 265 266 for_each_set_bo_vram_flag(vram_flag, bo_flags) { 267 u32 pl = bo_vram_flags_to_vram_placement(xe, bo_flags, vram_flag, type); 268 269 add_vram(xe, bo, bo->placements, bo_flags, pl, c); 270 } 271 } 272 273 static void try_add_stolen(struct xe_device *xe, struct xe_bo *bo, 274 u32 bo_flags, u32 *c) 275 { 276 if (bo_flags & XE_BO_FLAG_STOLEN) { 277 xe_assert(xe, *c < ARRAY_SIZE(bo->placements)); 278 279 bo->placements[*c] = (struct ttm_place) { 280 .mem_type = XE_PL_STOLEN, 281 .flags = force_contiguous(bo_flags) ? 282 TTM_PL_FLAG_CONTIGUOUS : 0, 283 }; 284 *c += 1; 285 } 286 } 287 288 static int __xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo, 289 u32 bo_flags, enum ttm_bo_type type) 290 { 291 u32 c = 0; 292 293 try_add_vram(xe, bo, bo_flags, type, &c); 294 try_add_system(xe, bo, bo_flags, &c); 295 try_add_stolen(xe, bo, bo_flags, &c); 296 297 if (!c) 298 return -EINVAL; 299 300 bo->placement = (struct ttm_placement) { 301 .num_placement = c, 302 .placement = bo->placements, 303 }; 304 305 return 0; 306 } 307 308 int xe_bo_placement_for_flags(struct xe_device *xe, struct xe_bo *bo, 309 u32 bo_flags, enum ttm_bo_type type) 310 { 311 xe_bo_assert_held(bo); 312 return __xe_bo_placement_for_flags(xe, bo, bo_flags, type); 313 } 314 315 static void xe_evict_flags(struct ttm_buffer_object *tbo, 316 struct ttm_placement *placement) 317 { 318 struct xe_device *xe = container_of(tbo->bdev, typeof(*xe), ttm); 319 bool device_unplugged = drm_dev_is_unplugged(&xe->drm); 320 struct xe_bo *bo; 321 322 if (!xe_bo_is_xe_bo(tbo)) { 323 /* Don't handle scatter gather BOs */ 324 if (tbo->type == ttm_bo_type_sg) { 325 placement->num_placement = 0; 326 return; 327 } 328 329 *placement = device_unplugged ? purge_placement : sys_placement; 330 return; 331 } 332 333 bo = ttm_to_xe_bo(tbo); 334 if (bo->flags & XE_BO_FLAG_CPU_ADDR_MIRROR) { 335 *placement = sys_placement; 336 return; 337 } 338 339 if (device_unplugged && !tbo->base.dma_buf) { 340 *placement = purge_placement; 341 return; 342 } 343 344 /* 345 * For xe, sg bos that are evicted to system just triggers a 346 * rebind of the sg list upon subsequent validation to XE_PL_TT. 347 */ 348 switch (tbo->resource->mem_type) { 349 case XE_PL_VRAM0: 350 case XE_PL_VRAM1: 351 case XE_PL_STOLEN: 352 *placement = tt_placement; 353 break; 354 case XE_PL_TT: 355 default: 356 *placement = sys_placement; 357 break; 358 } 359 } 360 361 /* struct xe_ttm_tt - Subclassed ttm_tt for xe */ 362 struct xe_ttm_tt { 363 struct ttm_tt ttm; 364 struct sg_table sgt; 365 struct sg_table *sg; 366 /** @purgeable: Whether the content of the pages of @ttm is purgeable. */ 367 bool purgeable; 368 }; 369 370 static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt) 371 { 372 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); 373 unsigned long num_pages = tt->num_pages; 374 int ret; 375 376 XE_WARN_ON((tt->page_flags & TTM_TT_FLAG_EXTERNAL) && 377 !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE)); 378 379 if (xe_tt->sg) 380 return 0; 381 382 ret = sg_alloc_table_from_pages_segment(&xe_tt->sgt, tt->pages, 383 num_pages, 0, 384 (u64)num_pages << PAGE_SHIFT, 385 xe_sg_segment_size(xe->drm.dev), 386 GFP_KERNEL); 387 if (ret) 388 return ret; 389 390 xe_tt->sg = &xe_tt->sgt; 391 ret = dma_map_sgtable(xe->drm.dev, xe_tt->sg, DMA_BIDIRECTIONAL, 392 DMA_ATTR_SKIP_CPU_SYNC); 393 if (ret) { 394 sg_free_table(xe_tt->sg); 395 xe_tt->sg = NULL; 396 return ret; 397 } 398 399 return 0; 400 } 401 402 static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt) 403 { 404 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); 405 406 if (xe_tt->sg) { 407 dma_unmap_sgtable(xe->drm.dev, xe_tt->sg, 408 DMA_BIDIRECTIONAL, 0); 409 sg_free_table(xe_tt->sg); 410 xe_tt->sg = NULL; 411 } 412 } 413 414 struct sg_table *xe_bo_sg(struct xe_bo *bo) 415 { 416 struct ttm_tt *tt = bo->ttm.ttm; 417 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); 418 419 return xe_tt->sg; 420 } 421 422 /* 423 * Account ttm pages against the device shrinker's shrinkable and 424 * purgeable counts. 425 */ 426 static void xe_ttm_tt_account_add(struct xe_device *xe, struct ttm_tt *tt) 427 { 428 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); 429 430 if (xe_tt->purgeable) 431 xe_shrinker_mod_pages(xe->mem.shrinker, 0, tt->num_pages); 432 else 433 xe_shrinker_mod_pages(xe->mem.shrinker, tt->num_pages, 0); 434 } 435 436 static void xe_ttm_tt_account_subtract(struct xe_device *xe, struct ttm_tt *tt) 437 { 438 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); 439 440 if (xe_tt->purgeable) 441 xe_shrinker_mod_pages(xe->mem.shrinker, 0, -(long)tt->num_pages); 442 else 443 xe_shrinker_mod_pages(xe->mem.shrinker, -(long)tt->num_pages, 0); 444 } 445 446 static void update_global_total_pages(struct ttm_device *ttm_dev, 447 long num_pages) 448 { 449 #if IS_ENABLED(CONFIG_TRACE_GPU_MEM) 450 struct xe_device *xe = ttm_to_xe_device(ttm_dev); 451 u64 global_total_pages = 452 atomic64_add_return(num_pages, &xe->global_total_pages); 453 454 trace_gpu_mem_total(xe->drm.primary->index, 0, 455 global_total_pages << PAGE_SHIFT); 456 #endif 457 } 458 459 static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo, 460 u32 page_flags) 461 { 462 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); 463 struct xe_device *xe = xe_bo_device(bo); 464 struct xe_ttm_tt *xe_tt; 465 struct ttm_tt *tt; 466 unsigned long extra_pages; 467 enum ttm_caching caching = ttm_cached; 468 int err; 469 470 xe_tt = kzalloc_obj(*xe_tt); 471 if (!xe_tt) 472 return NULL; 473 474 tt = &xe_tt->ttm; 475 476 extra_pages = 0; 477 if (xe_bo_needs_ccs_pages(bo)) 478 extra_pages = DIV_ROUND_UP(xe_device_ccs_bytes(xe, xe_bo_size(bo)), 479 PAGE_SIZE); 480 481 /* 482 * DGFX system memory is always WB / ttm_cached, since 483 * other caching modes are only supported on x86. DGFX 484 * GPU system memory accesses are always coherent with the 485 * CPU. 486 */ 487 if (!IS_DGFX(xe)) { 488 switch (bo->cpu_caching) { 489 case DRM_XE_GEM_CPU_CACHING_WC: 490 caching = ttm_write_combined; 491 break; 492 default: 493 caching = ttm_cached; 494 break; 495 } 496 497 WARN_ON((bo->flags & XE_BO_FLAG_USER) && !bo->cpu_caching); 498 499 /* 500 * For Xe_LPG and beyond up to NVL-P (excluding), PPGTT PTE 501 * lookups are also non-coherent and require a CPU:WC mapping. 502 */ 503 if ((!bo->cpu_caching && bo->flags & XE_BO_FLAG_FORCE_WC) || 504 (!xe->info.has_cached_pt && bo->flags & XE_BO_FLAG_PAGETABLE)) 505 caching = ttm_write_combined; 506 } 507 508 if (bo->flags & XE_BO_FLAG_NEEDS_UC) { 509 /* 510 * Valid only for internally-created buffers only, for 511 * which cpu_caching is never initialized. 512 */ 513 xe_assert(xe, bo->cpu_caching == 0); 514 caching = ttm_uncached; 515 } 516 517 if (ttm_bo->type != ttm_bo_type_sg) 518 page_flags |= TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_EXTERNAL_MAPPABLE; 519 520 err = ttm_tt_init(tt, &bo->ttm, page_flags, caching, extra_pages); 521 if (err) { 522 kfree(xe_tt); 523 return NULL; 524 } 525 526 if (ttm_bo->type != ttm_bo_type_sg) { 527 err = ttm_tt_setup_backup(tt); 528 if (err) { 529 ttm_tt_fini(tt); 530 kfree(xe_tt); 531 return NULL; 532 } 533 } 534 535 return tt; 536 } 537 538 static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt, 539 struct ttm_operation_ctx *ctx) 540 { 541 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); 542 int err; 543 544 /* 545 * dma-bufs are not populated with pages, and the dma- 546 * addresses are set up when moved to XE_PL_TT. 547 */ 548 if ((tt->page_flags & TTM_TT_FLAG_EXTERNAL) && 549 !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE)) 550 return 0; 551 552 if (ttm_tt_is_backed_up(tt) && !xe_tt->purgeable) { 553 err = ttm_tt_restore(ttm_dev, tt, ctx); 554 } else { 555 ttm_tt_clear_backed_up(tt); 556 err = ttm_pool_alloc(&ttm_dev->pool, tt, ctx); 557 } 558 if (err) 559 return err; 560 561 xe_tt->purgeable = false; 562 xe_ttm_tt_account_add(ttm_to_xe_device(ttm_dev), tt); 563 update_global_total_pages(ttm_dev, tt->num_pages); 564 565 return 0; 566 } 567 568 static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt) 569 { 570 struct xe_device *xe = ttm_to_xe_device(ttm_dev); 571 572 if ((tt->page_flags & TTM_TT_FLAG_EXTERNAL) && 573 !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE)) 574 return; 575 576 xe_tt_unmap_sg(xe, tt); 577 578 ttm_pool_free(&ttm_dev->pool, tt); 579 xe_ttm_tt_account_subtract(xe, tt); 580 update_global_total_pages(ttm_dev, -(long)tt->num_pages); 581 } 582 583 static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt) 584 { 585 ttm_tt_fini(tt); 586 kfree(tt); 587 } 588 589 static bool xe_ttm_resource_visible(struct xe_device *xe, struct ttm_resource *mem) 590 { 591 struct xe_ttm_vram_mgr_resource *vres; 592 593 if (mem->mem_type == XE_PL_STOLEN) { 594 struct xe_ttm_stolen_mgr *mgr = xe->mem.stolen_mgr; 595 596 return mgr->io_base && !xe_ttm_stolen_cpu_access_needs_ggtt(xe); 597 } 598 599 vres = to_xe_ttm_vram_mgr_resource(mem); 600 return vres->used_visible_size == mem->size; 601 } 602 603 /** 604 * xe_bo_is_visible_vram - check if BO is placed entirely in visible VRAM. 605 * @bo: The BO 606 * 607 * This function checks whether a given BO resides entirely in memory visible from the CPU 608 * 609 * Returns: true if the BO is entirely visible, false otherwise. 610 * 611 */ 612 bool xe_bo_is_visible_vram(struct xe_bo *bo) 613 { 614 if (drm_WARN_ON(bo->ttm.base.dev, !xe_bo_is_vram(bo))) 615 return false; 616 617 return xe_ttm_resource_visible(xe_bo_device(bo), bo->ttm.resource); 618 } 619 620 static int xe_ttm_io_mem_reserve(struct ttm_device *bdev, 621 struct ttm_resource *mem) 622 { 623 struct xe_device *xe = ttm_to_xe_device(bdev); 624 625 switch (mem->mem_type) { 626 case XE_PL_SYSTEM: 627 case XE_PL_TT: 628 return 0; 629 case XE_PL_VRAM0: 630 case XE_PL_VRAM1: { 631 struct xe_vram_region *vram = xe_map_resource_to_region(mem); 632 633 if (!xe_ttm_resource_visible(xe, mem)) 634 return -EINVAL; 635 636 mem->bus.offset = mem->start << PAGE_SHIFT; 637 638 if (vram->mapping && 639 mem->placement & TTM_PL_FLAG_CONTIGUOUS) 640 mem->bus.addr = (u8 __force *)vram->mapping + 641 mem->bus.offset; 642 643 mem->bus.offset += vram->io_start; 644 mem->bus.is_iomem = true; 645 646 #if !IS_ENABLED(CONFIG_X86) 647 mem->bus.caching = ttm_write_combined; 648 #endif 649 return 0; 650 } case XE_PL_STOLEN: 651 return xe_ttm_stolen_io_mem_reserve(xe, mem); 652 default: 653 return -EINVAL; 654 } 655 } 656 657 static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo, 658 const struct ttm_operation_ctx *ctx) 659 { 660 struct dma_resv_iter cursor; 661 struct dma_fence *fence; 662 struct drm_gem_object *obj = &bo->ttm.base; 663 struct drm_gpuvm_bo *vm_bo; 664 bool idle = false; 665 int ret = 0; 666 667 dma_resv_assert_held(bo->ttm.base.resv); 668 669 if (!list_empty(&bo->ttm.base.gpuva.list)) { 670 dma_resv_iter_begin(&cursor, bo->ttm.base.resv, 671 DMA_RESV_USAGE_BOOKKEEP); 672 dma_resv_for_each_fence_unlocked(&cursor, fence) 673 dma_fence_enable_sw_signaling(fence); 674 dma_resv_iter_end(&cursor); 675 } 676 677 drm_gem_for_each_gpuvm_bo(vm_bo, obj) { 678 struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm); 679 struct drm_gpuva *gpuva; 680 681 if (!xe_vm_in_fault_mode(vm)) { 682 drm_gpuvm_bo_evict(vm_bo, true); 683 /* 684 * L2 cache may not be flushed, so ensure that is done in 685 * xe_vm_invalidate_vma() below 686 */ 687 if (!xe_device_is_l2_flush_optimized(xe)) 688 continue; 689 } 690 691 if (!idle) { 692 long timeout; 693 694 if (ctx->no_wait_gpu && 695 !dma_resv_test_signaled(bo->ttm.base.resv, 696 DMA_RESV_USAGE_BOOKKEEP)) 697 return -EBUSY; 698 699 timeout = dma_resv_wait_timeout(bo->ttm.base.resv, 700 DMA_RESV_USAGE_BOOKKEEP, 701 ctx->interruptible, 702 MAX_SCHEDULE_TIMEOUT); 703 if (!timeout) 704 return -ETIME; 705 if (timeout < 0) 706 return timeout; 707 708 idle = true; 709 } 710 711 drm_gpuvm_bo_for_each_va(gpuva, vm_bo) { 712 struct xe_vma *vma = gpuva_to_vma(gpuva); 713 714 trace_xe_vma_evict(vma); 715 ret = xe_vm_invalidate_vma(vma); 716 if (XE_WARN_ON(ret)) 717 return ret; 718 } 719 } 720 721 return ret; 722 } 723 724 /* 725 * The dma-buf map_attachment() / unmap_attachment() is hooked up here. 726 * Note that unmapping the attachment is deferred to the next 727 * map_attachment time, or to bo destroy (after idling) whichever comes first. 728 * This is to avoid syncing before unmap_attachment(), assuming that the 729 * caller relies on idling the reservation object before moving the 730 * backing store out. Should that assumption not hold, then we will be able 731 * to unconditionally call unmap_attachment() when moving out to system. 732 */ 733 static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo, 734 struct ttm_resource *new_res) 735 { 736 struct dma_buf_attachment *attach = ttm_bo->base.import_attach; 737 struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt, 738 ttm); 739 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); 740 bool device_unplugged = drm_dev_is_unplugged(&xe->drm); 741 struct sg_table *sg; 742 743 xe_assert(xe, attach); 744 xe_assert(xe, ttm_bo->ttm); 745 746 if (device_unplugged && new_res->mem_type == XE_PL_SYSTEM && 747 ttm_bo->sg) { 748 dma_resv_wait_timeout(ttm_bo->base.resv, DMA_RESV_USAGE_BOOKKEEP, 749 false, MAX_SCHEDULE_TIMEOUT); 750 dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL); 751 ttm_bo->sg = NULL; 752 } 753 754 if (new_res->mem_type == XE_PL_SYSTEM) 755 goto out; 756 757 if (ttm_bo->sg) { 758 dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL); 759 ttm_bo->sg = NULL; 760 } 761 762 sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); 763 if (IS_ERR(sg)) 764 return PTR_ERR(sg); 765 766 ttm_bo->sg = sg; 767 xe_tt->sg = sg; 768 769 out: 770 ttm_bo_move_null(ttm_bo, new_res); 771 772 return 0; 773 } 774 775 /** 776 * xe_bo_move_notify - Notify subsystems of a pending move 777 * @bo: The buffer object 778 * @ctx: The struct ttm_operation_ctx controlling locking and waits. 779 * 780 * This function notifies subsystems of an upcoming buffer move. 781 * Upon receiving such a notification, subsystems should schedule 782 * halting access to the underlying pages and optionally add a fence 783 * to the buffer object's dma_resv object, that signals when access is 784 * stopped. The caller will wait on all dma_resv fences before 785 * starting the move. 786 * 787 * A subsystem may commence access to the object after obtaining 788 * bindings to the new backing memory under the object lock. 789 * 790 * Return: 0 on success, -EINTR or -ERESTARTSYS if interrupted in fault mode, 791 * negative error code on error. 792 */ 793 static int xe_bo_move_notify(struct xe_bo *bo, 794 const struct ttm_operation_ctx *ctx) 795 { 796 struct ttm_buffer_object *ttm_bo = &bo->ttm; 797 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); 798 struct ttm_resource *old_mem = ttm_bo->resource; 799 u32 old_mem_type = old_mem ? old_mem->mem_type : XE_PL_SYSTEM; 800 int ret; 801 802 /* 803 * If this starts to call into many components, consider 804 * using a notification chain here. 805 */ 806 807 if (xe_bo_is_pinned(bo)) 808 return -EINVAL; 809 810 xe_bo_vunmap(bo); 811 ret = xe_bo_trigger_rebind(xe, bo, ctx); 812 if (ret) 813 return ret; 814 815 /* Don't call move_notify() for imported dma-bufs. */ 816 if (ttm_bo->base.dma_buf && !ttm_bo->base.import_attach) 817 dma_buf_invalidate_mappings(ttm_bo->base.dma_buf); 818 819 /* 820 * TTM has already nuked the mmap for us (see ttm_bo_unmap_virtual), 821 * so if we moved from VRAM make sure to unlink this from the userfault 822 * tracking. 823 */ 824 if (mem_type_is_vram(old_mem_type)) { 825 mutex_lock(&xe->mem_access.vram_userfault.lock); 826 if (!list_empty(&bo->vram_userfault_link)) 827 list_del_init(&bo->vram_userfault_link); 828 mutex_unlock(&xe->mem_access.vram_userfault.lock); 829 } 830 831 return 0; 832 } 833 834 /** 835 * xe_bo_set_purgeable_shrinker() - Update shrinker accounting for purgeable state 836 * @bo: Buffer object 837 * @new_state: New purgeable state being set 838 * 839 * Transfers pages between shrinkable and purgeable buckets when the BO 840 * purgeable state changes. Called automatically from xe_bo_set_purgeable_state(). 841 */ 842 static void xe_bo_set_purgeable_shrinker(struct xe_bo *bo, 843 enum xe_madv_purgeable_state new_state) 844 { 845 struct ttm_buffer_object *ttm_bo = &bo->ttm; 846 struct ttm_tt *tt = ttm_bo->ttm; 847 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); 848 struct xe_ttm_tt *xe_tt; 849 long tt_pages; 850 851 xe_bo_assert_held(bo); 852 853 if (!tt || !ttm_tt_is_populated(tt)) 854 return; 855 856 xe_tt = container_of(tt, struct xe_ttm_tt, ttm); 857 tt_pages = tt->num_pages; 858 859 if (!xe_tt->purgeable && new_state == XE_MADV_PURGEABLE_DONTNEED) { 860 xe_tt->purgeable = true; 861 /* Transfer pages from shrinkable to purgeable count */ 862 xe_shrinker_mod_pages(xe->mem.shrinker, -tt_pages, tt_pages); 863 } else if (xe_tt->purgeable && new_state == XE_MADV_PURGEABLE_WILLNEED) { 864 xe_tt->purgeable = false; 865 /* Transfer pages from purgeable to shrinkable count */ 866 xe_shrinker_mod_pages(xe->mem.shrinker, tt_pages, -tt_pages); 867 } 868 } 869 870 /** 871 * xe_bo_set_purgeable_state() - Set BO purgeable state with validation 872 * @bo: Buffer object 873 * @new_state: New purgeable state 874 * 875 * Sets the purgeable state with lockdep assertions and validates state 876 * transitions. Once a BO is PURGED, it cannot transition to any other state. 877 * Invalid transitions are caught with xe_assert(). Shrinker page accounting 878 * is updated automatically. 879 */ 880 void xe_bo_set_purgeable_state(struct xe_bo *bo, 881 enum xe_madv_purgeable_state new_state) 882 { 883 struct xe_device *xe = xe_bo_device(bo); 884 885 xe_bo_assert_held(bo); 886 887 /* Validate state is one of the known values */ 888 xe_assert(xe, new_state == XE_MADV_PURGEABLE_WILLNEED || 889 new_state == XE_MADV_PURGEABLE_DONTNEED || 890 new_state == XE_MADV_PURGEABLE_PURGED); 891 892 /* Once purged, always purged - cannot transition out */ 893 xe_assert(xe, !(bo->purgeable.state == XE_MADV_PURGEABLE_PURGED && 894 new_state != XE_MADV_PURGEABLE_PURGED)); 895 896 bo->purgeable.state = new_state; 897 xe_bo_set_purgeable_shrinker(bo, new_state); 898 } 899 900 /** 901 * xe_ttm_bo_purge() - Purge buffer object backing store 902 * @ttm_bo: The TTM buffer object to purge 903 * @ctx: TTM operation context 904 * 905 * This function purges the backing store of a BO marked as DONTNEED and 906 * triggers rebind to invalidate stale GPU mappings. For fault-mode VMs, 907 * this zaps the PTEs. The next GPU access will trigger a page fault and 908 * perform NULL rebind (scratch pages or clear PTEs based on VM config). 909 * 910 * Return: 0 on success, negative error code on failure 911 */ 912 static int xe_ttm_bo_purge(struct ttm_buffer_object *ttm_bo, struct ttm_operation_ctx *ctx) 913 { 914 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); 915 struct ttm_placement place = {}; 916 int ret; 917 918 xe_bo_assert_held(bo); 919 920 if (!ttm_bo->ttm) 921 return 0; 922 923 if (!xe_bo_madv_is_dontneed(bo)) 924 return 0; 925 926 /* 927 * Use the standard pre-move hook so we share the same cleanup/invalidate 928 * path as migrations: drop any CPU vmap and schedule the necessary GPU 929 * unbind/rebind work. 930 * 931 * This must be called before ttm_bo_validate() frees the pages. 932 * May fail in no-wait contexts (fault/shrinker) or if the BO is 933 * pinned. Keep state unchanged on failure so we don't end up "PURGED" 934 * with stale mappings. 935 */ 936 ret = xe_bo_move_notify(bo, ctx); 937 if (ret) 938 return ret; 939 940 ret = ttm_bo_validate(ttm_bo, &place, ctx); 941 if (ret) 942 return ret; 943 944 /* Commit the state transition only once invalidation was queued */ 945 xe_bo_set_purgeable_state(bo, XE_MADV_PURGEABLE_PURGED); 946 947 return 0; 948 } 949 950 static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict, 951 struct ttm_operation_ctx *ctx, 952 struct ttm_resource *new_mem, 953 struct ttm_place *hop) 954 { 955 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); 956 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); 957 struct ttm_resource *old_mem = ttm_bo->resource; 958 u32 old_mem_type = old_mem ? old_mem->mem_type : XE_PL_SYSTEM; 959 struct ttm_tt *ttm = ttm_bo->ttm; 960 struct xe_migrate *migrate = NULL; 961 struct dma_fence *fence; 962 bool move_lacks_source; 963 bool tt_has_data; 964 bool needs_clear; 965 bool handle_system_ccs = (!IS_DGFX(xe) && xe_bo_needs_ccs_pages(bo) && 966 ttm && ttm_tt_is_populated(ttm)) ? true : false; 967 int ret = 0; 968 969 /* 970 * Purge only non-shared BOs explicitly marked DONTNEED by userspace. 971 * The move_notify callback will handle invalidation asynchronously. 972 */ 973 if (evict && xe_bo_madv_is_dontneed(bo)) { 974 ret = xe_ttm_bo_purge(ttm_bo, ctx); 975 if (ret) 976 return ret; 977 978 /* Free the unused eviction destination resource */ 979 ttm_resource_free(ttm_bo, &new_mem); 980 return 0; 981 } 982 983 /* Bo creation path, moving to system or TT. */ 984 if ((!old_mem && ttm) && !handle_system_ccs) { 985 if (new_mem->mem_type == XE_PL_TT) 986 ret = xe_tt_map_sg(xe, ttm); 987 if (!ret) 988 ttm_bo_move_null(ttm_bo, new_mem); 989 goto out; 990 } 991 992 if (ttm_bo->type == ttm_bo_type_sg) { 993 if (new_mem->mem_type == XE_PL_SYSTEM) 994 ret = xe_bo_move_notify(bo, ctx); 995 if (!ret) 996 ret = xe_bo_move_dmabuf(ttm_bo, new_mem); 997 return ret; 998 } 999 1000 tt_has_data = ttm && (ttm_tt_is_populated(ttm) || ttm_tt_is_swapped(ttm)); 1001 1002 move_lacks_source = !old_mem || (handle_system_ccs ? (!bo->ccs_cleared) : 1003 (!mem_type_is_vram(old_mem_type) && !tt_has_data)); 1004 1005 needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) || 1006 (!ttm && ttm_bo->type == ttm_bo_type_device); 1007 1008 if (new_mem->mem_type == XE_PL_TT) { 1009 ret = xe_tt_map_sg(xe, ttm); 1010 if (ret) 1011 goto out; 1012 } 1013 1014 if ((move_lacks_source && !needs_clear)) { 1015 ttm_bo_move_null(ttm_bo, new_mem); 1016 goto out; 1017 } 1018 1019 if (!move_lacks_source && (bo->flags & XE_BO_FLAG_CPU_ADDR_MIRROR) && 1020 new_mem->mem_type == XE_PL_SYSTEM) { 1021 ret = xe_svm_bo_evict(bo); 1022 if (!ret) { 1023 drm_dbg(&xe->drm, "Evict system allocator BO success\n"); 1024 ttm_bo_move_null(ttm_bo, new_mem); 1025 } else { 1026 drm_dbg(&xe->drm, "Evict system allocator BO failed=%pe\n", 1027 ERR_PTR(ret)); 1028 } 1029 1030 goto out; 1031 } 1032 1033 if (old_mem_type == XE_PL_SYSTEM && new_mem->mem_type == XE_PL_TT && !handle_system_ccs) { 1034 ttm_bo_move_null(ttm_bo, new_mem); 1035 goto out; 1036 } 1037 1038 /* 1039 * Failed multi-hop where the old_mem is still marked as 1040 * TTM_PL_FLAG_TEMPORARY, should just be a dummy move. 1041 */ 1042 if (old_mem_type == XE_PL_TT && 1043 new_mem->mem_type == XE_PL_TT) { 1044 ttm_bo_move_null(ttm_bo, new_mem); 1045 goto out; 1046 } 1047 1048 if (!move_lacks_source && !xe_bo_is_pinned(bo)) { 1049 ret = xe_bo_move_notify(bo, ctx); 1050 if (ret) 1051 goto out; 1052 } 1053 1054 if (old_mem_type == XE_PL_TT && 1055 new_mem->mem_type == XE_PL_SYSTEM) { 1056 long timeout = dma_resv_wait_timeout(ttm_bo->base.resv, 1057 DMA_RESV_USAGE_BOOKKEEP, 1058 false, 1059 MAX_SCHEDULE_TIMEOUT); 1060 if (timeout < 0) { 1061 ret = timeout; 1062 goto out; 1063 } 1064 1065 if (!handle_system_ccs) { 1066 ttm_bo_move_null(ttm_bo, new_mem); 1067 goto out; 1068 } 1069 } 1070 1071 if (!move_lacks_source && 1072 ((old_mem_type == XE_PL_SYSTEM && resource_is_vram(new_mem)) || 1073 (mem_type_is_vram(old_mem_type) && 1074 new_mem->mem_type == XE_PL_SYSTEM))) { 1075 hop->fpfn = 0; 1076 hop->lpfn = 0; 1077 hop->mem_type = XE_PL_TT; 1078 hop->flags = TTM_PL_FLAG_TEMPORARY; 1079 ret = -EMULTIHOP; 1080 goto out; 1081 } 1082 1083 if (bo->tile) 1084 migrate = bo->tile->migrate; 1085 else if (resource_is_vram(new_mem)) 1086 migrate = mem_type_to_migrate(xe, new_mem->mem_type); 1087 else if (mem_type_is_vram(old_mem_type)) 1088 migrate = mem_type_to_migrate(xe, old_mem_type); 1089 else 1090 migrate = xe->tiles[0].migrate; 1091 1092 xe_assert(xe, migrate); 1093 trace_xe_bo_move(bo, new_mem->mem_type, old_mem_type, move_lacks_source); 1094 if (xe_rpm_reclaim_safe(xe)) { 1095 /* 1096 * We might be called through swapout in the validation path of 1097 * another TTM device, so acquire rpm here. 1098 */ 1099 xe_pm_runtime_get(xe); 1100 } else { 1101 drm_WARN_ON(&xe->drm, handle_system_ccs); 1102 xe_pm_runtime_get_noresume(xe); 1103 } 1104 1105 if (move_lacks_source) { 1106 u32 flags = 0; 1107 1108 if (mem_type_is_vram(new_mem->mem_type)) 1109 flags |= XE_MIGRATE_CLEAR_FLAG_FULL; 1110 else if (handle_system_ccs) 1111 flags |= XE_MIGRATE_CLEAR_FLAG_CCS_DATA; 1112 1113 fence = xe_migrate_clear(migrate, bo, new_mem, flags); 1114 } else { 1115 fence = xe_migrate_copy(migrate, bo, bo, old_mem, new_mem, 1116 handle_system_ccs); 1117 } 1118 if (IS_ERR(fence)) { 1119 ret = PTR_ERR(fence); 1120 xe_pm_runtime_put(xe); 1121 goto out; 1122 } 1123 if (!move_lacks_source) { 1124 ret = ttm_bo_move_accel_cleanup(ttm_bo, fence, evict, true, 1125 new_mem); 1126 if (ret) { 1127 dma_fence_wait(fence, false); 1128 ttm_bo_move_null(ttm_bo, new_mem); 1129 ret = 0; 1130 } 1131 } else { 1132 /* 1133 * ttm_bo_move_accel_cleanup() may blow up if 1134 * bo->resource == NULL, so just attach the 1135 * fence and set the new resource. 1136 */ 1137 dma_resv_add_fence(ttm_bo->base.resv, fence, 1138 DMA_RESV_USAGE_KERNEL); 1139 ttm_bo_move_null(ttm_bo, new_mem); 1140 } 1141 1142 dma_fence_put(fence); 1143 xe_pm_runtime_put(xe); 1144 1145 /* 1146 * CCS meta data is migrated from TT -> SMEM. So, let us detach the 1147 * BBs from BO as it is no longer needed. 1148 */ 1149 if (IS_VF_CCS_READY(xe) && old_mem_type == XE_PL_TT && 1150 new_mem->mem_type == XE_PL_SYSTEM) 1151 xe_sriov_vf_ccs_detach_bo(bo); 1152 1153 if (IS_VF_CCS_READY(xe) && 1154 ((move_lacks_source && new_mem->mem_type == XE_PL_TT) || 1155 (old_mem_type == XE_PL_SYSTEM && new_mem->mem_type == XE_PL_TT)) && 1156 handle_system_ccs) 1157 ret = xe_sriov_vf_ccs_attach_bo(bo); 1158 1159 out: 1160 if ((!ttm_bo->resource || ttm_bo->resource->mem_type == XE_PL_SYSTEM) && 1161 ttm_bo->ttm) { 1162 long timeout = dma_resv_wait_timeout(ttm_bo->base.resv, 1163 DMA_RESV_USAGE_KERNEL, 1164 false, 1165 MAX_SCHEDULE_TIMEOUT); 1166 if (timeout < 0) 1167 ret = timeout; 1168 1169 if (IS_VF_CCS_READY(xe)) 1170 xe_sriov_vf_ccs_detach_bo(bo); 1171 1172 xe_tt_unmap_sg(xe, ttm_bo->ttm); 1173 } 1174 1175 return ret; 1176 } 1177 1178 static long xe_bo_shrink_purge(struct ttm_operation_ctx *ctx, 1179 struct ttm_buffer_object *bo, 1180 unsigned long *scanned) 1181 { 1182 struct xe_device *xe = ttm_to_xe_device(bo->bdev); 1183 struct ttm_tt *tt = bo->ttm; 1184 long lret; 1185 1186 /* Fake move to system, without copying data. */ 1187 if (bo->resource->mem_type != XE_PL_SYSTEM) { 1188 struct ttm_resource *new_resource; 1189 1190 lret = ttm_bo_wait_ctx(bo, ctx); 1191 if (lret) 1192 return lret; 1193 1194 lret = ttm_bo_mem_space(bo, &sys_placement, &new_resource, ctx); 1195 if (lret) 1196 return lret; 1197 1198 xe_tt_unmap_sg(xe, bo->ttm); 1199 ttm_bo_move_null(bo, new_resource); 1200 } 1201 1202 *scanned += bo->ttm->num_pages; 1203 lret = ttm_bo_shrink(ctx, bo, (struct ttm_bo_shrink_flags) 1204 {.purge = true, 1205 .writeback = false, 1206 .allow_move = false}); 1207 1208 if (lret > 0) { 1209 xe_ttm_tt_account_subtract(xe, bo->ttm); 1210 update_global_total_pages(bo->bdev, -(long)tt->num_pages); 1211 } 1212 1213 return lret; 1214 } 1215 1216 static bool 1217 xe_bo_eviction_valuable(struct ttm_buffer_object *bo, const struct ttm_place *place) 1218 { 1219 struct drm_gpuvm_bo *vm_bo; 1220 1221 if (!ttm_bo_eviction_valuable(bo, place)) 1222 return false; 1223 1224 if (!xe_bo_is_xe_bo(bo)) 1225 return true; 1226 1227 drm_gem_for_each_gpuvm_bo(vm_bo, &bo->base) { 1228 if (xe_vm_is_validating(gpuvm_to_vm(vm_bo->vm))) 1229 return false; 1230 } 1231 1232 return true; 1233 } 1234 1235 /** 1236 * xe_bo_shrink() - Try to shrink an xe bo. 1237 * @ctx: The struct ttm_operation_ctx used for shrinking. 1238 * @bo: The TTM buffer object whose pages to shrink. 1239 * @flags: Flags governing the shrink behaviour. 1240 * @scanned: Pointer to a counter of the number of pages 1241 * attempted to shrink. 1242 * 1243 * Try to shrink- or purge a bo, and if it succeeds, unmap dma. 1244 * Note that we need to be able to handle also non xe bos 1245 * (ghost bos), but only if the struct ttm_tt is embedded in 1246 * a struct xe_ttm_tt. When the function attempts to shrink 1247 * the pages of a buffer object, The value pointed to by @scanned 1248 * is updated. 1249 * 1250 * Return: The number of pages shrunken or purged, or negative error 1251 * code on failure. 1252 */ 1253 long xe_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo, 1254 const struct xe_bo_shrink_flags flags, 1255 unsigned long *scanned) 1256 { 1257 struct ttm_tt *tt = bo->ttm; 1258 struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm); 1259 struct ttm_place place = {.mem_type = bo->resource->mem_type}; 1260 struct xe_bo *xe_bo = ttm_to_xe_bo(bo); 1261 struct xe_device *xe = ttm_to_xe_device(bo->bdev); 1262 bool needs_rpm; 1263 long lret = 0L; 1264 1265 if (!(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE) || 1266 (flags.purge && !xe_tt->purgeable)) 1267 return -EBUSY; 1268 1269 if (!xe_bo_eviction_valuable(bo, &place)) 1270 return -EBUSY; 1271 1272 if (!xe_bo_is_xe_bo(bo) || !xe_bo_get_unless_zero(xe_bo)) 1273 return xe_bo_shrink_purge(ctx, bo, scanned); 1274 1275 if (xe_tt->purgeable) { 1276 if (bo->resource->mem_type != XE_PL_SYSTEM) 1277 lret = xe_bo_move_notify(xe_bo, ctx); 1278 if (!lret) 1279 lret = xe_bo_shrink_purge(ctx, bo, scanned); 1280 if (lret > 0 && xe_bo_madv_is_dontneed(xe_bo)) 1281 xe_bo_set_purgeable_state(xe_bo, 1282 XE_MADV_PURGEABLE_PURGED); 1283 goto out_unref; 1284 } 1285 1286 /* System CCS needs gpu copy when moving PL_TT -> PL_SYSTEM */ 1287 needs_rpm = (!IS_DGFX(xe) && bo->resource->mem_type != XE_PL_SYSTEM && 1288 xe_bo_needs_ccs_pages(xe_bo)); 1289 if (needs_rpm && !xe_pm_runtime_get_if_active(xe)) 1290 goto out_unref; 1291 1292 *scanned += tt->num_pages; 1293 lret = ttm_bo_shrink(ctx, bo, (struct ttm_bo_shrink_flags) 1294 {.purge = false, 1295 .writeback = flags.writeback, 1296 .allow_move = true}); 1297 if (needs_rpm) 1298 xe_pm_runtime_put(xe); 1299 1300 if (lret > 0) { 1301 xe_ttm_tt_account_subtract(xe, tt); 1302 update_global_total_pages(bo->bdev, -(long)tt->num_pages); 1303 } 1304 1305 out_unref: 1306 xe_bo_put(xe_bo); 1307 1308 return lret; 1309 } 1310 1311 /** 1312 * xe_bo_notifier_prepare_pinned() - Prepare a pinned VRAM object to be backed 1313 * up in system memory. 1314 * @bo: The buffer object to prepare. 1315 * 1316 * On successful completion, the object backup pages are allocated. Expectation 1317 * is that this is called from the PM notifier, prior to suspend/hibernation. 1318 * 1319 * Return: 0 on success. Negative error code on failure. 1320 */ 1321 int xe_bo_notifier_prepare_pinned(struct xe_bo *bo) 1322 { 1323 struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev); 1324 struct xe_validation_ctx ctx; 1325 struct drm_exec exec; 1326 struct xe_bo *backup; 1327 int ret = 0; 1328 1329 xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.exclusive = true}, ret) { 1330 ret = drm_exec_lock_obj(&exec, &bo->ttm.base); 1331 drm_exec_retry_on_contention(&exec); 1332 xe_assert(xe, !ret); 1333 xe_assert(xe, !bo->backup_obj); 1334 1335 /* 1336 * Since this is called from the PM notifier we might have raced with 1337 * someone unpinning this after we dropped the pinned list lock and 1338 * grabbing the above bo lock. 1339 */ 1340 if (!xe_bo_is_pinned(bo)) 1341 break; 1342 1343 if (!xe_bo_is_vram(bo)) 1344 break; 1345 1346 if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE) 1347 break; 1348 1349 backup = xe_bo_init_locked(xe, NULL, NULL, bo->ttm.base.resv, NULL, xe_bo_size(bo), 1350 DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel, 1351 XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS | 1352 XE_BO_FLAG_PINNED, &exec); 1353 if (IS_ERR(backup)) { 1354 drm_exec_retry_on_contention(&exec); 1355 ret = PTR_ERR(backup); 1356 xe_validation_retry_on_oom(&ctx, &ret); 1357 break; 1358 } 1359 1360 backup->parent_obj = xe_bo_get(bo); /* Released by bo_destroy */ 1361 ttm_bo_pin(&backup->ttm); 1362 bo->backup_obj = backup; 1363 } 1364 1365 return ret; 1366 } 1367 1368 /** 1369 * xe_bo_notifier_unprepare_pinned() - Undo the previous prepare operation. 1370 * @bo: The buffer object to undo the prepare for. 1371 * 1372 * Always returns 0. The backup object is removed, if still present. Expectation 1373 * it that this called from the PM notifier when undoing the prepare step. 1374 * 1375 * Return: Always returns 0. 1376 */ 1377 int xe_bo_notifier_unprepare_pinned(struct xe_bo *bo) 1378 { 1379 xe_bo_lock(bo, false); 1380 if (bo->backup_obj) { 1381 ttm_bo_unpin(&bo->backup_obj->ttm); 1382 xe_bo_put(bo->backup_obj); 1383 bo->backup_obj = NULL; 1384 } 1385 xe_bo_unlock(bo); 1386 1387 return 0; 1388 } 1389 1390 static int xe_bo_evict_pinned_copy(struct xe_bo *bo, struct xe_bo *backup) 1391 { 1392 struct xe_device *xe = xe_bo_device(bo); 1393 bool unmap = false; 1394 int ret = 0; 1395 1396 if (xe_bo_is_user(bo) || (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) { 1397 struct xe_migrate *migrate; 1398 struct dma_fence *fence; 1399 1400 if (bo->tile) 1401 migrate = bo->tile->migrate; 1402 else 1403 migrate = mem_type_to_migrate(xe, bo->ttm.resource->mem_type); 1404 1405 xe_assert(xe, bo->ttm.base.resv == backup->ttm.base.resv); 1406 ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1); 1407 if (ret) 1408 goto out_backup; 1409 1410 fence = xe_migrate_copy(migrate, bo, backup, bo->ttm.resource, 1411 backup->ttm.resource, false); 1412 if (IS_ERR(fence)) { 1413 ret = PTR_ERR(fence); 1414 goto out_backup; 1415 } 1416 1417 dma_resv_add_fence(bo->ttm.base.resv, fence, 1418 DMA_RESV_USAGE_KERNEL); 1419 dma_fence_put(fence); 1420 } else { 1421 ret = xe_bo_vmap(backup); 1422 if (ret) 1423 goto out_backup; 1424 1425 if (iosys_map_is_null(&bo->vmap)) { 1426 ret = xe_bo_vmap(bo); 1427 if (ret) 1428 goto out_vunmap; 1429 unmap = true; 1430 } 1431 1432 xe_map_memcpy_from(xe, backup->vmap.vaddr, &bo->vmap, 0, 1433 xe_bo_size(bo)); 1434 } 1435 1436 if (!bo->backup_obj) 1437 bo->backup_obj = backup; 1438 out_vunmap: 1439 xe_bo_vunmap(backup); 1440 out_backup: 1441 if (unmap) 1442 xe_bo_vunmap(bo); 1443 1444 return ret; 1445 } 1446 1447 /** 1448 * xe_bo_evict_pinned() - Evict a pinned VRAM object to system memory 1449 * @bo: The buffer object to move. 1450 * 1451 * On successful completion, the object memory will be moved to system memory. 1452 * 1453 * This is needed to for special handling of pinned VRAM object during 1454 * suspend-resume. 1455 * 1456 * Return: 0 on success. Negative error code on failure. 1457 */ 1458 int xe_bo_evict_pinned(struct xe_bo *bo) 1459 { 1460 struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev); 1461 struct xe_validation_ctx ctx; 1462 struct drm_exec exec; 1463 struct xe_bo *backup = bo->backup_obj; 1464 bool backup_created = false; 1465 int ret = 0; 1466 1467 xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.exclusive = true}, ret) { 1468 ret = drm_exec_lock_obj(&exec, &bo->ttm.base); 1469 drm_exec_retry_on_contention(&exec); 1470 xe_assert(xe, !ret); 1471 1472 if (WARN_ON(!bo->ttm.resource)) { 1473 ret = -EINVAL; 1474 break; 1475 } 1476 1477 if (WARN_ON(!xe_bo_is_pinned(bo))) { 1478 ret = -EINVAL; 1479 break; 1480 } 1481 1482 if (!xe_bo_is_vram(bo)) 1483 break; 1484 1485 if (bo->flags & XE_BO_FLAG_PINNED_NORESTORE) 1486 break; 1487 1488 if (!backup) { 1489 backup = xe_bo_init_locked(xe, NULL, NULL, bo->ttm.base.resv, NULL, 1490 xe_bo_size(bo), 1491 DRM_XE_GEM_CPU_CACHING_WB, ttm_bo_type_kernel, 1492 XE_BO_FLAG_SYSTEM | XE_BO_FLAG_NEEDS_CPU_ACCESS | 1493 XE_BO_FLAG_PINNED, &exec); 1494 if (IS_ERR(backup)) { 1495 drm_exec_retry_on_contention(&exec); 1496 ret = PTR_ERR(backup); 1497 xe_validation_retry_on_oom(&ctx, &ret); 1498 break; 1499 } 1500 backup->parent_obj = xe_bo_get(bo); /* Released by bo_destroy */ 1501 backup_created = true; 1502 } 1503 1504 ret = xe_bo_evict_pinned_copy(bo, backup); 1505 } 1506 1507 if (ret && backup_created) 1508 xe_bo_put(backup); 1509 1510 return ret; 1511 } 1512 1513 /** 1514 * xe_bo_restore_pinned() - Restore a pinned VRAM object 1515 * @bo: The buffer object to move. 1516 * 1517 * On successful completion, the object memory will be moved back to VRAM. 1518 * 1519 * This is needed to for special handling of pinned VRAM object during 1520 * suspend-resume. 1521 * 1522 * Return: 0 on success. Negative error code on failure. 1523 */ 1524 int xe_bo_restore_pinned(struct xe_bo *bo) 1525 { 1526 struct ttm_operation_ctx ctx = { 1527 .interruptible = false, 1528 .gfp_retry_mayfail = false, 1529 }; 1530 struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev); 1531 struct xe_bo *backup = bo->backup_obj; 1532 bool unmap = false; 1533 int ret; 1534 1535 if (!backup) 1536 return 0; 1537 1538 xe_bo_lock(bo, false); 1539 1540 if (!xe_bo_is_pinned(backup)) { 1541 ret = ttm_bo_validate(&backup->ttm, &backup->placement, &ctx); 1542 if (ret) 1543 goto out_unlock_bo; 1544 } 1545 1546 if (xe_bo_is_user(bo) || (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE)) { 1547 struct xe_migrate *migrate; 1548 struct dma_fence *fence; 1549 1550 if (bo->tile) 1551 migrate = bo->tile->migrate; 1552 else 1553 migrate = mem_type_to_migrate(xe, bo->ttm.resource->mem_type); 1554 1555 ret = dma_resv_reserve_fences(bo->ttm.base.resv, 1); 1556 if (ret) 1557 goto out_unlock_bo; 1558 1559 fence = xe_migrate_copy(migrate, backup, bo, 1560 backup->ttm.resource, bo->ttm.resource, 1561 false); 1562 if (IS_ERR(fence)) { 1563 ret = PTR_ERR(fence); 1564 goto out_unlock_bo; 1565 } 1566 1567 dma_resv_add_fence(bo->ttm.base.resv, fence, 1568 DMA_RESV_USAGE_KERNEL); 1569 dma_fence_put(fence); 1570 } else { 1571 ret = xe_bo_vmap(backup); 1572 if (ret) 1573 goto out_unlock_bo; 1574 1575 if (iosys_map_is_null(&bo->vmap)) { 1576 ret = xe_bo_vmap(bo); 1577 if (ret) 1578 goto out_backup; 1579 unmap = true; 1580 } 1581 1582 xe_map_memcpy_to(xe, &bo->vmap, 0, backup->vmap.vaddr, 1583 xe_bo_size(bo)); 1584 } 1585 1586 bo->backup_obj = NULL; 1587 1588 out_backup: 1589 xe_bo_vunmap(backup); 1590 if (!bo->backup_obj) { 1591 if (xe_bo_is_pinned(backup)) 1592 ttm_bo_unpin(&backup->ttm); 1593 xe_bo_put(backup); 1594 } 1595 out_unlock_bo: 1596 if (unmap) 1597 xe_bo_vunmap(bo); 1598 xe_bo_unlock(bo); 1599 return ret; 1600 } 1601 1602 int xe_bo_dma_unmap_pinned(struct xe_bo *bo) 1603 { 1604 struct ttm_buffer_object *ttm_bo = &bo->ttm; 1605 struct ttm_tt *tt = ttm_bo->ttm; 1606 1607 if (tt) { 1608 struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm); 1609 1610 if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) { 1611 dma_buf_unmap_attachment(ttm_bo->base.import_attach, 1612 ttm_bo->sg, 1613 DMA_BIDIRECTIONAL); 1614 ttm_bo->sg = NULL; 1615 xe_tt->sg = NULL; 1616 } else if (xe_tt->sg) { 1617 dma_unmap_sgtable(ttm_to_xe_device(ttm_bo->bdev)->drm.dev, 1618 xe_tt->sg, 1619 DMA_BIDIRECTIONAL, 0); 1620 sg_free_table(xe_tt->sg); 1621 xe_tt->sg = NULL; 1622 } 1623 } 1624 1625 return 0; 1626 } 1627 1628 static unsigned long xe_ttm_io_mem_pfn(struct ttm_buffer_object *ttm_bo, 1629 unsigned long page_offset) 1630 { 1631 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); 1632 struct xe_res_cursor cursor; 1633 struct xe_vram_region *vram; 1634 1635 if (ttm_bo->resource->mem_type == XE_PL_STOLEN) 1636 return xe_ttm_stolen_io_offset(bo, page_offset << PAGE_SHIFT) >> PAGE_SHIFT; 1637 1638 vram = xe_map_resource_to_region(ttm_bo->resource); 1639 xe_res_first(ttm_bo->resource, (u64)page_offset << PAGE_SHIFT, 0, &cursor); 1640 return (vram->io_start + cursor.start) >> PAGE_SHIFT; 1641 } 1642 1643 static void __xe_bo_vunmap(struct xe_bo *bo); 1644 1645 /* 1646 * TODO: Move this function to TTM so we don't rely on how TTM does its 1647 * locking, thereby abusing TTM internals. 1648 */ 1649 static bool xe_ttm_bo_lock_in_destructor(struct ttm_buffer_object *ttm_bo) 1650 { 1651 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); 1652 bool locked; 1653 1654 xe_assert(xe, !kref_read(&ttm_bo->kref)); 1655 1656 /* 1657 * We can typically only race with TTM trylocking under the 1658 * lru_lock, which will immediately be unlocked again since 1659 * the ttm_bo refcount is zero at this point. So trylocking *should* 1660 * always succeed here, as long as we hold the lru lock. 1661 */ 1662 spin_lock(&ttm_bo->bdev->lru_lock); 1663 locked = dma_resv_trylock(&ttm_bo->base._resv); 1664 spin_unlock(&ttm_bo->bdev->lru_lock); 1665 xe_assert(xe, locked); 1666 1667 return locked; 1668 } 1669 1670 static void xe_ttm_bo_release_notify(struct ttm_buffer_object *ttm_bo) 1671 { 1672 struct dma_resv_iter cursor; 1673 struct dma_fence *fence; 1674 struct dma_fence *replacement = NULL; 1675 struct xe_bo *bo; 1676 1677 if (!xe_bo_is_xe_bo(ttm_bo)) 1678 return; 1679 1680 bo = ttm_to_xe_bo(ttm_bo); 1681 xe_assert(xe_bo_device(bo), !(bo->created && kref_read(&ttm_bo->base.refcount))); 1682 1683 if (!xe_ttm_bo_lock_in_destructor(ttm_bo)) 1684 return; 1685 1686 /* 1687 * Scrub the preempt fences if any. The unbind fence is already 1688 * attached to the resv. 1689 * TODO: Don't do this for external bos once we scrub them after 1690 * unbind. 1691 */ 1692 dma_resv_for_each_fence(&cursor, &ttm_bo->base._resv, 1693 DMA_RESV_USAGE_BOOKKEEP, fence) { 1694 if (xe_fence_is_xe_preempt(fence) && 1695 !dma_fence_is_signaled(fence)) { 1696 if (!replacement) 1697 replacement = dma_fence_get_stub(); 1698 1699 dma_resv_replace_fences(&ttm_bo->base._resv, 1700 fence->context, 1701 replacement, 1702 DMA_RESV_USAGE_BOOKKEEP); 1703 } 1704 } 1705 dma_fence_put(replacement); 1706 1707 dma_resv_unlock(&ttm_bo->base._resv); 1708 } 1709 1710 static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo) 1711 { 1712 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); 1713 1714 if (!xe_bo_is_xe_bo(ttm_bo)) 1715 return; 1716 1717 if (IS_VF_CCS_READY(ttm_to_xe_device(ttm_bo->bdev))) 1718 xe_sriov_vf_ccs_detach_bo(bo); 1719 1720 /* 1721 * Object is idle and about to be destroyed. Release the 1722 * dma-buf attachment. 1723 */ 1724 if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) { 1725 struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, 1726 struct xe_ttm_tt, ttm); 1727 1728 dma_buf_unmap_attachment(ttm_bo->base.import_attach, ttm_bo->sg, 1729 DMA_BIDIRECTIONAL); 1730 ttm_bo->sg = NULL; 1731 xe_tt->sg = NULL; 1732 } 1733 } 1734 1735 static void xe_ttm_bo_swap_notify(struct ttm_buffer_object *ttm_bo) 1736 { 1737 struct ttm_operation_ctx ctx = { 1738 .interruptible = false, 1739 .gfp_retry_mayfail = false, 1740 }; 1741 1742 if (ttm_bo->ttm) { 1743 struct xe_ttm_tt *xe_tt = 1744 container_of(ttm_bo->ttm, struct xe_ttm_tt, ttm); 1745 1746 if (xe_tt->purgeable) 1747 xe_ttm_bo_purge(ttm_bo, &ctx); 1748 } 1749 } 1750 1751 static int xe_ttm_access_memory(struct ttm_buffer_object *ttm_bo, 1752 unsigned long offset, void *buf, int len, 1753 int write) 1754 { 1755 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); 1756 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); 1757 struct iosys_map vmap; 1758 struct xe_res_cursor cursor; 1759 struct xe_vram_region *vram; 1760 int bytes_left = len; 1761 int err = 0; 1762 1763 xe_bo_assert_held(bo); 1764 xe_device_assert_mem_access(xe); 1765 1766 if (!mem_type_is_vram(ttm_bo->resource->mem_type)) 1767 return -EIO; 1768 1769 if (!xe_bo_is_visible_vram(bo) || len >= SZ_16K) { 1770 struct xe_migrate *migrate = 1771 mem_type_to_migrate(xe, ttm_bo->resource->mem_type); 1772 1773 err = xe_migrate_access_memory(migrate, bo, offset, buf, len, 1774 write); 1775 goto out; 1776 } 1777 1778 vram = xe_map_resource_to_region(ttm_bo->resource); 1779 xe_res_first(ttm_bo->resource, offset & PAGE_MASK, 1780 xe_bo_size(bo) - (offset & PAGE_MASK), &cursor); 1781 1782 do { 1783 unsigned long page_offset = (offset & ~PAGE_MASK); 1784 int byte_count = min((int)(PAGE_SIZE - page_offset), bytes_left); 1785 1786 iosys_map_set_vaddr_iomem(&vmap, (u8 __iomem *)vram->mapping + 1787 cursor.start); 1788 if (write) 1789 xe_map_memcpy_to(xe, &vmap, page_offset, buf, byte_count); 1790 else 1791 xe_map_memcpy_from(xe, buf, &vmap, page_offset, byte_count); 1792 1793 buf += byte_count; 1794 offset += byte_count; 1795 bytes_left -= byte_count; 1796 if (bytes_left) 1797 xe_res_next(&cursor, PAGE_SIZE); 1798 } while (bytes_left); 1799 1800 out: 1801 return err ?: len; 1802 } 1803 1804 const struct ttm_device_funcs xe_ttm_funcs = { 1805 .ttm_tt_create = xe_ttm_tt_create, 1806 .ttm_tt_populate = xe_ttm_tt_populate, 1807 .ttm_tt_unpopulate = xe_ttm_tt_unpopulate, 1808 .ttm_tt_destroy = xe_ttm_tt_destroy, 1809 .evict_flags = xe_evict_flags, 1810 .move = xe_bo_move, 1811 .io_mem_reserve = xe_ttm_io_mem_reserve, 1812 .io_mem_pfn = xe_ttm_io_mem_pfn, 1813 .access_memory = xe_ttm_access_memory, 1814 .release_notify = xe_ttm_bo_release_notify, 1815 .eviction_valuable = xe_bo_eviction_valuable, 1816 .delete_mem_notify = xe_ttm_bo_delete_mem_notify, 1817 .swap_notify = xe_ttm_bo_swap_notify, 1818 }; 1819 1820 static void xe_ttm_bo_destroy(struct ttm_buffer_object *ttm_bo) 1821 { 1822 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); 1823 struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev); 1824 struct xe_tile *tile; 1825 u8 id; 1826 1827 if (bo->ttm.base.import_attach) 1828 drm_prime_gem_destroy(&bo->ttm.base, NULL); 1829 drm_gem_object_release(&bo->ttm.base); 1830 1831 xe_assert(xe, list_empty(&ttm_bo->base.gpuva.list)); 1832 1833 for_each_tile(tile, xe, id) 1834 if (bo->ggtt_node[id]) 1835 xe_ggtt_remove_bo(tile->mem.ggtt, bo); 1836 1837 #ifdef CONFIG_PROC_FS 1838 if (bo->client) 1839 xe_drm_client_remove_bo(bo); 1840 #endif 1841 1842 if (bo->vm && xe_bo_is_user(bo)) 1843 xe_vm_put(bo->vm); 1844 1845 if (bo->parent_obj) 1846 xe_bo_put(bo->parent_obj); 1847 1848 mutex_lock(&xe->mem_access.vram_userfault.lock); 1849 if (!list_empty(&bo->vram_userfault_link)) 1850 list_del(&bo->vram_userfault_link); 1851 mutex_unlock(&xe->mem_access.vram_userfault.lock); 1852 1853 kfree(bo); 1854 } 1855 1856 static void xe_gem_object_free(struct drm_gem_object *obj) 1857 { 1858 /* Our BO reference counting scheme works as follows: 1859 * 1860 * The gem object kref is typically used throughout the driver, 1861 * and the gem object holds a ttm_buffer_object refcount, so 1862 * that when the last gem object reference is put, which is when 1863 * we end up in this function, we put also that ttm_buffer_object 1864 * refcount. Anything using gem interfaces is then no longer 1865 * allowed to access the object in a way that requires a gem 1866 * refcount, including locking the object. 1867 * 1868 * driver ttm callbacks is allowed to use the ttm_buffer_object 1869 * refcount directly if needed. 1870 */ 1871 __xe_bo_vunmap(gem_to_xe_bo(obj)); 1872 ttm_bo_fini(container_of(obj, struct ttm_buffer_object, base)); 1873 } 1874 1875 static void xe_gem_object_close(struct drm_gem_object *obj, 1876 struct drm_file *file_priv) 1877 { 1878 struct xe_bo *bo = gem_to_xe_bo(obj); 1879 1880 if (bo->vm && !xe_vm_in_fault_mode(bo->vm)) { 1881 xe_assert(xe_bo_device(bo), xe_bo_is_user(bo)); 1882 1883 xe_bo_lock(bo, false); 1884 ttm_bo_set_bulk_move(&bo->ttm, NULL); 1885 xe_bo_unlock(bo); 1886 } 1887 } 1888 1889 static bool should_migrate_to_smem(struct xe_bo *bo) 1890 { 1891 /* 1892 * NOTE: The following atomic checks are platform-specific. For example, 1893 * if a device supports CXL atomics, these may not be necessary or 1894 * may behave differently. 1895 */ 1896 1897 return bo->attr.atomic_access == DRM_XE_ATOMIC_GLOBAL || 1898 bo->attr.atomic_access == DRM_XE_ATOMIC_CPU; 1899 } 1900 1901 static int xe_bo_wait_usage_kernel(struct xe_bo *bo, struct ttm_operation_ctx *ctx) 1902 { 1903 long lerr; 1904 1905 if (ctx->no_wait_gpu) 1906 return dma_resv_test_signaled(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL) ? 1907 0 : -EBUSY; 1908 1909 lerr = dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL, 1910 ctx->interruptible, MAX_SCHEDULE_TIMEOUT); 1911 if (lerr < 0) 1912 return lerr; 1913 if (lerr == 0) 1914 return -EBUSY; 1915 1916 return 0; 1917 } 1918 1919 /* Populate the bo if swapped out, or migrate if the access mode requires that. */ 1920 static int xe_bo_fault_migrate(struct xe_bo *bo, struct ttm_operation_ctx *ctx, 1921 struct drm_exec *exec) 1922 { 1923 struct ttm_buffer_object *tbo = &bo->ttm; 1924 int err = 0; 1925 1926 if (ttm_manager_type(tbo->bdev, tbo->resource->mem_type)->use_tt) { 1927 err = xe_bo_wait_usage_kernel(bo, ctx); 1928 if (!err) 1929 err = ttm_bo_populate(&bo->ttm, ctx); 1930 } else if (should_migrate_to_smem(bo)) { 1931 xe_assert(xe_bo_device(bo), bo->flags & XE_BO_FLAG_SYSTEM); 1932 err = xe_bo_migrate(bo, XE_PL_TT, ctx, exec); 1933 } 1934 1935 return err; 1936 } 1937 1938 /* Call into TTM to populate PTEs, and register bo for PTE removal on runtime suspend. */ 1939 static vm_fault_t __xe_bo_cpu_fault(struct vm_fault *vmf, struct xe_device *xe, struct xe_bo *bo) 1940 { 1941 vm_fault_t ret; 1942 1943 trace_xe_bo_cpu_fault(bo); 1944 1945 ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot, 1946 TTM_BO_VM_NUM_PREFAULT); 1947 /* 1948 * When TTM is actually called to insert PTEs, ensure no blocking conditions 1949 * remain, in which case TTM may drop locks and return VM_FAULT_RETRY. 1950 */ 1951 xe_assert(xe, ret != VM_FAULT_RETRY); 1952 1953 if (ret == VM_FAULT_NOPAGE && 1954 mem_type_is_vram(bo->ttm.resource->mem_type)) { 1955 mutex_lock(&xe->mem_access.vram_userfault.lock); 1956 if (list_empty(&bo->vram_userfault_link)) 1957 list_add(&bo->vram_userfault_link, 1958 &xe->mem_access.vram_userfault.list); 1959 mutex_unlock(&xe->mem_access.vram_userfault.lock); 1960 } 1961 1962 return ret; 1963 } 1964 1965 static vm_fault_t xe_err_to_fault_t(int err) 1966 { 1967 switch (err) { 1968 case 0: 1969 case -EINTR: 1970 case -ERESTARTSYS: 1971 case -EAGAIN: 1972 return VM_FAULT_NOPAGE; 1973 case -ENOMEM: 1974 case -ENOSPC: 1975 return VM_FAULT_OOM; 1976 default: 1977 break; 1978 } 1979 return VM_FAULT_SIGBUS; 1980 } 1981 1982 static bool xe_ttm_bo_is_imported(struct ttm_buffer_object *tbo) 1983 { 1984 dma_resv_assert_held(tbo->base.resv); 1985 1986 return tbo->ttm && 1987 (tbo->ttm->page_flags & (TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_EXTERNAL_MAPPABLE)) == 1988 TTM_TT_FLAG_EXTERNAL; 1989 } 1990 1991 static vm_fault_t xe_bo_cpu_fault_fastpath(struct vm_fault *vmf, struct xe_device *xe, 1992 struct xe_bo *bo, bool needs_rpm) 1993 { 1994 struct ttm_buffer_object *tbo = &bo->ttm; 1995 vm_fault_t ret = VM_FAULT_RETRY; 1996 struct xe_validation_ctx ctx; 1997 struct ttm_operation_ctx tctx = { 1998 .interruptible = true, 1999 .no_wait_gpu = true, 2000 .gfp_retry_mayfail = true, 2001 2002 }; 2003 int err; 2004 2005 if (needs_rpm && !xe_pm_runtime_get_if_active(xe)) 2006 return VM_FAULT_RETRY; 2007 2008 err = xe_validation_ctx_init(&ctx, &xe->val, NULL, 2009 (struct xe_val_flags) { 2010 .interruptible = true, 2011 .no_block = true 2012 }); 2013 if (err) 2014 goto out_pm; 2015 2016 if (!dma_resv_trylock(tbo->base.resv)) 2017 goto out_validation; 2018 2019 /* 2020 * Reject CPU faults to purgeable BOs. DONTNEED BOs can be purged 2021 * at any time, and purged BOs have no backing store. Either case 2022 * is undefined behavior for CPU access. 2023 */ 2024 if (xe_bo_madv_is_dontneed(bo) || xe_bo_is_purged(bo)) { 2025 ret = VM_FAULT_SIGBUS; 2026 goto out_unlock; 2027 } 2028 2029 if (xe_ttm_bo_is_imported(tbo)) { 2030 ret = VM_FAULT_SIGBUS; 2031 drm_dbg(&xe->drm, "CPU trying to access an imported buffer object.\n"); 2032 goto out_unlock; 2033 } 2034 2035 err = xe_bo_fault_migrate(bo, &tctx, NULL); 2036 if (err) { 2037 /* Return VM_FAULT_RETRY on these errors. */ 2038 if (err != -ENOMEM && err != -ENOSPC && err != -EBUSY) 2039 ret = xe_err_to_fault_t(err); 2040 goto out_unlock; 2041 } 2042 2043 if (dma_resv_test_signaled(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL)) 2044 ret = __xe_bo_cpu_fault(vmf, xe, bo); 2045 2046 out_unlock: 2047 dma_resv_unlock(tbo->base.resv); 2048 out_validation: 2049 xe_validation_ctx_fini(&ctx); 2050 out_pm: 2051 if (needs_rpm) 2052 xe_pm_runtime_put(xe); 2053 2054 return ret; 2055 } 2056 2057 static vm_fault_t xe_bo_cpu_fault(struct vm_fault *vmf) 2058 { 2059 struct ttm_buffer_object *tbo = vmf->vma->vm_private_data; 2060 struct drm_device *ddev = tbo->base.dev; 2061 struct xe_device *xe = to_xe_device(ddev); 2062 struct xe_bo *bo = ttm_to_xe_bo(tbo); 2063 bool needs_rpm = bo->flags & XE_BO_FLAG_VRAM_MASK; 2064 bool retry_after_wait = false; 2065 struct xe_validation_ctx ctx; 2066 struct drm_exec exec; 2067 vm_fault_t ret; 2068 int err = 0; 2069 int idx; 2070 2071 if (xe_device_wedged(xe) || !drm_dev_enter(&xe->drm, &idx)) 2072 return ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot); 2073 2074 ret = xe_bo_cpu_fault_fastpath(vmf, xe, bo, needs_rpm); 2075 if (ret != VM_FAULT_RETRY) 2076 goto out; 2077 2078 if (fault_flag_allow_retry_first(vmf->flags)) { 2079 if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) 2080 goto out; 2081 retry_after_wait = true; 2082 xe_bo_get(bo); 2083 mmap_read_unlock(vmf->vma->vm_mm); 2084 } else { 2085 ret = VM_FAULT_NOPAGE; 2086 } 2087 2088 /* 2089 * The fastpath failed and we were not required to return and retry immediately. 2090 * We're now running in one of two modes: 2091 * 2092 * 1) retry_after_wait == true: The mmap_read_lock() is dropped, and we're trying 2093 * to resolve blocking waits. But we can't resolve the fault since the 2094 * mmap_read_lock() is dropped. After retrying the fault, the aim is that the fastpath 2095 * should succeed. But it may fail since we drop the bo lock. 2096 * 2097 * 2) retry_after_wait == false: The fastpath failed, typically even after 2098 * a retry. Do whatever's necessary to resolve the fault. 2099 * 2100 * This construct is recommended to avoid excessive waits under the mmap_lock. 2101 */ 2102 2103 if (needs_rpm) 2104 xe_pm_runtime_get(xe); 2105 2106 xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = true}, 2107 err) { 2108 struct ttm_operation_ctx tctx = { 2109 .interruptible = true, 2110 .no_wait_gpu = false, 2111 .gfp_retry_mayfail = retry_after_wait, 2112 }; 2113 2114 err = drm_exec_lock_obj(&exec, &tbo->base); 2115 drm_exec_retry_on_contention(&exec); 2116 if (err) 2117 break; 2118 2119 /* 2120 * Reject CPU faults to purgeable BOs. DONTNEED BOs can be 2121 * purged at any time, and purged BOs have no backing store. 2122 */ 2123 if (xe_bo_madv_is_dontneed(bo) || xe_bo_is_purged(bo)) { 2124 err = -EFAULT; 2125 break; 2126 } 2127 2128 if (xe_ttm_bo_is_imported(tbo)) { 2129 err = -EFAULT; 2130 drm_dbg(&xe->drm, "CPU trying to access an imported buffer object.\n"); 2131 break; 2132 } 2133 2134 err = xe_bo_fault_migrate(bo, &tctx, &exec); 2135 if (err) { 2136 drm_exec_retry_on_contention(&exec); 2137 xe_validation_retry_on_oom(&ctx, &err); 2138 break; 2139 } 2140 2141 err = xe_bo_wait_usage_kernel(bo, &tctx); 2142 if (err) 2143 break; 2144 2145 if (!retry_after_wait) 2146 ret = __xe_bo_cpu_fault(vmf, xe, bo); 2147 } 2148 /* if retry_after_wait == true, we *must* return VM_FAULT_RETRY. */ 2149 if (err && !retry_after_wait) 2150 ret = xe_err_to_fault_t(err); 2151 2152 if (needs_rpm) 2153 xe_pm_runtime_put(xe); 2154 2155 if (retry_after_wait) 2156 xe_bo_put(bo); 2157 out: 2158 drm_dev_exit(idx); 2159 2160 return ret; 2161 } 2162 2163 static int xe_bo_vm_access(struct vm_area_struct *vma, unsigned long addr, 2164 void *buf, int len, int write) 2165 { 2166 struct ttm_buffer_object *ttm_bo = vma->vm_private_data; 2167 struct xe_bo *bo = ttm_to_xe_bo(ttm_bo); 2168 struct xe_device *xe = xe_bo_device(bo); 2169 2170 guard(xe_pm_runtime)(xe); 2171 return ttm_bo_vm_access(vma, addr, buf, len, write); 2172 } 2173 2174 /** 2175 * xe_bo_read() - Read from an xe_bo 2176 * @bo: The buffer object to read from. 2177 * @offset: The byte offset to start reading from. 2178 * @dst: Location to store the read. 2179 * @size: Size in bytes for the read. 2180 * 2181 * Read @size bytes from the @bo, starting from @offset, storing into @dst. 2182 * 2183 * Return: Zero on success, or negative error. 2184 */ 2185 int xe_bo_read(struct xe_bo *bo, u64 offset, void *dst, int size) 2186 { 2187 int ret; 2188 2189 ret = ttm_bo_access(&bo->ttm, offset, dst, size, 0); 2190 if (ret >= 0 && ret != size) 2191 ret = -EIO; 2192 else if (ret == size) 2193 ret = 0; 2194 2195 return ret; 2196 } 2197 2198 static const struct vm_operations_struct xe_gem_vm_ops = { 2199 .fault = xe_bo_cpu_fault, 2200 .open = ttm_bo_vm_open, 2201 .close = ttm_bo_vm_close, 2202 .access = xe_bo_vm_access, 2203 }; 2204 2205 static int xe_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) 2206 { 2207 struct xe_bo *bo = gem_to_xe_bo(obj); 2208 int err = 0; 2209 2210 /* 2211 * Reject mmap of purgeable BOs. DONTNEED BOs can be purged 2212 * at any time, making CPU access undefined behavior. Purged BOs have 2213 * no backing store and are permanently invalid. 2214 */ 2215 err = xe_bo_lock(bo, true); 2216 if (err) 2217 return err; 2218 2219 if (xe_bo_madv_is_dontneed(bo)) 2220 err = -EBUSY; 2221 else if (xe_bo_is_purged(bo)) 2222 err = -EINVAL; 2223 xe_bo_unlock(bo); 2224 if (err) 2225 return err; 2226 2227 return drm_gem_ttm_mmap(obj, vma); 2228 } 2229 2230 static const struct drm_gem_object_funcs xe_gem_object_funcs = { 2231 .free = xe_gem_object_free, 2232 .close = xe_gem_object_close, 2233 .mmap = xe_gem_object_mmap, 2234 .export = xe_gem_prime_export, 2235 .vm_ops = &xe_gem_vm_ops, 2236 }; 2237 2238 /** 2239 * xe_bo_alloc - Allocate storage for a struct xe_bo 2240 * 2241 * This function is intended to allocate storage to be used for input 2242 * to __xe_bo_create_locked(), in the case a pointer to the bo to be 2243 * created is needed before the call to __xe_bo_create_locked(). 2244 * If __xe_bo_create_locked ends up never to be called, then the 2245 * storage allocated with this function needs to be freed using 2246 * xe_bo_free(). 2247 * 2248 * Return: A pointer to an uninitialized struct xe_bo on success, 2249 * ERR_PTR(-ENOMEM) on error. 2250 */ 2251 struct xe_bo *xe_bo_alloc(void) 2252 { 2253 struct xe_bo *bo = kzalloc_obj(*bo); 2254 2255 if (!bo) 2256 return ERR_PTR(-ENOMEM); 2257 2258 return bo; 2259 } 2260 2261 /** 2262 * xe_bo_free - Free storage allocated using xe_bo_alloc() 2263 * @bo: The buffer object storage. 2264 * 2265 * Refer to xe_bo_alloc() documentation for valid use-cases. 2266 */ 2267 void xe_bo_free(struct xe_bo *bo) 2268 { 2269 kfree(bo); 2270 } 2271 2272 /** 2273 * xe_bo_init_locked() - Initialize or create an xe_bo. 2274 * @xe: The xe device. 2275 * @bo: An already allocated buffer object or NULL 2276 * if the function should allocate a new one. 2277 * @tile: The tile to select for migration of this bo, and the tile used for 2278 * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. 2279 * @resv: Pointer to a locked shared reservation object to use for this bo, 2280 * or NULL for the xe_bo to use its own. 2281 * @bulk: The bulk move to use for LRU bumping, or NULL for external bos. 2282 * @size: The storage size to use for the bo. 2283 * @cpu_caching: The cpu caching used for system memory backing store. 2284 * @type: The TTM buffer object type. 2285 * @flags: XE_BO_FLAG_ flags. 2286 * @exec: The drm_exec transaction to use for exhaustive eviction. 2287 * 2288 * Initialize or create an xe buffer object. On failure, any allocated buffer 2289 * object passed in @bo will have been unreferenced. 2290 * 2291 * Return: The buffer object on success. Negative error pointer on failure. 2292 */ 2293 struct xe_bo *xe_bo_init_locked(struct xe_device *xe, struct xe_bo *bo, 2294 struct xe_tile *tile, struct dma_resv *resv, 2295 struct ttm_lru_bulk_move *bulk, size_t size, 2296 u16 cpu_caching, enum ttm_bo_type type, 2297 u32 flags, struct drm_exec *exec) 2298 { 2299 struct ttm_operation_ctx ctx = { 2300 .interruptible = true, 2301 .no_wait_gpu = false, 2302 .gfp_retry_mayfail = true, 2303 }; 2304 struct ttm_placement *placement; 2305 uint32_t alignment; 2306 size_t aligned_size; 2307 int err; 2308 2309 /* Only kernel objects should set GT */ 2310 xe_assert(xe, !tile || type == ttm_bo_type_kernel); 2311 2312 if (XE_WARN_ON(!size)) { 2313 xe_bo_free(bo); 2314 return ERR_PTR(-EINVAL); 2315 } 2316 2317 /* XE_BO_FLAG_GGTTx requires XE_BO_FLAG_GGTT also be set */ 2318 if ((flags & XE_BO_FLAG_GGTT_ALL) && !(flags & XE_BO_FLAG_GGTT)) { 2319 xe_bo_free(bo); 2320 return ERR_PTR(-EINVAL); 2321 } 2322 2323 if (flags & (XE_BO_FLAG_VRAM_MASK | XE_BO_FLAG_STOLEN) && 2324 !(flags & XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE) && 2325 ((xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) || 2326 (flags & (XE_BO_FLAG_NEEDS_64K | XE_BO_FLAG_NEEDS_2M)))) { 2327 size_t align = flags & XE_BO_FLAG_NEEDS_2M ? SZ_2M : SZ_64K; 2328 2329 aligned_size = ALIGN(size, align); 2330 if (type != ttm_bo_type_device) 2331 size = ALIGN(size, align); 2332 flags |= XE_BO_FLAG_INTERNAL_64K; 2333 alignment = align >> PAGE_SHIFT; 2334 } else { 2335 aligned_size = ALIGN(size, SZ_4K); 2336 flags &= ~XE_BO_FLAG_INTERNAL_64K; 2337 alignment = SZ_4K >> PAGE_SHIFT; 2338 } 2339 2340 if (type == ttm_bo_type_device && aligned_size != size) { 2341 xe_bo_free(bo); 2342 return ERR_PTR(-EINVAL); 2343 } 2344 2345 if (!bo) { 2346 bo = xe_bo_alloc(); 2347 if (IS_ERR(bo)) 2348 return bo; 2349 } 2350 2351 bo->ccs_cleared = false; 2352 bo->tile = tile; 2353 bo->flags = flags; 2354 bo->cpu_caching = cpu_caching; 2355 bo->ttm.base.funcs = &xe_gem_object_funcs; 2356 bo->ttm.priority = XE_BO_PRIORITY_NORMAL; 2357 INIT_LIST_HEAD(&bo->pinned_link); 2358 #ifdef CONFIG_PROC_FS 2359 INIT_LIST_HEAD(&bo->client_link); 2360 #endif 2361 INIT_LIST_HEAD(&bo->vram_userfault_link); 2362 2363 /* Initialize purge advisory state */ 2364 bo->purgeable.state = XE_MADV_PURGEABLE_WILLNEED; 2365 2366 drm_gem_private_object_init(&xe->drm, &bo->ttm.base, size); 2367 2368 if (resv) { 2369 ctx.allow_res_evict = !(flags & XE_BO_FLAG_NO_RESV_EVICT); 2370 ctx.resv = resv; 2371 } 2372 2373 xe_validation_assert_exec(xe, exec, &bo->ttm.base); 2374 if (!(flags & XE_BO_FLAG_FIXED_PLACEMENT)) { 2375 err = __xe_bo_placement_for_flags(xe, bo, bo->flags, type); 2376 if (WARN_ON(err)) { 2377 xe_ttm_bo_destroy(&bo->ttm); 2378 return ERR_PTR(err); 2379 } 2380 } 2381 2382 /* Defer populating type_sg bos */ 2383 placement = (type == ttm_bo_type_sg || 2384 bo->flags & XE_BO_FLAG_DEFER_BACKING) ? &sys_placement : 2385 &bo->placement; 2386 err = ttm_bo_init_reserved(&xe->ttm, &bo->ttm, type, 2387 placement, alignment, 2388 &ctx, NULL, resv, xe_ttm_bo_destroy); 2389 if (err) 2390 return ERR_PTR(err); 2391 2392 /* 2393 * The VRAM pages underneath are potentially still being accessed by the 2394 * GPU, as per async GPU clearing and async evictions. However TTM makes 2395 * sure to add any corresponding move/clear fences into the objects 2396 * dma-resv using the DMA_RESV_USAGE_KERNEL slot. 2397 * 2398 * For KMD internal buffers we don't care about GPU clearing, however we 2399 * still need to handle async evictions, where the VRAM is still being 2400 * accessed by the GPU. Most internal callers are not expecting this, 2401 * since they are missing the required synchronisation before accessing 2402 * the memory. To keep things simple just sync wait any kernel fences 2403 * here, if the buffer is designated KMD internal. 2404 * 2405 * For normal userspace objects we should already have the required 2406 * pipelining or sync waiting elsewhere, since we already have to deal 2407 * with things like async GPU clearing. 2408 */ 2409 if (type == ttm_bo_type_kernel) { 2410 long timeout = dma_resv_wait_timeout(bo->ttm.base.resv, 2411 DMA_RESV_USAGE_KERNEL, 2412 ctx.interruptible, 2413 MAX_SCHEDULE_TIMEOUT); 2414 2415 if (timeout < 0) { 2416 if (!resv) 2417 dma_resv_unlock(bo->ttm.base.resv); 2418 xe_bo_put(bo); 2419 return ERR_PTR(timeout); 2420 } 2421 } 2422 2423 bo->created = true; 2424 if (bulk) 2425 ttm_bo_set_bulk_move(&bo->ttm, bulk); 2426 else 2427 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); 2428 2429 return bo; 2430 } 2431 2432 static int __xe_bo_fixed_placement(struct xe_device *xe, 2433 struct xe_bo *bo, enum ttm_bo_type type, 2434 u32 flags, 2435 u64 start, u64 end, u64 size) 2436 { 2437 struct ttm_place *place = bo->placements; 2438 u32 vram_flag, vram_stolen_flags; 2439 2440 /* 2441 * to allow fixed placement in GGTT of a VF, post-migration fixups would have to 2442 * include selecting a new fixed offset and shifting the page ranges for it 2443 */ 2444 xe_assert(xe, !IS_SRIOV_VF(xe) || !(bo->flags & XE_BO_FLAG_GGTT)); 2445 2446 if (flags & (XE_BO_FLAG_USER | XE_BO_FLAG_SYSTEM)) 2447 return -EINVAL; 2448 2449 vram_flag = flags & XE_BO_FLAG_VRAM_MASK; 2450 vram_stolen_flags = (flags & (XE_BO_FLAG_STOLEN)) | vram_flag; 2451 2452 /* check if more than one VRAM/STOLEN flag is set */ 2453 if (hweight32(vram_stolen_flags) > 1) 2454 return -EINVAL; 2455 2456 place->flags = TTM_PL_FLAG_CONTIGUOUS; 2457 place->fpfn = start >> PAGE_SHIFT; 2458 place->lpfn = end >> PAGE_SHIFT; 2459 2460 if (flags & XE_BO_FLAG_STOLEN) 2461 place->mem_type = XE_PL_STOLEN; 2462 else 2463 place->mem_type = bo_vram_flags_to_vram_placement(xe, flags, vram_flag, type); 2464 2465 bo->placement = (struct ttm_placement) { 2466 .num_placement = 1, 2467 .placement = place, 2468 }; 2469 2470 return 0; 2471 } 2472 2473 static struct xe_bo * 2474 __xe_bo_create_locked(struct xe_device *xe, 2475 struct xe_tile *tile, struct xe_vm *vm, 2476 size_t size, u64 start, u64 end, 2477 u16 cpu_caching, enum ttm_bo_type type, u32 flags, 2478 u64 alignment, struct drm_exec *exec) 2479 { 2480 struct xe_bo *bo = NULL; 2481 int err; 2482 2483 if (vm) 2484 xe_vm_assert_held(vm); 2485 2486 if (start || end != ~0ULL) { 2487 bo = xe_bo_alloc(); 2488 if (IS_ERR(bo)) 2489 return bo; 2490 2491 flags |= XE_BO_FLAG_FIXED_PLACEMENT; 2492 err = __xe_bo_fixed_placement(xe, bo, type, flags, start, end, size); 2493 if (err) { 2494 xe_bo_free(bo); 2495 return ERR_PTR(err); 2496 } 2497 } 2498 2499 bo = xe_bo_init_locked(xe, bo, tile, vm ? xe_vm_resv(vm) : NULL, 2500 vm && !xe_vm_in_fault_mode(vm) && 2501 flags & XE_BO_FLAG_USER ? 2502 &vm->lru_bulk_move : NULL, size, 2503 cpu_caching, type, flags, exec); 2504 if (IS_ERR(bo)) 2505 return bo; 2506 2507 bo->min_align = alignment; 2508 2509 /* 2510 * Note that instead of taking a reference no the drm_gpuvm_resv_bo(), 2511 * to ensure the shared resv doesn't disappear under the bo, the bo 2512 * will keep a reference to the vm, and avoid circular references 2513 * by having all the vm's bo refereferences released at vm close 2514 * time. 2515 */ 2516 if (vm && xe_bo_is_user(bo)) 2517 xe_vm_get(vm); 2518 bo->vm = vm; 2519 2520 if (bo->flags & XE_BO_FLAG_GGTT) { 2521 struct xe_tile *t; 2522 u8 id; 2523 2524 if (!(bo->flags & XE_BO_FLAG_GGTT_ALL)) { 2525 if (!tile && flags & XE_BO_FLAG_STOLEN) 2526 tile = xe_device_get_root_tile(xe); 2527 2528 xe_assert(xe, tile); 2529 } 2530 2531 for_each_tile(t, xe, id) { 2532 if (t != tile && !(bo->flags & XE_BO_FLAG_GGTTx(t))) 2533 continue; 2534 2535 if (flags & XE_BO_FLAG_FIXED_PLACEMENT) { 2536 err = xe_ggtt_insert_bo_at(t->mem.ggtt, bo, 2537 start + xe_bo_size(bo), U64_MAX, 2538 exec); 2539 } else { 2540 err = xe_ggtt_insert_bo(t->mem.ggtt, bo, exec); 2541 } 2542 if (err) 2543 goto err_unlock_put_bo; 2544 } 2545 } 2546 2547 trace_xe_bo_create(bo); 2548 return bo; 2549 2550 err_unlock_put_bo: 2551 __xe_bo_unset_bulk_move(bo); 2552 xe_bo_unlock_vm_held(bo); 2553 xe_bo_put(bo); 2554 return ERR_PTR(err); 2555 } 2556 2557 /** 2558 * xe_bo_create_locked() - Create a BO 2559 * @xe: The xe device. 2560 * @tile: The tile to select for migration of this bo, and the tile used for 2561 * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. 2562 * @vm: The local vm or NULL for external objects. 2563 * @size: The storage size to use for the bo. 2564 * @type: The TTM buffer object type. 2565 * @flags: XE_BO_FLAG_ flags. 2566 * @exec: The drm_exec transaction to use for exhaustive eviction. 2567 * 2568 * Create a locked xe BO with no range- nor alignment restrictions. 2569 * 2570 * Return: The buffer object on success. Negative error pointer on failure. 2571 */ 2572 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile, 2573 struct xe_vm *vm, size_t size, 2574 enum ttm_bo_type type, u32 flags, 2575 struct drm_exec *exec) 2576 { 2577 return __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 0, type, 2578 flags, 0, exec); 2579 } 2580 2581 static struct xe_bo *xe_bo_create_novm(struct xe_device *xe, struct xe_tile *tile, 2582 size_t size, u16 cpu_caching, 2583 enum ttm_bo_type type, u32 flags, 2584 u64 alignment, bool intr) 2585 { 2586 struct xe_validation_ctx ctx; 2587 struct drm_exec exec; 2588 struct xe_bo *bo; 2589 int ret = 0; 2590 2591 xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = intr}, 2592 ret) { 2593 bo = __xe_bo_create_locked(xe, tile, NULL, size, 0, ~0ULL, 2594 cpu_caching, type, flags, alignment, &exec); 2595 drm_exec_retry_on_contention(&exec); 2596 if (IS_ERR(bo)) { 2597 ret = PTR_ERR(bo); 2598 xe_validation_retry_on_oom(&ctx, &ret); 2599 } else { 2600 xe_bo_unlock(bo); 2601 } 2602 } 2603 2604 return ret ? ERR_PTR(ret) : bo; 2605 } 2606 2607 /** 2608 * xe_bo_create_user() - Create a user BO 2609 * @xe: The xe device. 2610 * @vm: The local vm or NULL for external objects. 2611 * @size: The storage size to use for the bo. 2612 * @cpu_caching: The caching mode to be used for system backing store. 2613 * @flags: XE_BO_FLAG_ flags. 2614 * @exec: The drm_exec transaction to use for exhaustive eviction, or NULL 2615 * if such a transaction should be initiated by the call. 2616 * 2617 * Create a bo on behalf of user-space. 2618 * 2619 * Return: The buffer object on success. Negative error pointer on failure. 2620 */ 2621 struct xe_bo *xe_bo_create_user(struct xe_device *xe, 2622 struct xe_vm *vm, size_t size, 2623 u16 cpu_caching, 2624 u32 flags, struct drm_exec *exec) 2625 { 2626 struct xe_bo *bo; 2627 2628 flags |= XE_BO_FLAG_USER; 2629 2630 if (vm || exec) { 2631 xe_assert(xe, exec); 2632 bo = __xe_bo_create_locked(xe, NULL, vm, size, 0, ~0ULL, 2633 cpu_caching, ttm_bo_type_device, 2634 flags, 0, exec); 2635 if (!IS_ERR(bo)) 2636 xe_bo_unlock_vm_held(bo); 2637 } else { 2638 bo = xe_bo_create_novm(xe, NULL, size, cpu_caching, 2639 ttm_bo_type_device, flags, 0, true); 2640 } 2641 2642 return bo; 2643 } 2644 2645 /** 2646 * xe_bo_create_pin_range_novm() - Create and pin a BO with range options. 2647 * @xe: The xe device. 2648 * @tile: The tile to select for migration of this bo, and the tile used for 2649 * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. 2650 * @size: The storage size to use for the bo. 2651 * @start: Start of fixed VRAM range or 0. 2652 * @end: End of fixed VRAM range or ~0ULL. 2653 * @type: The TTM buffer object type. 2654 * @flags: XE_BO_FLAG_ flags. 2655 * 2656 * Create an Xe BO with range- and options. If @start and @end indicate 2657 * a fixed VRAM range, this must be a ttm_bo_type_kernel bo with VRAM placement 2658 * only. 2659 * 2660 * Return: The buffer object on success. Negative error pointer on failure. 2661 */ 2662 struct xe_bo *xe_bo_create_pin_range_novm(struct xe_device *xe, struct xe_tile *tile, 2663 size_t size, u64 start, u64 end, 2664 enum ttm_bo_type type, u32 flags) 2665 { 2666 struct xe_validation_ctx ctx; 2667 struct drm_exec exec; 2668 struct xe_bo *bo; 2669 int err = 0; 2670 2671 xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {}, err) { 2672 bo = __xe_bo_create_locked(xe, tile, NULL, size, start, end, 2673 0, type, flags, 0, &exec); 2674 if (IS_ERR(bo)) { 2675 drm_exec_retry_on_contention(&exec); 2676 err = PTR_ERR(bo); 2677 xe_validation_retry_on_oom(&ctx, &err); 2678 break; 2679 } 2680 2681 err = xe_bo_pin(bo, &exec); 2682 xe_bo_unlock(bo); 2683 if (err) { 2684 xe_bo_put(bo); 2685 drm_exec_retry_on_contention(&exec); 2686 xe_validation_retry_on_oom(&ctx, &err); 2687 break; 2688 } 2689 } 2690 2691 return err ? ERR_PTR(err) : bo; 2692 } 2693 2694 static struct xe_bo *xe_bo_create_pin_map_at_aligned(struct xe_device *xe, 2695 struct xe_tile *tile, 2696 struct xe_vm *vm, 2697 size_t size, u64 offset, 2698 enum ttm_bo_type type, u32 flags, 2699 u64 alignment, struct drm_exec *exec) 2700 { 2701 struct xe_bo *bo; 2702 int err; 2703 u64 start = offset == ~0ull ? 0 : offset; 2704 u64 end = offset == ~0ull ? ~0ull : start + size; 2705 2706 if (flags & XE_BO_FLAG_STOLEN && 2707 xe_ttm_stolen_cpu_access_needs_ggtt(xe)) 2708 flags |= XE_BO_FLAG_GGTT; 2709 2710 bo = __xe_bo_create_locked(xe, tile, vm, size, start, end, 0, type, 2711 flags | XE_BO_FLAG_NEEDS_CPU_ACCESS | XE_BO_FLAG_PINNED, 2712 alignment, exec); 2713 if (IS_ERR(bo)) 2714 return bo; 2715 2716 err = xe_bo_pin(bo, exec); 2717 if (err) 2718 goto err_put; 2719 2720 err = xe_bo_vmap(bo); 2721 if (err) 2722 goto err_unpin; 2723 2724 xe_bo_unlock_vm_held(bo); 2725 2726 return bo; 2727 2728 err_unpin: 2729 xe_bo_unpin(bo); 2730 err_put: 2731 xe_bo_unlock_vm_held(bo); 2732 xe_bo_put(bo); 2733 return ERR_PTR(err); 2734 } 2735 2736 /** 2737 * xe_bo_create_pin_map_at_novm() - Create pinned and mapped bo at optional VRAM offset 2738 * @xe: The xe device. 2739 * @tile: The tile to select for migration of this bo, and the tile used for 2740 * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. 2741 * @size: The storage size to use for the bo. 2742 * @offset: Optional VRAM offset or %~0ull for don't care. 2743 * @type: The TTM buffer object type. 2744 * @flags: XE_BO_FLAG_ flags. 2745 * @alignment: GGTT alignment. 2746 * @intr: Whether to execute any waits for backing store interruptible. 2747 * 2748 * Create a pinned and optionally mapped bo with VRAM offset and GGTT alignment 2749 * options. The bo will be external and not associated with a VM. 2750 * 2751 * Return: The buffer object on success. Negative error pointer on failure. 2752 * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set 2753 * to true on entry. 2754 */ 2755 struct xe_bo * 2756 xe_bo_create_pin_map_at_novm(struct xe_device *xe, struct xe_tile *tile, 2757 size_t size, u64 offset, enum ttm_bo_type type, u32 flags, 2758 u64 alignment, bool intr) 2759 { 2760 struct xe_validation_ctx ctx; 2761 struct drm_exec exec; 2762 struct xe_bo *bo; 2763 int ret = 0; 2764 2765 xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = intr}, 2766 ret) { 2767 bo = xe_bo_create_pin_map_at_aligned(xe, tile, NULL, size, offset, 2768 type, flags, alignment, &exec); 2769 if (IS_ERR(bo)) { 2770 drm_exec_retry_on_contention(&exec); 2771 ret = PTR_ERR(bo); 2772 xe_validation_retry_on_oom(&ctx, &ret); 2773 } 2774 } 2775 2776 return ret ? ERR_PTR(ret) : bo; 2777 } 2778 2779 /** 2780 * xe_bo_create_pin_map() - Create pinned and mapped bo 2781 * @xe: The xe device. 2782 * @tile: The tile to select for migration of this bo, and the tile used for 2783 * @vm: The vm to associate the buffer object with. The vm's resv must be locked 2784 * with the transaction represented by @exec. 2785 * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. 2786 * @size: The storage size to use for the bo. 2787 * @type: The TTM buffer object type. 2788 * @flags: XE_BO_FLAG_ flags. 2789 * @exec: The drm_exec transaction to use for exhaustive eviction, and 2790 * previously used for locking @vm's resv. 2791 * 2792 * Create a pinned and mapped bo. The bo will be external and not associated 2793 * with a VM. 2794 * 2795 * Return: The buffer object on success. Negative error pointer on failure. 2796 * In particular, the function may return ERR_PTR(%-EINTR) if @exec was 2797 * configured for interruptible locking. 2798 */ 2799 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile, 2800 struct xe_vm *vm, size_t size, 2801 enum ttm_bo_type type, u32 flags, 2802 struct drm_exec *exec) 2803 { 2804 return xe_bo_create_pin_map_at_aligned(xe, tile, vm, size, ~0ull, type, flags, 2805 0, exec); 2806 } 2807 2808 /** 2809 * xe_bo_create_pin_map_novm() - Create pinned and mapped bo 2810 * @xe: The xe device. 2811 * @tile: The tile to select for migration of this bo, and the tile used for 2812 * GGTT binding if any. Only to be non-NULL for ttm_bo_type_kernel bos. 2813 * @size: The storage size to use for the bo. 2814 * @type: The TTM buffer object type. 2815 * @flags: XE_BO_FLAG_ flags. 2816 * @intr: Whether to execute any waits for backing store interruptible. 2817 * 2818 * Create a pinned and mapped bo. The bo will be external and not associated 2819 * with a VM. 2820 * 2821 * Return: The buffer object on success. Negative error pointer on failure. 2822 * In particular, the function may return ERR_PTR(%-EINTR) if @intr was set 2823 * to true on entry. 2824 */ 2825 struct xe_bo *xe_bo_create_pin_map_novm(struct xe_device *xe, struct xe_tile *tile, 2826 size_t size, enum ttm_bo_type type, u32 flags, 2827 bool intr) 2828 { 2829 return xe_bo_create_pin_map_at_novm(xe, tile, size, ~0ull, type, flags, 0, intr); 2830 } 2831 2832 static void __xe_bo_unpin_map_no_vm(void *arg) 2833 { 2834 xe_bo_unpin_map_no_vm(arg); 2835 } 2836 2837 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile, 2838 size_t size, u32 flags) 2839 { 2840 struct xe_bo *bo; 2841 int ret; 2842 2843 KUNIT_STATIC_STUB_REDIRECT(xe_managed_bo_create_pin_map, xe, tile, size, flags); 2844 bo = xe_bo_create_pin_map_novm(xe, tile, size, ttm_bo_type_kernel, flags, true); 2845 if (IS_ERR(bo)) 2846 return bo; 2847 2848 ret = devm_add_action_or_reset(xe->drm.dev, __xe_bo_unpin_map_no_vm, bo); 2849 if (ret) 2850 return ERR_PTR(ret); 2851 2852 return bo; 2853 } 2854 2855 void xe_managed_bo_unpin_map_no_vm(struct xe_bo *bo) 2856 { 2857 devm_release_action(xe_bo_device(bo)->drm.dev, __xe_bo_unpin_map_no_vm, bo); 2858 } 2859 2860 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile, 2861 const void *data, size_t size, u32 flags) 2862 { 2863 struct xe_bo *bo = xe_managed_bo_create_pin_map(xe, tile, ALIGN(size, PAGE_SIZE), flags); 2864 2865 if (IS_ERR(bo)) 2866 return bo; 2867 2868 xe_map_memcpy_to(xe, &bo->vmap, 0, data, size); 2869 2870 return bo; 2871 } 2872 2873 /** 2874 * xe_managed_bo_reinit_in_vram 2875 * @xe: xe device 2876 * @tile: Tile where the new buffer will be created 2877 * @src: Managed buffer object allocated in system memory 2878 * 2879 * Replace a managed src buffer object allocated in system memory with a new 2880 * one allocated in vram, copying the data between them. 2881 * Buffer object in VRAM is not going to have the same GGTT address, the caller 2882 * is responsible for making sure that any old references to it are updated. 2883 * 2884 * Returns 0 for success, negative error code otherwise. 2885 */ 2886 int xe_managed_bo_reinit_in_vram(struct xe_device *xe, struct xe_tile *tile, struct xe_bo **src) 2887 { 2888 struct xe_bo *bo; 2889 u32 dst_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile) | XE_BO_FLAG_GGTT; 2890 2891 dst_flags |= (*src)->flags & (XE_BO_FLAG_GGTT_INVALIDATE | 2892 XE_BO_FLAG_PINNED_NORESTORE); 2893 2894 xe_assert(xe, IS_DGFX(xe)); 2895 xe_assert(xe, !(*src)->vmap.is_iomem); 2896 2897 bo = xe_managed_bo_create_from_data(xe, tile, (*src)->vmap.vaddr, 2898 xe_bo_size(*src), dst_flags); 2899 if (IS_ERR(bo)) 2900 return PTR_ERR(bo); 2901 2902 devm_release_action(xe->drm.dev, __xe_bo_unpin_map_no_vm, *src); 2903 *src = bo; 2904 2905 return 0; 2906 } 2907 2908 /* 2909 * XXX: This is in the VM bind data path, likely should calculate this once and 2910 * store, with a recalculation if the BO is moved. 2911 */ 2912 uint64_t vram_region_gpu_offset(struct ttm_resource *res) 2913 { 2914 struct xe_device *xe = ttm_to_xe_device(res->bo->bdev); 2915 2916 switch (res->mem_type) { 2917 case XE_PL_STOLEN: 2918 return xe_ttm_stolen_gpu_offset(xe); 2919 case XE_PL_TT: 2920 case XE_PL_SYSTEM: 2921 return 0; 2922 default: 2923 return xe_map_resource_to_region(res)->dpa_base; 2924 } 2925 return 0; 2926 } 2927 2928 /** 2929 * xe_bo_pin_external - pin an external BO 2930 * @bo: buffer object to be pinned 2931 * @in_place: Pin in current placement, don't attempt to migrate. 2932 * @exec: The drm_exec transaction to use for exhaustive eviction. 2933 * 2934 * Pin an external (not tied to a VM, can be exported via dma-buf / prime FD) 2935 * BO. Unique call compared to xe_bo_pin as this function has it own set of 2936 * asserts and code to ensure evict / restore on suspend / resume. 2937 * 2938 * Returns 0 for success, negative error code otherwise. 2939 */ 2940 int xe_bo_pin_external(struct xe_bo *bo, bool in_place, struct drm_exec *exec) 2941 { 2942 struct xe_device *xe = xe_bo_device(bo); 2943 int err; 2944 2945 xe_assert(xe, !bo->vm); 2946 xe_assert(xe, xe_bo_is_user(bo)); 2947 2948 if (!xe_bo_is_pinned(bo)) { 2949 if (!in_place) { 2950 err = xe_bo_validate(bo, NULL, false, exec); 2951 if (err) 2952 return err; 2953 } 2954 2955 spin_lock(&xe->pinned.lock); 2956 list_add_tail(&bo->pinned_link, &xe->pinned.late.external); 2957 spin_unlock(&xe->pinned.lock); 2958 } 2959 2960 ttm_bo_pin(&bo->ttm); 2961 if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm)) 2962 xe_ttm_tt_account_subtract(xe, bo->ttm.ttm); 2963 2964 /* 2965 * FIXME: If we always use the reserve / unreserve functions for locking 2966 * we do not need this. 2967 */ 2968 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); 2969 2970 return 0; 2971 } 2972 2973 /** 2974 * xe_bo_pin() - Pin a kernel bo after potentially migrating it 2975 * @bo: The kernel bo to pin. 2976 * @exec: The drm_exec transaction to use for exhaustive eviction. 2977 * 2978 * Attempts to migrate a bo to @bo->placement. If that succeeds, 2979 * pins the bo. 2980 * 2981 * Return: %0 on success, negative error code on migration failure. 2982 */ 2983 int xe_bo_pin(struct xe_bo *bo, struct drm_exec *exec) 2984 { 2985 struct ttm_place *place = &bo->placements[0]; 2986 struct xe_device *xe = xe_bo_device(bo); 2987 int err; 2988 2989 /* We currently don't expect user BO to be pinned */ 2990 xe_assert(xe, !xe_bo_is_user(bo)); 2991 2992 /* Pinned object must be in GGTT or have pinned flag */ 2993 xe_assert(xe, bo->flags & (XE_BO_FLAG_PINNED | 2994 XE_BO_FLAG_GGTT)); 2995 2996 /* 2997 * No reason we can't support pinning imported dma-bufs we just don't 2998 * expect to pin an imported dma-buf. 2999 */ 3000 xe_assert(xe, !bo->ttm.base.import_attach); 3001 3002 /* We only expect at most 1 pin */ 3003 xe_assert(xe, !xe_bo_is_pinned(bo)); 3004 3005 err = xe_bo_validate(bo, NULL, false, exec); 3006 if (err) 3007 return err; 3008 3009 if (mem_type_is_vram(place->mem_type) || bo->flags & XE_BO_FLAG_GGTT) { 3010 spin_lock(&xe->pinned.lock); 3011 if (bo->flags & XE_BO_FLAG_PINNED_LATE_RESTORE) 3012 list_add_tail(&bo->pinned_link, &xe->pinned.late.kernel_bo_present); 3013 else 3014 list_add_tail(&bo->pinned_link, &xe->pinned.early.kernel_bo_present); 3015 spin_unlock(&xe->pinned.lock); 3016 } 3017 3018 ttm_bo_pin(&bo->ttm); 3019 if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm)) 3020 xe_ttm_tt_account_subtract(xe, bo->ttm.ttm); 3021 3022 /* 3023 * FIXME: If we always use the reserve / unreserve functions for locking 3024 * we do not need this. 3025 */ 3026 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); 3027 3028 return 0; 3029 } 3030 3031 /** 3032 * xe_bo_unpin_external - unpin an external BO 3033 * @bo: buffer object to be unpinned 3034 * 3035 * Unpin an external (not tied to a VM, can be exported via dma-buf / prime FD) 3036 * BO. Unique call compared to xe_bo_unpin as this function has it own set of 3037 * asserts and code to ensure evict / restore on suspend / resume. 3038 * 3039 * Returns 0 for success, negative error code otherwise. 3040 */ 3041 void xe_bo_unpin_external(struct xe_bo *bo) 3042 { 3043 struct xe_device *xe = xe_bo_device(bo); 3044 3045 xe_assert(xe, !bo->vm); 3046 xe_assert(xe, xe_bo_is_pinned(bo)); 3047 xe_assert(xe, xe_bo_is_user(bo)); 3048 3049 spin_lock(&xe->pinned.lock); 3050 if (bo->ttm.pin_count == 1 && !list_empty(&bo->pinned_link)) 3051 list_del_init(&bo->pinned_link); 3052 spin_unlock(&xe->pinned.lock); 3053 3054 ttm_bo_unpin(&bo->ttm); 3055 if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm)) 3056 xe_ttm_tt_account_add(xe, bo->ttm.ttm); 3057 3058 /* 3059 * FIXME: If we always use the reserve / unreserve functions for locking 3060 * we do not need this. 3061 */ 3062 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); 3063 } 3064 3065 void xe_bo_unpin(struct xe_bo *bo) 3066 { 3067 struct ttm_place *place = &bo->placements[0]; 3068 struct xe_device *xe = xe_bo_device(bo); 3069 3070 xe_assert(xe, !bo->ttm.base.import_attach); 3071 xe_assert(xe, xe_bo_is_pinned(bo)); 3072 3073 if (mem_type_is_vram(place->mem_type) || bo->flags & XE_BO_FLAG_GGTT) { 3074 spin_lock(&xe->pinned.lock); 3075 xe_assert(xe, !list_empty(&bo->pinned_link)); 3076 list_del_init(&bo->pinned_link); 3077 spin_unlock(&xe->pinned.lock); 3078 3079 if (bo->backup_obj) { 3080 if (xe_bo_is_pinned(bo->backup_obj)) 3081 ttm_bo_unpin(&bo->backup_obj->ttm); 3082 xe_bo_put(bo->backup_obj); 3083 bo->backup_obj = NULL; 3084 } 3085 } 3086 ttm_bo_unpin(&bo->ttm); 3087 if (bo->ttm.ttm && ttm_tt_is_populated(bo->ttm.ttm)) 3088 xe_ttm_tt_account_add(xe, bo->ttm.ttm); 3089 } 3090 3091 /** 3092 * xe_bo_validate() - Make sure the bo is in an allowed placement 3093 * @bo: The bo, 3094 * @vm: Pointer to a the vm the bo shares a locked dma_resv object with, or 3095 * NULL. Used together with @allow_res_evict. 3096 * @allow_res_evict: Whether it's allowed to evict bos sharing @vm's 3097 * reservation object. 3098 * @exec: The drm_exec transaction to use for exhaustive eviction. 3099 * 3100 * Make sure the bo is in allowed placement, migrating it if necessary. If 3101 * needed, other bos will be evicted. If bos selected for eviction shares 3102 * the @vm's reservation object, they can be evicted iff @allow_res_evict is 3103 * set to true, otherwise they will be bypassed. 3104 * 3105 * Return: 0 on success, negative error code on failure. May return 3106 * -EINTR or -ERESTARTSYS if internal waits are interrupted by a signal. 3107 */ 3108 int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict, 3109 struct drm_exec *exec) 3110 { 3111 struct ttm_operation_ctx ctx = { 3112 .interruptible = true, 3113 .no_wait_gpu = false, 3114 .gfp_retry_mayfail = true, 3115 }; 3116 int ret; 3117 3118 if (xe_bo_is_pinned(bo)) 3119 return 0; 3120 3121 if (vm) { 3122 lockdep_assert_held(&vm->lock); 3123 xe_vm_assert_held(vm); 3124 3125 ctx.allow_res_evict = allow_res_evict; 3126 ctx.resv = xe_vm_resv(vm); 3127 } 3128 3129 xe_vm_set_validating(vm, allow_res_evict); 3130 trace_xe_bo_validate(bo); 3131 xe_validation_assert_exec(xe_bo_device(bo), exec, &bo->ttm.base); 3132 ret = ttm_bo_validate(&bo->ttm, &bo->placement, &ctx); 3133 xe_vm_clear_validating(vm, allow_res_evict); 3134 3135 return ret; 3136 } 3137 3138 bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo) 3139 { 3140 if (bo->destroy == &xe_ttm_bo_destroy) 3141 return true; 3142 3143 return false; 3144 } 3145 3146 /* 3147 * Resolve a BO address. There is no assert to check if the proper lock is held 3148 * so it should only be used in cases where it is not fatal to get the wrong 3149 * address, such as printing debug information, but not in cases where memory is 3150 * written based on this result. 3151 */ 3152 dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size) 3153 { 3154 struct xe_device *xe = xe_bo_device(bo); 3155 struct xe_res_cursor cur; 3156 u64 page; 3157 3158 xe_assert(xe, page_size <= PAGE_SIZE); 3159 page = offset >> PAGE_SHIFT; 3160 offset &= (PAGE_SIZE - 1); 3161 3162 if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) { 3163 xe_assert(xe, bo->ttm.ttm); 3164 3165 xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT, 3166 page_size, &cur); 3167 return xe_res_dma(&cur) + offset; 3168 } else { 3169 struct xe_res_cursor cur; 3170 3171 xe_res_first(bo->ttm.resource, page << PAGE_SHIFT, 3172 page_size, &cur); 3173 return cur.start + offset + vram_region_gpu_offset(bo->ttm.resource); 3174 } 3175 } 3176 3177 dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size) 3178 { 3179 if (!READ_ONCE(bo->ttm.pin_count)) 3180 xe_bo_assert_held(bo); 3181 return __xe_bo_addr(bo, offset, page_size); 3182 } 3183 3184 int xe_bo_vmap(struct xe_bo *bo) 3185 { 3186 struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev); 3187 void *virtual; 3188 bool is_iomem; 3189 int ret; 3190 3191 xe_bo_assert_held(bo); 3192 3193 if (drm_WARN_ON(&xe->drm, !(bo->flags & XE_BO_FLAG_NEEDS_CPU_ACCESS) || 3194 !force_contiguous(bo->flags))) 3195 return -EINVAL; 3196 3197 if (!iosys_map_is_null(&bo->vmap)) 3198 return 0; 3199 3200 /* 3201 * We use this more or less deprecated interface for now since 3202 * ttm_bo_vmap() doesn't offer the optimization of kmapping 3203 * single page bos, which is done here. 3204 * TODO: Fix up ttm_bo_vmap to do that, or fix up ttm_bo_kmap 3205 * to use struct iosys_map. 3206 */ 3207 ret = ttm_bo_kmap(&bo->ttm, 0, xe_bo_size(bo) >> PAGE_SHIFT, &bo->kmap); 3208 if (ret) 3209 return ret; 3210 3211 virtual = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem); 3212 if (is_iomem) 3213 iosys_map_set_vaddr_iomem(&bo->vmap, (void __iomem *)virtual); 3214 else 3215 iosys_map_set_vaddr(&bo->vmap, virtual); 3216 3217 return 0; 3218 } 3219 3220 static void __xe_bo_vunmap(struct xe_bo *bo) 3221 { 3222 if (!iosys_map_is_null(&bo->vmap)) { 3223 iosys_map_clear(&bo->vmap); 3224 ttm_bo_kunmap(&bo->kmap); 3225 } 3226 } 3227 3228 void xe_bo_vunmap(struct xe_bo *bo) 3229 { 3230 xe_bo_assert_held(bo); 3231 __xe_bo_vunmap(bo); 3232 } 3233 3234 static int gem_create_set_pxp_type(struct xe_device *xe, struct xe_bo *bo, u64 value) 3235 { 3236 if (value == DRM_XE_PXP_TYPE_NONE) 3237 return 0; 3238 3239 /* we only support DRM_XE_PXP_TYPE_HWDRM for now */ 3240 if (XE_IOCTL_DBG(xe, value != DRM_XE_PXP_TYPE_HWDRM)) 3241 return -EINVAL; 3242 3243 return xe_pxp_key_assign(xe->pxp, bo); 3244 } 3245 3246 typedef int (*xe_gem_create_set_property_fn)(struct xe_device *xe, 3247 struct xe_bo *bo, 3248 u64 value); 3249 3250 static const xe_gem_create_set_property_fn gem_create_set_property_funcs[] = { 3251 [DRM_XE_GEM_CREATE_SET_PROPERTY_PXP_TYPE] = gem_create_set_pxp_type, 3252 }; 3253 3254 static int gem_create_user_ext_set_property(struct xe_device *xe, 3255 struct xe_bo *bo, 3256 u64 extension) 3257 { 3258 u64 __user *address = u64_to_user_ptr(extension); 3259 struct drm_xe_ext_set_property ext; 3260 int err; 3261 u32 idx; 3262 3263 err = copy_from_user(&ext, address, sizeof(ext)); 3264 if (XE_IOCTL_DBG(xe, err)) 3265 return -EFAULT; 3266 3267 if (XE_IOCTL_DBG(xe, ext.property >= 3268 ARRAY_SIZE(gem_create_set_property_funcs)) || 3269 XE_IOCTL_DBG(xe, ext.pad) || 3270 XE_IOCTL_DBG(xe, ext.property != DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY)) 3271 return -EINVAL; 3272 3273 idx = array_index_nospec(ext.property, ARRAY_SIZE(gem_create_set_property_funcs)); 3274 if (!gem_create_set_property_funcs[idx]) 3275 return -EINVAL; 3276 3277 return gem_create_set_property_funcs[idx](xe, bo, ext.value); 3278 } 3279 3280 typedef int (*xe_gem_create_user_extension_fn)(struct xe_device *xe, 3281 struct xe_bo *bo, 3282 u64 extension); 3283 3284 static const xe_gem_create_user_extension_fn gem_create_user_extension_funcs[] = { 3285 [DRM_XE_GEM_CREATE_EXTENSION_SET_PROPERTY] = gem_create_user_ext_set_property, 3286 }; 3287 3288 #define MAX_USER_EXTENSIONS 16 3289 static int gem_create_user_extensions(struct xe_device *xe, struct xe_bo *bo, 3290 u64 extensions, int ext_number) 3291 { 3292 u64 __user *address = u64_to_user_ptr(extensions); 3293 struct drm_xe_user_extension ext; 3294 int err; 3295 u32 idx; 3296 3297 if (XE_IOCTL_DBG(xe, ext_number >= MAX_USER_EXTENSIONS)) 3298 return -E2BIG; 3299 3300 err = copy_from_user(&ext, address, sizeof(ext)); 3301 if (XE_IOCTL_DBG(xe, err)) 3302 return -EFAULT; 3303 3304 if (XE_IOCTL_DBG(xe, ext.pad) || 3305 XE_IOCTL_DBG(xe, ext.name >= ARRAY_SIZE(gem_create_user_extension_funcs))) 3306 return -EINVAL; 3307 3308 idx = array_index_nospec(ext.name, 3309 ARRAY_SIZE(gem_create_user_extension_funcs)); 3310 err = gem_create_user_extension_funcs[idx](xe, bo, extensions); 3311 if (XE_IOCTL_DBG(xe, err)) 3312 return err; 3313 3314 if (ext.next_extension) 3315 return gem_create_user_extensions(xe, bo, ext.next_extension, 3316 ++ext_number); 3317 3318 return 0; 3319 } 3320 3321 int xe_gem_create_ioctl(struct drm_device *dev, void *data, 3322 struct drm_file *file) 3323 { 3324 struct xe_device *xe = to_xe_device(dev); 3325 struct xe_file *xef = to_xe_file(file); 3326 struct drm_xe_gem_create *args = data; 3327 struct xe_validation_ctx ctx; 3328 struct drm_exec exec; 3329 struct xe_vm *vm = NULL; 3330 struct xe_bo *bo; 3331 unsigned int bo_flags; 3332 u32 handle; 3333 int err; 3334 3335 if (XE_IOCTL_DBG(xe, args->pad[0] || args->pad[1] || args->pad[2]) || 3336 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) 3337 return -EINVAL; 3338 3339 /* at least one valid memory placement must be specified */ 3340 if (XE_IOCTL_DBG(xe, (args->placement & ~xe->info.mem_region_mask) || 3341 !args->placement)) 3342 return -EINVAL; 3343 3344 if (XE_IOCTL_DBG(xe, args->flags & 3345 ~(DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING | 3346 DRM_XE_GEM_CREATE_FLAG_SCANOUT | 3347 DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM | 3348 DRM_XE_GEM_CREATE_FLAG_NO_COMPRESSION))) 3349 return -EINVAL; 3350 3351 if (XE_IOCTL_DBG(xe, args->handle)) 3352 return -EINVAL; 3353 3354 if (XE_IOCTL_DBG(xe, !args->size)) 3355 return -EINVAL; 3356 3357 if (XE_IOCTL_DBG(xe, args->size > SIZE_MAX)) 3358 return -EINVAL; 3359 3360 if (XE_IOCTL_DBG(xe, args->size & ~PAGE_MASK)) 3361 return -EINVAL; 3362 3363 bo_flags = 0; 3364 if (args->flags & DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING) 3365 bo_flags |= XE_BO_FLAG_DEFER_BACKING; 3366 3367 /* 3368 * Display scanout is always non-coherent with the CPU cache. 3369 */ 3370 if (args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT) 3371 bo_flags |= XE_BO_FLAG_FORCE_WC; 3372 3373 if (args->flags & DRM_XE_GEM_CREATE_FLAG_NO_COMPRESSION) { 3374 if (XE_IOCTL_DBG(xe, GRAPHICS_VER(xe) < 20)) 3375 return -EOPNOTSUPP; 3376 bo_flags |= XE_BO_FLAG_NO_COMPRESSION; 3377 } 3378 3379 bo_flags |= args->placement << (ffs(XE_BO_FLAG_SYSTEM) - 1); 3380 3381 /* CCS formats need physical placement at a 64K alignment in VRAM. */ 3382 if ((bo_flags & XE_BO_FLAG_VRAM_MASK) && 3383 (args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT) && 3384 !(xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) && 3385 IS_ALIGNED(args->size, SZ_64K)) 3386 bo_flags |= XE_BO_FLAG_NEEDS_64K; 3387 3388 if (args->flags & DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM) { 3389 if (XE_IOCTL_DBG(xe, !(bo_flags & XE_BO_FLAG_VRAM_MASK))) 3390 return -EINVAL; 3391 3392 bo_flags |= XE_BO_FLAG_NEEDS_CPU_ACCESS; 3393 } 3394 3395 if (XE_IOCTL_DBG(xe, !args->cpu_caching || 3396 args->cpu_caching > DRM_XE_GEM_CPU_CACHING_WC)) 3397 return -EINVAL; 3398 3399 if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_VRAM_MASK && 3400 args->cpu_caching != DRM_XE_GEM_CPU_CACHING_WC)) 3401 return -EINVAL; 3402 3403 if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_FORCE_WC && 3404 args->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB)) 3405 return -EINVAL; 3406 3407 if (args->vm_id) { 3408 vm = xe_vm_lookup(xef, args->vm_id); 3409 if (XE_IOCTL_DBG(xe, !vm)) 3410 return -ENOENT; 3411 } 3412 3413 err = 0; 3414 xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {.interruptible = true}, 3415 err) { 3416 if (vm) { 3417 err = xe_vm_drm_exec_lock(vm, &exec); 3418 drm_exec_retry_on_contention(&exec); 3419 if (err) 3420 break; 3421 } 3422 bo = xe_bo_create_user(xe, vm, args->size, args->cpu_caching, 3423 bo_flags, &exec); 3424 drm_exec_retry_on_contention(&exec); 3425 if (IS_ERR(bo)) { 3426 err = PTR_ERR(bo); 3427 xe_validation_retry_on_oom(&ctx, &err); 3428 break; 3429 } 3430 } 3431 if (err) 3432 goto out_vm; 3433 3434 if (args->extensions) { 3435 err = gem_create_user_extensions(xe, bo, args->extensions, 0); 3436 if (err) 3437 goto out_bulk; 3438 } 3439 3440 err = drm_gem_handle_create(file, &bo->ttm.base, &handle); 3441 if (err) 3442 goto out_bulk; 3443 3444 args->handle = handle; 3445 goto out_put; 3446 3447 out_bulk: 3448 if (vm && !xe_vm_in_fault_mode(vm)) { 3449 xe_vm_lock(vm, false); 3450 __xe_bo_unset_bulk_move(bo); 3451 xe_vm_unlock(vm); 3452 } 3453 out_put: 3454 xe_bo_put(bo); 3455 out_vm: 3456 if (vm) 3457 xe_vm_put(vm); 3458 3459 return err; 3460 } 3461 3462 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data, 3463 struct drm_file *file) 3464 { 3465 struct xe_device *xe = to_xe_device(dev); 3466 struct drm_xe_gem_mmap_offset *args = data; 3467 struct drm_gem_object *gem_obj; 3468 3469 if (XE_IOCTL_DBG(xe, args->extensions) || 3470 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) 3471 return -EINVAL; 3472 3473 if (XE_IOCTL_DBG(xe, args->flags & 3474 ~DRM_XE_MMAP_OFFSET_FLAG_PCI_BARRIER)) 3475 return -EINVAL; 3476 3477 if (args->flags & DRM_XE_MMAP_OFFSET_FLAG_PCI_BARRIER) { 3478 if (XE_IOCTL_DBG(xe, !IS_DGFX(xe))) 3479 return -EINVAL; 3480 3481 if (XE_IOCTL_DBG(xe, args->handle)) 3482 return -EINVAL; 3483 3484 if (XE_IOCTL_DBG(xe, PAGE_SIZE > SZ_4K)) 3485 return -EINVAL; 3486 3487 BUILD_BUG_ON(((XE_PCI_BARRIER_MMAP_OFFSET >> XE_PTE_SHIFT) + 3488 SZ_4K) >= DRM_FILE_PAGE_OFFSET_START); 3489 args->offset = XE_PCI_BARRIER_MMAP_OFFSET; 3490 return 0; 3491 } 3492 3493 gem_obj = drm_gem_object_lookup(file, args->handle); 3494 if (XE_IOCTL_DBG(xe, !gem_obj)) 3495 return -ENOENT; 3496 3497 /* The mmap offset was set up at BO allocation time. */ 3498 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node); 3499 3500 xe_bo_put(gem_to_xe_bo(gem_obj)); 3501 return 0; 3502 } 3503 3504 /** 3505 * xe_bo_decompress - schedule in-place decompress and install fence 3506 * @bo: buffer object (caller should hold drm_exec reservations for VM+BO) 3507 * 3508 * Schedules an in-place resolve via the migrate layer and installs the 3509 * returned dma_fence into the BO kernel reservation slot (DMA_RESV_USAGE_KERNEL). 3510 * In preempt fence mode, this operation interrupts hardware execution 3511 * which is expensive. Page fault mode is recommended for better performance. 3512 * 3513 * The resolve path only runs for VRAM-backed buffers (currently dGPU-only); 3514 * iGPU/system-memory objects fail the resource check and bypass the resolve. 3515 * 3516 * Returns 0 on success, negative errno on error. 3517 */ 3518 int xe_bo_decompress(struct xe_bo *bo) 3519 { 3520 struct xe_device *xe = xe_bo_device(bo); 3521 struct xe_tile *tile = xe_device_get_root_tile(xe); 3522 struct dma_fence *decomp_fence = NULL; 3523 struct ttm_operation_ctx op_ctx = { 3524 .interruptible = true, 3525 .no_wait_gpu = false, 3526 .gfp_retry_mayfail = false, 3527 }; 3528 int err = 0; 3529 3530 /* Silently skip decompression for non-VRAM buffers */ 3531 if (!bo->ttm.resource || !mem_type_is_vram(bo->ttm.resource->mem_type)) 3532 return 0; 3533 3534 /* Notify before scheduling resolve */ 3535 err = xe_bo_move_notify(bo, &op_ctx); 3536 if (err) 3537 return err; 3538 3539 /* Reserve fence slot before scheduling */ 3540 err = dma_resv_reserve_fences(bo->ttm.base.resv, 1); 3541 if (err) 3542 return err; 3543 3544 /* Schedule the in-place decompression */ 3545 decomp_fence = xe_migrate_resolve(tile->migrate, 3546 bo, 3547 bo->ttm.resource); 3548 3549 if (IS_ERR(decomp_fence)) 3550 return PTR_ERR(decomp_fence); 3551 3552 /* Install kernel-usage fence */ 3553 dma_resv_add_fence(bo->ttm.base.resv, decomp_fence, DMA_RESV_USAGE_KERNEL); 3554 dma_fence_put(decomp_fence); 3555 3556 return 0; 3557 } 3558 3559 /** 3560 * xe_bo_lock() - Lock the buffer object's dma_resv object 3561 * @bo: The struct xe_bo whose lock is to be taken 3562 * @intr: Whether to perform any wait interruptible 3563 * 3564 * Locks the buffer object's dma_resv object. If the buffer object is 3565 * pointing to a shared dma_resv object, that shared lock is locked. 3566 * 3567 * Return: 0 on success, -EINTR if @intr is true and the wait for a 3568 * contended lock was interrupted. If @intr is set to false, the 3569 * function always returns 0. 3570 */ 3571 int xe_bo_lock(struct xe_bo *bo, bool intr) 3572 { 3573 if (intr) 3574 return dma_resv_lock_interruptible(bo->ttm.base.resv, NULL); 3575 3576 dma_resv_lock(bo->ttm.base.resv, NULL); 3577 3578 return 0; 3579 } 3580 3581 /** 3582 * xe_bo_unlock() - Unlock the buffer object's dma_resv object 3583 * @bo: The struct xe_bo whose lock is to be released. 3584 * 3585 * Unlock a buffer object lock that was locked by xe_bo_lock(). 3586 */ 3587 void xe_bo_unlock(struct xe_bo *bo) 3588 { 3589 dma_resv_unlock(bo->ttm.base.resv); 3590 } 3591 3592 /** 3593 * xe_bo_can_migrate - Whether a buffer object likely can be migrated 3594 * @bo: The buffer object to migrate 3595 * @mem_type: The TTM memory type intended to migrate to 3596 * 3597 * Check whether the buffer object supports migration to the 3598 * given memory type. Note that pinning may affect the ability to migrate as 3599 * returned by this function. 3600 * 3601 * This function is primarily intended as a helper for checking the 3602 * possibility to migrate buffer objects and can be called without 3603 * the object lock held. 3604 * 3605 * Return: true if migration is possible, false otherwise. 3606 */ 3607 bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type) 3608 { 3609 unsigned int cur_place; 3610 3611 if (bo->ttm.type == ttm_bo_type_kernel) 3612 return true; 3613 3614 if (bo->ttm.type == ttm_bo_type_sg) 3615 return false; 3616 3617 for (cur_place = 0; cur_place < bo->placement.num_placement; 3618 cur_place++) { 3619 if (bo->placements[cur_place].mem_type == mem_type) 3620 return true; 3621 } 3622 3623 return false; 3624 } 3625 3626 static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place) 3627 { 3628 memset(place, 0, sizeof(*place)); 3629 place->mem_type = mem_type; 3630 } 3631 3632 /** 3633 * xe_bo_migrate - Migrate an object to the desired region id 3634 * @bo: The buffer object to migrate. 3635 * @mem_type: The TTM region type to migrate to. 3636 * @tctx: A pointer to a struct ttm_operation_ctx or NULL if 3637 * a default interruptibe ctx is to be used. 3638 * @exec: The drm_exec transaction to use for exhaustive eviction. 3639 * 3640 * Attempt to migrate the buffer object to the desired memory region. The 3641 * buffer object may not be pinned, and must be locked. 3642 * On successful completion, the object memory type will be updated, 3643 * but an async migration task may not have completed yet, and to 3644 * accomplish that, the object's kernel fences must be signaled with 3645 * the object lock held. 3646 * 3647 * Return: 0 on success. Negative error code on failure. In particular may 3648 * return -EINTR or -ERESTARTSYS if signal pending. 3649 */ 3650 int xe_bo_migrate(struct xe_bo *bo, u32 mem_type, struct ttm_operation_ctx *tctx, 3651 struct drm_exec *exec) 3652 { 3653 struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev); 3654 struct ttm_operation_ctx ctx = { 3655 .interruptible = true, 3656 .no_wait_gpu = false, 3657 .gfp_retry_mayfail = true, 3658 }; 3659 struct ttm_placement placement; 3660 struct ttm_place requested; 3661 3662 xe_bo_assert_held(bo); 3663 tctx = tctx ? tctx : &ctx; 3664 3665 if (bo->ttm.resource->mem_type == mem_type) 3666 return 0; 3667 3668 if (xe_bo_is_pinned(bo)) 3669 return -EBUSY; 3670 3671 if (!xe_bo_can_migrate(bo, mem_type)) 3672 return -EINVAL; 3673 3674 xe_place_from_ttm_type(mem_type, &requested); 3675 placement.num_placement = 1; 3676 placement.placement = &requested; 3677 3678 /* 3679 * Stolen needs to be handled like below VRAM handling if we ever need 3680 * to support it. 3681 */ 3682 drm_WARN_ON(&xe->drm, mem_type == XE_PL_STOLEN); 3683 3684 if (mem_type_is_vram(mem_type)) { 3685 u32 c = 0; 3686 3687 add_vram(xe, bo, &requested, bo->flags, mem_type, &c); 3688 } 3689 3690 if (!tctx->no_wait_gpu) 3691 xe_validation_assert_exec(xe_bo_device(bo), exec, &bo->ttm.base); 3692 return ttm_bo_validate(&bo->ttm, &placement, tctx); 3693 } 3694 3695 /** 3696 * xe_bo_evict - Evict an object to evict placement 3697 * @bo: The buffer object to migrate. 3698 * @exec: The drm_exec transaction to use for exhaustive eviction. 3699 * 3700 * On successful completion, the object memory will be moved to evict 3701 * placement. This function blocks until the object has been fully moved. 3702 * 3703 * Return: 0 on success. Negative error code on failure. 3704 */ 3705 int xe_bo_evict(struct xe_bo *bo, struct drm_exec *exec) 3706 { 3707 struct ttm_operation_ctx ctx = { 3708 .interruptible = false, 3709 .no_wait_gpu = false, 3710 .gfp_retry_mayfail = true, 3711 }; 3712 struct ttm_placement placement; 3713 int ret; 3714 3715 xe_evict_flags(&bo->ttm, &placement); 3716 ret = ttm_bo_validate(&bo->ttm, &placement, &ctx); 3717 if (ret) 3718 return ret; 3719 3720 dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL, 3721 false, MAX_SCHEDULE_TIMEOUT); 3722 3723 return 0; 3724 } 3725 3726 /** 3727 * xe_bo_needs_ccs_pages - Whether a bo needs to back up CCS pages when 3728 * placed in system memory. 3729 * @bo: The xe_bo 3730 * 3731 * Return: true if extra pages need to be allocated, false otherwise. 3732 */ 3733 bool xe_bo_needs_ccs_pages(struct xe_bo *bo) 3734 { 3735 struct xe_device *xe = xe_bo_device(bo); 3736 3737 if (GRAPHICS_VER(xe) >= 20 && IS_DGFX(xe)) 3738 return false; 3739 3740 if (!xe_device_has_flat_ccs(xe) || bo->ttm.type != ttm_bo_type_device) 3741 return false; 3742 3743 /* On discrete GPUs, if the GPU can access this buffer from 3744 * system memory (i.e., it allows XE_PL_TT placement), FlatCCS 3745 * can't be used since there's no CCS storage associated with 3746 * non-VRAM addresses. 3747 */ 3748 if (IS_DGFX(xe) && (bo->flags & XE_BO_FLAG_SYSTEM)) 3749 return false; 3750 3751 /* Check if userspace explicitly requested no compression */ 3752 if (bo->flags & XE_BO_FLAG_NO_COMPRESSION) 3753 return false; 3754 3755 /* 3756 * For WB (Write-Back) CPU caching mode, check if the device 3757 * supports WB compression with coherency. 3758 */ 3759 if (bo->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB && 3760 xe->pat.idx[XE_CACHE_WB_COMPRESSION] == XE_PAT_INVALID_IDX) 3761 return false; 3762 3763 return true; 3764 } 3765 3766 /** 3767 * __xe_bo_release_dummy() - Dummy kref release function 3768 * @kref: The embedded struct kref. 3769 * 3770 * Dummy release function for xe_bo_put_deferred(). Keep off. 3771 */ 3772 void __xe_bo_release_dummy(struct kref *kref) 3773 { 3774 } 3775 3776 /** 3777 * xe_bo_put_commit() - Put bos whose put was deferred by xe_bo_put_deferred(). 3778 * @deferred: The lockless list used for the call to xe_bo_put_deferred(). 3779 * 3780 * Puts all bos whose put was deferred by xe_bo_put_deferred(). 3781 * The @deferred list can be either an onstack local list or a global 3782 * shared list used by a workqueue. 3783 */ 3784 void xe_bo_put_commit(struct llist_head *deferred) 3785 { 3786 struct llist_node *freed; 3787 struct xe_bo *bo, *next; 3788 3789 if (!deferred) 3790 return; 3791 3792 freed = llist_del_all(deferred); 3793 if (!freed) 3794 return; 3795 3796 llist_for_each_entry_safe(bo, next, freed, freed) 3797 drm_gem_object_free(&bo->ttm.base.refcount); 3798 } 3799 3800 static void xe_bo_dev_work_func(struct work_struct *work) 3801 { 3802 struct xe_bo_dev *bo_dev = container_of(work, typeof(*bo_dev), async_free); 3803 3804 xe_bo_put_commit(&bo_dev->async_list); 3805 } 3806 3807 /** 3808 * xe_bo_dev_init() - Initialize BO dev to manage async BO freeing 3809 * @bo_dev: The BO dev structure 3810 */ 3811 void xe_bo_dev_init(struct xe_bo_dev *bo_dev) 3812 { 3813 INIT_WORK(&bo_dev->async_free, xe_bo_dev_work_func); 3814 } 3815 3816 /** 3817 * xe_bo_dev_fini() - Finalize BO dev managing async BO freeing 3818 * @bo_dev: The BO dev structure 3819 */ 3820 void xe_bo_dev_fini(struct xe_bo_dev *bo_dev) 3821 { 3822 flush_work(&bo_dev->async_free); 3823 } 3824 3825 void xe_bo_put(struct xe_bo *bo) 3826 { 3827 struct xe_tile *tile; 3828 u8 id; 3829 3830 might_sleep(); 3831 if (bo) { 3832 #ifdef CONFIG_PROC_FS 3833 if (bo->client) 3834 might_lock(&bo->client->bos_lock); 3835 #endif 3836 for_each_tile(tile, xe_bo_device(bo), id) 3837 if (bo->ggtt_node[id]) 3838 xe_ggtt_might_lock(tile->mem.ggtt); 3839 drm_gem_object_put(&bo->ttm.base); 3840 } 3841 } 3842 3843 /** 3844 * xe_bo_dumb_create - Create a dumb bo as backing for a fb 3845 * @file_priv: ... 3846 * @dev: ... 3847 * @args: ... 3848 * 3849 * See dumb_create() hook in include/drm/drm_drv.h 3850 * 3851 * Return: ... 3852 */ 3853 int xe_bo_dumb_create(struct drm_file *file_priv, 3854 struct drm_device *dev, 3855 struct drm_mode_create_dumb *args) 3856 { 3857 struct xe_device *xe = to_xe_device(dev); 3858 struct xe_bo *bo; 3859 uint32_t handle; 3860 int err; 3861 u32 page_size = max_t(u32, PAGE_SIZE, 3862 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K); 3863 3864 err = drm_mode_size_dumb(dev, args, SZ_64, page_size); 3865 if (err) 3866 return err; 3867 3868 bo = xe_bo_create_user(xe, NULL, args->size, 3869 DRM_XE_GEM_CPU_CACHING_WC, 3870 XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) | 3871 XE_BO_FLAG_FORCE_WC | 3872 XE_BO_FLAG_NEEDS_CPU_ACCESS, NULL); 3873 if (IS_ERR(bo)) 3874 return PTR_ERR(bo); 3875 3876 err = drm_gem_handle_create(file_priv, &bo->ttm.base, &handle); 3877 /* drop reference from allocate - handle holds it now */ 3878 drm_gem_object_put(&bo->ttm.base); 3879 if (!err) 3880 args->handle = handle; 3881 return err; 3882 } 3883 3884 void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo) 3885 { 3886 struct ttm_buffer_object *tbo = &bo->ttm; 3887 struct ttm_device *bdev = tbo->bdev; 3888 3889 drm_vma_node_unmap(&tbo->base.vma_node, bdev->dev_mapping); 3890 3891 list_del_init(&bo->vram_userfault_link); 3892 } 3893 3894 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST) 3895 #include "tests/xe_bo.c" 3896 #endif 3897