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 { 1459 struct xe_bo *bo = NULL; 1460 int err; 1461 1462 if (vm) 1463 xe_vm_assert_held(vm); 1464 1465 if (start || end != ~0ULL) { 1466 bo = xe_bo_alloc(); 1467 if (IS_ERR(bo)) 1468 return bo; 1469 1470 flags |= XE_BO_FLAG_FIXED_PLACEMENT; 1471 err = __xe_bo_fixed_placement(xe, bo, flags, start, end, size); 1472 if (err) { 1473 xe_bo_free(bo); 1474 return ERR_PTR(err); 1475 } 1476 } 1477 1478 bo = ___xe_bo_create_locked(xe, bo, tile, vm ? xe_vm_resv(vm) : NULL, 1479 vm && !xe_vm_in_fault_mode(vm) && 1480 flags & XE_BO_FLAG_USER ? 1481 &vm->lru_bulk_move : NULL, size, 1482 cpu_caching, type, flags); 1483 if (IS_ERR(bo)) 1484 return bo; 1485 1486 /* 1487 * Note that instead of taking a reference no the drm_gpuvm_resv_bo(), 1488 * to ensure the shared resv doesn't disappear under the bo, the bo 1489 * will keep a reference to the vm, and avoid circular references 1490 * by having all the vm's bo refereferences released at vm close 1491 * time. 1492 */ 1493 if (vm && xe_bo_is_user(bo)) 1494 xe_vm_get(vm); 1495 bo->vm = vm; 1496 1497 if (bo->flags & XE_BO_FLAG_GGTT) { 1498 if (!tile && flags & XE_BO_FLAG_STOLEN) 1499 tile = xe_device_get_root_tile(xe); 1500 1501 xe_assert(xe, tile); 1502 1503 if (flags & XE_BO_FLAG_FIXED_PLACEMENT) { 1504 err = xe_ggtt_insert_bo_at(tile->mem.ggtt, bo, 1505 start + bo->size, U64_MAX); 1506 } else { 1507 err = xe_ggtt_insert_bo(tile->mem.ggtt, bo); 1508 } 1509 if (err) 1510 goto err_unlock_put_bo; 1511 } 1512 1513 return bo; 1514 1515 err_unlock_put_bo: 1516 __xe_bo_unset_bulk_move(bo); 1517 xe_bo_unlock_vm_held(bo); 1518 xe_bo_put(bo); 1519 return ERR_PTR(err); 1520 } 1521 1522 struct xe_bo * 1523 xe_bo_create_locked_range(struct xe_device *xe, 1524 struct xe_tile *tile, struct xe_vm *vm, 1525 size_t size, u64 start, u64 end, 1526 enum ttm_bo_type type, u32 flags) 1527 { 1528 return __xe_bo_create_locked(xe, tile, vm, size, start, end, 0, type, flags); 1529 } 1530 1531 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile, 1532 struct xe_vm *vm, size_t size, 1533 enum ttm_bo_type type, u32 flags) 1534 { 1535 return __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 0, type, flags); 1536 } 1537 1538 struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_tile *tile, 1539 struct xe_vm *vm, size_t size, 1540 u16 cpu_caching, 1541 u32 flags) 1542 { 1543 struct xe_bo *bo = __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 1544 cpu_caching, ttm_bo_type_device, 1545 flags | XE_BO_FLAG_USER); 1546 if (!IS_ERR(bo)) 1547 xe_bo_unlock_vm_held(bo); 1548 1549 return bo; 1550 } 1551 1552 struct xe_bo *xe_bo_create(struct xe_device *xe, struct xe_tile *tile, 1553 struct xe_vm *vm, size_t size, 1554 enum ttm_bo_type type, u32 flags) 1555 { 1556 struct xe_bo *bo = xe_bo_create_locked(xe, tile, vm, size, type, flags); 1557 1558 if (!IS_ERR(bo)) 1559 xe_bo_unlock_vm_held(bo); 1560 1561 return bo; 1562 } 1563 1564 struct xe_bo *xe_bo_create_pin_map_at(struct xe_device *xe, struct xe_tile *tile, 1565 struct xe_vm *vm, 1566 size_t size, u64 offset, 1567 enum ttm_bo_type type, u32 flags) 1568 { 1569 struct xe_bo *bo; 1570 int err; 1571 u64 start = offset == ~0ull ? 0 : offset; 1572 u64 end = offset == ~0ull ? offset : start + size; 1573 1574 if (flags & XE_BO_FLAG_STOLEN && 1575 xe_ttm_stolen_cpu_access_needs_ggtt(xe)) 1576 flags |= XE_BO_FLAG_GGTT; 1577 1578 bo = xe_bo_create_locked_range(xe, tile, vm, size, start, end, type, 1579 flags | XE_BO_FLAG_NEEDS_CPU_ACCESS); 1580 if (IS_ERR(bo)) 1581 return bo; 1582 1583 err = xe_bo_pin(bo); 1584 if (err) 1585 goto err_put; 1586 1587 err = xe_bo_vmap(bo); 1588 if (err) 1589 goto err_unpin; 1590 1591 xe_bo_unlock_vm_held(bo); 1592 1593 return bo; 1594 1595 err_unpin: 1596 xe_bo_unpin(bo); 1597 err_put: 1598 xe_bo_unlock_vm_held(bo); 1599 xe_bo_put(bo); 1600 return ERR_PTR(err); 1601 } 1602 1603 struct xe_bo *xe_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile, 1604 struct xe_vm *vm, size_t size, 1605 enum ttm_bo_type type, u32 flags) 1606 { 1607 return xe_bo_create_pin_map_at(xe, tile, vm, size, ~0ull, type, flags); 1608 } 1609 1610 struct xe_bo *xe_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile, 1611 const void *data, size_t size, 1612 enum ttm_bo_type type, u32 flags) 1613 { 1614 struct xe_bo *bo = xe_bo_create_pin_map(xe, tile, NULL, 1615 ALIGN(size, PAGE_SIZE), 1616 type, flags); 1617 if (IS_ERR(bo)) 1618 return bo; 1619 1620 xe_map_memcpy_to(xe, &bo->vmap, 0, data, size); 1621 1622 return bo; 1623 } 1624 1625 static void __xe_bo_unpin_map_no_vm(void *arg) 1626 { 1627 xe_bo_unpin_map_no_vm(arg); 1628 } 1629 1630 struct xe_bo *xe_managed_bo_create_pin_map(struct xe_device *xe, struct xe_tile *tile, 1631 size_t size, u32 flags) 1632 { 1633 struct xe_bo *bo; 1634 int ret; 1635 1636 bo = xe_bo_create_pin_map(xe, tile, NULL, size, ttm_bo_type_kernel, flags); 1637 if (IS_ERR(bo)) 1638 return bo; 1639 1640 ret = devm_add_action_or_reset(xe->drm.dev, __xe_bo_unpin_map_no_vm, bo); 1641 if (ret) 1642 return ERR_PTR(ret); 1643 1644 return bo; 1645 } 1646 1647 struct xe_bo *xe_managed_bo_create_from_data(struct xe_device *xe, struct xe_tile *tile, 1648 const void *data, size_t size, u32 flags) 1649 { 1650 struct xe_bo *bo = xe_managed_bo_create_pin_map(xe, tile, ALIGN(size, PAGE_SIZE), flags); 1651 1652 if (IS_ERR(bo)) 1653 return bo; 1654 1655 xe_map_memcpy_to(xe, &bo->vmap, 0, data, size); 1656 1657 return bo; 1658 } 1659 1660 /** 1661 * xe_managed_bo_reinit_in_vram 1662 * @xe: xe device 1663 * @tile: Tile where the new buffer will be created 1664 * @src: Managed buffer object allocated in system memory 1665 * 1666 * Replace a managed src buffer object allocated in system memory with a new 1667 * one allocated in vram, copying the data between them. 1668 * Buffer object in VRAM is not going to have the same GGTT address, the caller 1669 * is responsible for making sure that any old references to it are updated. 1670 * 1671 * Returns 0 for success, negative error code otherwise. 1672 */ 1673 int xe_managed_bo_reinit_in_vram(struct xe_device *xe, struct xe_tile *tile, struct xe_bo **src) 1674 { 1675 struct xe_bo *bo; 1676 u32 dst_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile) | XE_BO_FLAG_GGTT; 1677 1678 dst_flags |= (*src)->flags & XE_BO_FLAG_GGTT_INVALIDATE; 1679 1680 xe_assert(xe, IS_DGFX(xe)); 1681 xe_assert(xe, !(*src)->vmap.is_iomem); 1682 1683 bo = xe_managed_bo_create_from_data(xe, tile, (*src)->vmap.vaddr, 1684 (*src)->size, dst_flags); 1685 if (IS_ERR(bo)) 1686 return PTR_ERR(bo); 1687 1688 devm_release_action(xe->drm.dev, __xe_bo_unpin_map_no_vm, *src); 1689 *src = bo; 1690 1691 return 0; 1692 } 1693 1694 /* 1695 * XXX: This is in the VM bind data path, likely should calculate this once and 1696 * store, with a recalculation if the BO is moved. 1697 */ 1698 uint64_t vram_region_gpu_offset(struct ttm_resource *res) 1699 { 1700 struct xe_device *xe = ttm_to_xe_device(res->bo->bdev); 1701 1702 if (res->mem_type == XE_PL_STOLEN) 1703 return xe_ttm_stolen_gpu_offset(xe); 1704 1705 return res_to_mem_region(res)->dpa_base; 1706 } 1707 1708 /** 1709 * xe_bo_pin_external - pin an external BO 1710 * @bo: buffer object to be pinned 1711 * 1712 * Pin an external (not tied to a VM, can be exported via dma-buf / prime FD) 1713 * BO. Unique call compared to xe_bo_pin as this function has it own set of 1714 * asserts and code to ensure evict / restore on suspend / resume. 1715 * 1716 * Returns 0 for success, negative error code otherwise. 1717 */ 1718 int xe_bo_pin_external(struct xe_bo *bo) 1719 { 1720 struct xe_device *xe = xe_bo_device(bo); 1721 int err; 1722 1723 xe_assert(xe, !bo->vm); 1724 xe_assert(xe, xe_bo_is_user(bo)); 1725 1726 if (!xe_bo_is_pinned(bo)) { 1727 err = xe_bo_validate(bo, NULL, false); 1728 if (err) 1729 return err; 1730 1731 if (xe_bo_is_vram(bo)) { 1732 spin_lock(&xe->pinned.lock); 1733 list_add_tail(&bo->pinned_link, 1734 &xe->pinned.external_vram); 1735 spin_unlock(&xe->pinned.lock); 1736 } 1737 } 1738 1739 ttm_bo_pin(&bo->ttm); 1740 1741 /* 1742 * FIXME: If we always use the reserve / unreserve functions for locking 1743 * we do not need this. 1744 */ 1745 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); 1746 1747 return 0; 1748 } 1749 1750 int xe_bo_pin(struct xe_bo *bo) 1751 { 1752 struct xe_device *xe = xe_bo_device(bo); 1753 int err; 1754 1755 /* We currently don't expect user BO to be pinned */ 1756 xe_assert(xe, !xe_bo_is_user(bo)); 1757 1758 /* Pinned object must be in GGTT or have pinned flag */ 1759 xe_assert(xe, bo->flags & (XE_BO_FLAG_PINNED | 1760 XE_BO_FLAG_GGTT)); 1761 1762 /* 1763 * No reason we can't support pinning imported dma-bufs we just don't 1764 * expect to pin an imported dma-buf. 1765 */ 1766 xe_assert(xe, !bo->ttm.base.import_attach); 1767 1768 /* We only expect at most 1 pin */ 1769 xe_assert(xe, !xe_bo_is_pinned(bo)); 1770 1771 err = xe_bo_validate(bo, NULL, false); 1772 if (err) 1773 return err; 1774 1775 /* 1776 * For pinned objects in on DGFX, which are also in vram, we expect 1777 * these to be in contiguous VRAM memory. Required eviction / restore 1778 * during suspend / resume (force restore to same physical address). 1779 */ 1780 if (IS_DGFX(xe) && !(IS_ENABLED(CONFIG_DRM_XE_DEBUG) && 1781 bo->flags & XE_BO_FLAG_INTERNAL_TEST)) { 1782 struct ttm_place *place = &(bo->placements[0]); 1783 1784 if (mem_type_is_vram(place->mem_type)) { 1785 xe_assert(xe, place->flags & TTM_PL_FLAG_CONTIGUOUS); 1786 1787 place->fpfn = (xe_bo_addr(bo, 0, PAGE_SIZE) - 1788 vram_region_gpu_offset(bo->ttm.resource)) >> PAGE_SHIFT; 1789 place->lpfn = place->fpfn + (bo->size >> PAGE_SHIFT); 1790 1791 spin_lock(&xe->pinned.lock); 1792 list_add_tail(&bo->pinned_link, &xe->pinned.kernel_bo_present); 1793 spin_unlock(&xe->pinned.lock); 1794 } 1795 } 1796 1797 ttm_bo_pin(&bo->ttm); 1798 1799 /* 1800 * FIXME: If we always use the reserve / unreserve functions for locking 1801 * we do not need this. 1802 */ 1803 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); 1804 1805 return 0; 1806 } 1807 1808 /** 1809 * xe_bo_unpin_external - unpin an external BO 1810 * @bo: buffer object to be unpinned 1811 * 1812 * Unpin an external (not tied to a VM, can be exported via dma-buf / prime FD) 1813 * BO. Unique call compared to xe_bo_unpin as this function has it own set of 1814 * asserts and code to ensure evict / restore on suspend / resume. 1815 * 1816 * Returns 0 for success, negative error code otherwise. 1817 */ 1818 void xe_bo_unpin_external(struct xe_bo *bo) 1819 { 1820 struct xe_device *xe = xe_bo_device(bo); 1821 1822 xe_assert(xe, !bo->vm); 1823 xe_assert(xe, xe_bo_is_pinned(bo)); 1824 xe_assert(xe, xe_bo_is_user(bo)); 1825 1826 spin_lock(&xe->pinned.lock); 1827 if (bo->ttm.pin_count == 1 && !list_empty(&bo->pinned_link)) 1828 list_del_init(&bo->pinned_link); 1829 spin_unlock(&xe->pinned.lock); 1830 1831 ttm_bo_unpin(&bo->ttm); 1832 1833 /* 1834 * FIXME: If we always use the reserve / unreserve functions for locking 1835 * we do not need this. 1836 */ 1837 ttm_bo_move_to_lru_tail_unlocked(&bo->ttm); 1838 } 1839 1840 void xe_bo_unpin(struct xe_bo *bo) 1841 { 1842 struct xe_device *xe = xe_bo_device(bo); 1843 1844 xe_assert(xe, !bo->ttm.base.import_attach); 1845 xe_assert(xe, xe_bo_is_pinned(bo)); 1846 1847 if (IS_DGFX(xe) && !(IS_ENABLED(CONFIG_DRM_XE_DEBUG) && 1848 bo->flags & XE_BO_FLAG_INTERNAL_TEST)) { 1849 struct ttm_place *place = &(bo->placements[0]); 1850 1851 if (mem_type_is_vram(place->mem_type)) { 1852 spin_lock(&xe->pinned.lock); 1853 xe_assert(xe, !list_empty(&bo->pinned_link)); 1854 list_del_init(&bo->pinned_link); 1855 spin_unlock(&xe->pinned.lock); 1856 } 1857 } 1858 1859 ttm_bo_unpin(&bo->ttm); 1860 } 1861 1862 /** 1863 * xe_bo_validate() - Make sure the bo is in an allowed placement 1864 * @bo: The bo, 1865 * @vm: Pointer to a the vm the bo shares a locked dma_resv object with, or 1866 * NULL. Used together with @allow_res_evict. 1867 * @allow_res_evict: Whether it's allowed to evict bos sharing @vm's 1868 * reservation object. 1869 * 1870 * Make sure the bo is in allowed placement, migrating it if necessary. If 1871 * needed, other bos will be evicted. If bos selected for eviction shares 1872 * the @vm's reservation object, they can be evicted iff @allow_res_evict is 1873 * set to true, otherwise they will be bypassed. 1874 * 1875 * Return: 0 on success, negative error code on failure. May return 1876 * -EINTR or -ERESTARTSYS if internal waits are interrupted by a signal. 1877 */ 1878 int xe_bo_validate(struct xe_bo *bo, struct xe_vm *vm, bool allow_res_evict) 1879 { 1880 struct ttm_operation_ctx ctx = { 1881 .interruptible = true, 1882 .no_wait_gpu = false, 1883 }; 1884 1885 if (vm) { 1886 lockdep_assert_held(&vm->lock); 1887 xe_vm_assert_held(vm); 1888 1889 ctx.allow_res_evict = allow_res_evict; 1890 ctx.resv = xe_vm_resv(vm); 1891 } 1892 1893 return ttm_bo_validate(&bo->ttm, &bo->placement, &ctx); 1894 } 1895 1896 bool xe_bo_is_xe_bo(struct ttm_buffer_object *bo) 1897 { 1898 if (bo->destroy == &xe_ttm_bo_destroy) 1899 return true; 1900 1901 return false; 1902 } 1903 1904 /* 1905 * Resolve a BO address. There is no assert to check if the proper lock is held 1906 * so it should only be used in cases where it is not fatal to get the wrong 1907 * address, such as printing debug information, but not in cases where memory is 1908 * written based on this result. 1909 */ 1910 dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size) 1911 { 1912 struct xe_device *xe = xe_bo_device(bo); 1913 struct xe_res_cursor cur; 1914 u64 page; 1915 1916 xe_assert(xe, page_size <= PAGE_SIZE); 1917 page = offset >> PAGE_SHIFT; 1918 offset &= (PAGE_SIZE - 1); 1919 1920 if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) { 1921 xe_assert(xe, bo->ttm.ttm); 1922 1923 xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT, 1924 page_size, &cur); 1925 return xe_res_dma(&cur) + offset; 1926 } else { 1927 struct xe_res_cursor cur; 1928 1929 xe_res_first(bo->ttm.resource, page << PAGE_SHIFT, 1930 page_size, &cur); 1931 return cur.start + offset + vram_region_gpu_offset(bo->ttm.resource); 1932 } 1933 } 1934 1935 dma_addr_t xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size) 1936 { 1937 if (!READ_ONCE(bo->ttm.pin_count)) 1938 xe_bo_assert_held(bo); 1939 return __xe_bo_addr(bo, offset, page_size); 1940 } 1941 1942 int xe_bo_vmap(struct xe_bo *bo) 1943 { 1944 void *virtual; 1945 bool is_iomem; 1946 int ret; 1947 1948 xe_bo_assert_held(bo); 1949 1950 if (!(bo->flags & XE_BO_FLAG_NEEDS_CPU_ACCESS)) 1951 return -EINVAL; 1952 1953 if (!iosys_map_is_null(&bo->vmap)) 1954 return 0; 1955 1956 /* 1957 * We use this more or less deprecated interface for now since 1958 * ttm_bo_vmap() doesn't offer the optimization of kmapping 1959 * single page bos, which is done here. 1960 * TODO: Fix up ttm_bo_vmap to do that, or fix up ttm_bo_kmap 1961 * to use struct iosys_map. 1962 */ 1963 ret = ttm_bo_kmap(&bo->ttm, 0, bo->size >> PAGE_SHIFT, &bo->kmap); 1964 if (ret) 1965 return ret; 1966 1967 virtual = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem); 1968 if (is_iomem) 1969 iosys_map_set_vaddr_iomem(&bo->vmap, (void __iomem *)virtual); 1970 else 1971 iosys_map_set_vaddr(&bo->vmap, virtual); 1972 1973 return 0; 1974 } 1975 1976 static void __xe_bo_vunmap(struct xe_bo *bo) 1977 { 1978 if (!iosys_map_is_null(&bo->vmap)) { 1979 iosys_map_clear(&bo->vmap); 1980 ttm_bo_kunmap(&bo->kmap); 1981 } 1982 } 1983 1984 void xe_bo_vunmap(struct xe_bo *bo) 1985 { 1986 xe_bo_assert_held(bo); 1987 __xe_bo_vunmap(bo); 1988 } 1989 1990 int xe_gem_create_ioctl(struct drm_device *dev, void *data, 1991 struct drm_file *file) 1992 { 1993 struct xe_device *xe = to_xe_device(dev); 1994 struct xe_file *xef = to_xe_file(file); 1995 struct drm_xe_gem_create *args = data; 1996 struct xe_vm *vm = NULL; 1997 struct xe_bo *bo; 1998 unsigned int bo_flags; 1999 u32 handle; 2000 int err; 2001 2002 if (XE_IOCTL_DBG(xe, args->extensions) || 2003 XE_IOCTL_DBG(xe, args->pad[0] || args->pad[1] || args->pad[2]) || 2004 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) 2005 return -EINVAL; 2006 2007 /* at least one valid memory placement must be specified */ 2008 if (XE_IOCTL_DBG(xe, (args->placement & ~xe->info.mem_region_mask) || 2009 !args->placement)) 2010 return -EINVAL; 2011 2012 if (XE_IOCTL_DBG(xe, args->flags & 2013 ~(DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING | 2014 DRM_XE_GEM_CREATE_FLAG_SCANOUT | 2015 DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM))) 2016 return -EINVAL; 2017 2018 if (XE_IOCTL_DBG(xe, args->handle)) 2019 return -EINVAL; 2020 2021 if (XE_IOCTL_DBG(xe, !args->size)) 2022 return -EINVAL; 2023 2024 if (XE_IOCTL_DBG(xe, args->size > SIZE_MAX)) 2025 return -EINVAL; 2026 2027 if (XE_IOCTL_DBG(xe, args->size & ~PAGE_MASK)) 2028 return -EINVAL; 2029 2030 bo_flags = 0; 2031 if (args->flags & DRM_XE_GEM_CREATE_FLAG_DEFER_BACKING) 2032 bo_flags |= XE_BO_FLAG_DEFER_BACKING; 2033 2034 if (args->flags & DRM_XE_GEM_CREATE_FLAG_SCANOUT) 2035 bo_flags |= XE_BO_FLAG_SCANOUT; 2036 2037 bo_flags |= args->placement << (ffs(XE_BO_FLAG_SYSTEM) - 1); 2038 2039 /* CCS formats need physical placement at a 64K alignment in VRAM. */ 2040 if ((bo_flags & XE_BO_FLAG_VRAM_MASK) && 2041 (bo_flags & XE_BO_FLAG_SCANOUT) && 2042 !(xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K) && 2043 IS_ALIGNED(args->size, SZ_64K)) 2044 bo_flags |= XE_BO_FLAG_NEEDS_64K; 2045 2046 if (args->flags & DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM) { 2047 if (XE_IOCTL_DBG(xe, !(bo_flags & XE_BO_FLAG_VRAM_MASK))) 2048 return -EINVAL; 2049 2050 bo_flags |= XE_BO_FLAG_NEEDS_CPU_ACCESS; 2051 } 2052 2053 if (XE_IOCTL_DBG(xe, !args->cpu_caching || 2054 args->cpu_caching > DRM_XE_GEM_CPU_CACHING_WC)) 2055 return -EINVAL; 2056 2057 if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_VRAM_MASK && 2058 args->cpu_caching != DRM_XE_GEM_CPU_CACHING_WC)) 2059 return -EINVAL; 2060 2061 if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_FLAG_SCANOUT && 2062 args->cpu_caching == DRM_XE_GEM_CPU_CACHING_WB)) 2063 return -EINVAL; 2064 2065 if (args->vm_id) { 2066 vm = xe_vm_lookup(xef, args->vm_id); 2067 if (XE_IOCTL_DBG(xe, !vm)) 2068 return -ENOENT; 2069 err = xe_vm_lock(vm, true); 2070 if (err) 2071 goto out_vm; 2072 } 2073 2074 bo = xe_bo_create_user(xe, NULL, vm, args->size, args->cpu_caching, 2075 bo_flags); 2076 2077 if (vm) 2078 xe_vm_unlock(vm); 2079 2080 if (IS_ERR(bo)) { 2081 err = PTR_ERR(bo); 2082 goto out_vm; 2083 } 2084 2085 err = drm_gem_handle_create(file, &bo->ttm.base, &handle); 2086 if (err) 2087 goto out_bulk; 2088 2089 args->handle = handle; 2090 goto out_put; 2091 2092 out_bulk: 2093 if (vm && !xe_vm_in_fault_mode(vm)) { 2094 xe_vm_lock(vm, false); 2095 __xe_bo_unset_bulk_move(bo); 2096 xe_vm_unlock(vm); 2097 } 2098 out_put: 2099 xe_bo_put(bo); 2100 out_vm: 2101 if (vm) 2102 xe_vm_put(vm); 2103 2104 return err; 2105 } 2106 2107 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data, 2108 struct drm_file *file) 2109 { 2110 struct xe_device *xe = to_xe_device(dev); 2111 struct drm_xe_gem_mmap_offset *args = data; 2112 struct drm_gem_object *gem_obj; 2113 2114 if (XE_IOCTL_DBG(xe, args->extensions) || 2115 XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1])) 2116 return -EINVAL; 2117 2118 if (XE_IOCTL_DBG(xe, args->flags)) 2119 return -EINVAL; 2120 2121 gem_obj = drm_gem_object_lookup(file, args->handle); 2122 if (XE_IOCTL_DBG(xe, !gem_obj)) 2123 return -ENOENT; 2124 2125 /* The mmap offset was set up at BO allocation time. */ 2126 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node); 2127 2128 xe_bo_put(gem_to_xe_bo(gem_obj)); 2129 return 0; 2130 } 2131 2132 /** 2133 * xe_bo_lock() - Lock the buffer object's dma_resv object 2134 * @bo: The struct xe_bo whose lock is to be taken 2135 * @intr: Whether to perform any wait interruptible 2136 * 2137 * Locks the buffer object's dma_resv object. If the buffer object is 2138 * pointing to a shared dma_resv object, that shared lock is locked. 2139 * 2140 * Return: 0 on success, -EINTR if @intr is true and the wait for a 2141 * contended lock was interrupted. If @intr is set to false, the 2142 * function always returns 0. 2143 */ 2144 int xe_bo_lock(struct xe_bo *bo, bool intr) 2145 { 2146 if (intr) 2147 return dma_resv_lock_interruptible(bo->ttm.base.resv, NULL); 2148 2149 dma_resv_lock(bo->ttm.base.resv, NULL); 2150 2151 return 0; 2152 } 2153 2154 /** 2155 * xe_bo_unlock() - Unlock the buffer object's dma_resv object 2156 * @bo: The struct xe_bo whose lock is to be released. 2157 * 2158 * Unlock a buffer object lock that was locked by xe_bo_lock(). 2159 */ 2160 void xe_bo_unlock(struct xe_bo *bo) 2161 { 2162 dma_resv_unlock(bo->ttm.base.resv); 2163 } 2164 2165 /** 2166 * xe_bo_can_migrate - Whether a buffer object likely can be migrated 2167 * @bo: The buffer object to migrate 2168 * @mem_type: The TTM memory type intended to migrate to 2169 * 2170 * Check whether the buffer object supports migration to the 2171 * given memory type. Note that pinning may affect the ability to migrate as 2172 * returned by this function. 2173 * 2174 * This function is primarily intended as a helper for checking the 2175 * possibility to migrate buffer objects and can be called without 2176 * the object lock held. 2177 * 2178 * Return: true if migration is possible, false otherwise. 2179 */ 2180 bool xe_bo_can_migrate(struct xe_bo *bo, u32 mem_type) 2181 { 2182 unsigned int cur_place; 2183 2184 if (bo->ttm.type == ttm_bo_type_kernel) 2185 return true; 2186 2187 if (bo->ttm.type == ttm_bo_type_sg) 2188 return false; 2189 2190 for (cur_place = 0; cur_place < bo->placement.num_placement; 2191 cur_place++) { 2192 if (bo->placements[cur_place].mem_type == mem_type) 2193 return true; 2194 } 2195 2196 return false; 2197 } 2198 2199 static void xe_place_from_ttm_type(u32 mem_type, struct ttm_place *place) 2200 { 2201 memset(place, 0, sizeof(*place)); 2202 place->mem_type = mem_type; 2203 } 2204 2205 /** 2206 * xe_bo_migrate - Migrate an object to the desired region id 2207 * @bo: The buffer object to migrate. 2208 * @mem_type: The TTM region type to migrate to. 2209 * 2210 * Attempt to migrate the buffer object to the desired memory region. The 2211 * buffer object may not be pinned, and must be locked. 2212 * On successful completion, the object memory type will be updated, 2213 * but an async migration task may not have completed yet, and to 2214 * accomplish that, the object's kernel fences must be signaled with 2215 * the object lock held. 2216 * 2217 * Return: 0 on success. Negative error code on failure. In particular may 2218 * return -EINTR or -ERESTARTSYS if signal pending. 2219 */ 2220 int xe_bo_migrate(struct xe_bo *bo, u32 mem_type) 2221 { 2222 struct xe_device *xe = ttm_to_xe_device(bo->ttm.bdev); 2223 struct ttm_operation_ctx ctx = { 2224 .interruptible = true, 2225 .no_wait_gpu = false, 2226 }; 2227 struct ttm_placement placement; 2228 struct ttm_place requested; 2229 2230 xe_bo_assert_held(bo); 2231 2232 if (bo->ttm.resource->mem_type == mem_type) 2233 return 0; 2234 2235 if (xe_bo_is_pinned(bo)) 2236 return -EBUSY; 2237 2238 if (!xe_bo_can_migrate(bo, mem_type)) 2239 return -EINVAL; 2240 2241 xe_place_from_ttm_type(mem_type, &requested); 2242 placement.num_placement = 1; 2243 placement.placement = &requested; 2244 2245 /* 2246 * Stolen needs to be handled like below VRAM handling if we ever need 2247 * to support it. 2248 */ 2249 drm_WARN_ON(&xe->drm, mem_type == XE_PL_STOLEN); 2250 2251 if (mem_type_is_vram(mem_type)) { 2252 u32 c = 0; 2253 2254 add_vram(xe, bo, &requested, bo->flags, mem_type, &c); 2255 } 2256 2257 return ttm_bo_validate(&bo->ttm, &placement, &ctx); 2258 } 2259 2260 /** 2261 * xe_bo_evict - Evict an object to evict placement 2262 * @bo: The buffer object to migrate. 2263 * @force_alloc: Set force_alloc in ttm_operation_ctx 2264 * 2265 * On successful completion, the object memory will be moved to evict 2266 * placement. Ths function blocks until the object has been fully moved. 2267 * 2268 * Return: 0 on success. Negative error code on failure. 2269 */ 2270 int xe_bo_evict(struct xe_bo *bo, bool force_alloc) 2271 { 2272 struct ttm_operation_ctx ctx = { 2273 .interruptible = false, 2274 .no_wait_gpu = false, 2275 .force_alloc = force_alloc, 2276 }; 2277 struct ttm_placement placement; 2278 int ret; 2279 2280 xe_evict_flags(&bo->ttm, &placement); 2281 ret = ttm_bo_validate(&bo->ttm, &placement, &ctx); 2282 if (ret) 2283 return ret; 2284 2285 dma_resv_wait_timeout(bo->ttm.base.resv, DMA_RESV_USAGE_KERNEL, 2286 false, MAX_SCHEDULE_TIMEOUT); 2287 2288 return 0; 2289 } 2290 2291 /** 2292 * xe_bo_needs_ccs_pages - Whether a bo needs to back up CCS pages when 2293 * placed in system memory. 2294 * @bo: The xe_bo 2295 * 2296 * Return: true if extra pages need to be allocated, false otherwise. 2297 */ 2298 bool xe_bo_needs_ccs_pages(struct xe_bo *bo) 2299 { 2300 struct xe_device *xe = xe_bo_device(bo); 2301 2302 if (GRAPHICS_VER(xe) >= 20 && IS_DGFX(xe)) 2303 return false; 2304 2305 if (!xe_device_has_flat_ccs(xe) || bo->ttm.type != ttm_bo_type_device) 2306 return false; 2307 2308 /* On discrete GPUs, if the GPU can access this buffer from 2309 * system memory (i.e., it allows XE_PL_TT placement), FlatCCS 2310 * can't be used since there's no CCS storage associated with 2311 * non-VRAM addresses. 2312 */ 2313 if (IS_DGFX(xe) && (bo->flags & XE_BO_FLAG_SYSTEM)) 2314 return false; 2315 2316 return true; 2317 } 2318 2319 /** 2320 * __xe_bo_release_dummy() - Dummy kref release function 2321 * @kref: The embedded struct kref. 2322 * 2323 * Dummy release function for xe_bo_put_deferred(). Keep off. 2324 */ 2325 void __xe_bo_release_dummy(struct kref *kref) 2326 { 2327 } 2328 2329 /** 2330 * xe_bo_put_commit() - Put bos whose put was deferred by xe_bo_put_deferred(). 2331 * @deferred: The lockless list used for the call to xe_bo_put_deferred(). 2332 * 2333 * Puts all bos whose put was deferred by xe_bo_put_deferred(). 2334 * The @deferred list can be either an onstack local list or a global 2335 * shared list used by a workqueue. 2336 */ 2337 void xe_bo_put_commit(struct llist_head *deferred) 2338 { 2339 struct llist_node *freed; 2340 struct xe_bo *bo, *next; 2341 2342 if (!deferred) 2343 return; 2344 2345 freed = llist_del_all(deferred); 2346 if (!freed) 2347 return; 2348 2349 llist_for_each_entry_safe(bo, next, freed, freed) 2350 drm_gem_object_free(&bo->ttm.base.refcount); 2351 } 2352 2353 void xe_bo_put(struct xe_bo *bo) 2354 { 2355 might_sleep(); 2356 if (bo) { 2357 #ifdef CONFIG_PROC_FS 2358 if (bo->client) 2359 might_lock(&bo->client->bos_lock); 2360 #endif 2361 if (bo->ggtt_node && bo->ggtt_node->ggtt) 2362 might_lock(&bo->ggtt_node->ggtt->lock); 2363 drm_gem_object_put(&bo->ttm.base); 2364 } 2365 } 2366 2367 /** 2368 * xe_bo_dumb_create - Create a dumb bo as backing for a fb 2369 * @file_priv: ... 2370 * @dev: ... 2371 * @args: ... 2372 * 2373 * See dumb_create() hook in include/drm/drm_drv.h 2374 * 2375 * Return: ... 2376 */ 2377 int xe_bo_dumb_create(struct drm_file *file_priv, 2378 struct drm_device *dev, 2379 struct drm_mode_create_dumb *args) 2380 { 2381 struct xe_device *xe = to_xe_device(dev); 2382 struct xe_bo *bo; 2383 uint32_t handle; 2384 int cpp = DIV_ROUND_UP(args->bpp, 8); 2385 int err; 2386 u32 page_size = max_t(u32, PAGE_SIZE, 2387 xe->info.vram_flags & XE_VRAM_FLAGS_NEED64K ? SZ_64K : SZ_4K); 2388 2389 args->pitch = ALIGN(args->width * cpp, 64); 2390 args->size = ALIGN(mul_u32_u32(args->pitch, args->height), 2391 page_size); 2392 2393 bo = xe_bo_create_user(xe, NULL, NULL, args->size, 2394 DRM_XE_GEM_CPU_CACHING_WC, 2395 XE_BO_FLAG_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) | 2396 XE_BO_FLAG_SCANOUT | 2397 XE_BO_FLAG_NEEDS_CPU_ACCESS); 2398 if (IS_ERR(bo)) 2399 return PTR_ERR(bo); 2400 2401 err = drm_gem_handle_create(file_priv, &bo->ttm.base, &handle); 2402 /* drop reference from allocate - handle holds it now */ 2403 drm_gem_object_put(&bo->ttm.base); 2404 if (!err) 2405 args->handle = handle; 2406 return err; 2407 } 2408 2409 void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo) 2410 { 2411 struct ttm_buffer_object *tbo = &bo->ttm; 2412 struct ttm_device *bdev = tbo->bdev; 2413 2414 drm_vma_node_unmap(&tbo->base.vma_node, bdev->dev_mapping); 2415 2416 list_del_init(&bo->vram_userfault_link); 2417 } 2418 2419 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST) 2420 #include "tests/xe_bo.c" 2421 #endif 2422