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