i915_gem_ttm.c 34.4 KB
Newer Older
1 2 3 4 5
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2021 Intel Corporation
 */

6 7
#include <linux/shmem_fs.h>

8 9
#include <drm/ttm/ttm_bo_driver.h>
#include <drm/ttm/ttm_placement.h>
10
#include <drm/drm_buddy.h>
11 12

#include "i915_drv.h"
13
#include "i915_ttm_buddy_manager.h"
14 15 16
#include "intel_memory_region.h"
#include "intel_region_ttm.h"

17
#include "gem/i915_gem_mman.h"
18 19 20
#include "gem/i915_gem_object.h"
#include "gem/i915_gem_region.h"
#include "gem/i915_gem_ttm.h"
21
#include "gem/i915_gem_ttm_move.h"
22
#include "gem/i915_gem_ttm_pm.h"
23
#include "gt/intel_gpu_commands.h"
24 25 26 27

#define I915_TTM_PRIO_PURGE     0
#define I915_TTM_PRIO_NO_PAGES  1
#define I915_TTM_PRIO_HAS_PAGES 2
28
#define I915_TTM_PRIO_NEEDS_CPU_ACCESS 3
29

30 31 32 33 34
/*
 * Size of struct ttm_place vector in on-stack struct ttm_placement allocs
 */
#define I915_TTM_MAX_PLACEMENTS INTEL_REGION_UNKNOWN

35 36 37 38
/**
 * struct i915_ttm_tt - TTM page vector with additional private information
 * @ttm: The base TTM page vector.
 * @dev: The struct device used for dma mapping and unmapping.
39
 * @cached_rsgt: The cached scatter-gather table.
M
Matthew Auld 已提交
40 41
 * @is_shmem: Set if using shmem.
 * @filp: The shmem file, if using shmem backend.
42 43 44 45 46 47 48 49 50 51
 *
 * Note that DMA may be going on right up to the point where the page-
 * vector is unpopulated in delayed destroy. Hence keep the
 * scatter-gather table mapped and cached up to that point. This is
 * different from the cached gem object io scatter-gather table which
 * doesn't have an associated dma mapping.
 */
struct i915_ttm_tt {
	struct ttm_tt ttm;
	struct device *dev;
52
	struct i915_refct_sgt cached_rsgt;
M
Matthew Auld 已提交
53 54 55

	bool is_shmem;
	struct file *filp;
56 57
};

58 59 60 61 62
static const struct ttm_place sys_placement_flags = {
	.fpfn = 0,
	.lpfn = 0,
	.mem_type = I915_PL_SYSTEM,
	.flags = 0,
63 64 65 66
};

static struct ttm_placement i915_sys_placement = {
	.num_placement = 1,
67
	.placement = &sys_placement_flags,
68
	.num_busy_placement = 1,
69
	.busy_placement = &sys_placement_flags,
70 71
};

72 73 74 75 76 77 78 79 80 81 82 83 84 85
/**
 * i915_ttm_sys_placement - Return the struct ttm_placement to be
 * used for an object in system memory.
 *
 * Rather than making the struct extern, use this
 * function.
 *
 * Return: A pointer to a static variable for sys placement.
 */
struct ttm_placement *i915_ttm_sys_placement(void)
{
	return &i915_sys_placement;
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
static int i915_ttm_err_to_gem(int err)
{
	/* Fastpath */
	if (likely(!err))
		return 0;

	switch (err) {
	case -EBUSY:
		/*
		 * TTM likes to convert -EDEADLK to -EBUSY, and wants us to
		 * restart the operation, since we don't record the contending
		 * lock. We use -EAGAIN to restart.
		 */
		return -EAGAIN;
	case -ENOSPC:
		/*
		 * Memory type / region is full, and we can't evict.
		 * Except possibly system, that returns -ENOMEM;
		 */
		return -ENXIO;
	default:
		break;
	}

	return err;
}

113 114 115 116
static enum ttm_caching
i915_ttm_select_tt_caching(const struct drm_i915_gem_object *obj)
{
	/*
117 118 119
	 * Objects only allowed in system get cached cpu-mappings, or when
	 * evicting lmem-only buffers to system for swapping. Other objects get
	 * WC mapping for now. Even if in system.
120
	 */
121
	if (obj->mm.n_placements <= 1)
122 123 124 125 126 127 128
		return ttm_cached;

	return ttm_write_combined;
}

static void
i915_ttm_place_from_region(const struct intel_memory_region *mr,
129
			   struct ttm_place *place,
130 131
			   resource_size_t offset,
			   resource_size_t size,
132
			   unsigned int flags)
133 134 135
{
	memset(place, 0, sizeof(*place));
	place->mem_type = intel_region_to_ttm_type(mr);
136

137 138 139
	if (mr->type == INTEL_MEMORY_SYSTEM)
		return;

140
	if (flags & I915_BO_ALLOC_CONTIGUOUS)
141
		place->flags |= TTM_PL_FLAG_CONTIGUOUS;
142 143 144 145
	if (offset != I915_BO_INVALID_OFFSET) {
		place->fpfn = offset >> PAGE_SHIFT;
		place->lpfn = place->fpfn + (size >> PAGE_SHIFT);
	} else if (mr->io_size && mr->io_size < mr->total) {
146 147 148 149 150 151
		if (flags & I915_BO_ALLOC_GPU_ONLY) {
			place->flags |= TTM_PL_FLAG_TOPDOWN;
		} else {
			place->fpfn = 0;
			place->lpfn = mr->io_size >> PAGE_SHIFT;
		}
152
	}
153 154 155 156 157 158 159 160 161
}

static void
i915_ttm_placement_from_obj(const struct drm_i915_gem_object *obj,
			    struct ttm_place *requested,
			    struct ttm_place *busy,
			    struct ttm_placement *placement)
{
	unsigned int num_allowed = obj->mm.n_placements;
162
	unsigned int flags = obj->flags;
163 164 165 166
	unsigned int i;

	placement->num_placement = 1;
	i915_ttm_place_from_region(num_allowed ? obj->mm.placements[0] :
167 168
				   obj->mm.region, requested, obj->bo_offset,
				   obj->base.size, flags);
169 170 171 172

	/* Cache this on object? */
	placement->num_busy_placement = num_allowed;
	for (i = 0; i < placement->num_busy_placement; ++i)
173 174
		i915_ttm_place_from_region(obj->mm.placements[i], busy + i,
					   obj->bo_offset, obj->base.size, flags);
175 176 177 178 179 180 181 182 183 184

	if (num_allowed == 0) {
		*busy = *requested;
		placement->num_busy_placement = 1;
	}

	placement->placement = requested;
	placement->busy_placement = busy;
}

M
Matthew Auld 已提交
185 186 187 188 189 190 191 192
static int i915_ttm_tt_shmem_populate(struct ttm_device *bdev,
				      struct ttm_tt *ttm,
				      struct ttm_operation_ctx *ctx)
{
	struct drm_i915_private *i915 = container_of(bdev, typeof(*i915), bdev);
	struct intel_memory_region *mr = i915->mm.regions[INTEL_MEMORY_SYSTEM];
	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
	const unsigned int max_segment = i915_sg_segment_size();
193
	const size_t size = (size_t)ttm->num_pages << PAGE_SHIFT;
M
Matthew Auld 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	struct file *filp = i915_tt->filp;
	struct sgt_iter sgt_iter;
	struct sg_table *st;
	struct page *page;
	unsigned long i;
	int err;

	if (!filp) {
		struct address_space *mapping;
		gfp_t mask;

		filp = shmem_file_setup("i915-shmem-tt", size, VM_NORESERVE);
		if (IS_ERR(filp))
			return PTR_ERR(filp);

		mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;

		mapping = filp->f_mapping;
		mapping_set_gfp_mask(mapping, mask);
		GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));

		i915_tt->filp = filp;
	}

218 219 220 221 222
	st = &i915_tt->cached_rsgt.table;
	err = shmem_sg_alloc_table(i915, st, size, mr, filp->f_mapping,
				   max_segment);
	if (err)
		return err;
M
Matthew Auld 已提交
223

224 225 226
	err = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL,
			      DMA_ATTR_SKIP_CPU_SYNC);
	if (err)
M
Matthew Auld 已提交
227 228 229 230 231 232 233 234 235 236 237 238
		goto err_free_st;

	i = 0;
	for_each_sgt_page(page, sgt_iter, st)
		ttm->pages[i++] = page;

	if (ttm->page_flags & TTM_TT_FLAG_SWAPPED)
		ttm->page_flags &= ~TTM_TT_FLAG_SWAPPED;

	return 0;

err_free_st:
239 240
	shmem_sg_free_table(st, filp->f_mapping, false, false);

M
Matthew Auld 已提交
241 242 243 244 245 246 247
	return err;
}

static void i915_ttm_tt_shmem_unpopulate(struct ttm_tt *ttm)
{
	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
	bool backup = ttm->page_flags & TTM_TT_FLAG_SWAPPED;
248 249 250 251 252
	struct sg_table *st = &i915_tt->cached_rsgt.table;

	shmem_sg_free_table(st, file_inode(i915_tt->filp)->i_mapping,
			    backup, backup);
}
M
Matthew Auld 已提交
253

254 255 256 257 258
static void i915_ttm_tt_release(struct kref *ref)
{
	struct i915_ttm_tt *i915_tt =
		container_of(ref, typeof(*i915_tt), cached_rsgt.kref);
	struct sg_table *st = &i915_tt->cached_rsgt.table;
M
Matthew Auld 已提交
259

260 261 262
	GEM_WARN_ON(st->sgl);

	kfree(i915_tt);
M
Matthew Auld 已提交
263 264
}

265 266 267 268
static const struct i915_refct_sgt_ops tt_rsgt_ops = {
	.release = i915_ttm_tt_release
};

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
static inline bool
i915_gem_object_needs_ccs_pages(struct drm_i915_gem_object *obj)
{
	bool lmem_placement = false;
	int i;

	for (i = 0; i < obj->mm.n_placements; i++) {
		/* Compression is not allowed for the objects with smem placement */
		if (obj->mm.placements[i]->type == INTEL_MEMORY_SYSTEM)
			return false;
		if (!lmem_placement &&
		    obj->mm.placements[i]->type == INTEL_MEMORY_LOCAL)
			lmem_placement = true;
	}

	return lmem_placement;
}

287 288 289
static struct ttm_tt *i915_ttm_tt_create(struct ttm_buffer_object *bo,
					 uint32_t page_flags)
{
290 291
	struct drm_i915_private *i915 = container_of(bo->bdev, typeof(*i915),
						     bdev);
292 293 294
	struct ttm_resource_manager *man =
		ttm_manager_type(bo->bdev, bo->resource->mem_type);
	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
295
	unsigned long ccs_pages = 0;
296
	enum ttm_caching caching;
297 298 299
	struct i915_ttm_tt *i915_tt;
	int ret;

300 301 302
	if (!obj)
		return NULL;

303 304 305 306 307 308
	i915_tt = kzalloc(sizeof(*i915_tt), GFP_KERNEL);
	if (!i915_tt)
		return NULL;

	if (obj->flags & I915_BO_ALLOC_CPU_CLEAR &&
	    man->use_tt)
M
Matthew Auld 已提交
309
		page_flags |= TTM_TT_FLAG_ZERO_ALLOC;
310

311
	caching = i915_ttm_select_tt_caching(obj);
M
Matthew Auld 已提交
312 313 314 315
	if (i915_gem_object_is_shrinkable(obj) && caching == ttm_cached) {
		page_flags |= TTM_TT_FLAG_EXTERNAL |
			      TTM_TT_FLAG_EXTERNAL_MAPPABLE;
		i915_tt->is_shmem = true;
316 317
	}

318 319 320 321 322 323
	if (HAS_FLAT_CCS(i915) && i915_gem_object_needs_ccs_pages(obj))
		ccs_pages = DIV_ROUND_UP(DIV_ROUND_UP(bo->base.size,
						      NUM_BYTES_PER_CCS_BYTE),
					 PAGE_SIZE);

	ret = ttm_tt_init(&i915_tt->ttm, bo, page_flags, caching, ccs_pages);
M
Matthew Auld 已提交
324 325 326
	if (ret)
		goto err_free;

327 328 329
	__i915_refct_sgt_init(&i915_tt->cached_rsgt, bo->base.size,
			      &tt_rsgt_ops);

330 331 332
	i915_tt->dev = obj->base.dev->dev;

	return &i915_tt->ttm;
M
Matthew Auld 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

err_free:
	kfree(i915_tt);
	return NULL;
}

static int i915_ttm_tt_populate(struct ttm_device *bdev,
				struct ttm_tt *ttm,
				struct ttm_operation_ctx *ctx)
{
	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);

	if (i915_tt->is_shmem)
		return i915_ttm_tt_shmem_populate(bdev, ttm, ctx);

	return ttm_pool_alloc(&bdev->pool, ttm, ctx);
349 350 351 352 353
}

static void i915_ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm)
{
	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
354 355 356 357
	struct sg_table *st = &i915_tt->cached_rsgt.table;

	if (st->sgl)
		dma_unmap_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0);
358

M
Matthew Auld 已提交
359 360 361
	if (i915_tt->is_shmem) {
		i915_ttm_tt_shmem_unpopulate(ttm);
	} else {
362
		sg_free_table(st);
M
Matthew Auld 已提交
363
		ttm_pool_free(&bdev->pool, ttm);
364 365 366 367 368 369 370
	}
}

static void i915_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
{
	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);

M
Matthew Auld 已提交
371 372 373
	if (i915_tt->filp)
		fput(i915_tt->filp);

374
	ttm_tt_fini(ttm);
375
	i915_refct_sgt_put(&i915_tt->cached_rsgt);
376 377 378 379 380 381
}

static bool i915_ttm_eviction_valuable(struct ttm_buffer_object *bo,
				       const struct ttm_place *place)
{
	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
382
	struct ttm_resource *res = bo->resource;
383

384 385 386
	if (!obj)
		return false;

M
Matthew Auld 已提交
387 388 389 390 391 392
	/*
	 * EXTERNAL objects should never be swapped out by TTM, instead we need
	 * to handle that ourselves. TTM will already skip such objects for us,
	 * but we would like to avoid grabbing locks for no good reason.
	 */
	if (bo->ttm && bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
393
		return false;
M
Matthew Auld 已提交
394

395
	/* Will do for now. Our pinned objects are still on TTM's LRU lists */
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
	if (!i915_gem_object_evictable(obj))
		return false;

	switch (res->mem_type) {
	case I915_PL_LMEM0: {
		struct ttm_resource_manager *man =
			ttm_manager_type(bo->bdev, res->mem_type);
		struct i915_ttm_buddy_resource *bman_res =
			to_ttm_buddy_resource(res);
		struct drm_buddy *mm = bman_res->mm;
		struct drm_buddy_block *block;

		if (!place->fpfn && !place->lpfn)
			return true;

		GEM_BUG_ON(!place->lpfn);

		/*
		 * If we just want something mappable then we can quickly check
		 * if the current victim resource is using any of the CPU
		 * visible portion.
		 */
		if (!place->fpfn &&
		    place->lpfn == i915_ttm_buddy_man_visible_size(man))
			return bman_res->used_visible_size > 0;

		/* Real range allocation */
		list_for_each_entry(block, &bman_res->blocks, link) {
			unsigned long fpfn =
				drm_buddy_block_offset(block) >> PAGE_SHIFT;
			unsigned long lpfn = fpfn +
				(drm_buddy_block_size(mm, block) >> PAGE_SHIFT);

			if (place->fpfn < lpfn && place->lpfn > fpfn)
				return true;
		}
		return false;
	} default:
		break;
	}

	return true;
438 439 440 441 442 443 444 445
}

static void i915_ttm_evict_flags(struct ttm_buffer_object *bo,
				 struct ttm_placement *placement)
{
	*placement = i915_sys_placement;
}

446 447 448 449 450 451 452 453
/**
 * i915_ttm_free_cached_io_rsgt - Free object cached LMEM information
 * @obj: The GEM object
 * This function frees any LMEM-related information that is cached on
 * the object. For example the radix tree for fast page lookup and the
 * cached refcounted sg-table
 */
void i915_ttm_free_cached_io_rsgt(struct drm_i915_gem_object *obj)
454
{
455 456 457
	struct radix_tree_iter iter;
	void __rcu **slot;

458
	if (!obj->ttm.cached_io_rsgt)
459 460 461 462 463 464 465
		return;

	rcu_read_lock();
	radix_tree_for_each_slot(slot, &obj->ttm.get_io_page.radix, &iter, 0)
		radix_tree_delete(&obj->ttm.get_io_page.radix, iter.index);
	rcu_read_unlock();

466 467
	i915_refct_sgt_put(obj->ttm.cached_io_rsgt);
	obj->ttm.cached_io_rsgt = NULL;
468 469
}

470 471 472 473 474 475 476 477 478 479
/**
 * i915_ttm_purge - Clear an object of its memory
 * @obj: The object
 *
 * This function is called to clear an object of it's memory when it is
 * marked as not needed anymore.
 *
 * Return: 0 on success, negative error code on failure.
 */
int i915_ttm_purge(struct drm_i915_gem_object *obj)
480 481
{
	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
M
Matthew Auld 已提交
482 483
	struct i915_ttm_tt *i915_tt =
		container_of(bo->ttm, typeof(*i915_tt), ttm);
484 485 486 487 488 489 490 491
	struct ttm_operation_ctx ctx = {
		.interruptible = true,
		.no_wait_gpu = false,
	};
	struct ttm_placement place = {};
	int ret;

	if (obj->mm.madv == __I915_MADV_PURGED)
M
Matthew Auld 已提交
492
		return 0;
493 494

	ret = ttm_bo_validate(bo, &place, &ctx);
M
Matthew Auld 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507
	if (ret)
		return ret;

	if (bo->ttm && i915_tt->filp) {
		/*
		 * The below fput(which eventually calls shmem_truncate) might
		 * be delayed by worker, so when directly called to purge the
		 * pages(like by the shrinker) we should try to be more
		 * aggressive and release the pages immediately.
		 */
		shmem_truncate_range(file_inode(i915_tt->filp),
				     0, (loff_t)-1);
		fput(fetch_and_zero(&i915_tt->filp));
508
	}
M
Matthew Auld 已提交
509 510 511 512

	obj->write_domain = 0;
	obj->read_domains = 0;
	i915_ttm_adjust_gem_after_move(obj);
513
	i915_ttm_free_cached_io_rsgt(obj);
M
Matthew Auld 已提交
514
	obj->mm.madv = __I915_MADV_PURGED;
515

M
Matthew Auld 已提交
516 517 518
	return 0;
}

519
static int i915_ttm_shrink(struct drm_i915_gem_object *obj, unsigned int flags)
M
Matthew Auld 已提交
520 521 522 523 524 525
{
	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
	struct i915_ttm_tt *i915_tt =
		container_of(bo->ttm, typeof(*i915_tt), ttm);
	struct ttm_operation_ctx ctx = {
		.interruptible = true,
526
		.no_wait_gpu = flags & I915_GEM_OBJECT_SHRINK_NO_GPU_WAIT,
M
Matthew Auld 已提交
527 528 529 530 531 532 533 534 535 536 537 538
	};
	struct ttm_placement place = {};
	int ret;

	if (!bo->ttm || bo->resource->mem_type != TTM_PL_SYSTEM)
		return 0;

	GEM_BUG_ON(!i915_tt->is_shmem);

	if (!i915_tt->filp)
		return 0;

539 540 541 542
	ret = ttm_bo_wait_ctx(bo, &ctx);
	if (ret)
		return ret;

M
Matthew Auld 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	switch (obj->mm.madv) {
	case I915_MADV_DONTNEED:
		return i915_ttm_purge(obj);
	case __I915_MADV_PURGED:
		return 0;
	}

	if (bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED)
		return 0;

	bo->ttm->page_flags |= TTM_TT_FLAG_SWAPPED;
	ret = ttm_bo_validate(bo, &place, &ctx);
	if (ret) {
		bo->ttm->page_flags &= ~TTM_TT_FLAG_SWAPPED;
		return ret;
	}

560
	if (flags & I915_GEM_OBJECT_SHRINK_WRITEBACK)
M
Matthew Auld 已提交
561 562 563
		__shmem_writeback(obj->base.size, i915_tt->filp->f_mapping);

	return 0;
564 565 566 567 568 569 570
}

static void i915_ttm_delete_mem_notify(struct ttm_buffer_object *bo)
{
	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);

	if (likely(obj)) {
571
		__i915_gem_object_pages_fini(obj);
572
		i915_ttm_free_cached_io_rsgt(obj);
573 574 575
	}
}

576
static struct i915_refct_sgt *i915_ttm_tt_get_st(struct ttm_tt *ttm)
577 578 579 580 581
{
	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
	struct sg_table *st;
	int ret;

582 583
	if (i915_tt->cached_rsgt.table.sgl)
		return i915_refct_sgt_get(&i915_tt->cached_rsgt);
584

585
	st = &i915_tt->cached_rsgt.table;
586 587 588 589 590
	ret = sg_alloc_table_from_pages_segment(st,
			ttm->pages, ttm->num_pages,
			0, (unsigned long)ttm->num_pages << PAGE_SHIFT,
			i915_sg_segment_size(), GFP_KERNEL);
	if (ret) {
591
		st->sgl = NULL;
592
		return ERR_PTR(ret);
593 594 595 596 597 598 599 600
	}

	ret = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0);
	if (ret) {
		sg_free_table(st);
		return ERR_PTR(ret);
	}

601
	return i915_refct_sgt_get(&i915_tt->cached_rsgt);
602 603
}

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
/**
 * i915_ttm_resource_get_st - Get a refcounted sg-table pointing to the
 * resource memory
 * @obj: The GEM object used for sg-table caching
 * @res: The struct ttm_resource for which an sg-table is requested.
 *
 * This function returns a refcounted sg-table representing the memory
 * pointed to by @res. If @res is the object's current resource it may also
 * cache the sg_table on the object or attempt to access an already cached
 * sg-table. The refcounted sg-table needs to be put when no-longer in use.
 *
 * Return: A valid pointer to a struct i915_refct_sgt or error pointer on
 * failure.
 */
struct i915_refct_sgt *
619 620 621 622
i915_ttm_resource_get_st(struct drm_i915_gem_object *obj,
			 struct ttm_resource *res)
{
	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
623
	u64 page_alignment;
624

625
	if (!i915_ttm_gtt_binds_lmem(res))
626 627
		return i915_ttm_tt_get_st(bo->ttm);

628 629 630 631
	page_alignment = bo->page_alignment << PAGE_SHIFT;
	if (!page_alignment)
		page_alignment = obj->mm.region->min_page_size;

632 633 634 635
	/*
	 * If CPU mapping differs, we need to add the ttm_tt pages to
	 * the resulting st. Might make sense for GGTT.
	 */
636
	GEM_WARN_ON(!i915_ttm_cpu_maps_iomem(res));
637 638 639 640 641
	if (bo->resource == res) {
		if (!obj->ttm.cached_io_rsgt) {
			struct i915_refct_sgt *rsgt;

			rsgt = intel_region_ttm_resource_to_rsgt(obj->mm.region,
642 643
								 res,
								 page_alignment);
644 645 646 647 648 649 650 651
			if (IS_ERR(rsgt))
				return rsgt;

			obj->ttm.cached_io_rsgt = rsgt;
		}
		return i915_refct_sgt_get(obj->ttm.cached_io_rsgt);
	}

652 653
	return intel_region_ttm_resource_to_rsgt(obj->mm.region, res,
						 page_alignment);
654 655
}

656 657 658 659 660 661 662 663 664 665 666 667 668 669
static int i915_ttm_truncate(struct drm_i915_gem_object *obj)
{
	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
	int err;

	WARN_ON_ONCE(obj->mm.madv == I915_MADV_WILLNEED);

	err = i915_ttm_move_notify(bo);
	if (err)
		return err;

	return i915_ttm_purge(obj);
}

670
static void i915_ttm_swap_notify(struct ttm_buffer_object *bo)
671 672
{
	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
673 674 675 676
	int ret;

	if (!obj)
		return;
677

678
	ret = i915_ttm_move_notify(bo);
679 680 681
	GEM_WARN_ON(ret);
	GEM_WARN_ON(obj->ttm.cached_io_rsgt);
	if (!ret && obj->mm.madv != I915_MADV_WILLNEED)
682 683 684
		i915_ttm_purge(obj);
}

685 686 687 688 689 690 691 692 693 694
static bool i915_ttm_resource_mappable(struct ttm_resource *res)
{
	struct i915_ttm_buddy_resource *bman_res = to_ttm_buddy_resource(res);

	if (!i915_ttm_cpu_maps_iomem(res))
		return true;

	return bman_res->used_visible_size == bman_res->base.num_pages;
}

695 696
static int i915_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
{
697
	if (!i915_ttm_cpu_maps_iomem(mem))
698 699
		return 0;

700 701 702
	if (!i915_ttm_resource_mappable(mem))
		return -EINVAL;

703 704 705 706 707 708 709 710 711 712 713
	mem->bus.caching = ttm_write_combined;
	mem->bus.is_iomem = true;

	return 0;
}

static unsigned long i915_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
					 unsigned long page_offset)
{
	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
	struct scatterlist *sg;
714
	unsigned long base;
715 716
	unsigned int ofs;

717
	GEM_BUG_ON(!obj);
718 719
	GEM_WARN_ON(bo->ttm);

720
	base = obj->mm.region->iomap.base - obj->mm.region->region.start;
721
	sg = __i915_gem_object_get_sg(obj, &obj->ttm.get_io_page, page_offset, &ofs, true);
722 723 724 725

	return ((base + sg_dma_address(sg)) >> PAGE_SHIFT) + ofs;
}

726 727 728 729
/*
 * All callbacks need to take care not to downcast a struct ttm_buffer_object
 * without checking its subclass, since it might be a TTM ghost object.
 */
730 731
static struct ttm_device_funcs i915_ttm_bo_driver = {
	.ttm_tt_create = i915_ttm_tt_create,
M
Matthew Auld 已提交
732
	.ttm_tt_populate = i915_ttm_tt_populate,
733 734 735 736 737 738 739
	.ttm_tt_unpopulate = i915_ttm_tt_unpopulate,
	.ttm_tt_destroy = i915_ttm_tt_destroy,
	.eviction_valuable = i915_ttm_eviction_valuable,
	.evict_flags = i915_ttm_evict_flags,
	.move = i915_ttm_move,
	.swap_notify = i915_ttm_swap_notify,
	.delete_mem_notify = i915_ttm_delete_mem_notify,
740 741
	.io_mem_reserve = i915_ttm_io_mem_reserve,
	.io_mem_pfn = i915_ttm_io_mem_pfn,
742 743 744 745 746 747 748 749 750 751 752 753
};

/**
 * i915_ttm_driver - Return a pointer to the TTM device funcs
 *
 * Return: Pointer to statically allocated TTM device funcs.
 */
struct ttm_device_funcs *i915_ttm_driver(void)
{
	return &i915_ttm_bo_driver;
}

754 755
static int __i915_ttm_get_pages(struct drm_i915_gem_object *obj,
				struct ttm_placement *placement)
756 757 758 759 760 761
{
	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
	struct ttm_operation_ctx ctx = {
		.interruptible = true,
		.no_wait_gpu = false,
	};
762
	int real_num_busy;
763 764
	int ret;

765
	/* First try only the requested placement. No eviction. */
766 767
	real_num_busy = fetch_and_zero(&placement->num_busy_placement);
	ret = ttm_bo_validate(bo, placement, &ctx);
768 769 770 771 772 773 774 775 776
	if (ret) {
		ret = i915_ttm_err_to_gem(ret);
		/*
		 * Anything that wants to restart the operation gets to
		 * do that.
		 */
		if (ret == -EDEADLK || ret == -EINTR || ret == -ERESTARTSYS ||
		    ret == -EAGAIN)
			return ret;
777

778 779 780 781
		/*
		 * If the initial attempt fails, allow all accepted placements,
		 * evicting if necessary.
		 */
782 783
		placement->num_busy_placement = real_num_busy;
		ret = ttm_bo_validate(bo, placement, &ctx);
784 785 786
		if (ret)
			return i915_ttm_err_to_gem(ret);
	}
787

788 789 790 791 792 793 794 795 796
	if (bo->ttm && !ttm_tt_is_populated(bo->ttm)) {
		ret = ttm_tt_populate(bo->bdev, bo->ttm, &ctx);
		if (ret)
			return ret;

		i915_ttm_adjust_domains_after_move(obj);
		i915_ttm_adjust_gem_after_move(obj);
	}

797
	if (!i915_gem_object_has_pages(obj)) {
798 799 800 801 802
		struct i915_refct_sgt *rsgt =
			i915_ttm_resource_get_st(obj, bo->resource);

		if (IS_ERR(rsgt))
			return PTR_ERR(rsgt);
803

804 805 806 807
		GEM_BUG_ON(obj->mm.rsgt);
		obj->mm.rsgt = rsgt;
		__i915_gem_object_set_pages(obj, &rsgt->table,
					    i915_sg_dma_sizes(rsgt->table.sgl));
808
	}
809

810
	GEM_BUG_ON(bo->ttm && ((obj->base.size >> PAGE_SHIFT) < bo->ttm->num_pages));
811
	i915_ttm_adjust_lru(obj);
812 813 814
	return ret;
}

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
static int i915_ttm_get_pages(struct drm_i915_gem_object *obj)
{
	struct ttm_place requested, busy[I915_TTM_MAX_PLACEMENTS];
	struct ttm_placement placement;

	GEM_BUG_ON(obj->mm.n_placements > I915_TTM_MAX_PLACEMENTS);

	/* Move to the requested placement. */
	i915_ttm_placement_from_obj(obj, &requested, busy, &placement);

	return __i915_ttm_get_pages(obj, &placement);
}

/**
 * DOC: Migration vs eviction
 *
 * GEM migration may not be the same as TTM migration / eviction. If
 * the TTM core decides to evict an object it may be evicted to a
 * TTM memory type that is not in the object's allowable GEM regions, or
 * in fact theoretically to a TTM memory type that doesn't correspond to
 * a GEM memory region. In that case the object's GEM region is not
 * updated, and the data is migrated back to the GEM region at
 * get_pages time. TTM may however set up CPU ptes to the object even
 * when it is evicted.
 * Gem forced migration using the i915_ttm_migrate() op, is allowed even
 * to regions that are not in the object's list of allowable placements.
 */
842 843 844
static int __i915_ttm_migrate(struct drm_i915_gem_object *obj,
			      struct intel_memory_region *mr,
			      unsigned int flags)
845 846 847 848 849
{
	struct ttm_place requested;
	struct ttm_placement placement;
	int ret;

850 851
	i915_ttm_place_from_region(mr, &requested, obj->bo_offset,
				   obj->base.size, flags);
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
	placement.num_placement = 1;
	placement.num_busy_placement = 1;
	placement.placement = &requested;
	placement.busy_placement = &requested;

	ret = __i915_ttm_get_pages(obj, &placement);
	if (ret)
		return ret;

	/*
	 * Reinitialize the region bindings. This is primarily
	 * required for objects where the new region is not in
	 * its allowable placements.
	 */
	if (obj->mm.region != mr) {
		i915_gem_object_release_memory_region(obj);
		i915_gem_object_init_memory_region(obj, mr);
	}

	return 0;
}

874 875 876 877 878 879
static int i915_ttm_migrate(struct drm_i915_gem_object *obj,
			    struct intel_memory_region *mr)
{
	return __i915_ttm_migrate(obj, mr, obj->flags);
}

880 881 882 883 884 885 886 887 888 889
static void i915_ttm_put_pages(struct drm_i915_gem_object *obj,
			       struct sg_table *st)
{
	/*
	 * We're currently not called from a shrinker, so put_pages()
	 * typically means the object is about to destroyed, or called
	 * from move_notify(). So just avoid doing much for now.
	 * If the object is not destroyed next, The TTM eviction logic
	 * and shrinkers will move it out if needed.
	 */
890 891 892

	if (obj->mm.rsgt)
		i915_refct_sgt_put(fetch_and_zero(&obj->mm.rsgt));
893 894
}

895 896 897 898 899
/**
 * i915_ttm_adjust_lru - Adjust an object's position on relevant LRU lists.
 * @obj: The object
 */
void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj)
900 901
{
	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
M
Matthew Auld 已提交
902 903
	struct i915_ttm_tt *i915_tt =
		container_of(bo->ttm, typeof(*i915_tt), ttm);
904 905
	bool shrinkable =
		bo->ttm && i915_tt->filp && ttm_tt_is_populated(bo->ttm);
906 907 908 909 910 911 912 913

	/*
	 * Don't manipulate the TTM LRUs while in TTM bo destruction.
	 * We're called through i915_ttm_delete_mem_notify().
	 */
	if (!kref_read(&bo->kref))
		return;

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
	/*
	 * We skip managing the shrinker LRU in set_pages() and just manage
	 * everything here. This does at least solve the issue with having
	 * temporary shmem mappings(like with evicted lmem) not being visible to
	 * the shrinker. Only our shmem objects are shrinkable, everything else
	 * we keep as unshrinkable.
	 *
	 * To make sure everything plays nice we keep an extra shrink pin in TTM
	 * if the underlying pages are not currently shrinkable. Once we release
	 * our pin, like when the pages are moved to shmem, the pages will then
	 * be added to the shrinker LRU, assuming the caller isn't also holding
	 * a pin.
	 *
	 * TODO: consider maybe also bumping the shrinker list here when we have
	 * already unpinned it, which should give us something more like an LRU.
929 930 931 932 933 934 935 936
	 *
	 * TODO: There is a small window of opportunity for this function to
	 * get called from eviction after we've dropped the last GEM refcount,
	 * but before the TTM deleted flag is set on the object. Avoid
	 * adjusting the shrinker list in such cases, since the object is
	 * not available to the shrinker anyway due to its zero refcount.
	 * To fix this properly we should move to a TTM shrinker LRU list for
	 * these objects.
937
	 */
938 939 940 941 942 943 944 945 946 947 948 949
	if (kref_get_unless_zero(&obj->base.refcount)) {
		if (shrinkable != obj->mm.ttm_shrinkable) {
			if (shrinkable) {
				if (obj->mm.madv == I915_MADV_WILLNEED)
					__i915_gem_object_make_shrinkable(obj);
				else
					__i915_gem_object_make_purgeable(obj);
			} else {
				i915_gem_object_make_unshrinkable(obj);
			}

			obj->mm.ttm_shrinkable = shrinkable;
950
		}
951
		i915_gem_object_put(obj);
952 953
	}

954 955 956 957
	/*
	 * Put on the correct LRU list depending on the MADV status
	 */
	spin_lock(&bo->bdev->lru_lock);
958
	if (shrinkable) {
M
Matthew Auld 已提交
959 960 961
		/* Try to keep shmem_tt from being considered for shrinking. */
		bo->priority = TTM_MAX_BO_PRIORITY - 1;
	} else if (obj->mm.madv != I915_MADV_WILLNEED) {
962 963
		bo->priority = I915_TTM_PRIO_PURGE;
	} else if (!i915_gem_object_has_pages(obj)) {
964
		bo->priority = I915_TTM_PRIO_NO_PAGES;
965
	} else {
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
		struct ttm_resource_manager *man =
			ttm_manager_type(bo->bdev, bo->resource->mem_type);

		/*
		 * If we need to place an LMEM resource which doesn't need CPU
		 * access then we should try not to victimize mappable objects
		 * first, since we likely end up stealing more of the mappable
		 * portion. And likewise when we try to find space for a mappble
		 * object, we know not to ever victimize objects that don't
		 * occupy any mappable pages.
		 */
		if (i915_ttm_cpu_maps_iomem(bo->resource) &&
		    i915_ttm_buddy_man_visible_size(man) < man->size &&
		    !(obj->flags & I915_BO_ALLOC_GPU_ONLY))
			bo->priority = I915_TTM_PRIO_NEEDS_CPU_ACCESS;
		else
			bo->priority = I915_TTM_PRIO_HAS_PAGES;
983 984
	}

985
	ttm_bo_move_to_lru_tail(bo);
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
	spin_unlock(&bo->bdev->lru_lock);
}

/*
 * TTM-backed gem object destruction requires some clarification.
 * Basically we have two possibilities here. We can either rely on the
 * i915 delayed destruction and put the TTM object when the object
 * is idle. This would be detected by TTM which would bypass the
 * TTM delayed destroy handling. The other approach is to put the TTM
 * object early and rely on the TTM destroyed handling, and then free
 * the leftover parts of the GEM object once TTM's destroyed list handling is
 * complete. For now, we rely on the latter for two reasons:
 * a) TTM can evict an object even when it's on the delayed destroy list,
 * which in theory allows for complete eviction.
 * b) There is work going on in TTM to allow freeing an object even when
 * it's not idle, and using the TTM destroyed list handling could help us
 * benefit from that.
 */
static void i915_ttm_delayed_free(struct drm_i915_gem_object *obj)
{
1006 1007 1008
	GEM_BUG_ON(!obj->ttm.created);

	ttm_bo_put(i915_gem_to_ttm(obj));
1009 1010
}

1011 1012 1013
static vm_fault_t vm_fault_ttm(struct vm_fault *vmf)
{
	struct vm_area_struct *area = vmf->vma;
1014
	struct ttm_buffer_object *bo = area->vm_private_data;
1015
	struct drm_device *dev = bo->base.dev;
1016
	struct drm_i915_gem_object *obj;
1017 1018
	vm_fault_t ret;
	int idx;
1019

1020 1021 1022 1023
	obj = i915_ttm_to_gem(bo);
	if (!obj)
		return VM_FAULT_SIGBUS;

1024 1025 1026 1027 1028
	/* Sanity check that we allow writing into this object */
	if (unlikely(i915_gem_object_is_readonly(obj) &&
		     area->vm_flags & VM_WRITE))
		return VM_FAULT_SIGBUS;

1029 1030 1031 1032
	ret = ttm_bo_vm_reserve(bo, vmf);
	if (ret)
		return ret;

1033 1034 1035 1036 1037
	if (obj->mm.madv != I915_MADV_WILLNEED) {
		dma_resv_unlock(bo->base.resv);
		return VM_FAULT_SIGBUS;
	}

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	if (!i915_ttm_resource_mappable(bo->resource)) {
		int err = -ENODEV;
		int i;

		for (i = 0; i < obj->mm.n_placements; i++) {
			struct intel_memory_region *mr = obj->mm.placements[i];
			unsigned int flags;

			if (!mr->io_size && mr->type != INTEL_MEMORY_SYSTEM)
				continue;

			flags = obj->flags;
			flags &= ~I915_BO_ALLOC_GPU_ONLY;
			err = __i915_ttm_migrate(obj, mr, flags);
			if (!err)
				break;
		}

		if (err) {
			drm_dbg(dev, "Unable to make resource CPU accessible\n");
			dma_resv_unlock(bo->base.resv);
			return VM_FAULT_SIGBUS;
		}
	}

1063 1064
	if (drm_dev_enter(dev, &idx)) {
		ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
M
Matthew Auld 已提交
1065
					       TTM_BO_VM_NUM_PREFAULT);
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
		drm_dev_exit(idx);
	} else {
		ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
	}
	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
		return ret;

	i915_ttm_adjust_lru(obj);

	dma_resv_unlock(bo->base.resv);
	return ret;
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
}

static int
vm_access_ttm(struct vm_area_struct *area, unsigned long addr,
	      void *buf, int len, int write)
{
	struct drm_i915_gem_object *obj =
		i915_ttm_to_gem(area->vm_private_data);

	if (i915_gem_object_is_readonly(obj) && write)
		return -EACCES;

	return ttm_bo_vm_access(area, addr, buf, len, write);
}

static void ttm_vm_open(struct vm_area_struct *vma)
{
	struct drm_i915_gem_object *obj =
		i915_ttm_to_gem(vma->vm_private_data);

	GEM_BUG_ON(!obj);
	i915_gem_object_get(obj);
}

static void ttm_vm_close(struct vm_area_struct *vma)
{
	struct drm_i915_gem_object *obj =
		i915_ttm_to_gem(vma->vm_private_data);

	GEM_BUG_ON(!obj);
	i915_gem_object_put(obj);
}

static const struct vm_operations_struct vm_ops_ttm = {
	.fault = vm_fault_ttm,
	.access = vm_access_ttm,
	.open = ttm_vm_open,
	.close = ttm_vm_close,
};

static u64 i915_ttm_mmap_offset(struct drm_i915_gem_object *obj)
{
	/* The ttm_bo must be allocated with I915_BO_ALLOC_USER */
	GEM_BUG_ON(!drm_mm_node_allocated(&obj->base.vma_node.vm_node));

	return drm_vma_node_offset_addr(&obj->base.vma_node);
}

1125 1126 1127 1128 1129
static void i915_ttm_unmap_virtual(struct drm_i915_gem_object *obj)
{
	ttm_bo_unmap_virtual(i915_gem_to_ttm(obj));
}

M
Matthew Auld 已提交
1130
static const struct drm_i915_gem_object_ops i915_gem_ttm_obj_ops = {
1131
	.name = "i915_gem_object_ttm",
1132 1133
	.flags = I915_GEM_OBJECT_IS_SHRINKABLE |
		 I915_GEM_OBJECT_SELF_MANAGED_SHRINK_LIST,
1134 1135 1136

	.get_pages = i915_ttm_get_pages,
	.put_pages = i915_ttm_put_pages,
1137
	.truncate = i915_ttm_truncate,
1138
	.shrink = i915_ttm_shrink,
M
Matthew Auld 已提交
1139

1140 1141
	.adjust_lru = i915_ttm_adjust_lru,
	.delayed_free = i915_ttm_delayed_free,
1142
	.migrate = i915_ttm_migrate,
M
Matthew Auld 已提交
1143

1144
	.mmap_offset = i915_ttm_mmap_offset,
1145
	.unmap_virtual = i915_ttm_unmap_virtual,
1146
	.mmap_ops = &vm_ops_ttm,
1147 1148 1149 1150 1151 1152 1153
};

void i915_ttm_bo_destroy(struct ttm_buffer_object *bo)
{
	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);

	i915_gem_object_release_memory_region(obj);
1154
	mutex_destroy(&obj->ttm.get_io_page.lock);
1155

1156
	if (obj->ttm.created) {
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
		/*
		 * We freely manage the shrinker LRU outide of the mm.pages life
		 * cycle. As a result when destroying the object we should be
		 * extra paranoid and ensure we remove it from the LRU, before
		 * we free the object.
		 *
		 * Touching the ttm_shrinkable outside of the object lock here
		 * should be safe now that the last GEM object ref was dropped.
		 */
		if (obj->mm.ttm_shrinkable)
			i915_gem_object_make_unshrinkable(obj);

1169 1170 1171 1172 1173
		i915_ttm_backup_free(obj);

		/* This releases all gem object bindings to the backend. */
		__i915_gem_free_object(obj);

1174
		call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
1175 1176 1177
	} else {
		__i915_gem_object_fini(obj);
	}
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
}

/**
 * __i915_gem_ttm_object_init - Initialize a ttm-backed i915 gem object
 * @mem: The initial memory region for the object.
 * @obj: The gem object.
 * @size: Object size in bytes.
 * @flags: gem object flags.
 *
 * Return: 0 on success, negative error code on failure.
 */
int __i915_gem_ttm_object_init(struct intel_memory_region *mem,
			       struct drm_i915_gem_object *obj,
1191
			       resource_size_t offset,
1192
			       resource_size_t size,
1193
			       resource_size_t page_size,
1194 1195 1196 1197
			       unsigned int flags)
{
	static struct lock_class_key lock_class;
	struct drm_i915_private *i915 = mem->i915;
1198 1199 1200 1201
	struct ttm_operation_ctx ctx = {
		.interruptible = true,
		.no_wait_gpu = false,
	};
1202 1203 1204 1205 1206
	enum ttm_bo_type bo_type;
	int ret;

	drm_gem_private_object_init(&i915->drm, &obj->base, size);
	i915_gem_object_init(obj, &i915_gem_ttm_obj_ops, &lock_class, flags);
1207

1208 1209
	obj->bo_offset = offset;

1210
	/* Don't put on a region list until we're either locked or fully initialized. */
1211
	obj->mm.region = mem;
1212 1213
	INIT_LIST_HEAD(&obj->mm.region_link);

1214 1215
	INIT_RADIX_TREE(&obj->ttm.get_io_page.radix, GFP_KERNEL | __GFP_NOWARN);
	mutex_init(&obj->ttm.get_io_page.lock);
1216 1217 1218
	bo_type = (obj->flags & I915_BO_ALLOC_USER) ? ttm_bo_type_device :
		ttm_bo_type_kernel;

1219 1220
	obj->base.vma_node.driver_private = i915_gem_to_ttm(obj);

1221 1222 1223
	/* Forcing the page size is kernel internal only */
	GEM_BUG_ON(page_size && obj->mm.n_placements);

1224 1225 1226 1227 1228 1229 1230 1231
	/*
	 * Keep an extra shrink pin to prevent the object from being made
	 * shrinkable too early. If the ttm_tt is ever allocated in shmem, we
	 * drop the pin. The TTM backend manages the shrinker LRU itself,
	 * outside of the normal mm.pages life cycle.
	 */
	i915_gem_object_make_unshrinkable(obj);

1232 1233 1234 1235 1236 1237 1238
	/*
	 * If this function fails, it will call the destructor, but
	 * our caller still owns the object. So no freeing in the
	 * destructor until obj->ttm.created is true.
	 * Similarly, in delayed_destroy, we can't call ttm_bo_put()
	 * until successful initialization.
	 */
1239 1240
	ret = ttm_bo_init_reserved(&i915->bdev, i915_gem_to_ttm(obj), size,
				   bo_type, &i915_sys_placement,
1241
				   page_size >> PAGE_SHIFT,
1242 1243 1244
				   &ctx, NULL, NULL, i915_ttm_bo_destroy);
	if (ret)
		return i915_ttm_err_to_gem(ret);
1245

1246
	obj->ttm.created = true;
1247 1248
	i915_gem_object_release_memory_region(obj);
	i915_gem_object_init_memory_region(obj, mem);
1249 1250 1251
	i915_ttm_adjust_domains_after_move(obj);
	i915_ttm_adjust_gem_after_move(obj);
	i915_gem_object_unlock(obj);
1252

1253
	return 0;
1254
}
1255 1256 1257

static const struct intel_memory_region_ops ttm_system_region_ops = {
	.init_object = __i915_gem_ttm_object_init,
1258
	.release = intel_region_ttm_fini,
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
};

struct intel_memory_region *
i915_gem_ttm_system_setup(struct drm_i915_private *i915,
			  u16 type, u16 instance)
{
	struct intel_memory_region *mr;

	mr = intel_memory_region_create(i915, 0,
					totalram_pages() << PAGE_SHIFT,
M
Matthew Auld 已提交
1269
					PAGE_SIZE, 0, 0,
1270 1271 1272 1273 1274 1275 1276
					type, instance,
					&ttm_system_region_ops);
	if (IS_ERR(mr))
		return mr;

	intel_memory_region_set_name(mr, "system-ttm");
	return mr;
1277
}