1213d5092SThomas Hellström // SPDX-License-Identifier: MIT
2213d5092SThomas Hellström /*
3213d5092SThomas Hellström  * Copyright © 2021 Intel Corporation
4213d5092SThomas Hellström  */
5213d5092SThomas Hellström 
682508de2SJani Nikula #include <linux/shmem_fs.h>
782508de2SJani Nikula 
8213d5092SThomas Hellström #include <drm/ttm/ttm_bo_driver.h>
9213d5092SThomas Hellström #include <drm/ttm/ttm_placement.h>
1093735059SMatthew Auld #include <drm/drm_buddy.h>
11213d5092SThomas Hellström 
12213d5092SThomas Hellström #include "i915_drv.h"
1393735059SMatthew Auld #include "i915_ttm_buddy_manager.h"
14213d5092SThomas Hellström #include "intel_memory_region.h"
15213d5092SThomas Hellström #include "intel_region_ttm.h"
16213d5092SThomas Hellström 
17c56ce956SThomas Hellström #include "gem/i915_gem_mman.h"
18213d5092SThomas Hellström #include "gem/i915_gem_object.h"
19213d5092SThomas Hellström #include "gem/i915_gem_region.h"
20213d5092SThomas Hellström #include "gem/i915_gem_ttm.h"
213589fdbdSThomas Hellström #include "gem/i915_gem_ttm_move.h"
22c56ce956SThomas Hellström #include "gem/i915_gem_ttm_pm.h"
2376a6d563SRamalingam C #include "gt/intel_gpu_commands.h"
24213d5092SThomas Hellström 
25213d5092SThomas Hellström #define I915_TTM_PRIO_PURGE     0
26213d5092SThomas Hellström #define I915_TTM_PRIO_NO_PAGES  1
27213d5092SThomas Hellström #define I915_TTM_PRIO_HAS_PAGES 2
2893735059SMatthew Auld #define I915_TTM_PRIO_NEEDS_CPU_ACCESS 3
29213d5092SThomas Hellström 
3038f28c06SThomas Hellström /*
3138f28c06SThomas Hellström  * Size of struct ttm_place vector in on-stack struct ttm_placement allocs
3238f28c06SThomas Hellström  */
3338f28c06SThomas Hellström #define I915_TTM_MAX_PLACEMENTS INTEL_REGION_UNKNOWN
3438f28c06SThomas Hellström 
35213d5092SThomas Hellström /**
36213d5092SThomas Hellström  * struct i915_ttm_tt - TTM page vector with additional private information
37213d5092SThomas Hellström  * @ttm: The base TTM page vector.
38213d5092SThomas Hellström  * @dev: The struct device used for dma mapping and unmapping.
39cad7109aSThomas Hellström  * @cached_rsgt: The cached scatter-gather table.
407ae03459SMatthew Auld  * @is_shmem: Set if using shmem.
417ae03459SMatthew Auld  * @filp: The shmem file, if using shmem backend.
42213d5092SThomas Hellström  *
43213d5092SThomas Hellström  * Note that DMA may be going on right up to the point where the page-
44213d5092SThomas Hellström  * vector is unpopulated in delayed destroy. Hence keep the
45213d5092SThomas Hellström  * scatter-gather table mapped and cached up to that point. This is
46213d5092SThomas Hellström  * different from the cached gem object io scatter-gather table which
47213d5092SThomas Hellström  * doesn't have an associated dma mapping.
48213d5092SThomas Hellström  */
49213d5092SThomas Hellström struct i915_ttm_tt {
50213d5092SThomas Hellström 	struct ttm_tt ttm;
51213d5092SThomas Hellström 	struct device *dev;
52cad7109aSThomas Hellström 	struct i915_refct_sgt cached_rsgt;
537ae03459SMatthew Auld 
547ae03459SMatthew Auld 	bool is_shmem;
557ae03459SMatthew Auld 	struct file *filp;
56213d5092SThomas Hellström };
57213d5092SThomas Hellström 
5838f28c06SThomas Hellström static const struct ttm_place sys_placement_flags = {
59213d5092SThomas Hellström 	.fpfn = 0,
60213d5092SThomas Hellström 	.lpfn = 0,
61213d5092SThomas Hellström 	.mem_type = I915_PL_SYSTEM,
62213d5092SThomas Hellström 	.flags = 0,
63213d5092SThomas Hellström };
64213d5092SThomas Hellström 
65213d5092SThomas Hellström static struct ttm_placement i915_sys_placement = {
66213d5092SThomas Hellström 	.num_placement = 1,
6738f28c06SThomas Hellström 	.placement = &sys_placement_flags,
68213d5092SThomas Hellström 	.num_busy_placement = 1,
6938f28c06SThomas Hellström 	.busy_placement = &sys_placement_flags,
70213d5092SThomas Hellström };
71213d5092SThomas Hellström 
72c56ce956SThomas Hellström /**
73c56ce956SThomas Hellström  * i915_ttm_sys_placement - Return the struct ttm_placement to be
74c56ce956SThomas Hellström  * used for an object in system memory.
75c56ce956SThomas Hellström  *
76c56ce956SThomas Hellström  * Rather than making the struct extern, use this
77c56ce956SThomas Hellström  * function.
78c56ce956SThomas Hellström  *
79c56ce956SThomas Hellström  * Return: A pointer to a static variable for sys placement.
80c56ce956SThomas Hellström  */
81c56ce956SThomas Hellström struct ttm_placement *i915_ttm_sys_placement(void)
82c56ce956SThomas Hellström {
83c56ce956SThomas Hellström 	return &i915_sys_placement;
84c56ce956SThomas Hellström }
85c56ce956SThomas Hellström 
86b07a6483SThomas Hellström static int i915_ttm_err_to_gem(int err)
87b07a6483SThomas Hellström {
88b07a6483SThomas Hellström 	/* Fastpath */
89b07a6483SThomas Hellström 	if (likely(!err))
90b07a6483SThomas Hellström 		return 0;
91b07a6483SThomas Hellström 
92b07a6483SThomas Hellström 	switch (err) {
93b07a6483SThomas Hellström 	case -EBUSY:
94b07a6483SThomas Hellström 		/*
95b07a6483SThomas Hellström 		 * TTM likes to convert -EDEADLK to -EBUSY, and wants us to
96b07a6483SThomas Hellström 		 * restart the operation, since we don't record the contending
97b07a6483SThomas Hellström 		 * lock. We use -EAGAIN to restart.
98b07a6483SThomas Hellström 		 */
99b07a6483SThomas Hellström 		return -EAGAIN;
100b07a6483SThomas Hellström 	case -ENOSPC:
101b07a6483SThomas Hellström 		/*
102b07a6483SThomas Hellström 		 * Memory type / region is full, and we can't evict.
103b07a6483SThomas Hellström 		 * Except possibly system, that returns -ENOMEM;
104b07a6483SThomas Hellström 		 */
105b07a6483SThomas Hellström 		return -ENXIO;
106b07a6483SThomas Hellström 	default:
107b07a6483SThomas Hellström 		break;
108b07a6483SThomas Hellström 	}
109b07a6483SThomas Hellström 
110b07a6483SThomas Hellström 	return err;
111b07a6483SThomas Hellström }
112b07a6483SThomas Hellström 
11338f28c06SThomas Hellström static enum ttm_caching
11438f28c06SThomas Hellström i915_ttm_select_tt_caching(const struct drm_i915_gem_object *obj)
11538f28c06SThomas Hellström {
11638f28c06SThomas Hellström 	/*
1172eda4fc6SMatthew Auld 	 * Objects only allowed in system get cached cpu-mappings, or when
1182eda4fc6SMatthew Auld 	 * evicting lmem-only buffers to system for swapping. Other objects get
1192eda4fc6SMatthew Auld 	 * WC mapping for now. Even if in system.
12038f28c06SThomas Hellström 	 */
1212eda4fc6SMatthew Auld 	if (obj->mm.n_placements <= 1)
12238f28c06SThomas Hellström 		return ttm_cached;
12338f28c06SThomas Hellström 
12438f28c06SThomas Hellström 	return ttm_write_combined;
12538f28c06SThomas Hellström }
12638f28c06SThomas Hellström 
12738f28c06SThomas Hellström static void
12838f28c06SThomas Hellström i915_ttm_place_from_region(const struct intel_memory_region *mr,
129beb6a229SMatthew Auld 			   struct ttm_place *place,
130ecbf2060SMatthew Auld 			   resource_size_t offset,
131ecbf2060SMatthew Auld 			   resource_size_t size,
132beb6a229SMatthew Auld 			   unsigned int flags)
13338f28c06SThomas Hellström {
13438f28c06SThomas Hellström 	memset(place, 0, sizeof(*place));
13538f28c06SThomas Hellström 	place->mem_type = intel_region_to_ttm_type(mr);
136beb6a229SMatthew Auld 
13766ddc693SMatthew Auld 	if (mr->type == INTEL_MEMORY_SYSTEM)
13866ddc693SMatthew Auld 		return;
13966ddc693SMatthew Auld 
140beb6a229SMatthew Auld 	if (flags & I915_BO_ALLOC_CONTIGUOUS)
14130b9d1b3SMatthew Auld 		place->flags |= TTM_PL_FLAG_CONTIGUOUS;
142ecbf2060SMatthew Auld 	if (offset != I915_BO_INVALID_OFFSET) {
143ecbf2060SMatthew Auld 		place->fpfn = offset >> PAGE_SHIFT;
144ecbf2060SMatthew Auld 		place->lpfn = place->fpfn + (size >> PAGE_SHIFT);
145ecbf2060SMatthew Auld 	} else if (mr->io_size && mr->io_size < mr->total) {
14630b9d1b3SMatthew Auld 		if (flags & I915_BO_ALLOC_GPU_ONLY) {
14730b9d1b3SMatthew Auld 			place->flags |= TTM_PL_FLAG_TOPDOWN;
14830b9d1b3SMatthew Auld 		} else {
1493312a4acSMatthew Auld 			place->fpfn = 0;
1503312a4acSMatthew Auld 			place->lpfn = mr->io_size >> PAGE_SHIFT;
1513312a4acSMatthew Auld 		}
15238f28c06SThomas Hellström 	}
15330b9d1b3SMatthew Auld }
15438f28c06SThomas Hellström 
15538f28c06SThomas Hellström static void
15638f28c06SThomas Hellström i915_ttm_placement_from_obj(const struct drm_i915_gem_object *obj,
15738f28c06SThomas Hellström 			    struct ttm_place *requested,
15838f28c06SThomas Hellström 			    struct ttm_place *busy,
15938f28c06SThomas Hellström 			    struct ttm_placement *placement)
16038f28c06SThomas Hellström {
16138f28c06SThomas Hellström 	unsigned int num_allowed = obj->mm.n_placements;
162beb6a229SMatthew Auld 	unsigned int flags = obj->flags;
16338f28c06SThomas Hellström 	unsigned int i;
16438f28c06SThomas Hellström 
16538f28c06SThomas Hellström 	placement->num_placement = 1;
16638f28c06SThomas Hellström 	i915_ttm_place_from_region(num_allowed ? obj->mm.placements[0] :
167ecbf2060SMatthew Auld 				   obj->mm.region, requested, obj->bo_offset,
168ecbf2060SMatthew Auld 				   obj->base.size, flags);
16938f28c06SThomas Hellström 
17038f28c06SThomas Hellström 	/* Cache this on object? */
17138f28c06SThomas Hellström 	placement->num_busy_placement = num_allowed;
17238f28c06SThomas Hellström 	for (i = 0; i < placement->num_busy_placement; ++i)
173ecbf2060SMatthew Auld 		i915_ttm_place_from_region(obj->mm.placements[i], busy + i,
174ecbf2060SMatthew Auld 					   obj->bo_offset, obj->base.size, flags);
17538f28c06SThomas Hellström 
17638f28c06SThomas Hellström 	if (num_allowed == 0) {
17738f28c06SThomas Hellström 		*busy = *requested;
17838f28c06SThomas Hellström 		placement->num_busy_placement = 1;
17938f28c06SThomas Hellström 	}
18038f28c06SThomas Hellström 
18138f28c06SThomas Hellström 	placement->placement = requested;
18238f28c06SThomas Hellström 	placement->busy_placement = busy;
18338f28c06SThomas Hellström }
18438f28c06SThomas Hellström 
1857ae03459SMatthew Auld static int i915_ttm_tt_shmem_populate(struct ttm_device *bdev,
1867ae03459SMatthew Auld 				      struct ttm_tt *ttm,
1877ae03459SMatthew Auld 				      struct ttm_operation_ctx *ctx)
1887ae03459SMatthew Auld {
1897ae03459SMatthew Auld 	struct drm_i915_private *i915 = container_of(bdev, typeof(*i915), bdev);
1907ae03459SMatthew Auld 	struct intel_memory_region *mr = i915->mm.regions[INTEL_MEMORY_SYSTEM];
1917ae03459SMatthew Auld 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
19278a07fe7SRobert Beckett 	const unsigned int max_segment = i915_sg_segment_size(i915->drm.dev);
1935719d4feSRobert Beckett 	const size_t size = (size_t)ttm->num_pages << PAGE_SHIFT;
1947ae03459SMatthew Auld 	struct file *filp = i915_tt->filp;
1957ae03459SMatthew Auld 	struct sgt_iter sgt_iter;
1967ae03459SMatthew Auld 	struct sg_table *st;
1977ae03459SMatthew Auld 	struct page *page;
1987ae03459SMatthew Auld 	unsigned long i;
1997ae03459SMatthew Auld 	int err;
2007ae03459SMatthew Auld 
2017ae03459SMatthew Auld 	if (!filp) {
2027ae03459SMatthew Auld 		struct address_space *mapping;
2037ae03459SMatthew Auld 		gfp_t mask;
2047ae03459SMatthew Auld 
2057ae03459SMatthew Auld 		filp = shmem_file_setup("i915-shmem-tt", size, VM_NORESERVE);
2067ae03459SMatthew Auld 		if (IS_ERR(filp))
2077ae03459SMatthew Auld 			return PTR_ERR(filp);
2087ae03459SMatthew Auld 
2097ae03459SMatthew Auld 		mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
2107ae03459SMatthew Auld 
2117ae03459SMatthew Auld 		mapping = filp->f_mapping;
2127ae03459SMatthew Auld 		mapping_set_gfp_mask(mapping, mask);
2137ae03459SMatthew Auld 		GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
2147ae03459SMatthew Auld 
2157ae03459SMatthew Auld 		i915_tt->filp = filp;
2167ae03459SMatthew Auld 	}
2177ae03459SMatthew Auld 
218cad7109aSThomas Hellström 	st = &i915_tt->cached_rsgt.table;
219cad7109aSThomas Hellström 	err = shmem_sg_alloc_table(i915, st, size, mr, filp->f_mapping,
220cad7109aSThomas Hellström 				   max_segment);
221cad7109aSThomas Hellström 	if (err)
222cad7109aSThomas Hellström 		return err;
2237ae03459SMatthew Auld 
224cad7109aSThomas Hellström 	err = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL,
2257ae03459SMatthew Auld 			      DMA_ATTR_SKIP_CPU_SYNC);
226cad7109aSThomas Hellström 	if (err)
2277ae03459SMatthew Auld 		goto err_free_st;
2287ae03459SMatthew Auld 
2297ae03459SMatthew Auld 	i = 0;
2307ae03459SMatthew Auld 	for_each_sgt_page(page, sgt_iter, st)
2317ae03459SMatthew Auld 		ttm->pages[i++] = page;
2327ae03459SMatthew Auld 
2337ae03459SMatthew Auld 	if (ttm->page_flags & TTM_TT_FLAG_SWAPPED)
2347ae03459SMatthew Auld 		ttm->page_flags &= ~TTM_TT_FLAG_SWAPPED;
2357ae03459SMatthew Auld 
2367ae03459SMatthew Auld 	return 0;
2377ae03459SMatthew Auld 
2387ae03459SMatthew Auld err_free_st:
239cad7109aSThomas Hellström 	shmem_sg_free_table(st, filp->f_mapping, false, false);
240cad7109aSThomas Hellström 
2417ae03459SMatthew Auld 	return err;
2427ae03459SMatthew Auld }
2437ae03459SMatthew Auld 
2447ae03459SMatthew Auld static void i915_ttm_tt_shmem_unpopulate(struct ttm_tt *ttm)
2457ae03459SMatthew Auld {
2467ae03459SMatthew Auld 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
2477ae03459SMatthew Auld 	bool backup = ttm->page_flags & TTM_TT_FLAG_SWAPPED;
248cad7109aSThomas Hellström 	struct sg_table *st = &i915_tt->cached_rsgt.table;
2497ae03459SMatthew Auld 
250cad7109aSThomas Hellström 	shmem_sg_free_table(st, file_inode(i915_tt->filp)->i_mapping,
2517ae03459SMatthew Auld 			    backup, backup);
2527ae03459SMatthew Auld }
2537ae03459SMatthew Auld 
254cad7109aSThomas Hellström static void i915_ttm_tt_release(struct kref *ref)
255cad7109aSThomas Hellström {
256cad7109aSThomas Hellström 	struct i915_ttm_tt *i915_tt =
257cad7109aSThomas Hellström 		container_of(ref, typeof(*i915_tt), cached_rsgt.kref);
258cad7109aSThomas Hellström 	struct sg_table *st = &i915_tt->cached_rsgt.table;
259cad7109aSThomas Hellström 
260cad7109aSThomas Hellström 	GEM_WARN_ON(st->sgl);
261cad7109aSThomas Hellström 
262cad7109aSThomas Hellström 	kfree(i915_tt);
263cad7109aSThomas Hellström }
264cad7109aSThomas Hellström 
265cad7109aSThomas Hellström static const struct i915_refct_sgt_ops tt_rsgt_ops = {
266cad7109aSThomas Hellström 	.release = i915_ttm_tt_release
267cad7109aSThomas Hellström };
268cad7109aSThomas Hellström 
269213d5092SThomas Hellström static struct ttm_tt *i915_ttm_tt_create(struct ttm_buffer_object *bo,
270213d5092SThomas Hellström 					 uint32_t page_flags)
271213d5092SThomas Hellström {
27276a6d563SRamalingam C 	struct drm_i915_private *i915 = container_of(bo->bdev, typeof(*i915),
27376a6d563SRamalingam C 						     bdev);
274213d5092SThomas Hellström 	struct ttm_resource_manager *man =
275213d5092SThomas Hellström 		ttm_manager_type(bo->bdev, bo->resource->mem_type);
276213d5092SThomas Hellström 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
27776a6d563SRamalingam C 	unsigned long ccs_pages = 0;
2786385eb7aSThomas Hellström 	enum ttm_caching caching;
279213d5092SThomas Hellström 	struct i915_ttm_tt *i915_tt;
280213d5092SThomas Hellström 	int ret;
281213d5092SThomas Hellström 
2826667d78aSNirmoy Das 	if (i915_ttm_is_ghost_object(bo))
2836385eb7aSThomas Hellström 		return NULL;
2846385eb7aSThomas Hellström 
285213d5092SThomas Hellström 	i915_tt = kzalloc(sizeof(*i915_tt), GFP_KERNEL);
286213d5092SThomas Hellström 	if (!i915_tt)
287213d5092SThomas Hellström 		return NULL;
288213d5092SThomas Hellström 
289213d5092SThomas Hellström 	if (obj->flags & I915_BO_ALLOC_CPU_CLEAR &&
290213d5092SThomas Hellström 	    man->use_tt)
29143d46f0bSMatthew Auld 		page_flags |= TTM_TT_FLAG_ZERO_ALLOC;
292213d5092SThomas Hellström 
2936385eb7aSThomas Hellström 	caching = i915_ttm_select_tt_caching(obj);
2947ae03459SMatthew Auld 	if (i915_gem_object_is_shrinkable(obj) && caching == ttm_cached) {
2957ae03459SMatthew Auld 		page_flags |= TTM_TT_FLAG_EXTERNAL |
2967ae03459SMatthew Auld 			      TTM_TT_FLAG_EXTERNAL_MAPPABLE;
2977ae03459SMatthew Auld 		i915_tt->is_shmem = true;
298213d5092SThomas Hellström 	}
299213d5092SThomas Hellström 
300873fef88SMatthew Auld 	if (i915_gem_object_needs_ccs_pages(obj))
30176a6d563SRamalingam C 		ccs_pages = DIV_ROUND_UP(DIV_ROUND_UP(bo->base.size,
30276a6d563SRamalingam C 						      NUM_BYTES_PER_CCS_BYTE),
30376a6d563SRamalingam C 					 PAGE_SIZE);
30476a6d563SRamalingam C 
30576a6d563SRamalingam C 	ret = ttm_tt_init(&i915_tt->ttm, bo, page_flags, caching, ccs_pages);
3067ae03459SMatthew Auld 	if (ret)
3077ae03459SMatthew Auld 		goto err_free;
3087ae03459SMatthew Auld 
309cad7109aSThomas Hellström 	__i915_refct_sgt_init(&i915_tt->cached_rsgt, bo->base.size,
310cad7109aSThomas Hellström 			      &tt_rsgt_ops);
311cad7109aSThomas Hellström 
312213d5092SThomas Hellström 	i915_tt->dev = obj->base.dev->dev;
313213d5092SThomas Hellström 
314213d5092SThomas Hellström 	return &i915_tt->ttm;
3157ae03459SMatthew Auld 
3167ae03459SMatthew Auld err_free:
3177ae03459SMatthew Auld 	kfree(i915_tt);
3187ae03459SMatthew Auld 	return NULL;
3197ae03459SMatthew Auld }
3207ae03459SMatthew Auld 
3217ae03459SMatthew Auld static int i915_ttm_tt_populate(struct ttm_device *bdev,
3227ae03459SMatthew Auld 				struct ttm_tt *ttm,
3237ae03459SMatthew Auld 				struct ttm_operation_ctx *ctx)
3247ae03459SMatthew Auld {
3257ae03459SMatthew Auld 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
3267ae03459SMatthew Auld 
3277ae03459SMatthew Auld 	if (i915_tt->is_shmem)
3287ae03459SMatthew Auld 		return i915_ttm_tt_shmem_populate(bdev, ttm, ctx);
3297ae03459SMatthew Auld 
3307ae03459SMatthew Auld 	return ttm_pool_alloc(&bdev->pool, ttm, ctx);
331213d5092SThomas Hellström }
332213d5092SThomas Hellström 
333213d5092SThomas Hellström static void i915_ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm)
334213d5092SThomas Hellström {
335213d5092SThomas Hellström 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
336cad7109aSThomas Hellström 	struct sg_table *st = &i915_tt->cached_rsgt.table;
337cad7109aSThomas Hellström 
338cad7109aSThomas Hellström 	if (st->sgl)
339cad7109aSThomas Hellström 		dma_unmap_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0);
340213d5092SThomas Hellström 
3417ae03459SMatthew Auld 	if (i915_tt->is_shmem) {
3427ae03459SMatthew Auld 		i915_ttm_tt_shmem_unpopulate(ttm);
3437ae03459SMatthew Auld 	} else {
344cad7109aSThomas Hellström 		sg_free_table(st);
345213d5092SThomas Hellström 		ttm_pool_free(&bdev->pool, ttm);
346213d5092SThomas Hellström 	}
3477ae03459SMatthew Auld }
348213d5092SThomas Hellström 
349213d5092SThomas Hellström static void i915_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
350213d5092SThomas Hellström {
351213d5092SThomas Hellström 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
352213d5092SThomas Hellström 
3537ae03459SMatthew Auld 	if (i915_tt->filp)
3547ae03459SMatthew Auld 		fput(i915_tt->filp);
3557ae03459SMatthew Auld 
356c865204eSThomas Hellström 	ttm_tt_fini(ttm);
357cad7109aSThomas Hellström 	i915_refct_sgt_put(&i915_tt->cached_rsgt);
358213d5092SThomas Hellström }
359213d5092SThomas Hellström 
360213d5092SThomas Hellström static bool i915_ttm_eviction_valuable(struct ttm_buffer_object *bo,
361213d5092SThomas Hellström 				       const struct ttm_place *place)
362213d5092SThomas Hellström {
363213d5092SThomas Hellström 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
364213d5092SThomas Hellström 
3656667d78aSNirmoy Das 	if (i915_ttm_is_ghost_object(bo))
3666385eb7aSThomas Hellström 		return false;
3676385eb7aSThomas Hellström 
3687ae03459SMatthew Auld 	/*
3697ae03459SMatthew Auld 	 * EXTERNAL objects should never be swapped out by TTM, instead we need
3707ae03459SMatthew Auld 	 * to handle that ourselves. TTM will already skip such objects for us,
3717ae03459SMatthew Auld 	 * but we would like to avoid grabbing locks for no good reason.
3727ae03459SMatthew Auld 	 */
3737ae03459SMatthew Auld 	if (bo->ttm && bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
3746164807dSDan Carpenter 		return false;
3757ae03459SMatthew Auld 
376213d5092SThomas Hellström 	/* Will do for now. Our pinned objects are still on TTM's LRU lists */
37793735059SMatthew Auld 	if (!i915_gem_object_evictable(obj))
37893735059SMatthew Auld 		return false;
37993735059SMatthew Auld 
38092b2b55eSArunpravin Paneer Selvam 	return ttm_bo_eviction_valuable(bo, place);
381213d5092SThomas Hellström }
382213d5092SThomas Hellström 
383213d5092SThomas Hellström static void i915_ttm_evict_flags(struct ttm_buffer_object *bo,
384213d5092SThomas Hellström 				 struct ttm_placement *placement)
385213d5092SThomas Hellström {
386213d5092SThomas Hellström 	*placement = i915_sys_placement;
387213d5092SThomas Hellström }
388213d5092SThomas Hellström 
3893589fdbdSThomas Hellström /**
3903589fdbdSThomas Hellström  * i915_ttm_free_cached_io_rsgt - Free object cached LMEM information
3913589fdbdSThomas Hellström  * @obj: The GEM object
3923589fdbdSThomas Hellström  * This function frees any LMEM-related information that is cached on
3933589fdbdSThomas Hellström  * the object. For example the radix tree for fast page lookup and the
3943589fdbdSThomas Hellström  * cached refcounted sg-table
3953589fdbdSThomas Hellström  */
3963589fdbdSThomas Hellström void i915_ttm_free_cached_io_rsgt(struct drm_i915_gem_object *obj)
397213d5092SThomas Hellström {
398cf3e3e86SMaarten Lankhorst 	struct radix_tree_iter iter;
399cf3e3e86SMaarten Lankhorst 	void __rcu **slot;
400cf3e3e86SMaarten Lankhorst 
401cad7109aSThomas Hellström 	if (!obj->ttm.cached_io_rsgt)
402cf3e3e86SMaarten Lankhorst 		return;
403cf3e3e86SMaarten Lankhorst 
404cf3e3e86SMaarten Lankhorst 	rcu_read_lock();
405cf3e3e86SMaarten Lankhorst 	radix_tree_for_each_slot(slot, &obj->ttm.get_io_page.radix, &iter, 0)
406cf3e3e86SMaarten Lankhorst 		radix_tree_delete(&obj->ttm.get_io_page.radix, iter.index);
407cf3e3e86SMaarten Lankhorst 	rcu_read_unlock();
408cf3e3e86SMaarten Lankhorst 
409cad7109aSThomas Hellström 	i915_refct_sgt_put(obj->ttm.cached_io_rsgt);
410cad7109aSThomas Hellström 	obj->ttm.cached_io_rsgt = NULL;
411213d5092SThomas Hellström }
412213d5092SThomas Hellström 
4133589fdbdSThomas Hellström /**
4143589fdbdSThomas Hellström  * i915_ttm_purge - Clear an object of its memory
4153589fdbdSThomas Hellström  * @obj: The object
4163589fdbdSThomas Hellström  *
4173589fdbdSThomas Hellström  * This function is called to clear an object of it's memory when it is
4183589fdbdSThomas Hellström  * marked as not needed anymore.
4193589fdbdSThomas Hellström  *
4203589fdbdSThomas Hellström  * Return: 0 on success, negative error code on failure.
42132b7cf51SThomas Hellström  */
4223589fdbdSThomas Hellström int i915_ttm_purge(struct drm_i915_gem_object *obj)
423213d5092SThomas Hellström {
424213d5092SThomas Hellström 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
4257ae03459SMatthew Auld 	struct i915_ttm_tt *i915_tt =
4267ae03459SMatthew Auld 		container_of(bo->ttm, typeof(*i915_tt), ttm);
427213d5092SThomas Hellström 	struct ttm_operation_ctx ctx = {
428213d5092SThomas Hellström 		.interruptible = true,
429213d5092SThomas Hellström 		.no_wait_gpu = false,
430213d5092SThomas Hellström 	};
431213d5092SThomas Hellström 	struct ttm_placement place = {};
432213d5092SThomas Hellström 	int ret;
433213d5092SThomas Hellström 
434213d5092SThomas Hellström 	if (obj->mm.madv == __I915_MADV_PURGED)
4357ae03459SMatthew Auld 		return 0;
436213d5092SThomas Hellström 
437213d5092SThomas Hellström 	ret = ttm_bo_validate(bo, &place, &ctx);
4387ae03459SMatthew Auld 	if (ret)
4397ae03459SMatthew Auld 		return ret;
4407ae03459SMatthew Auld 
4417ae03459SMatthew Auld 	if (bo->ttm && i915_tt->filp) {
4427ae03459SMatthew Auld 		/*
4437ae03459SMatthew Auld 		 * The below fput(which eventually calls shmem_truncate) might
4447ae03459SMatthew Auld 		 * be delayed by worker, so when directly called to purge the
4457ae03459SMatthew Auld 		 * pages(like by the shrinker) we should try to be more
4467ae03459SMatthew Auld 		 * aggressive and release the pages immediately.
4477ae03459SMatthew Auld 		 */
4487ae03459SMatthew Auld 		shmem_truncate_range(file_inode(i915_tt->filp),
4497ae03459SMatthew Auld 				     0, (loff_t)-1);
4507ae03459SMatthew Auld 		fput(fetch_and_zero(&i915_tt->filp));
4517ae03459SMatthew Auld 	}
4527ae03459SMatthew Auld 
4533c2b8f32SThomas Hellström 	obj->write_domain = 0;
4543c2b8f32SThomas Hellström 	obj->read_domains = 0;
4553c2b8f32SThomas Hellström 	i915_ttm_adjust_gem_after_move(obj);
456cad7109aSThomas Hellström 	i915_ttm_free_cached_io_rsgt(obj);
457213d5092SThomas Hellström 	obj->mm.madv = __I915_MADV_PURGED;
4583589fdbdSThomas Hellström 
4597ae03459SMatthew Auld 	return 0;
460213d5092SThomas Hellström }
4617ae03459SMatthew Auld 
462ffa3fe08SMatthew Auld static int i915_ttm_shrink(struct drm_i915_gem_object *obj, unsigned int flags)
4637ae03459SMatthew Auld {
4647ae03459SMatthew Auld 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
4657ae03459SMatthew Auld 	struct i915_ttm_tt *i915_tt =
4667ae03459SMatthew Auld 		container_of(bo->ttm, typeof(*i915_tt), ttm);
4677ae03459SMatthew Auld 	struct ttm_operation_ctx ctx = {
4687ae03459SMatthew Auld 		.interruptible = true,
469ffa3fe08SMatthew Auld 		.no_wait_gpu = flags & I915_GEM_OBJECT_SHRINK_NO_GPU_WAIT,
4707ae03459SMatthew Auld 	};
4717ae03459SMatthew Auld 	struct ttm_placement place = {};
4727ae03459SMatthew Auld 	int ret;
4737ae03459SMatthew Auld 
4747ae03459SMatthew Auld 	if (!bo->ttm || bo->resource->mem_type != TTM_PL_SYSTEM)
4757ae03459SMatthew Auld 		return 0;
4767ae03459SMatthew Auld 
4777ae03459SMatthew Auld 	GEM_BUG_ON(!i915_tt->is_shmem);
4787ae03459SMatthew Auld 
4797ae03459SMatthew Auld 	if (!i915_tt->filp)
4807ae03459SMatthew Auld 		return 0;
4817ae03459SMatthew Auld 
482004746e4SThomas Hellström 	ret = ttm_bo_wait_ctx(bo, &ctx);
483004746e4SThomas Hellström 	if (ret)
484004746e4SThomas Hellström 		return ret;
485004746e4SThomas Hellström 
4867ae03459SMatthew Auld 	switch (obj->mm.madv) {
4877ae03459SMatthew Auld 	case I915_MADV_DONTNEED:
4887ae03459SMatthew Auld 		return i915_ttm_purge(obj);
4897ae03459SMatthew Auld 	case __I915_MADV_PURGED:
4907ae03459SMatthew Auld 		return 0;
4917ae03459SMatthew Auld 	}
4927ae03459SMatthew Auld 
4937ae03459SMatthew Auld 	if (bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED)
4947ae03459SMatthew Auld 		return 0;
4957ae03459SMatthew Auld 
4967ae03459SMatthew Auld 	bo->ttm->page_flags |= TTM_TT_FLAG_SWAPPED;
4977ae03459SMatthew Auld 	ret = ttm_bo_validate(bo, &place, &ctx);
4987ae03459SMatthew Auld 	if (ret) {
4997ae03459SMatthew Auld 		bo->ttm->page_flags &= ~TTM_TT_FLAG_SWAPPED;
5007ae03459SMatthew Auld 		return ret;
5017ae03459SMatthew Auld 	}
5027ae03459SMatthew Auld 
503ffa3fe08SMatthew Auld 	if (flags & I915_GEM_OBJECT_SHRINK_WRITEBACK)
5047ae03459SMatthew Auld 		__shmem_writeback(obj->base.size, i915_tt->filp->f_mapping);
5057ae03459SMatthew Auld 
5067ae03459SMatthew Auld 	return 0;
507213d5092SThomas Hellström }
508213d5092SThomas Hellström 
509213d5092SThomas Hellström static void i915_ttm_delete_mem_notify(struct ttm_buffer_object *bo)
510213d5092SThomas Hellström {
511213d5092SThomas Hellström 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
512213d5092SThomas Hellström 
5136667d78aSNirmoy Das 	if (bo->resource && !i915_ttm_is_ghost_object(bo)) {
514068396bbSThomas Hellström 		__i915_gem_object_pages_fini(obj);
515cad7109aSThomas Hellström 		i915_ttm_free_cached_io_rsgt(obj);
516213d5092SThomas Hellström 	}
517213d5092SThomas Hellström }
518213d5092SThomas Hellström 
519cad7109aSThomas Hellström static struct i915_refct_sgt *i915_ttm_tt_get_st(struct ttm_tt *ttm)
520213d5092SThomas Hellström {
521213d5092SThomas Hellström 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
522213d5092SThomas Hellström 	struct sg_table *st;
523213d5092SThomas Hellström 	int ret;
524213d5092SThomas Hellström 
525cad7109aSThomas Hellström 	if (i915_tt->cached_rsgt.table.sgl)
526cad7109aSThomas Hellström 		return i915_refct_sgt_get(&i915_tt->cached_rsgt);
527213d5092SThomas Hellström 
528cad7109aSThomas Hellström 	st = &i915_tt->cached_rsgt.table;
52923852becSLinus Torvalds 	ret = sg_alloc_table_from_pages_segment(st,
53023852becSLinus Torvalds 			ttm->pages, ttm->num_pages,
53123852becSLinus Torvalds 			0, (unsigned long)ttm->num_pages << PAGE_SHIFT,
53278a07fe7SRobert Beckett 			i915_sg_segment_size(i915_tt->dev), GFP_KERNEL);
53323852becSLinus Torvalds 	if (ret) {
534cad7109aSThomas Hellström 		st->sgl = NULL;
53523852becSLinus Torvalds 		return ERR_PTR(ret);
536213d5092SThomas Hellström 	}
537213d5092SThomas Hellström 
538213d5092SThomas Hellström 	ret = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0);
539213d5092SThomas Hellström 	if (ret) {
540213d5092SThomas Hellström 		sg_free_table(st);
541213d5092SThomas Hellström 		return ERR_PTR(ret);
542213d5092SThomas Hellström 	}
543213d5092SThomas Hellström 
544cad7109aSThomas Hellström 	return i915_refct_sgt_get(&i915_tt->cached_rsgt);
545213d5092SThomas Hellström }
546213d5092SThomas Hellström 
5473589fdbdSThomas Hellström /**
5483589fdbdSThomas Hellström  * i915_ttm_resource_get_st - Get a refcounted sg-table pointing to the
5493589fdbdSThomas Hellström  * resource memory
5503589fdbdSThomas Hellström  * @obj: The GEM object used for sg-table caching
5513589fdbdSThomas Hellström  * @res: The struct ttm_resource for which an sg-table is requested.
5523589fdbdSThomas Hellström  *
5533589fdbdSThomas Hellström  * This function returns a refcounted sg-table representing the memory
5543589fdbdSThomas Hellström  * pointed to by @res. If @res is the object's current resource it may also
5553589fdbdSThomas Hellström  * cache the sg_table on the object or attempt to access an already cached
5563589fdbdSThomas Hellström  * sg-table. The refcounted sg-table needs to be put when no-longer in use.
5573589fdbdSThomas Hellström  *
5583589fdbdSThomas Hellström  * Return: A valid pointer to a struct i915_refct_sgt or error pointer on
5593589fdbdSThomas Hellström  * failure.
5603589fdbdSThomas Hellström  */
5613589fdbdSThomas Hellström struct i915_refct_sgt *
562213d5092SThomas Hellström i915_ttm_resource_get_st(struct drm_i915_gem_object *obj,
563213d5092SThomas Hellström 			 struct ttm_resource *res)
564213d5092SThomas Hellström {
565213d5092SThomas Hellström 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
5669306b2b2SMatthew Auld 	u32 page_alignment;
567213d5092SThomas Hellström 
5683589fdbdSThomas Hellström 	if (!i915_ttm_gtt_binds_lmem(res))
569213d5092SThomas Hellström 		return i915_ttm_tt_get_st(bo->ttm);
570213d5092SThomas Hellström 
571bc99f120SMatthew Auld 	page_alignment = bo->page_alignment << PAGE_SHIFT;
572bc99f120SMatthew Auld 	if (!page_alignment)
573bc99f120SMatthew Auld 		page_alignment = obj->mm.region->min_page_size;
574bc99f120SMatthew Auld 
5753c2b8f32SThomas Hellström 	/*
5763c2b8f32SThomas Hellström 	 * If CPU mapping differs, we need to add the ttm_tt pages to
5773c2b8f32SThomas Hellström 	 * the resulting st. Might make sense for GGTT.
5783c2b8f32SThomas Hellström 	 */
5793589fdbdSThomas Hellström 	GEM_WARN_ON(!i915_ttm_cpu_maps_iomem(res));
580cad7109aSThomas Hellström 	if (bo->resource == res) {
581cad7109aSThomas Hellström 		if (!obj->ttm.cached_io_rsgt) {
582cad7109aSThomas Hellström 			struct i915_refct_sgt *rsgt;
583cad7109aSThomas Hellström 
584cad7109aSThomas Hellström 			rsgt = intel_region_ttm_resource_to_rsgt(obj->mm.region,
585bc99f120SMatthew Auld 								 res,
586bc99f120SMatthew Auld 								 page_alignment);
587cad7109aSThomas Hellström 			if (IS_ERR(rsgt))
588cad7109aSThomas Hellström 				return rsgt;
589cad7109aSThomas Hellström 
590cad7109aSThomas Hellström 			obj->ttm.cached_io_rsgt = rsgt;
591cad7109aSThomas Hellström 		}
592cad7109aSThomas Hellström 		return i915_refct_sgt_get(obj->ttm.cached_io_rsgt);
593cad7109aSThomas Hellström 	}
594cad7109aSThomas Hellström 
595bc99f120SMatthew Auld 	return intel_region_ttm_resource_to_rsgt(obj->mm.region, res,
596bc99f120SMatthew Auld 						 page_alignment);
597213d5092SThomas Hellström }
598213d5092SThomas Hellström 
5996ef295e3SMatthew Auld static int i915_ttm_truncate(struct drm_i915_gem_object *obj)
6006ef295e3SMatthew Auld {
6016ef295e3SMatthew Auld 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
6026ef295e3SMatthew Auld 	int err;
6036ef295e3SMatthew Auld 
6046ef295e3SMatthew Auld 	WARN_ON_ONCE(obj->mm.madv == I915_MADV_WILLNEED);
6056ef295e3SMatthew Auld 
6065524b5e5SMatthew Auld 	err = ttm_bo_wait(bo, true, false);
6075524b5e5SMatthew Auld 	if (err)
6085524b5e5SMatthew Auld 		return err;
6095524b5e5SMatthew Auld 
6106ef295e3SMatthew Auld 	err = i915_ttm_move_notify(bo);
6116ef295e3SMatthew Auld 	if (err)
6126ef295e3SMatthew Auld 		return err;
6136ef295e3SMatthew Auld 
6146ef295e3SMatthew Auld 	return i915_ttm_purge(obj);
6156ef295e3SMatthew Auld }
6166ef295e3SMatthew Auld 
6173589fdbdSThomas Hellström static void i915_ttm_swap_notify(struct ttm_buffer_object *bo)
618213d5092SThomas Hellström {
619213d5092SThomas Hellström 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
6206385eb7aSThomas Hellström 	int ret;
621213d5092SThomas Hellström 
6226667d78aSNirmoy Das 	if (i915_ttm_is_ghost_object(bo))
6236385eb7aSThomas Hellström 		return;
6246385eb7aSThomas Hellström 
6256385eb7aSThomas Hellström 	ret = i915_ttm_move_notify(bo);
6263589fdbdSThomas Hellström 	GEM_WARN_ON(ret);
6273589fdbdSThomas Hellström 	GEM_WARN_ON(obj->ttm.cached_io_rsgt);
6283589fdbdSThomas Hellström 	if (!ret && obj->mm.madv != I915_MADV_WILLNEED)
629213d5092SThomas Hellström 		i915_ttm_purge(obj);
630213d5092SThomas Hellström }
631213d5092SThomas Hellström 
632bfe53be2SMatthew Auld /**
633bfe53be2SMatthew Auld  * i915_ttm_resource_mappable - Return true if the ttm resource is CPU
634bfe53be2SMatthew Auld  * accessible.
635bfe53be2SMatthew Auld  * @res: The TTM resource to check.
636bfe53be2SMatthew Auld  *
637bfe53be2SMatthew Auld  * This is interesting on small-BAR systems where we may encounter lmem objects
638bfe53be2SMatthew Auld  * that can't be accessed via the CPU.
639bfe53be2SMatthew Auld  */
640bfe53be2SMatthew Auld bool i915_ttm_resource_mappable(struct ttm_resource *res)
641503725c2SMatthew Auld {
642503725c2SMatthew Auld 	struct i915_ttm_buddy_resource *bman_res = to_ttm_buddy_resource(res);
643503725c2SMatthew Auld 
644503725c2SMatthew Auld 	if (!i915_ttm_cpu_maps_iomem(res))
645503725c2SMatthew Auld 		return true;
646503725c2SMatthew Auld 
647e3c92eb4SSomalapuram Amaranath 	return bman_res->used_visible_size == PFN_UP(bman_res->base.size);
648503725c2SMatthew Auld }
649503725c2SMatthew Auld 
650cf3e3e86SMaarten Lankhorst static int i915_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
651cf3e3e86SMaarten Lankhorst {
652bfe53be2SMatthew Auld 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(mem->bo);
653bfe53be2SMatthew Auld 	bool unknown_state;
654bfe53be2SMatthew Auld 
6556667d78aSNirmoy Das 	if (i915_ttm_is_ghost_object(mem->bo))
656bfe53be2SMatthew Auld 		return -EINVAL;
657bfe53be2SMatthew Auld 
658bfe53be2SMatthew Auld 	if (!kref_get_unless_zero(&obj->base.refcount))
659bfe53be2SMatthew Auld 		return -EINVAL;
660bfe53be2SMatthew Auld 
661bfe53be2SMatthew Auld 	assert_object_held(obj);
662bfe53be2SMatthew Auld 
663bfe53be2SMatthew Auld 	unknown_state = i915_gem_object_has_unknown_state(obj);
664bfe53be2SMatthew Auld 	i915_gem_object_put(obj);
665bfe53be2SMatthew Auld 	if (unknown_state)
666bfe53be2SMatthew Auld 		return -EINVAL;
667bfe53be2SMatthew Auld 
6683589fdbdSThomas Hellström 	if (!i915_ttm_cpu_maps_iomem(mem))
669cf3e3e86SMaarten Lankhorst 		return 0;
670cf3e3e86SMaarten Lankhorst 
671503725c2SMatthew Auld 	if (!i915_ttm_resource_mappable(mem))
672503725c2SMatthew Auld 		return -EINVAL;
673503725c2SMatthew Auld 
674cf3e3e86SMaarten Lankhorst 	mem->bus.caching = ttm_write_combined;
675cf3e3e86SMaarten Lankhorst 	mem->bus.is_iomem = true;
676cf3e3e86SMaarten Lankhorst 
677cf3e3e86SMaarten Lankhorst 	return 0;
678cf3e3e86SMaarten Lankhorst }
679cf3e3e86SMaarten Lankhorst 
680cf3e3e86SMaarten Lankhorst static unsigned long i915_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
681cf3e3e86SMaarten Lankhorst 					 unsigned long page_offset)
682cf3e3e86SMaarten Lankhorst {
683cf3e3e86SMaarten Lankhorst 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
684cf3e3e86SMaarten Lankhorst 	struct scatterlist *sg;
6856385eb7aSThomas Hellström 	unsigned long base;
686cf3e3e86SMaarten Lankhorst 	unsigned int ofs;
687cf3e3e86SMaarten Lankhorst 
6886667d78aSNirmoy Das 	GEM_BUG_ON(i915_ttm_is_ghost_object(bo));
689cf3e3e86SMaarten Lankhorst 	GEM_WARN_ON(bo->ttm);
690cf3e3e86SMaarten Lankhorst 
6916385eb7aSThomas Hellström 	base = obj->mm.region->iomap.base - obj->mm.region->region.start;
692*f47e6306SChris Wilson 	sg = i915_gem_object_page_iter_get_sg(obj, &obj->ttm.get_io_page, page_offset, &ofs);
693cf3e3e86SMaarten Lankhorst 
694cf3e3e86SMaarten Lankhorst 	return ((base + sg_dma_address(sg)) >> PAGE_SHIFT) + ofs;
695cf3e3e86SMaarten Lankhorst }
696cf3e3e86SMaarten Lankhorst 
69726b15eb0SMatthew Auld static int i915_ttm_access_memory(struct ttm_buffer_object *bo,
69826b15eb0SMatthew Auld 				  unsigned long offset, void *buf,
69926b15eb0SMatthew Auld 				  int len, int write)
70026b15eb0SMatthew Auld {
70126b15eb0SMatthew Auld 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
70226b15eb0SMatthew Auld 	resource_size_t iomap = obj->mm.region->iomap.base -
70326b15eb0SMatthew Auld 		obj->mm.region->region.start;
70426b15eb0SMatthew Auld 	unsigned long page = offset >> PAGE_SHIFT;
70526b15eb0SMatthew Auld 	unsigned long bytes_left = len;
70626b15eb0SMatthew Auld 
70726b15eb0SMatthew Auld 	/*
70826b15eb0SMatthew Auld 	 * TODO: For now just let it fail if the resource is non-mappable,
70926b15eb0SMatthew Auld 	 * otherwise we need to perform the memcpy from the gpu here, without
71026b15eb0SMatthew Auld 	 * interfering with the object (like moving the entire thing).
71126b15eb0SMatthew Auld 	 */
71226b15eb0SMatthew Auld 	if (!i915_ttm_resource_mappable(bo->resource))
71326b15eb0SMatthew Auld 		return -EIO;
71426b15eb0SMatthew Auld 
71526b15eb0SMatthew Auld 	offset -= page << PAGE_SHIFT;
71626b15eb0SMatthew Auld 	do {
71726b15eb0SMatthew Auld 		unsigned long bytes = min(bytes_left, PAGE_SIZE - offset);
71826b15eb0SMatthew Auld 		void __iomem *ptr;
71926b15eb0SMatthew Auld 		dma_addr_t daddr;
72026b15eb0SMatthew Auld 
72126b15eb0SMatthew Auld 		daddr = i915_gem_object_get_dma_address(obj, page);
72226b15eb0SMatthew Auld 		ptr = ioremap_wc(iomap + daddr + offset, bytes);
72326b15eb0SMatthew Auld 		if (!ptr)
72426b15eb0SMatthew Auld 			return -EIO;
72526b15eb0SMatthew Auld 
72626b15eb0SMatthew Auld 		if (write)
72726b15eb0SMatthew Auld 			memcpy_toio(ptr, buf, bytes);
72826b15eb0SMatthew Auld 		else
72926b15eb0SMatthew Auld 			memcpy_fromio(buf, ptr, bytes);
73026b15eb0SMatthew Auld 		iounmap(ptr);
73126b15eb0SMatthew Auld 
73226b15eb0SMatthew Auld 		page++;
73326b15eb0SMatthew Auld 		buf += bytes;
73426b15eb0SMatthew Auld 		bytes_left -= bytes;
73526b15eb0SMatthew Auld 		offset = 0;
73626b15eb0SMatthew Auld 	} while (bytes_left);
73726b15eb0SMatthew Auld 
73826b15eb0SMatthew Auld 	return len;
73926b15eb0SMatthew Auld }
74026b15eb0SMatthew Auld 
7416385eb7aSThomas Hellström /*
7426385eb7aSThomas Hellström  * All callbacks need to take care not to downcast a struct ttm_buffer_object
7436385eb7aSThomas Hellström  * without checking its subclass, since it might be a TTM ghost object.
7446385eb7aSThomas Hellström  */
745213d5092SThomas Hellström static struct ttm_device_funcs i915_ttm_bo_driver = {
746213d5092SThomas Hellström 	.ttm_tt_create = i915_ttm_tt_create,
7477ae03459SMatthew Auld 	.ttm_tt_populate = i915_ttm_tt_populate,
748213d5092SThomas Hellström 	.ttm_tt_unpopulate = i915_ttm_tt_unpopulate,
749213d5092SThomas Hellström 	.ttm_tt_destroy = i915_ttm_tt_destroy,
750213d5092SThomas Hellström 	.eviction_valuable = i915_ttm_eviction_valuable,
751213d5092SThomas Hellström 	.evict_flags = i915_ttm_evict_flags,
752213d5092SThomas Hellström 	.move = i915_ttm_move,
753213d5092SThomas Hellström 	.swap_notify = i915_ttm_swap_notify,
754213d5092SThomas Hellström 	.delete_mem_notify = i915_ttm_delete_mem_notify,
755cf3e3e86SMaarten Lankhorst 	.io_mem_reserve = i915_ttm_io_mem_reserve,
756cf3e3e86SMaarten Lankhorst 	.io_mem_pfn = i915_ttm_io_mem_pfn,
75726b15eb0SMatthew Auld 	.access_memory = i915_ttm_access_memory,
758213d5092SThomas Hellström };
759213d5092SThomas Hellström 
760213d5092SThomas Hellström /**
761213d5092SThomas Hellström  * i915_ttm_driver - Return a pointer to the TTM device funcs
762213d5092SThomas Hellström  *
763213d5092SThomas Hellström  * Return: Pointer to statically allocated TTM device funcs.
764213d5092SThomas Hellström  */
765213d5092SThomas Hellström struct ttm_device_funcs *i915_ttm_driver(void)
766213d5092SThomas Hellström {
767213d5092SThomas Hellström 	return &i915_ttm_bo_driver;
768213d5092SThomas Hellström }
769213d5092SThomas Hellström 
770b6e913e1SThomas Hellström static int __i915_ttm_get_pages(struct drm_i915_gem_object *obj,
771b6e913e1SThomas Hellström 				struct ttm_placement *placement)
772213d5092SThomas Hellström {
773213d5092SThomas Hellström 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
774213d5092SThomas Hellström 	struct ttm_operation_ctx ctx = {
775213d5092SThomas Hellström 		.interruptible = true,
776213d5092SThomas Hellström 		.no_wait_gpu = false,
777213d5092SThomas Hellström 	};
778b07a6483SThomas Hellström 	int real_num_busy;
779213d5092SThomas Hellström 	int ret;
780213d5092SThomas Hellström 
781b07a6483SThomas Hellström 	/* First try only the requested placement. No eviction. */
782b6e913e1SThomas Hellström 	real_num_busy = fetch_and_zero(&placement->num_busy_placement);
783b6e913e1SThomas Hellström 	ret = ttm_bo_validate(bo, placement, &ctx);
784b07a6483SThomas Hellström 	if (ret) {
785b07a6483SThomas Hellström 		ret = i915_ttm_err_to_gem(ret);
786b07a6483SThomas Hellström 		/*
787b07a6483SThomas Hellström 		 * Anything that wants to restart the operation gets to
788b07a6483SThomas Hellström 		 * do that.
789b07a6483SThomas Hellström 		 */
790b07a6483SThomas Hellström 		if (ret == -EDEADLK || ret == -EINTR || ret == -ERESTARTSYS ||
791b07a6483SThomas Hellström 		    ret == -EAGAIN)
792b07a6483SThomas Hellström 			return ret;
793213d5092SThomas Hellström 
794b07a6483SThomas Hellström 		/*
795b07a6483SThomas Hellström 		 * If the initial attempt fails, allow all accepted placements,
796b07a6483SThomas Hellström 		 * evicting if necessary.
797b07a6483SThomas Hellström 		 */
798b6e913e1SThomas Hellström 		placement->num_busy_placement = real_num_busy;
799b6e913e1SThomas Hellström 		ret = ttm_bo_validate(bo, placement, &ctx);
800213d5092SThomas Hellström 		if (ret)
801b07a6483SThomas Hellström 			return i915_ttm_err_to_gem(ret);
802b07a6483SThomas Hellström 	}
803213d5092SThomas Hellström 
8043c2b8f32SThomas Hellström 	if (bo->ttm && !ttm_tt_is_populated(bo->ttm)) {
8053c2b8f32SThomas Hellström 		ret = ttm_tt_populate(bo->bdev, bo->ttm, &ctx);
8063c2b8f32SThomas Hellström 		if (ret)
8073c2b8f32SThomas Hellström 			return ret;
8083c2b8f32SThomas Hellström 
8093c2b8f32SThomas Hellström 		i915_ttm_adjust_domains_after_move(obj);
8103c2b8f32SThomas Hellström 		i915_ttm_adjust_gem_after_move(obj);
8113c2b8f32SThomas Hellström 	}
8123c2b8f32SThomas Hellström 
81375e38285SJason Ekstrand 	if (!i915_gem_object_has_pages(obj)) {
814cad7109aSThomas Hellström 		struct i915_refct_sgt *rsgt =
815cad7109aSThomas Hellström 			i915_ttm_resource_get_st(obj, bo->resource);
816213d5092SThomas Hellström 
817cad7109aSThomas Hellström 		if (IS_ERR(rsgt))
818cad7109aSThomas Hellström 			return PTR_ERR(rsgt);
819cad7109aSThomas Hellström 
820cad7109aSThomas Hellström 		GEM_BUG_ON(obj->mm.rsgt);
821cad7109aSThomas Hellström 		obj->mm.rsgt = rsgt;
8228c949515SMatthew Auld 		__i915_gem_object_set_pages(obj, &rsgt->table);
82375e38285SJason Ekstrand 	}
824213d5092SThomas Hellström 
82576a6d563SRamalingam C 	GEM_BUG_ON(bo->ttm && ((obj->base.size >> PAGE_SHIFT) < bo->ttm->num_pages));
826ebd4a8ecSMatthew Auld 	i915_ttm_adjust_lru(obj);
827213d5092SThomas Hellström 	return ret;
828213d5092SThomas Hellström }
829213d5092SThomas Hellström 
830b6e913e1SThomas Hellström static int i915_ttm_get_pages(struct drm_i915_gem_object *obj)
831b6e913e1SThomas Hellström {
832b6e913e1SThomas Hellström 	struct ttm_place requested, busy[I915_TTM_MAX_PLACEMENTS];
833b6e913e1SThomas Hellström 	struct ttm_placement placement;
834b6e913e1SThomas Hellström 
835b6e913e1SThomas Hellström 	GEM_BUG_ON(obj->mm.n_placements > I915_TTM_MAX_PLACEMENTS);
836b6e913e1SThomas Hellström 
837b6e913e1SThomas Hellström 	/* Move to the requested placement. */
838b6e913e1SThomas Hellström 	i915_ttm_placement_from_obj(obj, &requested, busy, &placement);
839b6e913e1SThomas Hellström 
840b6e913e1SThomas Hellström 	return __i915_ttm_get_pages(obj, &placement);
841b6e913e1SThomas Hellström }
842b6e913e1SThomas Hellström 
843b6e913e1SThomas Hellström /**
844b6e913e1SThomas Hellström  * DOC: Migration vs eviction
845b6e913e1SThomas Hellström  *
846b6e913e1SThomas Hellström  * GEM migration may not be the same as TTM migration / eviction. If
847b6e913e1SThomas Hellström  * the TTM core decides to evict an object it may be evicted to a
848b6e913e1SThomas Hellström  * TTM memory type that is not in the object's allowable GEM regions, or
849b6e913e1SThomas Hellström  * in fact theoretically to a TTM memory type that doesn't correspond to
850b6e913e1SThomas Hellström  * a GEM memory region. In that case the object's GEM region is not
851b6e913e1SThomas Hellström  * updated, and the data is migrated back to the GEM region at
852b6e913e1SThomas Hellström  * get_pages time. TTM may however set up CPU ptes to the object even
853b6e913e1SThomas Hellström  * when it is evicted.
854b6e913e1SThomas Hellström  * Gem forced migration using the i915_ttm_migrate() op, is allowed even
855b6e913e1SThomas Hellström  * to regions that are not in the object's list of allowable placements.
856b6e913e1SThomas Hellström  */
857503725c2SMatthew Auld static int __i915_ttm_migrate(struct drm_i915_gem_object *obj,
858503725c2SMatthew Auld 			      struct intel_memory_region *mr,
859503725c2SMatthew Auld 			      unsigned int flags)
860b6e913e1SThomas Hellström {
861b6e913e1SThomas Hellström 	struct ttm_place requested;
862b6e913e1SThomas Hellström 	struct ttm_placement placement;
863b6e913e1SThomas Hellström 	int ret;
864b6e913e1SThomas Hellström 
865ecbf2060SMatthew Auld 	i915_ttm_place_from_region(mr, &requested, obj->bo_offset,
866ecbf2060SMatthew Auld 				   obj->base.size, flags);
867b6e913e1SThomas Hellström 	placement.num_placement = 1;
868b6e913e1SThomas Hellström 	placement.num_busy_placement = 1;
869b6e913e1SThomas Hellström 	placement.placement = &requested;
870b6e913e1SThomas Hellström 	placement.busy_placement = &requested;
871b6e913e1SThomas Hellström 
872b6e913e1SThomas Hellström 	ret = __i915_ttm_get_pages(obj, &placement);
873b6e913e1SThomas Hellström 	if (ret)
874b6e913e1SThomas Hellström 		return ret;
875b6e913e1SThomas Hellström 
876b6e913e1SThomas Hellström 	/*
877b6e913e1SThomas Hellström 	 * Reinitialize the region bindings. This is primarily
878b6e913e1SThomas Hellström 	 * required for objects where the new region is not in
879b6e913e1SThomas Hellström 	 * its allowable placements.
880b6e913e1SThomas Hellström 	 */
881b6e913e1SThomas Hellström 	if (obj->mm.region != mr) {
882b6e913e1SThomas Hellström 		i915_gem_object_release_memory_region(obj);
883b6e913e1SThomas Hellström 		i915_gem_object_init_memory_region(obj, mr);
884b6e913e1SThomas Hellström 	}
885b6e913e1SThomas Hellström 
886b6e913e1SThomas Hellström 	return 0;
887b6e913e1SThomas Hellström }
888b6e913e1SThomas Hellström 
889503725c2SMatthew Auld static int i915_ttm_migrate(struct drm_i915_gem_object *obj,
890695ddc93SMatthew Auld 			    struct intel_memory_region *mr,
891695ddc93SMatthew Auld 			    unsigned int flags)
892503725c2SMatthew Auld {
893695ddc93SMatthew Auld 	return __i915_ttm_migrate(obj, mr, flags);
894503725c2SMatthew Auld }
895503725c2SMatthew Auld 
896213d5092SThomas Hellström static void i915_ttm_put_pages(struct drm_i915_gem_object *obj,
897213d5092SThomas Hellström 			       struct sg_table *st)
898213d5092SThomas Hellström {
899213d5092SThomas Hellström 	/*
900213d5092SThomas Hellström 	 * We're currently not called from a shrinker, so put_pages()
901213d5092SThomas Hellström 	 * typically means the object is about to destroyed, or called
902213d5092SThomas Hellström 	 * from move_notify(). So just avoid doing much for now.
903213d5092SThomas Hellström 	 * If the object is not destroyed next, The TTM eviction logic
904213d5092SThomas Hellström 	 * and shrinkers will move it out if needed.
905213d5092SThomas Hellström 	 */
906cad7109aSThomas Hellström 
907cad7109aSThomas Hellström 	if (obj->mm.rsgt)
908cad7109aSThomas Hellström 		i915_refct_sgt_put(fetch_and_zero(&obj->mm.rsgt));
909213d5092SThomas Hellström }
910213d5092SThomas Hellström 
9113589fdbdSThomas Hellström /**
9123589fdbdSThomas Hellström  * i915_ttm_adjust_lru - Adjust an object's position on relevant LRU lists.
9133589fdbdSThomas Hellström  * @obj: The object
9143589fdbdSThomas Hellström  */
9153589fdbdSThomas Hellström void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj)
916213d5092SThomas Hellström {
917213d5092SThomas Hellström 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
9187ae03459SMatthew Auld 	struct i915_ttm_tt *i915_tt =
9197ae03459SMatthew Auld 		container_of(bo->ttm, typeof(*i915_tt), ttm);
920ebd4a8ecSMatthew Auld 	bool shrinkable =
921ebd4a8ecSMatthew Auld 		bo->ttm && i915_tt->filp && ttm_tt_is_populated(bo->ttm);
922213d5092SThomas Hellström 
923213d5092SThomas Hellström 	/*
924213d5092SThomas Hellström 	 * Don't manipulate the TTM LRUs while in TTM bo destruction.
925213d5092SThomas Hellström 	 * We're called through i915_ttm_delete_mem_notify().
926213d5092SThomas Hellström 	 */
927213d5092SThomas Hellström 	if (!kref_read(&bo->kref))
928213d5092SThomas Hellström 		return;
929213d5092SThomas Hellström 
930213d5092SThomas Hellström 	/*
931ebd4a8ecSMatthew Auld 	 * We skip managing the shrinker LRU in set_pages() and just manage
932ebd4a8ecSMatthew Auld 	 * everything here. This does at least solve the issue with having
933ebd4a8ecSMatthew Auld 	 * temporary shmem mappings(like with evicted lmem) not being visible to
934ebd4a8ecSMatthew Auld 	 * the shrinker. Only our shmem objects are shrinkable, everything else
935ebd4a8ecSMatthew Auld 	 * we keep as unshrinkable.
936ebd4a8ecSMatthew Auld 	 *
937ebd4a8ecSMatthew Auld 	 * To make sure everything plays nice we keep an extra shrink pin in TTM
938ebd4a8ecSMatthew Auld 	 * if the underlying pages are not currently shrinkable. Once we release
939ebd4a8ecSMatthew Auld 	 * our pin, like when the pages are moved to shmem, the pages will then
940ebd4a8ecSMatthew Auld 	 * be added to the shrinker LRU, assuming the caller isn't also holding
941ebd4a8ecSMatthew Auld 	 * a pin.
942ebd4a8ecSMatthew Auld 	 *
943ebd4a8ecSMatthew Auld 	 * TODO: consider maybe also bumping the shrinker list here when we have
944ebd4a8ecSMatthew Auld 	 * already unpinned it, which should give us something more like an LRU.
945d3cb30f8SThomas Hellström 	 *
946d3cb30f8SThomas Hellström 	 * TODO: There is a small window of opportunity for this function to
947d3cb30f8SThomas Hellström 	 * get called from eviction after we've dropped the last GEM refcount,
948d3cb30f8SThomas Hellström 	 * but before the TTM deleted flag is set on the object. Avoid
949d3cb30f8SThomas Hellström 	 * adjusting the shrinker list in such cases, since the object is
950d3cb30f8SThomas Hellström 	 * not available to the shrinker anyway due to its zero refcount.
951d3cb30f8SThomas Hellström 	 * To fix this properly we should move to a TTM shrinker LRU list for
952d3cb30f8SThomas Hellström 	 * these objects.
953ebd4a8ecSMatthew Auld 	 */
954d3cb30f8SThomas Hellström 	if (kref_get_unless_zero(&obj->base.refcount)) {
955ebd4a8ecSMatthew Auld 		if (shrinkable != obj->mm.ttm_shrinkable) {
956ebd4a8ecSMatthew Auld 			if (shrinkable) {
957ebd4a8ecSMatthew Auld 				if (obj->mm.madv == I915_MADV_WILLNEED)
958ebd4a8ecSMatthew Auld 					__i915_gem_object_make_shrinkable(obj);
959ebd4a8ecSMatthew Auld 				else
960ebd4a8ecSMatthew Auld 					__i915_gem_object_make_purgeable(obj);
961ebd4a8ecSMatthew Auld 			} else {
962ebd4a8ecSMatthew Auld 				i915_gem_object_make_unshrinkable(obj);
963ebd4a8ecSMatthew Auld 			}
964ebd4a8ecSMatthew Auld 
965ebd4a8ecSMatthew Auld 			obj->mm.ttm_shrinkable = shrinkable;
966ebd4a8ecSMatthew Auld 		}
967d3cb30f8SThomas Hellström 		i915_gem_object_put(obj);
968d3cb30f8SThomas Hellström 	}
969ebd4a8ecSMatthew Auld 
970ebd4a8ecSMatthew Auld 	/*
971213d5092SThomas Hellström 	 * Put on the correct LRU list depending on the MADV status
972213d5092SThomas Hellström 	 */
973213d5092SThomas Hellström 	spin_lock(&bo->bdev->lru_lock);
974ebd4a8ecSMatthew Auld 	if (shrinkable) {
9757ae03459SMatthew Auld 		/* Try to keep shmem_tt from being considered for shrinking. */
9767ae03459SMatthew Auld 		bo->priority = TTM_MAX_BO_PRIORITY - 1;
9777ae03459SMatthew Auld 	} else if (obj->mm.madv != I915_MADV_WILLNEED) {
978213d5092SThomas Hellström 		bo->priority = I915_TTM_PRIO_PURGE;
979213d5092SThomas Hellström 	} else if (!i915_gem_object_has_pages(obj)) {
980213d5092SThomas Hellström 		bo->priority = I915_TTM_PRIO_NO_PAGES;
981ba2c5d15SMatthew Auld 	} else {
98293735059SMatthew Auld 		struct ttm_resource_manager *man =
98393735059SMatthew Auld 			ttm_manager_type(bo->bdev, bo->resource->mem_type);
98493735059SMatthew Auld 
98593735059SMatthew Auld 		/*
98693735059SMatthew Auld 		 * If we need to place an LMEM resource which doesn't need CPU
98793735059SMatthew Auld 		 * access then we should try not to victimize mappable objects
98893735059SMatthew Auld 		 * first, since we likely end up stealing more of the mappable
98993735059SMatthew Auld 		 * portion. And likewise when we try to find space for a mappble
99093735059SMatthew Auld 		 * object, we know not to ever victimize objects that don't
99193735059SMatthew Auld 		 * occupy any mappable pages.
99293735059SMatthew Auld 		 */
99393735059SMatthew Auld 		if (i915_ttm_cpu_maps_iomem(bo->resource) &&
99493735059SMatthew Auld 		    i915_ttm_buddy_man_visible_size(man) < man->size &&
99593735059SMatthew Auld 		    !(obj->flags & I915_BO_ALLOC_GPU_ONLY))
99693735059SMatthew Auld 			bo->priority = I915_TTM_PRIO_NEEDS_CPU_ACCESS;
99793735059SMatthew Auld 		else
998ba2c5d15SMatthew Auld 			bo->priority = I915_TTM_PRIO_HAS_PAGES;
999213d5092SThomas Hellström 	}
1000213d5092SThomas Hellström 
1001fee2ede1SChristian König 	ttm_bo_move_to_lru_tail(bo);
1002213d5092SThomas Hellström 	spin_unlock(&bo->bdev->lru_lock);
1003213d5092SThomas Hellström }
1004213d5092SThomas Hellström 
1005213d5092SThomas Hellström /*
1006213d5092SThomas Hellström  * TTM-backed gem object destruction requires some clarification.
1007213d5092SThomas Hellström  * Basically we have two possibilities here. We can either rely on the
1008213d5092SThomas Hellström  * i915 delayed destruction and put the TTM object when the object
1009213d5092SThomas Hellström  * is idle. This would be detected by TTM which would bypass the
1010213d5092SThomas Hellström  * TTM delayed destroy handling. The other approach is to put the TTM
1011213d5092SThomas Hellström  * object early and rely on the TTM destroyed handling, and then free
1012213d5092SThomas Hellström  * the leftover parts of the GEM object once TTM's destroyed list handling is
1013213d5092SThomas Hellström  * complete. For now, we rely on the latter for two reasons:
1014213d5092SThomas Hellström  * a) TTM can evict an object even when it's on the delayed destroy list,
1015213d5092SThomas Hellström  * which in theory allows for complete eviction.
1016213d5092SThomas Hellström  * b) There is work going on in TTM to allow freeing an object even when
1017213d5092SThomas Hellström  * it's not idle, and using the TTM destroyed list handling could help us
1018213d5092SThomas Hellström  * benefit from that.
1019213d5092SThomas Hellström  */
1020213d5092SThomas Hellström static void i915_ttm_delayed_free(struct drm_i915_gem_object *obj)
1021213d5092SThomas Hellström {
1022068396bbSThomas Hellström 	GEM_BUG_ON(!obj->ttm.created);
1023068396bbSThomas Hellström 
1024213d5092SThomas Hellström 	ttm_bo_put(i915_gem_to_ttm(obj));
1025213d5092SThomas Hellström }
1026213d5092SThomas Hellström 
1027cf3e3e86SMaarten Lankhorst static vm_fault_t vm_fault_ttm(struct vm_fault *vmf)
1028cf3e3e86SMaarten Lankhorst {
1029cf3e3e86SMaarten Lankhorst 	struct vm_area_struct *area = vmf->vma;
10306385eb7aSThomas Hellström 	struct ttm_buffer_object *bo = area->vm_private_data;
1031ebd4a8ecSMatthew Auld 	struct drm_device *dev = bo->base.dev;
10326667d78aSNirmoy Das 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
1033ad74457aSAnshuman Gupta 	intel_wakeref_t wakeref = 0;
1034ebd4a8ecSMatthew Auld 	vm_fault_t ret;
1035ebd4a8ecSMatthew Auld 	int idx;
1036cf3e3e86SMaarten Lankhorst 
1037cf3e3e86SMaarten Lankhorst 	/* Sanity check that we allow writing into this object */
1038cf3e3e86SMaarten Lankhorst 	if (unlikely(i915_gem_object_is_readonly(obj) &&
1039cf3e3e86SMaarten Lankhorst 		     area->vm_flags & VM_WRITE))
1040cf3e3e86SMaarten Lankhorst 		return VM_FAULT_SIGBUS;
1041cf3e3e86SMaarten Lankhorst 
1042ebd4a8ecSMatthew Auld 	ret = ttm_bo_vm_reserve(bo, vmf);
1043ebd4a8ecSMatthew Auld 	if (ret)
1044ebd4a8ecSMatthew Auld 		return ret;
1045ebd4a8ecSMatthew Auld 
104603ee5956SMatthew Auld 	if (obj->mm.madv != I915_MADV_WILLNEED) {
104703ee5956SMatthew Auld 		dma_resv_unlock(bo->base.resv);
104803ee5956SMatthew Auld 		return VM_FAULT_SIGBUS;
104903ee5956SMatthew Auld 	}
105003ee5956SMatthew Auld 
1051503725c2SMatthew Auld 	if (!i915_ttm_resource_mappable(bo->resource)) {
1052503725c2SMatthew Auld 		int err = -ENODEV;
1053503725c2SMatthew Auld 		int i;
1054503725c2SMatthew Auld 
1055503725c2SMatthew Auld 		for (i = 0; i < obj->mm.n_placements; i++) {
1056503725c2SMatthew Auld 			struct intel_memory_region *mr = obj->mm.placements[i];
1057503725c2SMatthew Auld 			unsigned int flags;
1058503725c2SMatthew Auld 
1059503725c2SMatthew Auld 			if (!mr->io_size && mr->type != INTEL_MEMORY_SYSTEM)
1060503725c2SMatthew Auld 				continue;
1061503725c2SMatthew Auld 
1062503725c2SMatthew Auld 			flags = obj->flags;
1063503725c2SMatthew Auld 			flags &= ~I915_BO_ALLOC_GPU_ONLY;
1064503725c2SMatthew Auld 			err = __i915_ttm_migrate(obj, mr, flags);
1065503725c2SMatthew Auld 			if (!err)
1066503725c2SMatthew Auld 				break;
1067503725c2SMatthew Auld 		}
1068503725c2SMatthew Auld 
1069503725c2SMatthew Auld 		if (err) {
1070e5cedf98SNirmoy Das 			drm_dbg(dev, "Unable to make resource CPU accessible(err = %pe)\n",
1071e5cedf98SNirmoy Das 				ERR_PTR(err));
1072503725c2SMatthew Auld 			dma_resv_unlock(bo->base.resv);
1073ad74457aSAnshuman Gupta 			ret = VM_FAULT_SIGBUS;
1074ad74457aSAnshuman Gupta 			goto out_rpm;
1075503725c2SMatthew Auld 		}
1076503725c2SMatthew Auld 	}
1077503725c2SMatthew Auld 
1078625b7446SMatthew Auld 	if (i915_ttm_cpu_maps_iomem(bo->resource))
1079625b7446SMatthew Auld 		wakeref = intel_runtime_pm_get(&to_i915(obj->base.dev)->runtime_pm);
1080625b7446SMatthew Auld 
1081ebd4a8ecSMatthew Auld 	if (drm_dev_enter(dev, &idx)) {
1082ebd4a8ecSMatthew Auld 		ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
1083be373fadSMatthew Auld 					       TTM_BO_VM_NUM_PREFAULT);
1084ebd4a8ecSMatthew Auld 		drm_dev_exit(idx);
1085ebd4a8ecSMatthew Auld 	} else {
1086ebd4a8ecSMatthew Auld 		ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
1087ebd4a8ecSMatthew Auld 	}
1088ad74457aSAnshuman Gupta 
1089ebd4a8ecSMatthew Auld 	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
1090ad74457aSAnshuman Gupta 		goto out_rpm;
1091ad74457aSAnshuman Gupta 
10921cacd689SAnshuman Gupta 	/*
10931cacd689SAnshuman Gupta 	 * ttm_bo_vm_reserve() already has dma_resv_lock.
10941cacd689SAnshuman Gupta 	 * userfault_count is protected by dma_resv lock and rpm wakeref.
10951cacd689SAnshuman Gupta 	 */
1096ad74457aSAnshuman Gupta 	if (ret == VM_FAULT_NOPAGE && wakeref && !obj->userfault_count) {
1097ad74457aSAnshuman Gupta 		obj->userfault_count = 1;
10981cacd689SAnshuman Gupta 		spin_lock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
1099e66c8dcfSAnshuman Gupta 		list_add(&obj->userfault_link, &to_i915(obj->base.dev)->runtime_pm.lmem_userfault_list);
11001cacd689SAnshuman Gupta 		spin_unlock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
1101ccb0e027SMatthew Auld 
1102ccb0e027SMatthew Auld 		GEM_WARN_ON(!i915_ttm_cpu_maps_iomem(bo->resource));
1103ad74457aSAnshuman Gupta 	}
1104ad74457aSAnshuman Gupta 
1105ad74457aSAnshuman Gupta 	if (wakeref & CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND)
1106e66c8dcfSAnshuman Gupta 		intel_wakeref_auto(&to_i915(obj->base.dev)->runtime_pm.userfault_wakeref,
1107ad74457aSAnshuman Gupta 				   msecs_to_jiffies_timeout(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND));
1108ebd4a8ecSMatthew Auld 
1109ebd4a8ecSMatthew Auld 	i915_ttm_adjust_lru(obj);
1110ebd4a8ecSMatthew Auld 
1111ebd4a8ecSMatthew Auld 	dma_resv_unlock(bo->base.resv);
1112ad74457aSAnshuman Gupta 
1113ad74457aSAnshuman Gupta out_rpm:
1114ad74457aSAnshuman Gupta 	if (wakeref)
1115ad74457aSAnshuman Gupta 		intel_runtime_pm_put(&to_i915(obj->base.dev)->runtime_pm, wakeref);
1116ad74457aSAnshuman Gupta 
1117ebd4a8ecSMatthew Auld 	return ret;
1118cf3e3e86SMaarten Lankhorst }
1119cf3e3e86SMaarten Lankhorst 
1120cf3e3e86SMaarten Lankhorst static int
1121cf3e3e86SMaarten Lankhorst vm_access_ttm(struct vm_area_struct *area, unsigned long addr,
1122cf3e3e86SMaarten Lankhorst 	      void *buf, int len, int write)
1123cf3e3e86SMaarten Lankhorst {
1124cf3e3e86SMaarten Lankhorst 	struct drm_i915_gem_object *obj =
1125cf3e3e86SMaarten Lankhorst 		i915_ttm_to_gem(area->vm_private_data);
1126cf3e3e86SMaarten Lankhorst 
1127cf3e3e86SMaarten Lankhorst 	if (i915_gem_object_is_readonly(obj) && write)
1128cf3e3e86SMaarten Lankhorst 		return -EACCES;
1129cf3e3e86SMaarten Lankhorst 
1130cf3e3e86SMaarten Lankhorst 	return ttm_bo_vm_access(area, addr, buf, len, write);
1131cf3e3e86SMaarten Lankhorst }
1132cf3e3e86SMaarten Lankhorst 
1133cf3e3e86SMaarten Lankhorst static void ttm_vm_open(struct vm_area_struct *vma)
1134cf3e3e86SMaarten Lankhorst {
1135cf3e3e86SMaarten Lankhorst 	struct drm_i915_gem_object *obj =
1136cf3e3e86SMaarten Lankhorst 		i915_ttm_to_gem(vma->vm_private_data);
1137cf3e3e86SMaarten Lankhorst 
11386667d78aSNirmoy Das 	GEM_BUG_ON(i915_ttm_is_ghost_object(vma->vm_private_data));
1139cf3e3e86SMaarten Lankhorst 	i915_gem_object_get(obj);
1140cf3e3e86SMaarten Lankhorst }
1141cf3e3e86SMaarten Lankhorst 
1142cf3e3e86SMaarten Lankhorst static void ttm_vm_close(struct vm_area_struct *vma)
1143cf3e3e86SMaarten Lankhorst {
1144cf3e3e86SMaarten Lankhorst 	struct drm_i915_gem_object *obj =
1145cf3e3e86SMaarten Lankhorst 		i915_ttm_to_gem(vma->vm_private_data);
1146cf3e3e86SMaarten Lankhorst 
11476667d78aSNirmoy Das 	GEM_BUG_ON(i915_ttm_is_ghost_object(vma->vm_private_data));
1148cf3e3e86SMaarten Lankhorst 	i915_gem_object_put(obj);
1149cf3e3e86SMaarten Lankhorst }
1150cf3e3e86SMaarten Lankhorst 
1151cf3e3e86SMaarten Lankhorst static const struct vm_operations_struct vm_ops_ttm = {
1152cf3e3e86SMaarten Lankhorst 	.fault = vm_fault_ttm,
1153cf3e3e86SMaarten Lankhorst 	.access = vm_access_ttm,
1154cf3e3e86SMaarten Lankhorst 	.open = ttm_vm_open,
1155cf3e3e86SMaarten Lankhorst 	.close = ttm_vm_close,
1156cf3e3e86SMaarten Lankhorst };
1157cf3e3e86SMaarten Lankhorst 
1158cf3e3e86SMaarten Lankhorst static u64 i915_ttm_mmap_offset(struct drm_i915_gem_object *obj)
1159cf3e3e86SMaarten Lankhorst {
1160cf3e3e86SMaarten Lankhorst 	/* The ttm_bo must be allocated with I915_BO_ALLOC_USER */
1161cf3e3e86SMaarten Lankhorst 	GEM_BUG_ON(!drm_mm_node_allocated(&obj->base.vma_node.vm_node));
1162cf3e3e86SMaarten Lankhorst 
1163cf3e3e86SMaarten Lankhorst 	return drm_vma_node_offset_addr(&obj->base.vma_node);
1164cf3e3e86SMaarten Lankhorst }
1165cf3e3e86SMaarten Lankhorst 
11668ee262baSMatthew Auld static void i915_ttm_unmap_virtual(struct drm_i915_gem_object *obj)
11678ee262baSMatthew Auld {
11681cacd689SAnshuman Gupta 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
11691cacd689SAnshuman Gupta 	intel_wakeref_t wakeref = 0;
11701cacd689SAnshuman Gupta 
11711cacd689SAnshuman Gupta 	assert_object_held_shared(obj);
11721cacd689SAnshuman Gupta 
11731cacd689SAnshuman Gupta 	if (i915_ttm_cpu_maps_iomem(bo->resource)) {
11741cacd689SAnshuman Gupta 		wakeref = intel_runtime_pm_get(&to_i915(obj->base.dev)->runtime_pm);
11751cacd689SAnshuman Gupta 
11761cacd689SAnshuman Gupta 		/* userfault_count is protected by obj lock and rpm wakeref. */
11771cacd689SAnshuman Gupta 		if (obj->userfault_count) {
11781cacd689SAnshuman Gupta 			spin_lock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
11791cacd689SAnshuman Gupta 			list_del(&obj->userfault_link);
11801cacd689SAnshuman Gupta 			spin_unlock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
11811cacd689SAnshuman Gupta 			obj->userfault_count = 0;
11821cacd689SAnshuman Gupta 		}
11831cacd689SAnshuman Gupta 	}
11841cacd689SAnshuman Gupta 
1185ccb0e027SMatthew Auld 	GEM_WARN_ON(obj->userfault_count);
1186ccb0e027SMatthew Auld 
11878ee262baSMatthew Auld 	ttm_bo_unmap_virtual(i915_gem_to_ttm(obj));
11881cacd689SAnshuman Gupta 
11891cacd689SAnshuman Gupta 	if (wakeref)
11901cacd689SAnshuman Gupta 		intel_runtime_pm_put(&to_i915(obj->base.dev)->runtime_pm, wakeref);
11918ee262baSMatthew Auld }
11928ee262baSMatthew Auld 
11934bc2d574SMatthew Auld static const struct drm_i915_gem_object_ops i915_gem_ttm_obj_ops = {
1194213d5092SThomas Hellström 	.name = "i915_gem_object_ttm",
11955d12ffe6SMatthew Auld 	.flags = I915_GEM_OBJECT_IS_SHRINKABLE |
11965d12ffe6SMatthew Auld 		 I915_GEM_OBJECT_SELF_MANAGED_SHRINK_LIST,
1197213d5092SThomas Hellström 
1198213d5092SThomas Hellström 	.get_pages = i915_ttm_get_pages,
1199213d5092SThomas Hellström 	.put_pages = i915_ttm_put_pages,
12006ef295e3SMatthew Auld 	.truncate = i915_ttm_truncate,
1201ffa3fe08SMatthew Auld 	.shrink = i915_ttm_shrink,
12027ae03459SMatthew Auld 
1203213d5092SThomas Hellström 	.adjust_lru = i915_ttm_adjust_lru,
1204213d5092SThomas Hellström 	.delayed_free = i915_ttm_delayed_free,
1205b6e913e1SThomas Hellström 	.migrate = i915_ttm_migrate,
12067ae03459SMatthew Auld 
1207cf3e3e86SMaarten Lankhorst 	.mmap_offset = i915_ttm_mmap_offset,
12088ee262baSMatthew Auld 	.unmap_virtual = i915_ttm_unmap_virtual,
1209cf3e3e86SMaarten Lankhorst 	.mmap_ops = &vm_ops_ttm,
1210213d5092SThomas Hellström };
1211213d5092SThomas Hellström 
1212213d5092SThomas Hellström void i915_ttm_bo_destroy(struct ttm_buffer_object *bo)
1213213d5092SThomas Hellström {
1214213d5092SThomas Hellström 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
1215213d5092SThomas Hellström 
1216213d5092SThomas Hellström 	i915_gem_object_release_memory_region(obj);
1217cf3e3e86SMaarten Lankhorst 	mutex_destroy(&obj->ttm.get_io_page.lock);
1218068396bbSThomas Hellström 
1219068396bbSThomas Hellström 	if (obj->ttm.created) {
1220ebd4a8ecSMatthew Auld 		/*
1221ebd4a8ecSMatthew Auld 		 * We freely manage the shrinker LRU outide of the mm.pages life
1222ebd4a8ecSMatthew Auld 		 * cycle. As a result when destroying the object we should be
1223ebd4a8ecSMatthew Auld 		 * extra paranoid and ensure we remove it from the LRU, before
1224ebd4a8ecSMatthew Auld 		 * we free the object.
1225ebd4a8ecSMatthew Auld 		 *
1226ebd4a8ecSMatthew Auld 		 * Touching the ttm_shrinkable outside of the object lock here
1227ebd4a8ecSMatthew Auld 		 * should be safe now that the last GEM object ref was dropped.
1228ebd4a8ecSMatthew Auld 		 */
1229ebd4a8ecSMatthew Auld 		if (obj->mm.ttm_shrinkable)
1230ebd4a8ecSMatthew Auld 			i915_gem_object_make_unshrinkable(obj);
1231ebd4a8ecSMatthew Auld 
1232c56ce956SThomas Hellström 		i915_ttm_backup_free(obj);
1233c56ce956SThomas Hellström 
123448b09612SMaarten Lankhorst 		/* This releases all gem object bindings to the backend. */
123548b09612SMaarten Lankhorst 		__i915_gem_free_object(obj);
123648b09612SMaarten Lankhorst 
1237213d5092SThomas Hellström 		call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
1238068396bbSThomas Hellström 	} else {
1239068396bbSThomas Hellström 		__i915_gem_object_fini(obj);
1240068396bbSThomas Hellström 	}
1241213d5092SThomas Hellström }
1242213d5092SThomas Hellström 
1243213d5092SThomas Hellström /**
1244213d5092SThomas Hellström  * __i915_gem_ttm_object_init - Initialize a ttm-backed i915 gem object
1245213d5092SThomas Hellström  * @mem: The initial memory region for the object.
1246213d5092SThomas Hellström  * @obj: The gem object.
1247213d5092SThomas Hellström  * @size: Object size in bytes.
1248213d5092SThomas Hellström  * @flags: gem object flags.
1249213d5092SThomas Hellström  *
1250213d5092SThomas Hellström  * Return: 0 on success, negative error code on failure.
1251213d5092SThomas Hellström  */
1252213d5092SThomas Hellström int __i915_gem_ttm_object_init(struct intel_memory_region *mem,
1253213d5092SThomas Hellström 			       struct drm_i915_gem_object *obj,
12549b78b5daSMatthew Auld 			       resource_size_t offset,
1255213d5092SThomas Hellström 			       resource_size_t size,
1256d22632c8SMatthew Auld 			       resource_size_t page_size,
1257213d5092SThomas Hellström 			       unsigned int flags)
1258213d5092SThomas Hellström {
1259213d5092SThomas Hellström 	static struct lock_class_key lock_class;
1260213d5092SThomas Hellström 	struct drm_i915_private *i915 = mem->i915;
12613c2b8f32SThomas Hellström 	struct ttm_operation_ctx ctx = {
12623c2b8f32SThomas Hellström 		.interruptible = true,
12633c2b8f32SThomas Hellström 		.no_wait_gpu = false,
12643c2b8f32SThomas Hellström 	};
1265213d5092SThomas Hellström 	enum ttm_bo_type bo_type;
1266213d5092SThomas Hellström 	int ret;
1267213d5092SThomas Hellström 
1268213d5092SThomas Hellström 	drm_gem_private_object_init(&i915->drm, &obj->base, size);
1269213d5092SThomas Hellström 	i915_gem_object_init(obj, &i915_gem_ttm_obj_ops, &lock_class, flags);
1270068396bbSThomas Hellström 
1271ecbf2060SMatthew Auld 	obj->bo_offset = offset;
1272ecbf2060SMatthew Auld 
1273068396bbSThomas Hellström 	/* Don't put on a region list until we're either locked or fully initialized. */
12748b1f7f92SThomas Hellström 	obj->mm.region = mem;
1275068396bbSThomas Hellström 	INIT_LIST_HEAD(&obj->mm.region_link);
1276068396bbSThomas Hellström 
1277cf3e3e86SMaarten Lankhorst 	INIT_RADIX_TREE(&obj->ttm.get_io_page.radix, GFP_KERNEL | __GFP_NOWARN);
1278cf3e3e86SMaarten Lankhorst 	mutex_init(&obj->ttm.get_io_page.lock);
1279213d5092SThomas Hellström 	bo_type = (obj->flags & I915_BO_ALLOC_USER) ? ttm_bo_type_device :
1280213d5092SThomas Hellström 		ttm_bo_type_kernel;
1281213d5092SThomas Hellström 
12823c2b8f32SThomas Hellström 	obj->base.vma_node.driver_private = i915_gem_to_ttm(obj);
12833c2b8f32SThomas Hellström 
1284d22632c8SMatthew Auld 	/* Forcing the page size is kernel internal only */
1285d22632c8SMatthew Auld 	GEM_BUG_ON(page_size && obj->mm.n_placements);
1286d22632c8SMatthew Auld 
1287213d5092SThomas Hellström 	/*
1288ebd4a8ecSMatthew Auld 	 * Keep an extra shrink pin to prevent the object from being made
1289ebd4a8ecSMatthew Auld 	 * shrinkable too early. If the ttm_tt is ever allocated in shmem, we
1290ebd4a8ecSMatthew Auld 	 * drop the pin. The TTM backend manages the shrinker LRU itself,
1291ebd4a8ecSMatthew Auld 	 * outside of the normal mm.pages life cycle.
1292ebd4a8ecSMatthew Auld 	 */
1293ebd4a8ecSMatthew Auld 	i915_gem_object_make_unshrinkable(obj);
1294ebd4a8ecSMatthew Auld 
1295ebd4a8ecSMatthew Auld 	/*
1296213d5092SThomas Hellström 	 * If this function fails, it will call the destructor, but
1297213d5092SThomas Hellström 	 * our caller still owns the object. So no freeing in the
1298213d5092SThomas Hellström 	 * destructor until obj->ttm.created is true.
1299213d5092SThomas Hellström 	 * Similarly, in delayed_destroy, we can't call ttm_bo_put()
1300213d5092SThomas Hellström 	 * until successful initialization.
1301213d5092SThomas Hellström 	 */
1302347987a2SChristian König 	ret = ttm_bo_init_reserved(&i915->bdev, i915_gem_to_ttm(obj), bo_type,
1303347987a2SChristian König 				   &i915_sys_placement, page_size >> PAGE_SHIFT,
13043c2b8f32SThomas Hellström 				   &ctx, NULL, NULL, i915_ttm_bo_destroy);
13053c2b8f32SThomas Hellström 	if (ret)
1306b07a6483SThomas Hellström 		return i915_ttm_err_to_gem(ret);
1307213d5092SThomas Hellström 
1308213d5092SThomas Hellström 	obj->ttm.created = true;
1309068396bbSThomas Hellström 	i915_gem_object_release_memory_region(obj);
1310068396bbSThomas Hellström 	i915_gem_object_init_memory_region(obj, mem);
13113c2b8f32SThomas Hellström 	i915_ttm_adjust_domains_after_move(obj);
13123c2b8f32SThomas Hellström 	i915_ttm_adjust_gem_after_move(obj);
13133c2b8f32SThomas Hellström 	i915_gem_object_unlock(obj);
1314213d5092SThomas Hellström 
13153c2b8f32SThomas Hellström 	return 0;
1316213d5092SThomas Hellström }
131732b7cf51SThomas Hellström 
131832b7cf51SThomas Hellström static const struct intel_memory_region_ops ttm_system_region_ops = {
131932b7cf51SThomas Hellström 	.init_object = __i915_gem_ttm_object_init,
13208b1f7f92SThomas Hellström 	.release = intel_region_ttm_fini,
132132b7cf51SThomas Hellström };
132232b7cf51SThomas Hellström 
132332b7cf51SThomas Hellström struct intel_memory_region *
132432b7cf51SThomas Hellström i915_gem_ttm_system_setup(struct drm_i915_private *i915,
132532b7cf51SThomas Hellström 			  u16 type, u16 instance)
132632b7cf51SThomas Hellström {
132732b7cf51SThomas Hellström 	struct intel_memory_region *mr;
132832b7cf51SThomas Hellström 
132932b7cf51SThomas Hellström 	mr = intel_memory_region_create(i915, 0,
133032b7cf51SThomas Hellström 					totalram_pages() << PAGE_SHIFT,
1331235582caSMatthew Auld 					PAGE_SIZE, 0, 0,
133232b7cf51SThomas Hellström 					type, instance,
133332b7cf51SThomas Hellström 					&ttm_system_region_ops);
133432b7cf51SThomas Hellström 	if (IS_ERR(mr))
133532b7cf51SThomas Hellström 		return mr;
133632b7cf51SThomas Hellström 
133732b7cf51SThomas Hellström 	intel_memory_region_set_name(mr, "system-ttm");
133832b7cf51SThomas Hellström 	return mr;
1339213d5092SThomas Hellström }
1340