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