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