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