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