1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2021 Intel Corporation 4 */ 5 6 #include <linux/shmem_fs.h> 7 8 #include <drm/ttm/ttm_bo_driver.h> 9 #include <drm/ttm/ttm_placement.h> 10 #include <drm/drm_buddy.h> 11 12 #include "i915_drv.h" 13 #include "i915_ttm_buddy_manager.h" 14 #include "intel_memory_region.h" 15 #include "intel_region_ttm.h" 16 17 #include "gem/i915_gem_mman.h" 18 #include "gem/i915_gem_object.h" 19 #include "gem/i915_gem_region.h" 20 #include "gem/i915_gem_ttm.h" 21 #include "gem/i915_gem_ttm_move.h" 22 #include "gem/i915_gem_ttm_pm.h" 23 #include "gt/intel_gpu_commands.h" 24 25 #define I915_TTM_PRIO_PURGE 0 26 #define I915_TTM_PRIO_NO_PAGES 1 27 #define I915_TTM_PRIO_HAS_PAGES 2 28 #define I915_TTM_PRIO_NEEDS_CPU_ACCESS 3 29 30 /* 31 * Size of struct ttm_place vector in on-stack struct ttm_placement allocs 32 */ 33 #define I915_TTM_MAX_PLACEMENTS INTEL_REGION_UNKNOWN 34 35 /** 36 * struct i915_ttm_tt - TTM page vector with additional private information 37 * @ttm: The base TTM page vector. 38 * @dev: The struct device used for dma mapping and unmapping. 39 * @cached_rsgt: The cached scatter-gather table. 40 * @is_shmem: Set if using shmem. 41 * @filp: The shmem file, if using shmem backend. 42 * 43 * Note that DMA may be going on right up to the point where the page- 44 * vector is unpopulated in delayed destroy. Hence keep the 45 * scatter-gather table mapped and cached up to that point. This is 46 * different from the cached gem object io scatter-gather table which 47 * doesn't have an associated dma mapping. 48 */ 49 struct i915_ttm_tt { 50 struct ttm_tt ttm; 51 struct device *dev; 52 struct i915_refct_sgt cached_rsgt; 53 54 bool is_shmem; 55 struct file *filp; 56 }; 57 58 static const struct ttm_place sys_placement_flags = { 59 .fpfn = 0, 60 .lpfn = 0, 61 .mem_type = I915_PL_SYSTEM, 62 .flags = 0, 63 }; 64 65 static struct ttm_placement i915_sys_placement = { 66 .num_placement = 1, 67 .placement = &sys_placement_flags, 68 .num_busy_placement = 1, 69 .busy_placement = &sys_placement_flags, 70 }; 71 72 /** 73 * i915_ttm_sys_placement - Return the struct ttm_placement to be 74 * used for an object in system memory. 75 * 76 * Rather than making the struct extern, use this 77 * function. 78 * 79 * Return: A pointer to a static variable for sys placement. 80 */ 81 struct ttm_placement *i915_ttm_sys_placement(void) 82 { 83 return &i915_sys_placement; 84 } 85 86 static int i915_ttm_err_to_gem(int err) 87 { 88 /* Fastpath */ 89 if (likely(!err)) 90 return 0; 91 92 switch (err) { 93 case -EBUSY: 94 /* 95 * TTM likes to convert -EDEADLK to -EBUSY, and wants us to 96 * restart the operation, since we don't record the contending 97 * lock. We use -EAGAIN to restart. 98 */ 99 return -EAGAIN; 100 case -ENOSPC: 101 /* 102 * Memory type / region is full, and we can't evict. 103 * Except possibly system, that returns -ENOMEM; 104 */ 105 return -ENXIO; 106 default: 107 break; 108 } 109 110 return err; 111 } 112 113 static enum ttm_caching 114 i915_ttm_select_tt_caching(const struct drm_i915_gem_object *obj) 115 { 116 /* 117 * Objects only allowed in system get cached cpu-mappings, or when 118 * evicting lmem-only buffers to system for swapping. Other objects get 119 * WC mapping for now. Even if in system. 120 */ 121 if (obj->mm.n_placements <= 1) 122 return ttm_cached; 123 124 return ttm_write_combined; 125 } 126 127 static void 128 i915_ttm_place_from_region(const struct intel_memory_region *mr, 129 struct ttm_place *place, 130 resource_size_t offset, 131 resource_size_t size, 132 unsigned int flags) 133 { 134 memset(place, 0, sizeof(*place)); 135 place->mem_type = intel_region_to_ttm_type(mr); 136 137 if (mr->type == INTEL_MEMORY_SYSTEM) 138 return; 139 140 if (flags & I915_BO_ALLOC_CONTIGUOUS) 141 place->flags |= TTM_PL_FLAG_CONTIGUOUS; 142 if (offset != I915_BO_INVALID_OFFSET) { 143 place->fpfn = offset >> PAGE_SHIFT; 144 place->lpfn = place->fpfn + (size >> PAGE_SHIFT); 145 } else if (mr->io_size && mr->io_size < mr->total) { 146 if (flags & I915_BO_ALLOC_GPU_ONLY) { 147 place->flags |= TTM_PL_FLAG_TOPDOWN; 148 } else { 149 place->fpfn = 0; 150 place->lpfn = mr->io_size >> PAGE_SHIFT; 151 } 152 } 153 } 154 155 static void 156 i915_ttm_placement_from_obj(const struct drm_i915_gem_object *obj, 157 struct ttm_place *requested, 158 struct ttm_place *busy, 159 struct ttm_placement *placement) 160 { 161 unsigned int num_allowed = obj->mm.n_placements; 162 unsigned int flags = obj->flags; 163 unsigned int i; 164 165 placement->num_placement = 1; 166 i915_ttm_place_from_region(num_allowed ? obj->mm.placements[0] : 167 obj->mm.region, requested, obj->bo_offset, 168 obj->base.size, flags); 169 170 /* Cache this on object? */ 171 placement->num_busy_placement = num_allowed; 172 for (i = 0; i < placement->num_busy_placement; ++i) 173 i915_ttm_place_from_region(obj->mm.placements[i], busy + i, 174 obj->bo_offset, obj->base.size, flags); 175 176 if (num_allowed == 0) { 177 *busy = *requested; 178 placement->num_busy_placement = 1; 179 } 180 181 placement->placement = requested; 182 placement->busy_placement = busy; 183 } 184 185 static int i915_ttm_tt_shmem_populate(struct ttm_device *bdev, 186 struct ttm_tt *ttm, 187 struct ttm_operation_ctx *ctx) 188 { 189 struct drm_i915_private *i915 = container_of(bdev, typeof(*i915), bdev); 190 struct intel_memory_region *mr = i915->mm.regions[INTEL_MEMORY_SYSTEM]; 191 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm); 192 const unsigned int max_segment = i915_sg_segment_size(); 193 const size_t size = (size_t)ttm->num_pages << PAGE_SHIFT; 194 struct file *filp = i915_tt->filp; 195 struct sgt_iter sgt_iter; 196 struct sg_table *st; 197 struct page *page; 198 unsigned long i; 199 int err; 200 201 if (!filp) { 202 struct address_space *mapping; 203 gfp_t mask; 204 205 filp = shmem_file_setup("i915-shmem-tt", size, VM_NORESERVE); 206 if (IS_ERR(filp)) 207 return PTR_ERR(filp); 208 209 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; 210 211 mapping = filp->f_mapping; 212 mapping_set_gfp_mask(mapping, mask); 213 GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); 214 215 i915_tt->filp = filp; 216 } 217 218 st = &i915_tt->cached_rsgt.table; 219 err = shmem_sg_alloc_table(i915, st, size, mr, filp->f_mapping, 220 max_segment); 221 if (err) 222 return err; 223 224 err = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 225 DMA_ATTR_SKIP_CPU_SYNC); 226 if (err) 227 goto err_free_st; 228 229 i = 0; 230 for_each_sgt_page(page, sgt_iter, st) 231 ttm->pages[i++] = page; 232 233 if (ttm->page_flags & TTM_TT_FLAG_SWAPPED) 234 ttm->page_flags &= ~TTM_TT_FLAG_SWAPPED; 235 236 return 0; 237 238 err_free_st: 239 shmem_sg_free_table(st, filp->f_mapping, false, false); 240 241 return err; 242 } 243 244 static void i915_ttm_tt_shmem_unpopulate(struct ttm_tt *ttm) 245 { 246 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm); 247 bool backup = ttm->page_flags & TTM_TT_FLAG_SWAPPED; 248 struct sg_table *st = &i915_tt->cached_rsgt.table; 249 250 shmem_sg_free_table(st, file_inode(i915_tt->filp)->i_mapping, 251 backup, backup); 252 } 253 254 static void i915_ttm_tt_release(struct kref *ref) 255 { 256 struct i915_ttm_tt *i915_tt = 257 container_of(ref, typeof(*i915_tt), cached_rsgt.kref); 258 struct sg_table *st = &i915_tt->cached_rsgt.table; 259 260 GEM_WARN_ON(st->sgl); 261 262 kfree(i915_tt); 263 } 264 265 static const struct i915_refct_sgt_ops tt_rsgt_ops = { 266 .release = i915_ttm_tt_release 267 }; 268 269 static inline bool 270 i915_gem_object_needs_ccs_pages(struct drm_i915_gem_object *obj) 271 { 272 bool lmem_placement = false; 273 int i; 274 275 for (i = 0; i < obj->mm.n_placements; i++) { 276 /* Compression is not allowed for the objects with smem placement */ 277 if (obj->mm.placements[i]->type == INTEL_MEMORY_SYSTEM) 278 return false; 279 if (!lmem_placement && 280 obj->mm.placements[i]->type == INTEL_MEMORY_LOCAL) 281 lmem_placement = true; 282 } 283 284 return lmem_placement; 285 } 286 287 static struct ttm_tt *i915_ttm_tt_create(struct ttm_buffer_object *bo, 288 uint32_t page_flags) 289 { 290 struct drm_i915_private *i915 = container_of(bo->bdev, typeof(*i915), 291 bdev); 292 struct ttm_resource_manager *man = 293 ttm_manager_type(bo->bdev, bo->resource->mem_type); 294 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 295 unsigned long ccs_pages = 0; 296 enum ttm_caching caching; 297 struct i915_ttm_tt *i915_tt; 298 int ret; 299 300 if (!obj) 301 return NULL; 302 303 i915_tt = kzalloc(sizeof(*i915_tt), GFP_KERNEL); 304 if (!i915_tt) 305 return NULL; 306 307 if (obj->flags & I915_BO_ALLOC_CPU_CLEAR && 308 man->use_tt) 309 page_flags |= TTM_TT_FLAG_ZERO_ALLOC; 310 311 caching = i915_ttm_select_tt_caching(obj); 312 if (i915_gem_object_is_shrinkable(obj) && caching == ttm_cached) { 313 page_flags |= TTM_TT_FLAG_EXTERNAL | 314 TTM_TT_FLAG_EXTERNAL_MAPPABLE; 315 i915_tt->is_shmem = true; 316 } 317 318 if (HAS_FLAT_CCS(i915) && i915_gem_object_needs_ccs_pages(obj)) 319 ccs_pages = DIV_ROUND_UP(DIV_ROUND_UP(bo->base.size, 320 NUM_BYTES_PER_CCS_BYTE), 321 PAGE_SIZE); 322 323 ret = ttm_tt_init(&i915_tt->ttm, bo, page_flags, caching, ccs_pages); 324 if (ret) 325 goto err_free; 326 327 __i915_refct_sgt_init(&i915_tt->cached_rsgt, bo->base.size, 328 &tt_rsgt_ops); 329 330 i915_tt->dev = obj->base.dev->dev; 331 332 return &i915_tt->ttm; 333 334 err_free: 335 kfree(i915_tt); 336 return NULL; 337 } 338 339 static int i915_ttm_tt_populate(struct ttm_device *bdev, 340 struct ttm_tt *ttm, 341 struct ttm_operation_ctx *ctx) 342 { 343 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm); 344 345 if (i915_tt->is_shmem) 346 return i915_ttm_tt_shmem_populate(bdev, ttm, ctx); 347 348 return ttm_pool_alloc(&bdev->pool, ttm, ctx); 349 } 350 351 static void i915_ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm) 352 { 353 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm); 354 struct sg_table *st = &i915_tt->cached_rsgt.table; 355 356 if (st->sgl) 357 dma_unmap_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0); 358 359 if (i915_tt->is_shmem) { 360 i915_ttm_tt_shmem_unpopulate(ttm); 361 } else { 362 sg_free_table(st); 363 ttm_pool_free(&bdev->pool, ttm); 364 } 365 } 366 367 static void i915_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm) 368 { 369 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm); 370 371 if (i915_tt->filp) 372 fput(i915_tt->filp); 373 374 ttm_tt_fini(ttm); 375 i915_refct_sgt_put(&i915_tt->cached_rsgt); 376 } 377 378 static bool i915_ttm_eviction_valuable(struct ttm_buffer_object *bo, 379 const struct ttm_place *place) 380 { 381 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 382 struct ttm_resource *res = bo->resource; 383 384 if (!obj) 385 return false; 386 387 /* 388 * EXTERNAL objects should never be swapped out by TTM, instead we need 389 * to handle that ourselves. TTM will already skip such objects for us, 390 * but we would like to avoid grabbing locks for no good reason. 391 */ 392 if (bo->ttm && bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL) 393 return false; 394 395 /* Will do for now. Our pinned objects are still on TTM's LRU lists */ 396 if (!i915_gem_object_evictable(obj)) 397 return false; 398 399 switch (res->mem_type) { 400 case I915_PL_LMEM0: { 401 struct ttm_resource_manager *man = 402 ttm_manager_type(bo->bdev, res->mem_type); 403 struct i915_ttm_buddy_resource *bman_res = 404 to_ttm_buddy_resource(res); 405 struct drm_buddy *mm = bman_res->mm; 406 struct drm_buddy_block *block; 407 408 if (!place->fpfn && !place->lpfn) 409 return true; 410 411 GEM_BUG_ON(!place->lpfn); 412 413 /* 414 * If we just want something mappable then we can quickly check 415 * if the current victim resource is using any of the CPU 416 * visible portion. 417 */ 418 if (!place->fpfn && 419 place->lpfn == i915_ttm_buddy_man_visible_size(man)) 420 return bman_res->used_visible_size > 0; 421 422 /* Real range allocation */ 423 list_for_each_entry(block, &bman_res->blocks, link) { 424 unsigned long fpfn = 425 drm_buddy_block_offset(block) >> PAGE_SHIFT; 426 unsigned long lpfn = fpfn + 427 (drm_buddy_block_size(mm, block) >> PAGE_SHIFT); 428 429 if (place->fpfn < lpfn && place->lpfn > fpfn) 430 return true; 431 } 432 return false; 433 } default: 434 break; 435 } 436 437 return true; 438 } 439 440 static void i915_ttm_evict_flags(struct ttm_buffer_object *bo, 441 struct ttm_placement *placement) 442 { 443 *placement = i915_sys_placement; 444 } 445 446 /** 447 * i915_ttm_free_cached_io_rsgt - Free object cached LMEM information 448 * @obj: The GEM object 449 * This function frees any LMEM-related information that is cached on 450 * the object. For example the radix tree for fast page lookup and the 451 * cached refcounted sg-table 452 */ 453 void i915_ttm_free_cached_io_rsgt(struct drm_i915_gem_object *obj) 454 { 455 struct radix_tree_iter iter; 456 void __rcu **slot; 457 458 if (!obj->ttm.cached_io_rsgt) 459 return; 460 461 rcu_read_lock(); 462 radix_tree_for_each_slot(slot, &obj->ttm.get_io_page.radix, &iter, 0) 463 radix_tree_delete(&obj->ttm.get_io_page.radix, iter.index); 464 rcu_read_unlock(); 465 466 i915_refct_sgt_put(obj->ttm.cached_io_rsgt); 467 obj->ttm.cached_io_rsgt = NULL; 468 } 469 470 /** 471 * i915_ttm_purge - Clear an object of its memory 472 * @obj: The object 473 * 474 * This function is called to clear an object of it's memory when it is 475 * marked as not needed anymore. 476 * 477 * Return: 0 on success, negative error code on failure. 478 */ 479 int i915_ttm_purge(struct drm_i915_gem_object *obj) 480 { 481 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 482 struct i915_ttm_tt *i915_tt = 483 container_of(bo->ttm, typeof(*i915_tt), ttm); 484 struct ttm_operation_ctx ctx = { 485 .interruptible = true, 486 .no_wait_gpu = false, 487 }; 488 struct ttm_placement place = {}; 489 int ret; 490 491 if (obj->mm.madv == __I915_MADV_PURGED) 492 return 0; 493 494 ret = ttm_bo_validate(bo, &place, &ctx); 495 if (ret) 496 return ret; 497 498 if (bo->ttm && i915_tt->filp) { 499 /* 500 * The below fput(which eventually calls shmem_truncate) might 501 * be delayed by worker, so when directly called to purge the 502 * pages(like by the shrinker) we should try to be more 503 * aggressive and release the pages immediately. 504 */ 505 shmem_truncate_range(file_inode(i915_tt->filp), 506 0, (loff_t)-1); 507 fput(fetch_and_zero(&i915_tt->filp)); 508 } 509 510 obj->write_domain = 0; 511 obj->read_domains = 0; 512 i915_ttm_adjust_gem_after_move(obj); 513 i915_ttm_free_cached_io_rsgt(obj); 514 obj->mm.madv = __I915_MADV_PURGED; 515 516 return 0; 517 } 518 519 static int i915_ttm_shrink(struct drm_i915_gem_object *obj, unsigned int flags) 520 { 521 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 522 struct i915_ttm_tt *i915_tt = 523 container_of(bo->ttm, typeof(*i915_tt), ttm); 524 struct ttm_operation_ctx ctx = { 525 .interruptible = true, 526 .no_wait_gpu = flags & I915_GEM_OBJECT_SHRINK_NO_GPU_WAIT, 527 }; 528 struct ttm_placement place = {}; 529 int ret; 530 531 if (!bo->ttm || bo->resource->mem_type != TTM_PL_SYSTEM) 532 return 0; 533 534 GEM_BUG_ON(!i915_tt->is_shmem); 535 536 if (!i915_tt->filp) 537 return 0; 538 539 ret = ttm_bo_wait_ctx(bo, &ctx); 540 if (ret) 541 return ret; 542 543 switch (obj->mm.madv) { 544 case I915_MADV_DONTNEED: 545 return i915_ttm_purge(obj); 546 case __I915_MADV_PURGED: 547 return 0; 548 } 549 550 if (bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED) 551 return 0; 552 553 bo->ttm->page_flags |= TTM_TT_FLAG_SWAPPED; 554 ret = ttm_bo_validate(bo, &place, &ctx); 555 if (ret) { 556 bo->ttm->page_flags &= ~TTM_TT_FLAG_SWAPPED; 557 return ret; 558 } 559 560 if (flags & I915_GEM_OBJECT_SHRINK_WRITEBACK) 561 __shmem_writeback(obj->base.size, i915_tt->filp->f_mapping); 562 563 return 0; 564 } 565 566 static void i915_ttm_delete_mem_notify(struct ttm_buffer_object *bo) 567 { 568 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 569 570 if (likely(obj)) { 571 __i915_gem_object_pages_fini(obj); 572 i915_ttm_free_cached_io_rsgt(obj); 573 } 574 } 575 576 static struct i915_refct_sgt *i915_ttm_tt_get_st(struct ttm_tt *ttm) 577 { 578 struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm); 579 struct sg_table *st; 580 int ret; 581 582 if (i915_tt->cached_rsgt.table.sgl) 583 return i915_refct_sgt_get(&i915_tt->cached_rsgt); 584 585 st = &i915_tt->cached_rsgt.table; 586 ret = sg_alloc_table_from_pages_segment(st, 587 ttm->pages, ttm->num_pages, 588 0, (unsigned long)ttm->num_pages << PAGE_SHIFT, 589 i915_sg_segment_size(), GFP_KERNEL); 590 if (ret) { 591 st->sgl = NULL; 592 return ERR_PTR(ret); 593 } 594 595 ret = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0); 596 if (ret) { 597 sg_free_table(st); 598 return ERR_PTR(ret); 599 } 600 601 return i915_refct_sgt_get(&i915_tt->cached_rsgt); 602 } 603 604 /** 605 * i915_ttm_resource_get_st - Get a refcounted sg-table pointing to the 606 * resource memory 607 * @obj: The GEM object used for sg-table caching 608 * @res: The struct ttm_resource for which an sg-table is requested. 609 * 610 * This function returns a refcounted sg-table representing the memory 611 * pointed to by @res. If @res is the object's current resource it may also 612 * cache the sg_table on the object or attempt to access an already cached 613 * sg-table. The refcounted sg-table needs to be put when no-longer in use. 614 * 615 * Return: A valid pointer to a struct i915_refct_sgt or error pointer on 616 * failure. 617 */ 618 struct i915_refct_sgt * 619 i915_ttm_resource_get_st(struct drm_i915_gem_object *obj, 620 struct ttm_resource *res) 621 { 622 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 623 u32 page_alignment; 624 625 if (!i915_ttm_gtt_binds_lmem(res)) 626 return i915_ttm_tt_get_st(bo->ttm); 627 628 page_alignment = bo->page_alignment << PAGE_SHIFT; 629 if (!page_alignment) 630 page_alignment = obj->mm.region->min_page_size; 631 632 /* 633 * If CPU mapping differs, we need to add the ttm_tt pages to 634 * the resulting st. Might make sense for GGTT. 635 */ 636 GEM_WARN_ON(!i915_ttm_cpu_maps_iomem(res)); 637 if (bo->resource == res) { 638 if (!obj->ttm.cached_io_rsgt) { 639 struct i915_refct_sgt *rsgt; 640 641 rsgt = intel_region_ttm_resource_to_rsgt(obj->mm.region, 642 res, 643 page_alignment); 644 if (IS_ERR(rsgt)) 645 return rsgt; 646 647 obj->ttm.cached_io_rsgt = rsgt; 648 } 649 return i915_refct_sgt_get(obj->ttm.cached_io_rsgt); 650 } 651 652 return intel_region_ttm_resource_to_rsgt(obj->mm.region, res, 653 page_alignment); 654 } 655 656 static int i915_ttm_truncate(struct drm_i915_gem_object *obj) 657 { 658 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 659 int err; 660 661 WARN_ON_ONCE(obj->mm.madv == I915_MADV_WILLNEED); 662 663 err = i915_ttm_move_notify(bo); 664 if (err) 665 return err; 666 667 return i915_ttm_purge(obj); 668 } 669 670 static void i915_ttm_swap_notify(struct ttm_buffer_object *bo) 671 { 672 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 673 int ret; 674 675 if (!obj) 676 return; 677 678 ret = i915_ttm_move_notify(bo); 679 GEM_WARN_ON(ret); 680 GEM_WARN_ON(obj->ttm.cached_io_rsgt); 681 if (!ret && obj->mm.madv != I915_MADV_WILLNEED) 682 i915_ttm_purge(obj); 683 } 684 685 static bool i915_ttm_resource_mappable(struct ttm_resource *res) 686 { 687 struct i915_ttm_buddy_resource *bman_res = to_ttm_buddy_resource(res); 688 689 if (!i915_ttm_cpu_maps_iomem(res)) 690 return true; 691 692 return bman_res->used_visible_size == bman_res->base.num_pages; 693 } 694 695 static int i915_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem) 696 { 697 if (!i915_ttm_cpu_maps_iomem(mem)) 698 return 0; 699 700 if (!i915_ttm_resource_mappable(mem)) 701 return -EINVAL; 702 703 mem->bus.caching = ttm_write_combined; 704 mem->bus.is_iomem = true; 705 706 return 0; 707 } 708 709 static unsigned long i915_ttm_io_mem_pfn(struct ttm_buffer_object *bo, 710 unsigned long page_offset) 711 { 712 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 713 struct scatterlist *sg; 714 unsigned long base; 715 unsigned int ofs; 716 717 GEM_BUG_ON(!obj); 718 GEM_WARN_ON(bo->ttm); 719 720 base = obj->mm.region->iomap.base - obj->mm.region->region.start; 721 sg = __i915_gem_object_get_sg(obj, &obj->ttm.get_io_page, page_offset, &ofs, true); 722 723 return ((base + sg_dma_address(sg)) >> PAGE_SHIFT) + ofs; 724 } 725 726 /* 727 * All callbacks need to take care not to downcast a struct ttm_buffer_object 728 * without checking its subclass, since it might be a TTM ghost object. 729 */ 730 static struct ttm_device_funcs i915_ttm_bo_driver = { 731 .ttm_tt_create = i915_ttm_tt_create, 732 .ttm_tt_populate = i915_ttm_tt_populate, 733 .ttm_tt_unpopulate = i915_ttm_tt_unpopulate, 734 .ttm_tt_destroy = i915_ttm_tt_destroy, 735 .eviction_valuable = i915_ttm_eviction_valuable, 736 .evict_flags = i915_ttm_evict_flags, 737 .move = i915_ttm_move, 738 .swap_notify = i915_ttm_swap_notify, 739 .delete_mem_notify = i915_ttm_delete_mem_notify, 740 .io_mem_reserve = i915_ttm_io_mem_reserve, 741 .io_mem_pfn = i915_ttm_io_mem_pfn, 742 }; 743 744 /** 745 * i915_ttm_driver - Return a pointer to the TTM device funcs 746 * 747 * Return: Pointer to statically allocated TTM device funcs. 748 */ 749 struct ttm_device_funcs *i915_ttm_driver(void) 750 { 751 return &i915_ttm_bo_driver; 752 } 753 754 static int __i915_ttm_get_pages(struct drm_i915_gem_object *obj, 755 struct ttm_placement *placement) 756 { 757 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 758 struct ttm_operation_ctx ctx = { 759 .interruptible = true, 760 .no_wait_gpu = false, 761 }; 762 int real_num_busy; 763 int ret; 764 765 /* First try only the requested placement. No eviction. */ 766 real_num_busy = fetch_and_zero(&placement->num_busy_placement); 767 ret = ttm_bo_validate(bo, placement, &ctx); 768 if (ret) { 769 ret = i915_ttm_err_to_gem(ret); 770 /* 771 * Anything that wants to restart the operation gets to 772 * do that. 773 */ 774 if (ret == -EDEADLK || ret == -EINTR || ret == -ERESTARTSYS || 775 ret == -EAGAIN) 776 return ret; 777 778 /* 779 * If the initial attempt fails, allow all accepted placements, 780 * evicting if necessary. 781 */ 782 placement->num_busy_placement = real_num_busy; 783 ret = ttm_bo_validate(bo, placement, &ctx); 784 if (ret) 785 return i915_ttm_err_to_gem(ret); 786 } 787 788 if (bo->ttm && !ttm_tt_is_populated(bo->ttm)) { 789 ret = ttm_tt_populate(bo->bdev, bo->ttm, &ctx); 790 if (ret) 791 return ret; 792 793 i915_ttm_adjust_domains_after_move(obj); 794 i915_ttm_adjust_gem_after_move(obj); 795 } 796 797 if (!i915_gem_object_has_pages(obj)) { 798 struct i915_refct_sgt *rsgt = 799 i915_ttm_resource_get_st(obj, bo->resource); 800 801 if (IS_ERR(rsgt)) 802 return PTR_ERR(rsgt); 803 804 GEM_BUG_ON(obj->mm.rsgt); 805 obj->mm.rsgt = rsgt; 806 __i915_gem_object_set_pages(obj, &rsgt->table, 807 i915_sg_dma_sizes(rsgt->table.sgl)); 808 } 809 810 GEM_BUG_ON(bo->ttm && ((obj->base.size >> PAGE_SHIFT) < bo->ttm->num_pages)); 811 i915_ttm_adjust_lru(obj); 812 return ret; 813 } 814 815 static int i915_ttm_get_pages(struct drm_i915_gem_object *obj) 816 { 817 struct ttm_place requested, busy[I915_TTM_MAX_PLACEMENTS]; 818 struct ttm_placement placement; 819 820 GEM_BUG_ON(obj->mm.n_placements > I915_TTM_MAX_PLACEMENTS); 821 822 /* Move to the requested placement. */ 823 i915_ttm_placement_from_obj(obj, &requested, busy, &placement); 824 825 return __i915_ttm_get_pages(obj, &placement); 826 } 827 828 /** 829 * DOC: Migration vs eviction 830 * 831 * GEM migration may not be the same as TTM migration / eviction. If 832 * the TTM core decides to evict an object it may be evicted to a 833 * TTM memory type that is not in the object's allowable GEM regions, or 834 * in fact theoretically to a TTM memory type that doesn't correspond to 835 * a GEM memory region. In that case the object's GEM region is not 836 * updated, and the data is migrated back to the GEM region at 837 * get_pages time. TTM may however set up CPU ptes to the object even 838 * when it is evicted. 839 * Gem forced migration using the i915_ttm_migrate() op, is allowed even 840 * to regions that are not in the object's list of allowable placements. 841 */ 842 static int __i915_ttm_migrate(struct drm_i915_gem_object *obj, 843 struct intel_memory_region *mr, 844 unsigned int flags) 845 { 846 struct ttm_place requested; 847 struct ttm_placement placement; 848 int ret; 849 850 i915_ttm_place_from_region(mr, &requested, obj->bo_offset, 851 obj->base.size, flags); 852 placement.num_placement = 1; 853 placement.num_busy_placement = 1; 854 placement.placement = &requested; 855 placement.busy_placement = &requested; 856 857 ret = __i915_ttm_get_pages(obj, &placement); 858 if (ret) 859 return ret; 860 861 /* 862 * Reinitialize the region bindings. This is primarily 863 * required for objects where the new region is not in 864 * its allowable placements. 865 */ 866 if (obj->mm.region != mr) { 867 i915_gem_object_release_memory_region(obj); 868 i915_gem_object_init_memory_region(obj, mr); 869 } 870 871 return 0; 872 } 873 874 static int i915_ttm_migrate(struct drm_i915_gem_object *obj, 875 struct intel_memory_region *mr) 876 { 877 return __i915_ttm_migrate(obj, mr, obj->flags); 878 } 879 880 static void i915_ttm_put_pages(struct drm_i915_gem_object *obj, 881 struct sg_table *st) 882 { 883 /* 884 * We're currently not called from a shrinker, so put_pages() 885 * typically means the object is about to destroyed, or called 886 * from move_notify(). So just avoid doing much for now. 887 * If the object is not destroyed next, The TTM eviction logic 888 * and shrinkers will move it out if needed. 889 */ 890 891 if (obj->mm.rsgt) 892 i915_refct_sgt_put(fetch_and_zero(&obj->mm.rsgt)); 893 } 894 895 /** 896 * i915_ttm_adjust_lru - Adjust an object's position on relevant LRU lists. 897 * @obj: The object 898 */ 899 void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj) 900 { 901 struct ttm_buffer_object *bo = i915_gem_to_ttm(obj); 902 struct i915_ttm_tt *i915_tt = 903 container_of(bo->ttm, typeof(*i915_tt), ttm); 904 bool shrinkable = 905 bo->ttm && i915_tt->filp && ttm_tt_is_populated(bo->ttm); 906 907 /* 908 * Don't manipulate the TTM LRUs while in TTM bo destruction. 909 * We're called through i915_ttm_delete_mem_notify(). 910 */ 911 if (!kref_read(&bo->kref)) 912 return; 913 914 /* 915 * We skip managing the shrinker LRU in set_pages() and just manage 916 * everything here. This does at least solve the issue with having 917 * temporary shmem mappings(like with evicted lmem) not being visible to 918 * the shrinker. Only our shmem objects are shrinkable, everything else 919 * we keep as unshrinkable. 920 * 921 * To make sure everything plays nice we keep an extra shrink pin in TTM 922 * if the underlying pages are not currently shrinkable. Once we release 923 * our pin, like when the pages are moved to shmem, the pages will then 924 * be added to the shrinker LRU, assuming the caller isn't also holding 925 * a pin. 926 * 927 * TODO: consider maybe also bumping the shrinker list here when we have 928 * already unpinned it, which should give us something more like an LRU. 929 * 930 * TODO: There is a small window of opportunity for this function to 931 * get called from eviction after we've dropped the last GEM refcount, 932 * but before the TTM deleted flag is set on the object. Avoid 933 * adjusting the shrinker list in such cases, since the object is 934 * not available to the shrinker anyway due to its zero refcount. 935 * To fix this properly we should move to a TTM shrinker LRU list for 936 * these objects. 937 */ 938 if (kref_get_unless_zero(&obj->base.refcount)) { 939 if (shrinkable != obj->mm.ttm_shrinkable) { 940 if (shrinkable) { 941 if (obj->mm.madv == I915_MADV_WILLNEED) 942 __i915_gem_object_make_shrinkable(obj); 943 else 944 __i915_gem_object_make_purgeable(obj); 945 } else { 946 i915_gem_object_make_unshrinkable(obj); 947 } 948 949 obj->mm.ttm_shrinkable = shrinkable; 950 } 951 i915_gem_object_put(obj); 952 } 953 954 /* 955 * Put on the correct LRU list depending on the MADV status 956 */ 957 spin_lock(&bo->bdev->lru_lock); 958 if (shrinkable) { 959 /* Try to keep shmem_tt from being considered for shrinking. */ 960 bo->priority = TTM_MAX_BO_PRIORITY - 1; 961 } else if (obj->mm.madv != I915_MADV_WILLNEED) { 962 bo->priority = I915_TTM_PRIO_PURGE; 963 } else if (!i915_gem_object_has_pages(obj)) { 964 bo->priority = I915_TTM_PRIO_NO_PAGES; 965 } else { 966 struct ttm_resource_manager *man = 967 ttm_manager_type(bo->bdev, bo->resource->mem_type); 968 969 /* 970 * If we need to place an LMEM resource which doesn't need CPU 971 * access then we should try not to victimize mappable objects 972 * first, since we likely end up stealing more of the mappable 973 * portion. And likewise when we try to find space for a mappble 974 * object, we know not to ever victimize objects that don't 975 * occupy any mappable pages. 976 */ 977 if (i915_ttm_cpu_maps_iomem(bo->resource) && 978 i915_ttm_buddy_man_visible_size(man) < man->size && 979 !(obj->flags & I915_BO_ALLOC_GPU_ONLY)) 980 bo->priority = I915_TTM_PRIO_NEEDS_CPU_ACCESS; 981 else 982 bo->priority = I915_TTM_PRIO_HAS_PAGES; 983 } 984 985 ttm_bo_move_to_lru_tail(bo); 986 spin_unlock(&bo->bdev->lru_lock); 987 } 988 989 /* 990 * TTM-backed gem object destruction requires some clarification. 991 * Basically we have two possibilities here. We can either rely on the 992 * i915 delayed destruction and put the TTM object when the object 993 * is idle. This would be detected by TTM which would bypass the 994 * TTM delayed destroy handling. The other approach is to put the TTM 995 * object early and rely on the TTM destroyed handling, and then free 996 * the leftover parts of the GEM object once TTM's destroyed list handling is 997 * complete. For now, we rely on the latter for two reasons: 998 * a) TTM can evict an object even when it's on the delayed destroy list, 999 * which in theory allows for complete eviction. 1000 * b) There is work going on in TTM to allow freeing an object even when 1001 * it's not idle, and using the TTM destroyed list handling could help us 1002 * benefit from that. 1003 */ 1004 static void i915_ttm_delayed_free(struct drm_i915_gem_object *obj) 1005 { 1006 GEM_BUG_ON(!obj->ttm.created); 1007 1008 ttm_bo_put(i915_gem_to_ttm(obj)); 1009 } 1010 1011 static vm_fault_t vm_fault_ttm(struct vm_fault *vmf) 1012 { 1013 struct vm_area_struct *area = vmf->vma; 1014 struct ttm_buffer_object *bo = area->vm_private_data; 1015 struct drm_device *dev = bo->base.dev; 1016 struct drm_i915_gem_object *obj; 1017 vm_fault_t ret; 1018 int idx; 1019 1020 obj = i915_ttm_to_gem(bo); 1021 if (!obj) 1022 return VM_FAULT_SIGBUS; 1023 1024 /* Sanity check that we allow writing into this object */ 1025 if (unlikely(i915_gem_object_is_readonly(obj) && 1026 area->vm_flags & VM_WRITE)) 1027 return VM_FAULT_SIGBUS; 1028 1029 ret = ttm_bo_vm_reserve(bo, vmf); 1030 if (ret) 1031 return ret; 1032 1033 if (obj->mm.madv != I915_MADV_WILLNEED) { 1034 dma_resv_unlock(bo->base.resv); 1035 return VM_FAULT_SIGBUS; 1036 } 1037 1038 if (!i915_ttm_resource_mappable(bo->resource)) { 1039 int err = -ENODEV; 1040 int i; 1041 1042 for (i = 0; i < obj->mm.n_placements; i++) { 1043 struct intel_memory_region *mr = obj->mm.placements[i]; 1044 unsigned int flags; 1045 1046 if (!mr->io_size && mr->type != INTEL_MEMORY_SYSTEM) 1047 continue; 1048 1049 flags = obj->flags; 1050 flags &= ~I915_BO_ALLOC_GPU_ONLY; 1051 err = __i915_ttm_migrate(obj, mr, flags); 1052 if (!err) 1053 break; 1054 } 1055 1056 if (err) { 1057 drm_dbg(dev, "Unable to make resource CPU accessible\n"); 1058 dma_resv_unlock(bo->base.resv); 1059 return VM_FAULT_SIGBUS; 1060 } 1061 } 1062 1063 if (drm_dev_enter(dev, &idx)) { 1064 ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot, 1065 TTM_BO_VM_NUM_PREFAULT); 1066 drm_dev_exit(idx); 1067 } else { 1068 ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot); 1069 } 1070 if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) 1071 return ret; 1072 1073 i915_ttm_adjust_lru(obj); 1074 1075 dma_resv_unlock(bo->base.resv); 1076 return ret; 1077 } 1078 1079 static int 1080 vm_access_ttm(struct vm_area_struct *area, unsigned long addr, 1081 void *buf, int len, int write) 1082 { 1083 struct drm_i915_gem_object *obj = 1084 i915_ttm_to_gem(area->vm_private_data); 1085 1086 if (i915_gem_object_is_readonly(obj) && write) 1087 return -EACCES; 1088 1089 return ttm_bo_vm_access(area, addr, buf, len, write); 1090 } 1091 1092 static void ttm_vm_open(struct vm_area_struct *vma) 1093 { 1094 struct drm_i915_gem_object *obj = 1095 i915_ttm_to_gem(vma->vm_private_data); 1096 1097 GEM_BUG_ON(!obj); 1098 i915_gem_object_get(obj); 1099 } 1100 1101 static void ttm_vm_close(struct vm_area_struct *vma) 1102 { 1103 struct drm_i915_gem_object *obj = 1104 i915_ttm_to_gem(vma->vm_private_data); 1105 1106 GEM_BUG_ON(!obj); 1107 i915_gem_object_put(obj); 1108 } 1109 1110 static const struct vm_operations_struct vm_ops_ttm = { 1111 .fault = vm_fault_ttm, 1112 .access = vm_access_ttm, 1113 .open = ttm_vm_open, 1114 .close = ttm_vm_close, 1115 }; 1116 1117 static u64 i915_ttm_mmap_offset(struct drm_i915_gem_object *obj) 1118 { 1119 /* The ttm_bo must be allocated with I915_BO_ALLOC_USER */ 1120 GEM_BUG_ON(!drm_mm_node_allocated(&obj->base.vma_node.vm_node)); 1121 1122 return drm_vma_node_offset_addr(&obj->base.vma_node); 1123 } 1124 1125 static void i915_ttm_unmap_virtual(struct drm_i915_gem_object *obj) 1126 { 1127 ttm_bo_unmap_virtual(i915_gem_to_ttm(obj)); 1128 } 1129 1130 static const struct drm_i915_gem_object_ops i915_gem_ttm_obj_ops = { 1131 .name = "i915_gem_object_ttm", 1132 .flags = I915_GEM_OBJECT_IS_SHRINKABLE | 1133 I915_GEM_OBJECT_SELF_MANAGED_SHRINK_LIST, 1134 1135 .get_pages = i915_ttm_get_pages, 1136 .put_pages = i915_ttm_put_pages, 1137 .truncate = i915_ttm_truncate, 1138 .shrink = i915_ttm_shrink, 1139 1140 .adjust_lru = i915_ttm_adjust_lru, 1141 .delayed_free = i915_ttm_delayed_free, 1142 .migrate = i915_ttm_migrate, 1143 1144 .mmap_offset = i915_ttm_mmap_offset, 1145 .unmap_virtual = i915_ttm_unmap_virtual, 1146 .mmap_ops = &vm_ops_ttm, 1147 }; 1148 1149 void i915_ttm_bo_destroy(struct ttm_buffer_object *bo) 1150 { 1151 struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo); 1152 1153 i915_gem_object_release_memory_region(obj); 1154 mutex_destroy(&obj->ttm.get_io_page.lock); 1155 1156 if (obj->ttm.created) { 1157 /* 1158 * We freely manage the shrinker LRU outide of the mm.pages life 1159 * cycle. As a result when destroying the object we should be 1160 * extra paranoid and ensure we remove it from the LRU, before 1161 * we free the object. 1162 * 1163 * Touching the ttm_shrinkable outside of the object lock here 1164 * should be safe now that the last GEM object ref was dropped. 1165 */ 1166 if (obj->mm.ttm_shrinkable) 1167 i915_gem_object_make_unshrinkable(obj); 1168 1169 i915_ttm_backup_free(obj); 1170 1171 /* This releases all gem object bindings to the backend. */ 1172 __i915_gem_free_object(obj); 1173 1174 call_rcu(&obj->rcu, __i915_gem_free_object_rcu); 1175 } else { 1176 __i915_gem_object_fini(obj); 1177 } 1178 } 1179 1180 /** 1181 * __i915_gem_ttm_object_init - Initialize a ttm-backed i915 gem object 1182 * @mem: The initial memory region for the object. 1183 * @obj: The gem object. 1184 * @size: Object size in bytes. 1185 * @flags: gem object flags. 1186 * 1187 * Return: 0 on success, negative error code on failure. 1188 */ 1189 int __i915_gem_ttm_object_init(struct intel_memory_region *mem, 1190 struct drm_i915_gem_object *obj, 1191 resource_size_t offset, 1192 resource_size_t size, 1193 resource_size_t page_size, 1194 unsigned int flags) 1195 { 1196 static struct lock_class_key lock_class; 1197 struct drm_i915_private *i915 = mem->i915; 1198 struct ttm_operation_ctx ctx = { 1199 .interruptible = true, 1200 .no_wait_gpu = false, 1201 }; 1202 enum ttm_bo_type bo_type; 1203 int ret; 1204 1205 drm_gem_private_object_init(&i915->drm, &obj->base, size); 1206 i915_gem_object_init(obj, &i915_gem_ttm_obj_ops, &lock_class, flags); 1207 1208 obj->bo_offset = offset; 1209 1210 /* Don't put on a region list until we're either locked or fully initialized. */ 1211 obj->mm.region = mem; 1212 INIT_LIST_HEAD(&obj->mm.region_link); 1213 1214 INIT_RADIX_TREE(&obj->ttm.get_io_page.radix, GFP_KERNEL | __GFP_NOWARN); 1215 mutex_init(&obj->ttm.get_io_page.lock); 1216 bo_type = (obj->flags & I915_BO_ALLOC_USER) ? ttm_bo_type_device : 1217 ttm_bo_type_kernel; 1218 1219 obj->base.vma_node.driver_private = i915_gem_to_ttm(obj); 1220 1221 /* Forcing the page size is kernel internal only */ 1222 GEM_BUG_ON(page_size && obj->mm.n_placements); 1223 1224 /* 1225 * Keep an extra shrink pin to prevent the object from being made 1226 * shrinkable too early. If the ttm_tt is ever allocated in shmem, we 1227 * drop the pin. The TTM backend manages the shrinker LRU itself, 1228 * outside of the normal mm.pages life cycle. 1229 */ 1230 i915_gem_object_make_unshrinkable(obj); 1231 1232 /* 1233 * If this function fails, it will call the destructor, but 1234 * our caller still owns the object. So no freeing in the 1235 * destructor until obj->ttm.created is true. 1236 * Similarly, in delayed_destroy, we can't call ttm_bo_put() 1237 * until successful initialization. 1238 */ 1239 ret = ttm_bo_init_reserved(&i915->bdev, i915_gem_to_ttm(obj), size, 1240 bo_type, &i915_sys_placement, 1241 page_size >> PAGE_SHIFT, 1242 &ctx, NULL, NULL, i915_ttm_bo_destroy); 1243 if (ret) 1244 return i915_ttm_err_to_gem(ret); 1245 1246 obj->ttm.created = true; 1247 i915_gem_object_release_memory_region(obj); 1248 i915_gem_object_init_memory_region(obj, mem); 1249 i915_ttm_adjust_domains_after_move(obj); 1250 i915_ttm_adjust_gem_after_move(obj); 1251 i915_gem_object_unlock(obj); 1252 1253 return 0; 1254 } 1255 1256 static const struct intel_memory_region_ops ttm_system_region_ops = { 1257 .init_object = __i915_gem_ttm_object_init, 1258 .release = intel_region_ttm_fini, 1259 }; 1260 1261 struct intel_memory_region * 1262 i915_gem_ttm_system_setup(struct drm_i915_private *i915, 1263 u16 type, u16 instance) 1264 { 1265 struct intel_memory_region *mr; 1266 1267 mr = intel_memory_region_create(i915, 0, 1268 totalram_pages() << PAGE_SHIFT, 1269 PAGE_SIZE, 0, 0, 1270 type, instance, 1271 &ttm_system_region_ops); 1272 if (IS_ERR(mr)) 1273 return mr; 1274 1275 intel_memory_region_set_name(mr, "system-ttm"); 1276 return mr; 1277 } 1278