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