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