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