File Index
Files and directories
Sign in
FILE

i915_gem_ttm.c

File preview with syntax highlighting.

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