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