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_placement.h>
9a3185f91SChristian König #include <drm/ttm/ttm_tt.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  */
i915_ttm_sys_placement(void)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 
i915_ttm_err_to_gem(int err)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
i915_ttm_select_tt_caching(const struct drm_i915_gem_object * obj)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
i915_ttm_place_from_region(const struct intel_memory_region * mr,struct ttm_place * place,resource_size_t offset,resource_size_t size,unsigned int flags)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) {
1436949aa0eSGwan-gyeong Mun 		WARN_ON(overflows_type(offset >> PAGE_SHIFT, place->fpfn));
144ecbf2060SMatthew Auld 		place->fpfn = offset >> PAGE_SHIFT;
1456949aa0eSGwan-gyeong Mun 		WARN_ON(overflows_type(place->fpfn + (size >> PAGE_SHIFT), place->lpfn));
146ecbf2060SMatthew Auld 		place->lpfn = place->fpfn + (size >> PAGE_SHIFT);
147ecbf2060SMatthew Auld 	} else if (mr->io_size && mr->io_size < mr->total) {
14830b9d1b3SMatthew Auld 		if (flags & I915_BO_ALLOC_GPU_ONLY) {
14930b9d1b3SMatthew Auld 			place->flags |= TTM_PL_FLAG_TOPDOWN;
15030b9d1b3SMatthew Auld 		} else {
1513312a4acSMatthew Auld 			place->fpfn = 0;
1526949aa0eSGwan-gyeong Mun 			WARN_ON(overflows_type(mr->io_size >> PAGE_SHIFT, place->lpfn));
1533312a4acSMatthew Auld 			place->lpfn = mr->io_size >> PAGE_SHIFT;
1543312a4acSMatthew Auld 		}
15538f28c06SThomas Hellström 	}
15630b9d1b3SMatthew Auld }
15738f28c06SThomas Hellström 
15838f28c06SThomas Hellström 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)15938f28c06SThomas Hellström i915_ttm_placement_from_obj(const struct drm_i915_gem_object *obj,
16038f28c06SThomas Hellström 			    struct ttm_place *requested,
16138f28c06SThomas Hellström 			    struct ttm_place *busy,
16238f28c06SThomas Hellström 			    struct ttm_placement *placement)
16338f28c06SThomas Hellström {
16438f28c06SThomas Hellström 	unsigned int num_allowed = obj->mm.n_placements;
165beb6a229SMatthew Auld 	unsigned int flags = obj->flags;
16638f28c06SThomas Hellström 	unsigned int i;
16738f28c06SThomas Hellström 
16838f28c06SThomas Hellström 	placement->num_placement = 1;
16938f28c06SThomas Hellström 	i915_ttm_place_from_region(num_allowed ? obj->mm.placements[0] :
170ecbf2060SMatthew Auld 				   obj->mm.region, requested, obj->bo_offset,
171ecbf2060SMatthew Auld 				   obj->base.size, flags);
17238f28c06SThomas Hellström 
17338f28c06SThomas Hellström 	/* Cache this on object? */
17438f28c06SThomas Hellström 	placement->num_busy_placement = num_allowed;
17538f28c06SThomas Hellström 	for (i = 0; i < placement->num_busy_placement; ++i)
176ecbf2060SMatthew Auld 		i915_ttm_place_from_region(obj->mm.placements[i], busy + i,
177ecbf2060SMatthew Auld 					   obj->bo_offset, obj->base.size, flags);
17838f28c06SThomas Hellström 
17938f28c06SThomas Hellström 	if (num_allowed == 0) {
18038f28c06SThomas Hellström 		*busy = *requested;
18138f28c06SThomas Hellström 		placement->num_busy_placement = 1;
18238f28c06SThomas Hellström 	}
18338f28c06SThomas Hellström 
18438f28c06SThomas Hellström 	placement->placement = requested;
18538f28c06SThomas Hellström 	placement->busy_placement = busy;
18638f28c06SThomas Hellström }
18738f28c06SThomas Hellström 
i915_ttm_tt_shmem_populate(struct ttm_device * bdev,struct ttm_tt * ttm,struct ttm_operation_ctx * ctx)1887ae03459SMatthew Auld static int i915_ttm_tt_shmem_populate(struct ttm_device *bdev,
1897ae03459SMatthew Auld 				      struct ttm_tt *ttm,
1907ae03459SMatthew Auld 				      struct ttm_operation_ctx *ctx)
1917ae03459SMatthew Auld {
1927ae03459SMatthew Auld 	struct drm_i915_private *i915 = container_of(bdev, typeof(*i915), bdev);
1937ae03459SMatthew Auld 	struct intel_memory_region *mr = i915->mm.regions[INTEL_MEMORY_SYSTEM];
1947ae03459SMatthew Auld 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
19578a07fe7SRobert Beckett 	const unsigned int max_segment = i915_sg_segment_size(i915->drm.dev);
1965719d4feSRobert Beckett 	const size_t size = (size_t)ttm->num_pages << PAGE_SHIFT;
1977ae03459SMatthew Auld 	struct file *filp = i915_tt->filp;
1987ae03459SMatthew Auld 	struct sgt_iter sgt_iter;
1997ae03459SMatthew Auld 	struct sg_table *st;
2007ae03459SMatthew Auld 	struct page *page;
2017ae03459SMatthew Auld 	unsigned long i;
2027ae03459SMatthew Auld 	int err;
2037ae03459SMatthew Auld 
2047ae03459SMatthew Auld 	if (!filp) {
2057ae03459SMatthew Auld 		struct address_space *mapping;
2067ae03459SMatthew Auld 		gfp_t mask;
2077ae03459SMatthew Auld 
2087ae03459SMatthew Auld 		filp = shmem_file_setup("i915-shmem-tt", size, VM_NORESERVE);
2097ae03459SMatthew Auld 		if (IS_ERR(filp))
2107ae03459SMatthew Auld 			return PTR_ERR(filp);
2117ae03459SMatthew Auld 
2127ae03459SMatthew Auld 		mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
2137ae03459SMatthew Auld 
2147ae03459SMatthew Auld 		mapping = filp->f_mapping;
2157ae03459SMatthew Auld 		mapping_set_gfp_mask(mapping, mask);
2167ae03459SMatthew Auld 		GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
2177ae03459SMatthew Auld 
2187ae03459SMatthew Auld 		i915_tt->filp = filp;
2197ae03459SMatthew Auld 	}
2207ae03459SMatthew Auld 
221cad7109aSThomas Hellström 	st = &i915_tt->cached_rsgt.table;
222cad7109aSThomas Hellström 	err = shmem_sg_alloc_table(i915, st, size, mr, filp->f_mapping,
223cad7109aSThomas Hellström 				   max_segment);
224cad7109aSThomas Hellström 	if (err)
225cad7109aSThomas Hellström 		return err;
2267ae03459SMatthew Auld 
227cad7109aSThomas Hellström 	err = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL,
2287ae03459SMatthew Auld 			      DMA_ATTR_SKIP_CPU_SYNC);
229cad7109aSThomas Hellström 	if (err)
2307ae03459SMatthew Auld 		goto err_free_st;
2317ae03459SMatthew Auld 
2327ae03459SMatthew Auld 	i = 0;
2337ae03459SMatthew Auld 	for_each_sgt_page(page, sgt_iter, st)
2347ae03459SMatthew Auld 		ttm->pages[i++] = page;
2357ae03459SMatthew Auld 
2367ae03459SMatthew Auld 	if (ttm->page_flags & TTM_TT_FLAG_SWAPPED)
2377ae03459SMatthew Auld 		ttm->page_flags &= ~TTM_TT_FLAG_SWAPPED;
2387ae03459SMatthew Auld 
2397ae03459SMatthew Auld 	return 0;
2407ae03459SMatthew Auld 
2417ae03459SMatthew Auld err_free_st:
242cad7109aSThomas Hellström 	shmem_sg_free_table(st, filp->f_mapping, false, false);
243cad7109aSThomas Hellström 
2447ae03459SMatthew Auld 	return err;
2457ae03459SMatthew Auld }
2467ae03459SMatthew Auld 
i915_ttm_tt_shmem_unpopulate(struct ttm_tt * ttm)2477ae03459SMatthew Auld static void i915_ttm_tt_shmem_unpopulate(struct ttm_tt *ttm)
2487ae03459SMatthew Auld {
2497ae03459SMatthew Auld 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
2507ae03459SMatthew Auld 	bool backup = ttm->page_flags & TTM_TT_FLAG_SWAPPED;
251cad7109aSThomas Hellström 	struct sg_table *st = &i915_tt->cached_rsgt.table;
2527ae03459SMatthew Auld 
253cad7109aSThomas Hellström 	shmem_sg_free_table(st, file_inode(i915_tt->filp)->i_mapping,
2547ae03459SMatthew Auld 			    backup, backup);
2557ae03459SMatthew Auld }
2567ae03459SMatthew Auld 
i915_ttm_tt_release(struct kref * ref)257cad7109aSThomas Hellström static void i915_ttm_tt_release(struct kref *ref)
258cad7109aSThomas Hellström {
259cad7109aSThomas Hellström 	struct i915_ttm_tt *i915_tt =
260cad7109aSThomas Hellström 		container_of(ref, typeof(*i915_tt), cached_rsgt.kref);
261cad7109aSThomas Hellström 	struct sg_table *st = &i915_tt->cached_rsgt.table;
262cad7109aSThomas Hellström 
263cad7109aSThomas Hellström 	GEM_WARN_ON(st->sgl);
264cad7109aSThomas Hellström 
265cad7109aSThomas Hellström 	kfree(i915_tt);
266cad7109aSThomas Hellström }
267cad7109aSThomas Hellström 
268cad7109aSThomas Hellström static const struct i915_refct_sgt_ops tt_rsgt_ops = {
269cad7109aSThomas Hellström 	.release = i915_ttm_tt_release
270cad7109aSThomas Hellström };
271cad7109aSThomas Hellström 
i915_ttm_tt_create(struct ttm_buffer_object * bo,uint32_t page_flags)272213d5092SThomas Hellström static struct ttm_tt *i915_ttm_tt_create(struct ttm_buffer_object *bo,
273213d5092SThomas Hellström 					 uint32_t page_flags)
274213d5092SThomas Hellström {
27576a6d563SRamalingam C 	struct drm_i915_private *i915 = container_of(bo->bdev, typeof(*i915),
27676a6d563SRamalingam C 						     bdev);
277213d5092SThomas Hellström 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
278213d5092SThomas Hellström 	unsigned long ccs_pages = 0;
279213d5092SThomas Hellström 	enum ttm_caching caching;
28076a6d563SRamalingam C 	struct i915_ttm_tt *i915_tt;
2816385eb7aSThomas Hellström 	int ret;
282213d5092SThomas Hellström 
283213d5092SThomas Hellström 	if (i915_ttm_is_ghost_object(bo))
284213d5092SThomas Hellström 		return NULL;
2856667d78aSNirmoy Das 
2866385eb7aSThomas Hellström 	i915_tt = kzalloc(sizeof(*i915_tt), GFP_KERNEL);
2876385eb7aSThomas Hellström 	if (!i915_tt)
288213d5092SThomas Hellström 		return NULL;
289213d5092SThomas Hellström 
290213d5092SThomas Hellström 	if (obj->flags & I915_BO_ALLOC_CPU_CLEAR && (!bo->resource ||
291213d5092SThomas Hellström 	    ttm_manager_type(bo->bdev, bo->resource->mem_type)->use_tt))
292213d5092SThomas Hellström 		page_flags |= TTM_TT_FLAG_ZERO_ALLOC;
293213d5092SThomas Hellström 
29443d46f0bSMatthew Auld 	caching = i915_ttm_select_tt_caching(obj);
295213d5092SThomas Hellström 	if (i915_gem_object_is_shrinkable(obj) && caching == ttm_cached) {
2966385eb7aSThomas Hellström 		page_flags |= TTM_TT_FLAG_EXTERNAL |
2977ae03459SMatthew Auld 			      TTM_TT_FLAG_EXTERNAL_MAPPABLE;
2987ae03459SMatthew Auld 		i915_tt->is_shmem = true;
2997ae03459SMatthew Auld 	}
3007ae03459SMatthew Auld 
301213d5092SThomas Hellström 	if (i915_gem_object_needs_ccs_pages(obj))
302213d5092SThomas Hellström 		ccs_pages = DIV_ROUND_UP(DIV_ROUND_UP(bo->base.size,
303873fef88SMatthew Auld 						      NUM_BYTES_PER_CCS_BYTE),
30476a6d563SRamalingam C 					 PAGE_SIZE);
30576a6d563SRamalingam C 
30676a6d563SRamalingam C 	ret = ttm_tt_init(&i915_tt->ttm, bo, page_flags, caching, ccs_pages);
30776a6d563SRamalingam C 	if (ret)
30876a6d563SRamalingam C 		goto err_free;
3097ae03459SMatthew Auld 
3107ae03459SMatthew Auld 	__i915_refct_sgt_init(&i915_tt->cached_rsgt, bo->base.size,
3117ae03459SMatthew Auld 			      &tt_rsgt_ops);
312cad7109aSThomas Hellström 
313cad7109aSThomas Hellström 	i915_tt->dev = obj->base.dev->dev;
314cad7109aSThomas Hellström 
315213d5092SThomas Hellström 	return &i915_tt->ttm;
316213d5092SThomas Hellström 
317213d5092SThomas Hellström err_free:
3187ae03459SMatthew Auld 	kfree(i915_tt);
3197ae03459SMatthew Auld 	return NULL;
3207ae03459SMatthew Auld }
3217ae03459SMatthew Auld 
i915_ttm_tt_populate(struct ttm_device * bdev,struct ttm_tt * ttm,struct ttm_operation_ctx * ctx)3227ae03459SMatthew Auld static int i915_ttm_tt_populate(struct ttm_device *bdev,
3237ae03459SMatthew Auld 				struct ttm_tt *ttm,
3247ae03459SMatthew Auld 				struct ttm_operation_ctx *ctx)
3257ae03459SMatthew Auld {
3267ae03459SMatthew Auld 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
3277ae03459SMatthew Auld 
3287ae03459SMatthew Auld 	if (i915_tt->is_shmem)
3297ae03459SMatthew Auld 		return i915_ttm_tt_shmem_populate(bdev, ttm, ctx);
3307ae03459SMatthew Auld 
3317ae03459SMatthew Auld 	return ttm_pool_alloc(&bdev->pool, ttm, ctx);
3327ae03459SMatthew Auld }
3337ae03459SMatthew Auld 
i915_ttm_tt_unpopulate(struct ttm_device * bdev,struct ttm_tt * ttm)334213d5092SThomas Hellström static void i915_ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm)
335213d5092SThomas Hellström {
336213d5092SThomas Hellström 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
337213d5092SThomas Hellström 	struct sg_table *st = &i915_tt->cached_rsgt.table;
338213d5092SThomas Hellström 
339cad7109aSThomas Hellström 	if (st->sgl)
340cad7109aSThomas Hellström 		dma_unmap_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0);
341cad7109aSThomas Hellström 
342cad7109aSThomas Hellström 	if (i915_tt->is_shmem) {
343213d5092SThomas Hellström 		i915_ttm_tt_shmem_unpopulate(ttm);
3447ae03459SMatthew Auld 	} else {
3457ae03459SMatthew Auld 		sg_free_table(st);
3467ae03459SMatthew Auld 		ttm_pool_free(&bdev->pool, ttm);
347cad7109aSThomas Hellström 	}
348213d5092SThomas Hellström }
349213d5092SThomas Hellström 
i915_ttm_tt_destroy(struct ttm_device * bdev,struct ttm_tt * ttm)3507ae03459SMatthew Auld static void i915_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
351213d5092SThomas Hellström {
352213d5092SThomas Hellström 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
353213d5092SThomas Hellström 
354213d5092SThomas Hellström 	if (i915_tt->filp)
355213d5092SThomas Hellström 		fput(i915_tt->filp);
3567ae03459SMatthew Auld 
3577ae03459SMatthew Auld 	ttm_tt_fini(ttm);
3587ae03459SMatthew Auld 	i915_refct_sgt_put(&i915_tt->cached_rsgt);
359c865204eSThomas Hellström }
360cad7109aSThomas Hellström 
i915_ttm_eviction_valuable(struct ttm_buffer_object * bo,const struct ttm_place * place)361213d5092SThomas Hellström static bool i915_ttm_eviction_valuable(struct ttm_buffer_object *bo,
362213d5092SThomas Hellström 				       const struct ttm_place *place)
363213d5092SThomas Hellström {
364213d5092SThomas Hellström 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
365213d5092SThomas Hellström 
366213d5092SThomas Hellström 	if (i915_ttm_is_ghost_object(bo))
367213d5092SThomas Hellström 		return false;
3686667d78aSNirmoy Das 
3696385eb7aSThomas Hellström 	/*
3706385eb7aSThomas Hellström 	 * EXTERNAL objects should never be swapped out by TTM, instead we need
3717ae03459SMatthew Auld 	 * to handle that ourselves. TTM will already skip such objects for us,
3727ae03459SMatthew Auld 	 * but we would like to avoid grabbing locks for no good reason.
3737ae03459SMatthew Auld 	 */
3747ae03459SMatthew Auld 	if (bo->ttm && bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
3757ae03459SMatthew Auld 		return false;
3767ae03459SMatthew Auld 
3776164807dSDan Carpenter 	/* Will do for now. Our pinned objects are still on TTM's LRU lists */
3787ae03459SMatthew Auld 	if (!i915_gem_object_evictable(obj))
379213d5092SThomas Hellström 		return false;
38093735059SMatthew Auld 
38193735059SMatthew Auld 	return ttm_bo_eviction_valuable(bo, place);
38293735059SMatthew Auld }
38392b2b55eSArunpravin Paneer Selvam 
i915_ttm_evict_flags(struct ttm_buffer_object * bo,struct ttm_placement * placement)384213d5092SThomas Hellström static void i915_ttm_evict_flags(struct ttm_buffer_object *bo,
385213d5092SThomas Hellström 				 struct ttm_placement *placement)
386213d5092SThomas Hellström {
387213d5092SThomas Hellström 	*placement = i915_sys_placement;
388213d5092SThomas Hellström }
389213d5092SThomas Hellström 
390213d5092SThomas Hellström /**
391213d5092SThomas Hellström  * i915_ttm_free_cached_io_rsgt - Free object cached LMEM information
3923589fdbdSThomas Hellström  * @obj: The GEM object
3933589fdbdSThomas Hellström  * This function frees any LMEM-related information that is cached on
3943589fdbdSThomas Hellström  * the object. For example the radix tree for fast page lookup and the
3953589fdbdSThomas Hellström  * cached refcounted sg-table
3963589fdbdSThomas Hellström  */
i915_ttm_free_cached_io_rsgt(struct drm_i915_gem_object * obj)3973589fdbdSThomas Hellström void i915_ttm_free_cached_io_rsgt(struct drm_i915_gem_object *obj)
3983589fdbdSThomas Hellström {
3993589fdbdSThomas Hellström 	struct radix_tree_iter iter;
400213d5092SThomas Hellström 	void __rcu **slot;
401cf3e3e86SMaarten Lankhorst 
402cf3e3e86SMaarten Lankhorst 	if (!obj->ttm.cached_io_rsgt)
403cf3e3e86SMaarten Lankhorst 		return;
404cad7109aSThomas Hellström 
405cf3e3e86SMaarten Lankhorst 	rcu_read_lock();
406cf3e3e86SMaarten Lankhorst 	radix_tree_for_each_slot(slot, &obj->ttm.get_io_page.radix, &iter, 0)
407cf3e3e86SMaarten Lankhorst 		radix_tree_delete(&obj->ttm.get_io_page.radix, iter.index);
408cf3e3e86SMaarten Lankhorst 	rcu_read_unlock();
409cf3e3e86SMaarten Lankhorst 
410cf3e3e86SMaarten Lankhorst 	i915_refct_sgt_put(obj->ttm.cached_io_rsgt);
411cf3e3e86SMaarten Lankhorst 	obj->ttm.cached_io_rsgt = NULL;
412cad7109aSThomas Hellström }
413cad7109aSThomas Hellström 
414213d5092SThomas Hellström /**
415213d5092SThomas Hellström  * i915_ttm_purge - Clear an object of its memory
4163589fdbdSThomas Hellström  * @obj: The object
4173589fdbdSThomas Hellström  *
4183589fdbdSThomas Hellström  * This function is called to clear an object of it's memory when it is
4193589fdbdSThomas Hellström  * marked as not needed anymore.
4203589fdbdSThomas Hellström  *
4213589fdbdSThomas Hellström  * Return: 0 on success, negative error code on failure.
4223589fdbdSThomas Hellström  */
i915_ttm_purge(struct drm_i915_gem_object * obj)4233589fdbdSThomas Hellström int i915_ttm_purge(struct drm_i915_gem_object *obj)
42432b7cf51SThomas Hellström {
4253589fdbdSThomas Hellström 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
426213d5092SThomas Hellström 	struct i915_ttm_tt *i915_tt =
427213d5092SThomas Hellström 		container_of(bo->ttm, typeof(*i915_tt), ttm);
4287ae03459SMatthew Auld 	struct ttm_operation_ctx ctx = {
4297ae03459SMatthew Auld 		.interruptible = true,
430213d5092SThomas Hellström 		.no_wait_gpu = false,
431213d5092SThomas Hellström 	};
432213d5092SThomas Hellström 	struct ttm_placement place = {};
433213d5092SThomas Hellström 	int ret;
434213d5092SThomas Hellström 
435213d5092SThomas Hellström 	if (obj->mm.madv == __I915_MADV_PURGED)
436213d5092SThomas Hellström 		return 0;
437213d5092SThomas Hellström 
4387ae03459SMatthew Auld 	ret = ttm_bo_validate(bo, &place, &ctx);
439213d5092SThomas Hellström 	if (ret)
440213d5092SThomas Hellström 		return ret;
4417ae03459SMatthew Auld 
4427ae03459SMatthew Auld 	if (bo->ttm && i915_tt->filp) {
4437ae03459SMatthew Auld 		/*
4447ae03459SMatthew Auld 		 * The below fput(which eventually calls shmem_truncate) might
4457ae03459SMatthew Auld 		 * be delayed by worker, so when directly called to purge the
4467ae03459SMatthew Auld 		 * pages(like by the shrinker) we should try to be more
4477ae03459SMatthew Auld 		 * aggressive and release the pages immediately.
4487ae03459SMatthew Auld 		 */
4497ae03459SMatthew Auld 		shmem_truncate_range(file_inode(i915_tt->filp),
4507ae03459SMatthew Auld 				     0, (loff_t)-1);
4517ae03459SMatthew Auld 		fput(fetch_and_zero(&i915_tt->filp));
4527ae03459SMatthew Auld 	}
4537ae03459SMatthew Auld 
4547ae03459SMatthew Auld 	obj->write_domain = 0;
4557ae03459SMatthew Auld 	obj->read_domains = 0;
4563c2b8f32SThomas Hellström 	i915_ttm_adjust_gem_after_move(obj);
4573c2b8f32SThomas Hellström 	i915_ttm_free_cached_io_rsgt(obj);
4583c2b8f32SThomas Hellström 	obj->mm.madv = __I915_MADV_PURGED;
459cad7109aSThomas Hellström 
460213d5092SThomas Hellström 	return 0;
4613589fdbdSThomas Hellström }
4627ae03459SMatthew Auld 
i915_ttm_shrink(struct drm_i915_gem_object * obj,unsigned int flags)463213d5092SThomas Hellström static int i915_ttm_shrink(struct drm_i915_gem_object *obj, unsigned int flags)
4647ae03459SMatthew Auld {
465ffa3fe08SMatthew Auld 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
4667ae03459SMatthew Auld 	struct i915_ttm_tt *i915_tt =
4677ae03459SMatthew Auld 		container_of(bo->ttm, typeof(*i915_tt), ttm);
4687ae03459SMatthew Auld 	struct ttm_operation_ctx ctx = {
4697ae03459SMatthew Auld 		.interruptible = true,
4707ae03459SMatthew Auld 		.no_wait_gpu = flags & I915_GEM_OBJECT_SHRINK_NO_GPU_WAIT,
4717ae03459SMatthew Auld 	};
472ffa3fe08SMatthew Auld 	struct ttm_placement place = {};
4737ae03459SMatthew Auld 	int ret;
4747ae03459SMatthew Auld 
4757ae03459SMatthew Auld 	if (!bo->ttm || i915_ttm_cpu_maps_iomem(bo->resource))
4767ae03459SMatthew Auld 		return 0;
4777ae03459SMatthew Auld 
4787ae03459SMatthew Auld 	GEM_BUG_ON(!i915_tt->is_shmem);
4797ae03459SMatthew Auld 
4807ae03459SMatthew Auld 	if (!i915_tt->filp)
4817ae03459SMatthew Auld 		return 0;
4827ae03459SMatthew Auld 
4837ae03459SMatthew Auld 	ret = ttm_bo_wait_ctx(bo, &ctx);
4847ae03459SMatthew Auld 	if (ret)
485004746e4SThomas Hellström 		return ret;
486004746e4SThomas Hellström 
487004746e4SThomas Hellström 	switch (obj->mm.madv) {
488004746e4SThomas Hellström 	case I915_MADV_DONTNEED:
4897ae03459SMatthew Auld 		return i915_ttm_purge(obj);
4907ae03459SMatthew Auld 	case __I915_MADV_PURGED:
4917ae03459SMatthew Auld 		return 0;
4927ae03459SMatthew Auld 	}
4937ae03459SMatthew Auld 
4947ae03459SMatthew Auld 	if (bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED)
4957ae03459SMatthew Auld 		return 0;
4967ae03459SMatthew Auld 
4977ae03459SMatthew Auld 	bo->ttm->page_flags |= TTM_TT_FLAG_SWAPPED;
4987ae03459SMatthew Auld 	ret = ttm_bo_validate(bo, &place, &ctx);
4997ae03459SMatthew Auld 	if (ret) {
5007ae03459SMatthew Auld 		bo->ttm->page_flags &= ~TTM_TT_FLAG_SWAPPED;
5017ae03459SMatthew Auld 		return ret;
5027ae03459SMatthew Auld 	}
5037ae03459SMatthew Auld 
5047ae03459SMatthew Auld 	if (flags & I915_GEM_OBJECT_SHRINK_WRITEBACK)
5057ae03459SMatthew Auld 		__shmem_writeback(obj->base.size, i915_tt->filp->f_mapping);
506ffa3fe08SMatthew Auld 
5077ae03459SMatthew Auld 	return 0;
5087ae03459SMatthew Auld }
5097ae03459SMatthew Auld 
i915_ttm_delete_mem_notify(struct ttm_buffer_object * bo)510213d5092SThomas Hellström static void i915_ttm_delete_mem_notify(struct ttm_buffer_object *bo)
511213d5092SThomas Hellström {
512213d5092SThomas Hellström 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
513213d5092SThomas Hellström 
514213d5092SThomas Hellström 	/*
515213d5092SThomas Hellström 	 * This gets called twice by ttm, so long as we have a ttm resource or
5166667d78aSNirmoy Das 	 * ttm_tt then we can still safely call this. Due to pipeline-gutting,
517068396bbSThomas Hellström 	 * we maybe have NULL bo->resource, but in that case we should always
518cad7109aSThomas Hellström 	 * have a ttm alive (like if the pages are swapped out).
519213d5092SThomas Hellström 	 */
520213d5092SThomas Hellström 	if ((bo->resource || bo->ttm) && !i915_ttm_is_ghost_object(bo)) {
521213d5092SThomas Hellström 		__i915_gem_object_pages_fini(obj);
522cad7109aSThomas Hellström 		i915_ttm_free_cached_io_rsgt(obj);
523213d5092SThomas Hellström 	}
524213d5092SThomas Hellström }
525213d5092SThomas Hellström 
i915_ttm_tt_get_st(struct ttm_tt * ttm)526213d5092SThomas Hellström static struct i915_refct_sgt *i915_ttm_tt_get_st(struct ttm_tt *ttm)
527213d5092SThomas Hellström {
528cad7109aSThomas Hellström 	struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
529cad7109aSThomas Hellström 	struct sg_table *st;
530213d5092SThomas Hellström 	int ret;
531cad7109aSThomas Hellström 
53223852becSLinus Torvalds 	if (i915_tt->cached_rsgt.table.sgl)
53323852becSLinus Torvalds 		return i915_refct_sgt_get(&i915_tt->cached_rsgt);
53423852becSLinus Torvalds 
53578a07fe7SRobert Beckett 	st = &i915_tt->cached_rsgt.table;
53623852becSLinus Torvalds 	ret = sg_alloc_table_from_pages_segment(st,
537cad7109aSThomas Hellström 			ttm->pages, ttm->num_pages,
53823852becSLinus Torvalds 			0, (unsigned long)ttm->num_pages << PAGE_SHIFT,
539213d5092SThomas Hellström 			i915_sg_segment_size(i915_tt->dev), GFP_KERNEL);
540213d5092SThomas Hellström 	if (ret) {
541213d5092SThomas Hellström 		st->sgl = NULL;
542213d5092SThomas Hellström 		return ERR_PTR(ret);
543213d5092SThomas Hellström 	}
544213d5092SThomas Hellström 
545213d5092SThomas Hellström 	ret = dma_map_sgtable(i915_tt->dev, st, DMA_BIDIRECTIONAL, 0);
546213d5092SThomas Hellström 	if (ret) {
547cad7109aSThomas Hellström 		sg_free_table(st);
548213d5092SThomas Hellström 		return ERR_PTR(ret);
549213d5092SThomas Hellström 	}
5503589fdbdSThomas Hellström 
5513589fdbdSThomas Hellström 	return i915_refct_sgt_get(&i915_tt->cached_rsgt);
5523589fdbdSThomas Hellström }
5533589fdbdSThomas Hellström 
5543589fdbdSThomas Hellström /**
5553589fdbdSThomas Hellström  * i915_ttm_resource_get_st - Get a refcounted sg-table pointing to the
5563589fdbdSThomas Hellström  * resource memory
5573589fdbdSThomas Hellström  * @obj: The GEM object used for sg-table caching
5583589fdbdSThomas Hellström  * @res: The struct ttm_resource for which an sg-table is requested.
5593589fdbdSThomas Hellström  *
5603589fdbdSThomas Hellström  * This function returns a refcounted sg-table representing the memory
5613589fdbdSThomas Hellström  * pointed to by @res. If @res is the object's current resource it may also
5623589fdbdSThomas Hellström  * cache the sg_table on the object or attempt to access an already cached
5633589fdbdSThomas Hellström  * sg-table. The refcounted sg-table needs to be put when no-longer in use.
5643589fdbdSThomas Hellström  *
565213d5092SThomas Hellström  * Return: A valid pointer to a struct i915_refct_sgt or error pointer on
566213d5092SThomas Hellström  * failure.
567213d5092SThomas Hellström  */
568213d5092SThomas Hellström struct i915_refct_sgt *
i915_ttm_resource_get_st(struct drm_i915_gem_object * obj,struct ttm_resource * res)5699306b2b2SMatthew Auld i915_ttm_resource_get_st(struct drm_i915_gem_object *obj,
570213d5092SThomas Hellström 			 struct ttm_resource *res)
5713589fdbdSThomas Hellström {
572213d5092SThomas Hellström 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
573213d5092SThomas Hellström 	u32 page_alignment;
574bc99f120SMatthew Auld 
575bc99f120SMatthew Auld 	if (!i915_ttm_gtt_binds_lmem(res))
576bc99f120SMatthew Auld 		return i915_ttm_tt_get_st(bo->ttm);
577bc99f120SMatthew Auld 
5783c2b8f32SThomas Hellström 	page_alignment = bo->page_alignment << PAGE_SHIFT;
5793c2b8f32SThomas Hellström 	if (!page_alignment)
5803c2b8f32SThomas Hellström 		page_alignment = obj->mm.region->min_page_size;
5813c2b8f32SThomas Hellström 
5823589fdbdSThomas Hellström 	/*
583cad7109aSThomas Hellström 	 * If CPU mapping differs, we need to add the ttm_tt pages to
584cad7109aSThomas Hellström 	 * the resulting st. Might make sense for GGTT.
585cad7109aSThomas Hellström 	 */
586cad7109aSThomas Hellström 	GEM_WARN_ON(!i915_ttm_cpu_maps_iomem(res));
587cad7109aSThomas Hellström 	if (bo->resource == res) {
588bc99f120SMatthew Auld 		if (!obj->ttm.cached_io_rsgt) {
589bc99f120SMatthew Auld 			struct i915_refct_sgt *rsgt;
590cad7109aSThomas Hellström 
591cad7109aSThomas Hellström 			rsgt = intel_region_ttm_resource_to_rsgt(obj->mm.region,
592cad7109aSThomas Hellström 								 res,
593cad7109aSThomas Hellström 								 page_alignment);
594cad7109aSThomas Hellström 			if (IS_ERR(rsgt))
595cad7109aSThomas Hellström 				return rsgt;
596cad7109aSThomas Hellström 
597cad7109aSThomas Hellström 			obj->ttm.cached_io_rsgt = rsgt;
598bc99f120SMatthew Auld 		}
599bc99f120SMatthew Auld 		return i915_refct_sgt_get(obj->ttm.cached_io_rsgt);
600213d5092SThomas Hellström 	}
601213d5092SThomas Hellström 
6026ef295e3SMatthew Auld 	return intel_region_ttm_resource_to_rsgt(obj->mm.region, res,
6036ef295e3SMatthew Auld 						 page_alignment);
6046ef295e3SMatthew Auld }
60558377de4SChristian König 
i915_ttm_truncate(struct drm_i915_gem_object * obj)6066ef295e3SMatthew Auld static int i915_ttm_truncate(struct drm_i915_gem_object *obj)
6076ef295e3SMatthew Auld {
6086ef295e3SMatthew Auld 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
60958377de4SChristian König 	long err;
61058377de4SChristian König 
61158377de4SChristian König 	WARN_ON_ONCE(obj->mm.madv == I915_MADV_WILLNEED);
6125524b5e5SMatthew Auld 
61358377de4SChristian König 	err = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
61458377de4SChristian König 				    true, 15 * HZ);
6155524b5e5SMatthew Auld 	if (err < 0)
6166ef295e3SMatthew Auld 		return err;
6176ef295e3SMatthew Auld 	if (err == 0)
6186ef295e3SMatthew Auld 		return -EBUSY;
6196ef295e3SMatthew Auld 
6206ef295e3SMatthew Auld 	err = i915_ttm_move_notify(bo);
6216ef295e3SMatthew Auld 	if (err)
6226ef295e3SMatthew Auld 		return err;
6233589fdbdSThomas Hellström 
624213d5092SThomas Hellström 	return i915_ttm_purge(obj);
625213d5092SThomas Hellström }
6266385eb7aSThomas Hellström 
i915_ttm_swap_notify(struct ttm_buffer_object * bo)627213d5092SThomas Hellström static void i915_ttm_swap_notify(struct ttm_buffer_object *bo)
6286667d78aSNirmoy Das {
6296385eb7aSThomas Hellström 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
6306385eb7aSThomas Hellström 	int ret;
6316385eb7aSThomas Hellström 
6323589fdbdSThomas Hellström 	if (i915_ttm_is_ghost_object(bo))
6333589fdbdSThomas Hellström 		return;
6343589fdbdSThomas Hellström 
635213d5092SThomas Hellström 	ret = i915_ttm_move_notify(bo);
636213d5092SThomas Hellström 	GEM_WARN_ON(ret);
637213d5092SThomas Hellström 	GEM_WARN_ON(obj->ttm.cached_io_rsgt);
638bfe53be2SMatthew Auld 	if (!ret && obj->mm.madv != I915_MADV_WILLNEED)
639bfe53be2SMatthew Auld 		i915_ttm_purge(obj);
640bfe53be2SMatthew Auld }
641bfe53be2SMatthew Auld 
642bfe53be2SMatthew Auld /**
643bfe53be2SMatthew Auld  * i915_ttm_resource_mappable - Return true if the ttm resource is CPU
644bfe53be2SMatthew Auld  * accessible.
645bfe53be2SMatthew Auld  * @res: The TTM resource to check.
646bfe53be2SMatthew Auld  *
647503725c2SMatthew Auld  * This is interesting on small-BAR systems where we may encounter lmem objects
648503725c2SMatthew Auld  * that can't be accessed via the CPU.
649503725c2SMatthew Auld  */
i915_ttm_resource_mappable(struct ttm_resource * res)650503725c2SMatthew Auld bool i915_ttm_resource_mappable(struct ttm_resource *res)
651503725c2SMatthew Auld {
652503725c2SMatthew Auld 	struct i915_ttm_buddy_resource *bman_res = to_ttm_buddy_resource(res);
653e3c92eb4SSomalapuram Amaranath 
654503725c2SMatthew Auld 	if (!i915_ttm_cpu_maps_iomem(res))
655503725c2SMatthew Auld 		return true;
656cf3e3e86SMaarten Lankhorst 
657cf3e3e86SMaarten Lankhorst 	return bman_res->used_visible_size == PFN_UP(bman_res->base.size);
658bfe53be2SMatthew Auld }
659bfe53be2SMatthew Auld 
i915_ttm_io_mem_reserve(struct ttm_device * bdev,struct ttm_resource * mem)660bfe53be2SMatthew Auld static int i915_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
6616667d78aSNirmoy Das {
662bfe53be2SMatthew Auld 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(mem->bo);
663bfe53be2SMatthew Auld 	bool unknown_state;
664bfe53be2SMatthew Auld 
665bfe53be2SMatthew Auld 	if (i915_ttm_is_ghost_object(mem->bo))
666bfe53be2SMatthew Auld 		return -EINVAL;
667bfe53be2SMatthew Auld 
668bfe53be2SMatthew Auld 	if (!kref_get_unless_zero(&obj->base.refcount))
669bfe53be2SMatthew Auld 		return -EINVAL;
670bfe53be2SMatthew Auld 
671bfe53be2SMatthew Auld 	assert_object_held(obj);
672bfe53be2SMatthew Auld 
673bfe53be2SMatthew Auld 	unknown_state = i915_gem_object_has_unknown_state(obj);
6743589fdbdSThomas Hellström 	i915_gem_object_put(obj);
675cf3e3e86SMaarten Lankhorst 	if (unknown_state)
676cf3e3e86SMaarten Lankhorst 		return -EINVAL;
677503725c2SMatthew Auld 
678503725c2SMatthew Auld 	if (!i915_ttm_cpu_maps_iomem(mem))
679503725c2SMatthew Auld 		return 0;
680cf3e3e86SMaarten Lankhorst 
681cf3e3e86SMaarten Lankhorst 	if (!i915_ttm_resource_mappable(mem))
682cf3e3e86SMaarten Lankhorst 		return -EINVAL;
683cf3e3e86SMaarten Lankhorst 
684cf3e3e86SMaarten Lankhorst 	mem->bus.caching = ttm_write_combined;
685cf3e3e86SMaarten Lankhorst 	mem->bus.is_iomem = true;
686cf3e3e86SMaarten Lankhorst 
687cf3e3e86SMaarten Lankhorst 	return 0;
688cf3e3e86SMaarten Lankhorst }
689cf3e3e86SMaarten Lankhorst 
i915_ttm_io_mem_pfn(struct ttm_buffer_object * bo,unsigned long page_offset)690cf3e3e86SMaarten Lankhorst static unsigned long i915_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
6916385eb7aSThomas Hellström 					 unsigned long page_offset)
692cf3e3e86SMaarten Lankhorst {
693cf3e3e86SMaarten Lankhorst 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
6946667d78aSNirmoy Das 	struct scatterlist *sg;
695cf3e3e86SMaarten Lankhorst 	unsigned long base;
696cf3e3e86SMaarten Lankhorst 	unsigned int ofs;
6976385eb7aSThomas Hellström 
698f47e6306SChris Wilson 	GEM_BUG_ON(i915_ttm_is_ghost_object(bo));
699cf3e3e86SMaarten Lankhorst 	GEM_WARN_ON(bo->ttm);
700cf3e3e86SMaarten Lankhorst 
701cf3e3e86SMaarten Lankhorst 	base = obj->mm.region->iomap.base - obj->mm.region->region.start;
702cf3e3e86SMaarten Lankhorst 	sg = i915_gem_object_page_iter_get_sg(obj, &obj->ttm.get_io_page, page_offset, &ofs);
70326b15eb0SMatthew Auld 
70426b15eb0SMatthew Auld 	return ((base + sg_dma_address(sg)) >> PAGE_SHIFT) + ofs;
70526b15eb0SMatthew Auld }
70626b15eb0SMatthew Auld 
i915_ttm_access_memory(struct ttm_buffer_object * bo,unsigned long offset,void * buf,int len,int write)70726b15eb0SMatthew Auld static int i915_ttm_access_memory(struct ttm_buffer_object *bo,
70826b15eb0SMatthew Auld 				  unsigned long offset, void *buf,
70926b15eb0SMatthew Auld 				  int len, int write)
71026b15eb0SMatthew Auld {
71126b15eb0SMatthew Auld 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
71226b15eb0SMatthew Auld 	resource_size_t iomap = obj->mm.region->iomap.base -
71326b15eb0SMatthew Auld 		obj->mm.region->region.start;
71426b15eb0SMatthew Auld 	unsigned long page = offset >> PAGE_SHIFT;
71526b15eb0SMatthew Auld 	unsigned long bytes_left = len;
71626b15eb0SMatthew Auld 
71726b15eb0SMatthew Auld 	/*
71826b15eb0SMatthew Auld 	 * TODO: For now just let it fail if the resource is non-mappable,
71926b15eb0SMatthew Auld 	 * otherwise we need to perform the memcpy from the gpu here, without
72026b15eb0SMatthew Auld 	 * interfering with the object (like moving the entire thing).
72126b15eb0SMatthew Auld 	 */
72226b15eb0SMatthew Auld 	if (!i915_ttm_resource_mappable(bo->resource))
72326b15eb0SMatthew Auld 		return -EIO;
72426b15eb0SMatthew Auld 
72526b15eb0SMatthew Auld 	offset -= page << PAGE_SHIFT;
72626b15eb0SMatthew Auld 	do {
72726b15eb0SMatthew Auld 		unsigned long bytes = min(bytes_left, PAGE_SIZE - offset);
72826b15eb0SMatthew Auld 		void __iomem *ptr;
72926b15eb0SMatthew Auld 		dma_addr_t daddr;
73026b15eb0SMatthew Auld 
73126b15eb0SMatthew Auld 		daddr = i915_gem_object_get_dma_address(obj, page);
73226b15eb0SMatthew Auld 		ptr = ioremap_wc(iomap + daddr + offset, bytes);
73326b15eb0SMatthew Auld 		if (!ptr)
73426b15eb0SMatthew Auld 			return -EIO;
73526b15eb0SMatthew Auld 
73626b15eb0SMatthew Auld 		if (write)
73726b15eb0SMatthew Auld 			memcpy_toio(ptr, buf, bytes);
73826b15eb0SMatthew Auld 		else
73926b15eb0SMatthew Auld 			memcpy_fromio(buf, ptr, bytes);
74026b15eb0SMatthew Auld 		iounmap(ptr);
74126b15eb0SMatthew Auld 
74226b15eb0SMatthew Auld 		page++;
74326b15eb0SMatthew Auld 		buf += bytes;
74426b15eb0SMatthew Auld 		bytes_left -= bytes;
74526b15eb0SMatthew Auld 		offset = 0;
74626b15eb0SMatthew Auld 	} while (bytes_left);
7476385eb7aSThomas Hellström 
7486385eb7aSThomas Hellström 	return len;
7496385eb7aSThomas Hellström }
7506385eb7aSThomas Hellström 
751213d5092SThomas Hellström /*
752213d5092SThomas Hellström  * All callbacks need to take care not to downcast a struct ttm_buffer_object
7537ae03459SMatthew Auld  * without checking its subclass, since it might be a TTM ghost object.
754213d5092SThomas Hellström  */
755213d5092SThomas Hellström static struct ttm_device_funcs i915_ttm_bo_driver = {
756213d5092SThomas Hellström 	.ttm_tt_create = i915_ttm_tt_create,
757213d5092SThomas Hellström 	.ttm_tt_populate = i915_ttm_tt_populate,
758213d5092SThomas Hellström 	.ttm_tt_unpopulate = i915_ttm_tt_unpopulate,
759213d5092SThomas Hellström 	.ttm_tt_destroy = i915_ttm_tt_destroy,
760213d5092SThomas Hellström 	.eviction_valuable = i915_ttm_eviction_valuable,
761cf3e3e86SMaarten Lankhorst 	.evict_flags = i915_ttm_evict_flags,
762cf3e3e86SMaarten Lankhorst 	.move = i915_ttm_move,
76326b15eb0SMatthew Auld 	.swap_notify = i915_ttm_swap_notify,
764213d5092SThomas Hellström 	.delete_mem_notify = i915_ttm_delete_mem_notify,
765213d5092SThomas Hellström 	.io_mem_reserve = i915_ttm_io_mem_reserve,
766213d5092SThomas Hellström 	.io_mem_pfn = i915_ttm_io_mem_pfn,
767213d5092SThomas Hellström 	.access_memory = i915_ttm_access_memory,
768213d5092SThomas Hellström };
769213d5092SThomas Hellström 
770213d5092SThomas Hellström /**
771213d5092SThomas Hellström  * i915_ttm_driver - Return a pointer to the TTM device funcs
772213d5092SThomas Hellström  *
773213d5092SThomas Hellström  * Return: Pointer to statically allocated TTM device funcs.
774213d5092SThomas Hellström  */
i915_ttm_driver(void)775213d5092SThomas Hellström struct ttm_device_funcs *i915_ttm_driver(void)
776b6e913e1SThomas Hellström {
777b6e913e1SThomas Hellström 	return &i915_ttm_bo_driver;
778213d5092SThomas Hellström }
779213d5092SThomas Hellström 
__i915_ttm_get_pages(struct drm_i915_gem_object * obj,struct ttm_placement * placement)780213d5092SThomas Hellström static int __i915_ttm_get_pages(struct drm_i915_gem_object *obj,
781213d5092SThomas Hellström 				struct ttm_placement *placement)
782213d5092SThomas Hellström {
783213d5092SThomas Hellström 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
784b07a6483SThomas Hellström 	struct ttm_operation_ctx ctx = {
785213d5092SThomas Hellström 		.interruptible = true,
786213d5092SThomas Hellström 		.no_wait_gpu = false,
787b07a6483SThomas Hellström 	};
788b6e913e1SThomas Hellström 	int real_num_busy;
789b6e913e1SThomas Hellström 	int ret;
790b07a6483SThomas Hellström 
791b07a6483SThomas Hellström 	/* First try only the requested placement. No eviction. */
792b07a6483SThomas Hellström 	real_num_busy = fetch_and_zero(&placement->num_busy_placement);
793b07a6483SThomas Hellström 	ret = ttm_bo_validate(bo, placement, &ctx);
794b07a6483SThomas Hellström 	if (ret) {
795b07a6483SThomas Hellström 		ret = i915_ttm_err_to_gem(ret);
796b07a6483SThomas Hellström 		/*
797b07a6483SThomas Hellström 		 * Anything that wants to restart the operation gets to
798b07a6483SThomas Hellström 		 * do that.
799213d5092SThomas Hellström 		 */
800b07a6483SThomas Hellström 		if (ret == -EDEADLK || ret == -EINTR || ret == -ERESTARTSYS ||
801b07a6483SThomas Hellström 		    ret == -EAGAIN)
802b07a6483SThomas Hellström 			return ret;
803b07a6483SThomas Hellström 
804b6e913e1SThomas Hellström 		/*
805b6e913e1SThomas Hellström 		 * If the initial attempt fails, allow all accepted placements,
806213d5092SThomas Hellström 		 * evicting if necessary.
807b07a6483SThomas Hellström 		 */
808b07a6483SThomas Hellström 		placement->num_busy_placement = real_num_busy;
809213d5092SThomas Hellström 		ret = ttm_bo_validate(bo, placement, &ctx);
8103c2b8f32SThomas Hellström 		if (ret)
8113c2b8f32SThomas Hellström 			return i915_ttm_err_to_gem(ret);
8123c2b8f32SThomas Hellström 	}
8133c2b8f32SThomas Hellström 
8143c2b8f32SThomas Hellström 	if (bo->ttm && !ttm_tt_is_populated(bo->ttm)) {
8153c2b8f32SThomas Hellström 		ret = ttm_tt_populate(bo->bdev, bo->ttm, &ctx);
8163c2b8f32SThomas Hellström 		if (ret)
8173c2b8f32SThomas Hellström 			return ret;
8183c2b8f32SThomas Hellström 
81975e38285SJason Ekstrand 		i915_ttm_adjust_domains_after_move(obj);
820cad7109aSThomas Hellström 		i915_ttm_adjust_gem_after_move(obj);
821cad7109aSThomas Hellström 	}
822213d5092SThomas Hellström 
823cad7109aSThomas Hellström 	if (!i915_gem_object_has_pages(obj)) {
824cad7109aSThomas Hellström 		struct i915_refct_sgt *rsgt =
825cad7109aSThomas Hellström 			i915_ttm_resource_get_st(obj, bo->resource);
826cad7109aSThomas Hellström 
827cad7109aSThomas Hellström 		if (IS_ERR(rsgt))
8288c949515SMatthew Auld 			return PTR_ERR(rsgt);
82975e38285SJason Ekstrand 
830213d5092SThomas Hellström 		GEM_BUG_ON(obj->mm.rsgt);
83176a6d563SRamalingam C 		obj->mm.rsgt = rsgt;
832ebd4a8ecSMatthew Auld 		__i915_gem_object_set_pages(obj, &rsgt->table);
833213d5092SThomas Hellström 	}
834213d5092SThomas Hellström 
835213d5092SThomas Hellström 	GEM_BUG_ON(bo->ttm && ((obj->base.size >> PAGE_SHIFT) < bo->ttm->num_pages));
836b6e913e1SThomas Hellström 	i915_ttm_adjust_lru(obj);
837b6e913e1SThomas Hellström 	return ret;
838b6e913e1SThomas Hellström }
839b6e913e1SThomas Hellström 
i915_ttm_get_pages(struct drm_i915_gem_object * obj)840b6e913e1SThomas Hellström static int i915_ttm_get_pages(struct drm_i915_gem_object *obj)
841c3bfba9aSChris Wilson {
842c3bfba9aSChris Wilson 	struct ttm_place requested, busy[I915_TTM_MAX_PLACEMENTS];
843c3bfba9aSChris Wilson 	struct ttm_placement placement;
844c3bfba9aSChris Wilson 
845b6e913e1SThomas Hellström 	/* restricted by sg_alloc_table */
846b6e913e1SThomas Hellström 	if (overflows_type(obj->base.size >> PAGE_SHIFT, unsigned int))
847b6e913e1SThomas Hellström 		return -E2BIG;
848b6e913e1SThomas Hellström 
849b6e913e1SThomas Hellström 	GEM_BUG_ON(obj->mm.n_placements > I915_TTM_MAX_PLACEMENTS);
850b6e913e1SThomas Hellström 
851b6e913e1SThomas Hellström 	/* Move to the requested placement. */
852b6e913e1SThomas Hellström 	i915_ttm_placement_from_obj(obj, &requested, busy, &placement);
853b6e913e1SThomas Hellström 
854b6e913e1SThomas Hellström 	return __i915_ttm_get_pages(obj, &placement);
855b6e913e1SThomas Hellström }
856b6e913e1SThomas Hellström 
857b6e913e1SThomas Hellström /**
858b6e913e1SThomas Hellström  * DOC: Migration vs eviction
859b6e913e1SThomas Hellström  *
860b6e913e1SThomas Hellström  * GEM migration may not be the same as TTM migration / eviction. If
861b6e913e1SThomas Hellström  * the TTM core decides to evict an object it may be evicted to a
862b6e913e1SThomas Hellström  * TTM memory type that is not in the object's allowable GEM regions, or
863b6e913e1SThomas Hellström  * in fact theoretically to a TTM memory type that doesn't correspond to
864b6e913e1SThomas Hellström  * a GEM memory region. In that case the object's GEM region is not
865b6e913e1SThomas Hellström  * updated, and the data is migrated back to the GEM region at
866b6e913e1SThomas Hellström  * get_pages time. TTM may however set up CPU ptes to the object even
867503725c2SMatthew Auld  * when it is evicted.
868503725c2SMatthew Auld  * Gem forced migration using the i915_ttm_migrate() op, is allowed even
869503725c2SMatthew Auld  * to regions that are not in the object's list of allowable placements.
870b6e913e1SThomas Hellström  */
__i915_ttm_migrate(struct drm_i915_gem_object * obj,struct intel_memory_region * mr,unsigned int flags)871b6e913e1SThomas Hellström static int __i915_ttm_migrate(struct drm_i915_gem_object *obj,
872b6e913e1SThomas Hellström 			      struct intel_memory_region *mr,
873b6e913e1SThomas Hellström 			      unsigned int flags)
874b6e913e1SThomas Hellström {
875ecbf2060SMatthew Auld 	struct ttm_place requested;
876ecbf2060SMatthew Auld 	struct ttm_placement placement;
877b6e913e1SThomas Hellström 	int ret;
878b6e913e1SThomas Hellström 
879b6e913e1SThomas Hellström 	i915_ttm_place_from_region(mr, &requested, obj->bo_offset,
880b6e913e1SThomas Hellström 				   obj->base.size, flags);
881b6e913e1SThomas Hellström 	placement.num_placement = 1;
882b6e913e1SThomas Hellström 	placement.num_busy_placement = 1;
883b6e913e1SThomas Hellström 	placement.placement = &requested;
884b6e913e1SThomas Hellström 	placement.busy_placement = &requested;
885b6e913e1SThomas Hellström 
886b6e913e1SThomas Hellström 	ret = __i915_ttm_get_pages(obj, &placement);
887b6e913e1SThomas Hellström 	if (ret)
888b6e913e1SThomas Hellström 		return ret;
889b6e913e1SThomas Hellström 
890b6e913e1SThomas Hellström 	/*
891b6e913e1SThomas Hellström 	 * Reinitialize the region bindings. This is primarily
892b6e913e1SThomas Hellström 	 * required for objects where the new region is not in
893b6e913e1SThomas Hellström 	 * its allowable placements.
894b6e913e1SThomas Hellström 	 */
895b6e913e1SThomas Hellström 	if (obj->mm.region != mr) {
896b6e913e1SThomas Hellström 		i915_gem_object_release_memory_region(obj);
897b6e913e1SThomas Hellström 		i915_gem_object_init_memory_region(obj, mr);
898b6e913e1SThomas Hellström 	}
899503725c2SMatthew Auld 
900695ddc93SMatthew Auld 	return 0;
901695ddc93SMatthew Auld }
902503725c2SMatthew Auld 
i915_ttm_migrate(struct drm_i915_gem_object * obj,struct intel_memory_region * mr,unsigned int flags)903695ddc93SMatthew Auld static int i915_ttm_migrate(struct drm_i915_gem_object *obj,
904503725c2SMatthew Auld 			    struct intel_memory_region *mr,
905503725c2SMatthew Auld 			    unsigned int flags)
906213d5092SThomas Hellström {
907213d5092SThomas Hellström 	return __i915_ttm_migrate(obj, mr, flags);
908213d5092SThomas Hellström }
909213d5092SThomas Hellström 
i915_ttm_put_pages(struct drm_i915_gem_object * obj,struct sg_table * st)910213d5092SThomas Hellström static void i915_ttm_put_pages(struct drm_i915_gem_object *obj,
911213d5092SThomas Hellström 			       struct sg_table *st)
912213d5092SThomas Hellström {
913213d5092SThomas Hellström 	/*
914213d5092SThomas Hellström 	 * We're currently not called from a shrinker, so put_pages()
915213d5092SThomas Hellström 	 * typically means the object is about to destroyed, or called
916cad7109aSThomas Hellström 	 * from move_notify(). So just avoid doing much for now.
917cad7109aSThomas Hellström 	 * If the object is not destroyed next, The TTM eviction logic
918cad7109aSThomas Hellström 	 * and shrinkers will move it out if needed.
919213d5092SThomas Hellström 	 */
920213d5092SThomas Hellström 
9213589fdbdSThomas Hellström 	if (obj->mm.rsgt)
9223589fdbdSThomas Hellström 		i915_refct_sgt_put(fetch_and_zero(&obj->mm.rsgt));
9233589fdbdSThomas Hellström }
9243589fdbdSThomas Hellström 
9253589fdbdSThomas Hellström /**
926213d5092SThomas Hellström  * i915_ttm_adjust_lru - Adjust an object's position on relevant LRU lists.
927213d5092SThomas Hellström  * @obj: The object
9287ae03459SMatthew Auld  */
i915_ttm_adjust_lru(struct drm_i915_gem_object * obj)9297ae03459SMatthew Auld void i915_ttm_adjust_lru(struct drm_i915_gem_object *obj)
930ebd4a8ecSMatthew Auld {
931ebd4a8ecSMatthew Auld 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
932213d5092SThomas Hellström 	struct i915_ttm_tt *i915_tt =
933213d5092SThomas Hellström 		container_of(bo->ttm, typeof(*i915_tt), ttm);
934213d5092SThomas Hellström 	bool shrinkable =
935213d5092SThomas Hellström 		bo->ttm && i915_tt->filp && ttm_tt_is_populated(bo->ttm);
936213d5092SThomas Hellström 
937213d5092SThomas Hellström 	/*
938213d5092SThomas Hellström 	 * Don't manipulate the TTM LRUs while in TTM bo destruction.
939213d5092SThomas Hellström 	 * We're called through i915_ttm_delete_mem_notify().
940213d5092SThomas Hellström 	 */
941ebd4a8ecSMatthew Auld 	if (!kref_read(&bo->kref))
942ebd4a8ecSMatthew Auld 		return;
943ebd4a8ecSMatthew Auld 
944ebd4a8ecSMatthew Auld 	/*
945ebd4a8ecSMatthew Auld 	 * We skip managing the shrinker LRU in set_pages() and just manage
946ebd4a8ecSMatthew Auld 	 * everything here. This does at least solve the issue with having
947ebd4a8ecSMatthew Auld 	 * temporary shmem mappings(like with evicted lmem) not being visible to
948ebd4a8ecSMatthew Auld 	 * the shrinker. Only our shmem objects are shrinkable, everything else
949ebd4a8ecSMatthew Auld 	 * we keep as unshrinkable.
950ebd4a8ecSMatthew Auld 	 *
951ebd4a8ecSMatthew Auld 	 * To make sure everything plays nice we keep an extra shrink pin in TTM
952ebd4a8ecSMatthew Auld 	 * if the underlying pages are not currently shrinkable. Once we release
953ebd4a8ecSMatthew Auld 	 * our pin, like when the pages are moved to shmem, the pages will then
954ebd4a8ecSMatthew Auld 	 * be added to the shrinker LRU, assuming the caller isn't also holding
955d3cb30f8SThomas Hellström 	 * a pin.
956d3cb30f8SThomas Hellström 	 *
957d3cb30f8SThomas Hellström 	 * TODO: consider maybe also bumping the shrinker list here when we have
958d3cb30f8SThomas Hellström 	 * already unpinned it, which should give us something more like an LRU.
959d3cb30f8SThomas Hellström 	 *
960d3cb30f8SThomas Hellström 	 * TODO: There is a small window of opportunity for this function to
961d3cb30f8SThomas Hellström 	 * get called from eviction after we've dropped the last GEM refcount,
962d3cb30f8SThomas Hellström 	 * but before the TTM deleted flag is set on the object. Avoid
963ebd4a8ecSMatthew Auld 	 * adjusting the shrinker list in such cases, since the object is
964d3cb30f8SThomas Hellström 	 * not available to the shrinker anyway due to its zero refcount.
965ebd4a8ecSMatthew Auld 	 * To fix this properly we should move to a TTM shrinker LRU list for
966ebd4a8ecSMatthew Auld 	 * these objects.
967ebd4a8ecSMatthew Auld 	 */
968ebd4a8ecSMatthew Auld 	if (kref_get_unless_zero(&obj->base.refcount)) {
969ebd4a8ecSMatthew Auld 		if (shrinkable != obj->mm.ttm_shrinkable) {
970ebd4a8ecSMatthew Auld 			if (shrinkable) {
971ebd4a8ecSMatthew Auld 				if (obj->mm.madv == I915_MADV_WILLNEED)
972ebd4a8ecSMatthew Auld 					__i915_gem_object_make_shrinkable(obj);
973ebd4a8ecSMatthew Auld 				else
974ebd4a8ecSMatthew Auld 					__i915_gem_object_make_purgeable(obj);
975ebd4a8ecSMatthew Auld 			} else {
976ebd4a8ecSMatthew Auld 				i915_gem_object_make_unshrinkable(obj);
977d3cb30f8SThomas Hellström 			}
978d3cb30f8SThomas Hellström 
979ebd4a8ecSMatthew Auld 			obj->mm.ttm_shrinkable = shrinkable;
980ebd4a8ecSMatthew Auld 		}
981213d5092SThomas Hellström 		i915_gem_object_put(obj);
982213d5092SThomas Hellström 	}
983213d5092SThomas Hellström 
984ebd4a8ecSMatthew Auld 	/*
9857ae03459SMatthew Auld 	 * Put on the correct LRU list depending on the MADV status
9867ae03459SMatthew Auld 	 */
9877ae03459SMatthew Auld 	spin_lock(&bo->bdev->lru_lock);
988213d5092SThomas Hellström 	if (shrinkable) {
989213d5092SThomas Hellström 		/* Try to keep shmem_tt from being considered for shrinking. */
990213d5092SThomas Hellström 		bo->priority = TTM_MAX_BO_PRIORITY - 1;
991ba2c5d15SMatthew Auld 	} else if (obj->mm.madv != I915_MADV_WILLNEED) {
99293735059SMatthew Auld 		bo->priority = I915_TTM_PRIO_PURGE;
99393735059SMatthew Auld 	} else if (!i915_gem_object_has_pages(obj)) {
99493735059SMatthew Auld 		bo->priority = I915_TTM_PRIO_NO_PAGES;
99593735059SMatthew Auld 	} else {
99693735059SMatthew Auld 		struct ttm_resource_manager *man =
99793735059SMatthew Auld 			ttm_manager_type(bo->bdev, bo->resource->mem_type);
99893735059SMatthew Auld 
99993735059SMatthew Auld 		/*
100093735059SMatthew Auld 		 * If we need to place an LMEM resource which doesn't need CPU
100193735059SMatthew Auld 		 * access then we should try not to victimize mappable objects
100293735059SMatthew Auld 		 * first, since we likely end up stealing more of the mappable
100393735059SMatthew Auld 		 * portion. And likewise when we try to find space for a mappble
100493735059SMatthew Auld 		 * object, we know not to ever victimize objects that don't
100593735059SMatthew Auld 		 * occupy any mappable pages.
100693735059SMatthew Auld 		 */
100793735059SMatthew Auld 		if (i915_ttm_cpu_maps_iomem(bo->resource) &&
1008ba2c5d15SMatthew Auld 		    i915_ttm_buddy_man_visible_size(man) < man->size &&
1009213d5092SThomas Hellström 		    !(obj->flags & I915_BO_ALLOC_GPU_ONLY))
1010213d5092SThomas Hellström 			bo->priority = I915_TTM_PRIO_NEEDS_CPU_ACCESS;
1011fee2ede1SChristian König 		else
1012213d5092SThomas Hellström 			bo->priority = I915_TTM_PRIO_HAS_PAGES;
1013213d5092SThomas Hellström 	}
1014213d5092SThomas Hellström 
1015213d5092SThomas Hellström 	ttm_bo_move_to_lru_tail(bo);
1016213d5092SThomas Hellström 	spin_unlock(&bo->bdev->lru_lock);
1017213d5092SThomas Hellström }
1018213d5092SThomas Hellström 
1019213d5092SThomas Hellström /*
1020213d5092SThomas Hellström  * TTM-backed gem object destruction requires some clarification.
1021213d5092SThomas Hellström  * Basically we have two possibilities here. We can either rely on the
1022213d5092SThomas Hellström  * i915 delayed destruction and put the TTM object when the object
1023213d5092SThomas Hellström  * is idle. This would be detected by TTM which would bypass the
1024213d5092SThomas Hellström  * TTM delayed destroy handling. The other approach is to put the TTM
1025213d5092SThomas Hellström  * object early and rely on the TTM destroyed handling, and then free
1026213d5092SThomas Hellström  * the leftover parts of the GEM object once TTM's destroyed list handling is
1027213d5092SThomas Hellström  * complete. For now, we rely on the latter for two reasons:
1028213d5092SThomas Hellström  * a) TTM can evict an object even when it's on the delayed destroy list,
1029213d5092SThomas Hellström  * which in theory allows for complete eviction.
1030213d5092SThomas Hellström  * b) There is work going on in TTM to allow freeing an object even when
1031213d5092SThomas Hellström  * it's not idle, and using the TTM destroyed list handling could help us
1032068396bbSThomas Hellström  * benefit from that.
1033068396bbSThomas Hellström  */
i915_ttm_delayed_free(struct drm_i915_gem_object * obj)1034213d5092SThomas Hellström static void i915_ttm_delayed_free(struct drm_i915_gem_object *obj)
1035213d5092SThomas Hellström {
1036213d5092SThomas Hellström 	GEM_BUG_ON(!obj->ttm.created);
1037cf3e3e86SMaarten Lankhorst 
1038cf3e3e86SMaarten Lankhorst 	ttm_bo_put(i915_gem_to_ttm(obj));
1039cf3e3e86SMaarten Lankhorst }
10406385eb7aSThomas Hellström 
vm_fault_ttm(struct vm_fault * vmf)1041ebd4a8ecSMatthew Auld static vm_fault_t vm_fault_ttm(struct vm_fault *vmf)
10426667d78aSNirmoy Das {
1043ad74457aSAnshuman Gupta 	struct vm_area_struct *area = vmf->vma;
1044ebd4a8ecSMatthew Auld 	struct ttm_buffer_object *bo = area->vm_private_data;
1045ebd4a8ecSMatthew Auld 	struct drm_device *dev = bo->base.dev;
1046cf3e3e86SMaarten Lankhorst 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
1047cf3e3e86SMaarten Lankhorst 	intel_wakeref_t wakeref = 0;
1048cf3e3e86SMaarten Lankhorst 	vm_fault_t ret;
1049cf3e3e86SMaarten Lankhorst 	int idx;
1050cf3e3e86SMaarten Lankhorst 
1051cf3e3e86SMaarten Lankhorst 	/* Sanity check that we allow writing into this object */
1052ebd4a8ecSMatthew Auld 	if (unlikely(i915_gem_object_is_readonly(obj) &&
1053ebd4a8ecSMatthew Auld 		     area->vm_flags & VM_WRITE))
1054ebd4a8ecSMatthew Auld 		return VM_FAULT_SIGBUS;
1055ebd4a8ecSMatthew Auld 
105603ee5956SMatthew Auld 	ret = ttm_bo_vm_reserve(bo, vmf);
105703ee5956SMatthew Auld 	if (ret)
105803ee5956SMatthew Auld 		return ret;
105903ee5956SMatthew Auld 
106003ee5956SMatthew Auld 	if (obj->mm.madv != I915_MADV_WILLNEED) {
1061503725c2SMatthew Auld 		dma_resv_unlock(bo->base.resv);
1062503725c2SMatthew Auld 		return VM_FAULT_SIGBUS;
1063503725c2SMatthew Auld 	}
1064503725c2SMatthew Auld 
1065503725c2SMatthew Auld 	/*
1066503725c2SMatthew Auld 	 * This must be swapped out with shmem ttm_tt (pipeline-gutting).
1067503725c2SMatthew Auld 	 * Calling ttm_bo_validate() here with TTM_PL_SYSTEM should only go as
1068503725c2SMatthew Auld 	 * far as far doing a ttm_bo_move_null(), which should skip all the
1069503725c2SMatthew Auld 	 * other junk.
1070503725c2SMatthew Auld 	 */
1071503725c2SMatthew Auld 	if (!bo->resource) {
1072503725c2SMatthew Auld 		struct ttm_operation_ctx ctx = {
1073503725c2SMatthew Auld 			.interruptible = true,
1074503725c2SMatthew Auld 			.no_wait_gpu = true, /* should be idle already */
1075503725c2SMatthew Auld 		};
1076503725c2SMatthew Auld 		int err;
1077503725c2SMatthew Auld 
1078503725c2SMatthew Auld 		GEM_BUG_ON(!bo->ttm || !(bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED));
1079503725c2SMatthew Auld 
1080e5cedf98SNirmoy Das 		err = ttm_bo_validate(bo, i915_ttm_sys_placement(), &ctx);
1081e5cedf98SNirmoy Das 		if (err) {
1082503725c2SMatthew Auld 			dma_resv_unlock(bo->base.resv);
1083ad74457aSAnshuman Gupta 			return VM_FAULT_SIGBUS;
1084ad74457aSAnshuman Gupta 		}
1085503725c2SMatthew Auld 	} else if (!i915_ttm_resource_mappable(bo->resource)) {
1086503725c2SMatthew Auld 		int err = -ENODEV;
1087503725c2SMatthew Auld 		int i;
1088625b7446SMatthew Auld 
1089625b7446SMatthew Auld 		for (i = 0; i < obj->mm.n_placements; i++) {
1090625b7446SMatthew Auld 			struct intel_memory_region *mr = obj->mm.placements[i];
1091ebd4a8ecSMatthew Auld 			unsigned int flags;
1092ebd4a8ecSMatthew Auld 
1093be373fadSMatthew Auld 			if (!mr->io_size && mr->type != INTEL_MEMORY_SYSTEM)
1094ebd4a8ecSMatthew Auld 				continue;
1095ebd4a8ecSMatthew Auld 
1096ebd4a8ecSMatthew Auld 			flags = obj->flags;
1097ebd4a8ecSMatthew Auld 			flags &= ~I915_BO_ALLOC_GPU_ONLY;
1098ad74457aSAnshuman Gupta 			err = __i915_ttm_migrate(obj, mr, flags);
1099ebd4a8ecSMatthew Auld 			if (!err)
1100ad74457aSAnshuman Gupta 				break;
1101ad74457aSAnshuman Gupta 		}
11021cacd689SAnshuman Gupta 
11031cacd689SAnshuman Gupta 		if (err) {
11041cacd689SAnshuman Gupta 			drm_dbg(dev, "Unable to make resource CPU accessible(err = %pe)\n",
11051cacd689SAnshuman Gupta 				ERR_PTR(err));
1106ad74457aSAnshuman Gupta 			dma_resv_unlock(bo->base.resv);
1107ad74457aSAnshuman Gupta 			ret = VM_FAULT_SIGBUS;
11081cacd689SAnshuman Gupta 			goto out_rpm;
1109e66c8dcfSAnshuman Gupta 		}
11101cacd689SAnshuman Gupta 	}
1111ccb0e027SMatthew Auld 
1112ccb0e027SMatthew Auld 	if (i915_ttm_cpu_maps_iomem(bo->resource))
1113ad74457aSAnshuman Gupta 		wakeref = intel_runtime_pm_get(&to_i915(obj->base.dev)->runtime_pm);
1114ad74457aSAnshuman Gupta 
1115ad74457aSAnshuman Gupta 	if (drm_dev_enter(dev, &idx)) {
1116e66c8dcfSAnshuman Gupta 		ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
1117ad74457aSAnshuman Gupta 					       TTM_BO_VM_NUM_PREFAULT);
1118ebd4a8ecSMatthew Auld 		drm_dev_exit(idx);
1119ebd4a8ecSMatthew Auld 	} else {
1120ebd4a8ecSMatthew Auld 		ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
1121ebd4a8ecSMatthew Auld 	}
1122ad74457aSAnshuman Gupta 
1123ad74457aSAnshuman Gupta 	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
1124ad74457aSAnshuman Gupta 		goto out_rpm;
1125ad74457aSAnshuman Gupta 
1126ad74457aSAnshuman Gupta 	/*
1127ebd4a8ecSMatthew Auld 	 * ttm_bo_vm_reserve() already has dma_resv_lock.
1128cf3e3e86SMaarten Lankhorst 	 * userfault_count is protected by dma_resv lock and rpm wakeref.
1129cf3e3e86SMaarten Lankhorst 	 */
1130cf3e3e86SMaarten Lankhorst 	if (ret == VM_FAULT_NOPAGE && wakeref && !obj->userfault_count) {
1131cf3e3e86SMaarten Lankhorst 		obj->userfault_count = 1;
1132cf3e3e86SMaarten Lankhorst 		spin_lock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
1133cf3e3e86SMaarten Lankhorst 		list_add(&obj->userfault_link, &to_i915(obj->base.dev)->runtime_pm.lmem_userfault_list);
1134cf3e3e86SMaarten Lankhorst 		spin_unlock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
1135cf3e3e86SMaarten Lankhorst 
1136cf3e3e86SMaarten Lankhorst 		GEM_WARN_ON(!i915_ttm_cpu_maps_iomem(bo->resource));
1137cf3e3e86SMaarten Lankhorst 	}
1138cf3e3e86SMaarten Lankhorst 
1139cf3e3e86SMaarten Lankhorst 	if (wakeref & CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND)
1140cf3e3e86SMaarten Lankhorst 		intel_wakeref_auto(&to_i915(obj->base.dev)->runtime_pm.userfault_wakeref,
1141cf3e3e86SMaarten Lankhorst 				   msecs_to_jiffies_timeout(CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND));
1142cf3e3e86SMaarten Lankhorst 
1143cf3e3e86SMaarten Lankhorst 	i915_ttm_adjust_lru(obj);
1144cf3e3e86SMaarten Lankhorst 
1145cf3e3e86SMaarten Lankhorst 	dma_resv_unlock(bo->base.resv);
1146cf3e3e86SMaarten Lankhorst 
1147cf3e3e86SMaarten Lankhorst out_rpm:
11486667d78aSNirmoy Das 	if (wakeref)
1149cf3e3e86SMaarten Lankhorst 		intel_runtime_pm_put(&to_i915(obj->base.dev)->runtime_pm, wakeref);
1150cf3e3e86SMaarten Lankhorst 
1151cf3e3e86SMaarten Lankhorst 	return ret;
1152cf3e3e86SMaarten Lankhorst }
1153cf3e3e86SMaarten Lankhorst 
1154cf3e3e86SMaarten Lankhorst static int
vm_access_ttm(struct vm_area_struct * area,unsigned long addr,void * buf,int len,int write)1155cf3e3e86SMaarten Lankhorst vm_access_ttm(struct vm_area_struct *area, unsigned long addr,
1156cf3e3e86SMaarten Lankhorst 	      void *buf, int len, int write)
11576667d78aSNirmoy Das {
1158cf3e3e86SMaarten Lankhorst 	struct drm_i915_gem_object *obj =
1159cf3e3e86SMaarten Lankhorst 		i915_ttm_to_gem(area->vm_private_data);
1160cf3e3e86SMaarten Lankhorst 
1161cf3e3e86SMaarten Lankhorst 	if (i915_gem_object_is_readonly(obj) && write)
1162cf3e3e86SMaarten Lankhorst 		return -EACCES;
1163cf3e3e86SMaarten Lankhorst 
1164cf3e3e86SMaarten Lankhorst 	return ttm_bo_vm_access(area, addr, buf, len, write);
1165cf3e3e86SMaarten Lankhorst }
1166cf3e3e86SMaarten Lankhorst 
ttm_vm_open(struct vm_area_struct * vma)1167cf3e3e86SMaarten Lankhorst static void ttm_vm_open(struct vm_area_struct *vma)
1168cf3e3e86SMaarten Lankhorst {
1169cf3e3e86SMaarten Lankhorst 	struct drm_i915_gem_object *obj =
1170cf3e3e86SMaarten Lankhorst 		i915_ttm_to_gem(vma->vm_private_data);
1171cf3e3e86SMaarten Lankhorst 
1172cf3e3e86SMaarten Lankhorst 	GEM_BUG_ON(i915_ttm_is_ghost_object(vma->vm_private_data));
1173cf3e3e86SMaarten Lankhorst 	i915_gem_object_get(obj);
1174cf3e3e86SMaarten Lankhorst }
1175cf3e3e86SMaarten Lankhorst 
ttm_vm_close(struct vm_area_struct * vma)11768ee262baSMatthew Auld static void ttm_vm_close(struct vm_area_struct *vma)
11778ee262baSMatthew Auld {
11781cacd689SAnshuman Gupta 	struct drm_i915_gem_object *obj =
11791cacd689SAnshuman Gupta 		i915_ttm_to_gem(vma->vm_private_data);
11801cacd689SAnshuman Gupta 
11811cacd689SAnshuman Gupta 	GEM_BUG_ON(i915_ttm_is_ghost_object(vma->vm_private_data));
11821cacd689SAnshuman Gupta 	i915_gem_object_put(obj);
11831cacd689SAnshuman Gupta }
11841cacd689SAnshuman Gupta 
11851cacd689SAnshuman Gupta static const struct vm_operations_struct vm_ops_ttm = {
11861cacd689SAnshuman Gupta 	.fault = vm_fault_ttm,
11871cacd689SAnshuman Gupta 	.access = vm_access_ttm,
11881cacd689SAnshuman Gupta 	.open = ttm_vm_open,
11891cacd689SAnshuman Gupta 	.close = ttm_vm_close,
11901cacd689SAnshuman Gupta };
11911cacd689SAnshuman Gupta 
i915_ttm_mmap_offset(struct drm_i915_gem_object * obj)11921cacd689SAnshuman Gupta static u64 i915_ttm_mmap_offset(struct drm_i915_gem_object *obj)
11931cacd689SAnshuman Gupta {
11941cacd689SAnshuman Gupta 	/* The ttm_bo must be allocated with I915_BO_ALLOC_USER */
1195ccb0e027SMatthew Auld 	GEM_BUG_ON(!drm_mm_node_allocated(&obj->base.vma_node.vm_node));
1196ccb0e027SMatthew Auld 
11978ee262baSMatthew Auld 	return drm_vma_node_offset_addr(&obj->base.vma_node);
11981cacd689SAnshuman Gupta }
11991cacd689SAnshuman Gupta 
i915_ttm_unmap_virtual(struct drm_i915_gem_object * obj)12001cacd689SAnshuman Gupta static void i915_ttm_unmap_virtual(struct drm_i915_gem_object *obj)
12018ee262baSMatthew Auld {
12028ee262baSMatthew Auld 	struct ttm_buffer_object *bo = i915_gem_to_ttm(obj);
12034bc2d574SMatthew Auld 	intel_wakeref_t wakeref = 0;
1204213d5092SThomas Hellström 
12055d12ffe6SMatthew Auld 	assert_object_held_shared(obj);
12065d12ffe6SMatthew Auld 
1207213d5092SThomas Hellström 	if (i915_ttm_cpu_maps_iomem(bo->resource)) {
1208213d5092SThomas Hellström 		wakeref = intel_runtime_pm_get(&to_i915(obj->base.dev)->runtime_pm);
1209213d5092SThomas Hellström 
12106ef295e3SMatthew Auld 		/* userfault_count is protected by obj lock and rpm wakeref. */
1211ffa3fe08SMatthew Auld 		if (obj->userfault_count) {
12127ae03459SMatthew Auld 			spin_lock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
1213213d5092SThomas Hellström 			list_del(&obj->userfault_link);
1214213d5092SThomas Hellström 			spin_unlock(&to_i915(obj->base.dev)->runtime_pm.lmem_userfault_lock);
1215b6e913e1SThomas Hellström 			obj->userfault_count = 0;
12167ae03459SMatthew Auld 		}
1217cf3e3e86SMaarten Lankhorst 	}
12188ee262baSMatthew Auld 
1219cf3e3e86SMaarten Lankhorst 	GEM_WARN_ON(obj->userfault_count);
1220213d5092SThomas Hellström 
1221213d5092SThomas Hellström 	ttm_bo_unmap_virtual(i915_gem_to_ttm(obj));
1222213d5092SThomas Hellström 
1223213d5092SThomas Hellström 	if (wakeref)
1224213d5092SThomas Hellström 		intel_runtime_pm_put(&to_i915(obj->base.dev)->runtime_pm, wakeref);
1225213d5092SThomas Hellström }
1226213d5092SThomas Hellström 
1227cf3e3e86SMaarten Lankhorst static const struct drm_i915_gem_object_ops i915_gem_ttm_obj_ops = {
1228068396bbSThomas Hellström 	.name = "i915_gem_object_ttm",
1229068396bbSThomas Hellström 	.flags = I915_GEM_OBJECT_IS_SHRINKABLE |
1230ebd4a8ecSMatthew Auld 		 I915_GEM_OBJECT_SELF_MANAGED_SHRINK_LIST,
1231ebd4a8ecSMatthew Auld 
1232ebd4a8ecSMatthew Auld 	.get_pages = i915_ttm_get_pages,
1233ebd4a8ecSMatthew Auld 	.put_pages = i915_ttm_put_pages,
1234ebd4a8ecSMatthew Auld 	.truncate = i915_ttm_truncate,
1235ebd4a8ecSMatthew Auld 	.shrink = i915_ttm_shrink,
1236ebd4a8ecSMatthew Auld 
1237ebd4a8ecSMatthew Auld 	.adjust_lru = i915_ttm_adjust_lru,
1238ebd4a8ecSMatthew Auld 	.delayed_free = i915_ttm_delayed_free,
1239ebd4a8ecSMatthew Auld 	.migrate = i915_ttm_migrate,
1240ebd4a8ecSMatthew Auld 
1241ebd4a8ecSMatthew Auld 	.mmap_offset = i915_ttm_mmap_offset,
1242c56ce956SThomas Hellström 	.unmap_virtual = i915_ttm_unmap_virtual,
1243c56ce956SThomas Hellström 	.mmap_ops = &vm_ops_ttm,
124448b09612SMaarten Lankhorst };
124548b09612SMaarten Lankhorst 
i915_ttm_bo_destroy(struct ttm_buffer_object * bo)124648b09612SMaarten Lankhorst void i915_ttm_bo_destroy(struct ttm_buffer_object *bo)
1247213d5092SThomas Hellström {
1248068396bbSThomas Hellström 	struct drm_i915_gem_object *obj = i915_ttm_to_gem(bo);
1249068396bbSThomas Hellström 
1250068396bbSThomas Hellström 	i915_gem_object_release_memory_region(obj);
1251213d5092SThomas Hellström 	mutex_destroy(&obj->ttm.get_io_page.lock);
1252213d5092SThomas Hellström 
1253*98a1daccSLee Jones 	if (obj->ttm.created) {
1254213d5092SThomas Hellström 		/*
1255213d5092SThomas Hellström 		 * We freely manage the shrinker LRU outide of the mm.pages life
1256213d5092SThomas Hellström 		 * cycle. As a result when destroying the object we should be
1257213d5092SThomas Hellström 		 * extra paranoid and ensure we remove it from the LRU, before
1258213d5092SThomas Hellström 		 * we free the object.
1259213d5092SThomas Hellström 		 *
1260213d5092SThomas Hellström 		 * Touching the ttm_shrinkable outside of the object lock here
1261213d5092SThomas Hellström 		 * should be safe now that the last GEM object ref was dropped.
1262213d5092SThomas Hellström 		 */
1263213d5092SThomas Hellström 		if (obj->mm.ttm_shrinkable)
12649b78b5daSMatthew Auld 			i915_gem_object_make_unshrinkable(obj);
1265213d5092SThomas Hellström 
1266d22632c8SMatthew Auld 		i915_ttm_backup_free(obj);
1267213d5092SThomas Hellström 
1268213d5092SThomas Hellström 		/* This releases all gem object bindings to the backend. */
1269213d5092SThomas Hellström 		__i915_gem_free_object(obj);
1270213d5092SThomas Hellström 
12713c2b8f32SThomas Hellström 		call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
12723c2b8f32SThomas Hellström 	} else {
12733c2b8f32SThomas Hellström 		__i915_gem_object_fini(obj);
12743c2b8f32SThomas Hellström 	}
1275213d5092SThomas Hellström }
1276213d5092SThomas Hellström 
1277213d5092SThomas Hellström /*
1278213d5092SThomas Hellström  * __i915_gem_ttm_object_init - Initialize a ttm-backed i915 gem object
1279213d5092SThomas Hellström  * @mem: The initial memory region for the object.
1280068396bbSThomas Hellström  * @obj: The gem object.
1281ecbf2060SMatthew Auld  * @size: Object size in bytes.
1282ecbf2060SMatthew Auld  * @flags: gem object flags.
1283068396bbSThomas Hellström  *
12848b1f7f92SThomas Hellström  * Return: 0 on success, negative error code on failure.
1285068396bbSThomas Hellström  */
__i915_gem_ttm_object_init(struct intel_memory_region * mem,struct drm_i915_gem_object * obj,resource_size_t offset,resource_size_t size,resource_size_t page_size,unsigned int flags)1286068396bbSThomas Hellström int __i915_gem_ttm_object_init(struct intel_memory_region *mem,
1287cf3e3e86SMaarten Lankhorst 			       struct drm_i915_gem_object *obj,
1288cf3e3e86SMaarten Lankhorst 			       resource_size_t offset,
1289213d5092SThomas Hellström 			       resource_size_t size,
1290213d5092SThomas Hellström 			       resource_size_t page_size,
1291213d5092SThomas Hellström 			       unsigned int flags)
12923c2b8f32SThomas Hellström {
12933c2b8f32SThomas Hellström 	static struct lock_class_key lock_class;
1294d22632c8SMatthew Auld 	struct drm_i915_private *i915 = mem->i915;
1295d22632c8SMatthew Auld 	struct ttm_operation_ctx ctx = {
1296d22632c8SMatthew Auld 		.interruptible = true,
1297213d5092SThomas Hellström 		.no_wait_gpu = false,
1298ebd4a8ecSMatthew Auld 	};
1299ebd4a8ecSMatthew Auld 	enum ttm_bo_type bo_type;
1300ebd4a8ecSMatthew Auld 	int ret;
1301ebd4a8ecSMatthew Auld 
1302ebd4a8ecSMatthew Auld 	drm_gem_private_object_init(&i915->drm, &obj->base, size);
1303ebd4a8ecSMatthew Auld 	i915_gem_object_init(obj, &i915_gem_ttm_obj_ops, &lock_class, flags);
1304ebd4a8ecSMatthew Auld 
1305ebd4a8ecSMatthew Auld 	obj->bo_offset = offset;
1306213d5092SThomas Hellström 
1307213d5092SThomas Hellström 	/* Don't put on a region list until we're either locked or fully initialized. */
1308213d5092SThomas Hellström 	obj->mm.region = mem;
1309213d5092SThomas Hellström 	INIT_LIST_HEAD(&obj->mm.region_link);
1310213d5092SThomas Hellström 
1311213d5092SThomas Hellström 	INIT_RADIX_TREE(&obj->ttm.get_io_page.radix, GFP_KERNEL | __GFP_NOWARN);
1312347987a2SChristian König 	mutex_init(&obj->ttm.get_io_page.lock);
1313347987a2SChristian König 	bo_type = (obj->flags & I915_BO_ALLOC_USER) ? ttm_bo_type_device :
13143c2b8f32SThomas Hellström 		ttm_bo_type_kernel;
131518f968cbSGwan-gyeong Mun 
131618f968cbSGwan-gyeong Mun 	obj->base.vma_node.driver_private = i915_gem_to_ttm(obj);
131718f968cbSGwan-gyeong Mun 
131818f968cbSGwan-gyeong Mun 	/* Forcing the page size is kernel internal only */
131918f968cbSGwan-gyeong Mun 	GEM_BUG_ON(page_size && obj->mm.n_placements);
132018f968cbSGwan-gyeong Mun 
132118f968cbSGwan-gyeong Mun 	/*
132218f968cbSGwan-gyeong Mun 	 * Keep an extra shrink pin to prevent the object from being made
132318f968cbSGwan-gyeong Mun 	 * shrinkable too early. If the ttm_tt is ever allocated in shmem, we
132418f968cbSGwan-gyeong Mun 	 * drop the pin. The TTM backend manages the shrinker LRU itself,
132518f968cbSGwan-gyeong Mun 	 * outside of the normal mm.pages life cycle.
13263c2b8f32SThomas Hellström 	 */
1327b07a6483SThomas Hellström 	i915_gem_object_make_unshrinkable(obj);
1328213d5092SThomas Hellström 
1329213d5092SThomas Hellström 	/*
1330068396bbSThomas Hellström 	 * If this function fails, it will call the destructor, but
1331068396bbSThomas Hellström 	 * our caller still owns the object. So no freeing in the
13323c2b8f32SThomas Hellström 	 * destructor until obj->ttm.created is true.
13333c2b8f32SThomas Hellström 	 * Similarly, in delayed_destroy, we can't call ttm_bo_put()
13343c2b8f32SThomas Hellström 	 * until successful initialization.
1335213d5092SThomas Hellström 	 */
13363c2b8f32SThomas Hellström 	ret = ttm_bo_init_reserved(&i915->bdev, i915_gem_to_ttm(obj), bo_type,
1337213d5092SThomas Hellström 				   &i915_sys_placement, page_size >> PAGE_SHIFT,
133832b7cf51SThomas Hellström 				   &ctx, NULL, NULL, i915_ttm_bo_destroy);
133932b7cf51SThomas Hellström 
134032b7cf51SThomas Hellström 	/*
13418b1f7f92SThomas Hellström 	 * XXX: The ttm_bo_init_reserved() functions returns -ENOSPC if the size
134232b7cf51SThomas Hellström 	 * is too big to add vma. The direct function that returns -ENOSPC is
134332b7cf51SThomas Hellström 	 * drm_mm_insert_node_in_range(). To handle the same error as other code
134432b7cf51SThomas Hellström 	 * that returns -E2BIG when the size is too large, it converts -ENOSPC to
134532b7cf51SThomas Hellström 	 * -E2BIG.
134632b7cf51SThomas Hellström 	 */
134732b7cf51SThomas Hellström 	if (size >> PAGE_SHIFT > INT_MAX && ret == -ENOSPC)
134832b7cf51SThomas Hellström 		ret = -E2BIG;
134932b7cf51SThomas Hellström 
135032b7cf51SThomas Hellström 	if (ret)
135132b7cf51SThomas Hellström 		return i915_ttm_err_to_gem(ret);
1352235582caSMatthew Auld 
135332b7cf51SThomas Hellström 	obj->ttm.created = true;
135432b7cf51SThomas Hellström 	i915_gem_object_release_memory_region(obj);
135532b7cf51SThomas Hellström 	i915_gem_object_init_memory_region(obj, mem);
135632b7cf51SThomas Hellström 	i915_ttm_adjust_domains_after_move(obj);
135732b7cf51SThomas Hellström 	i915_ttm_adjust_gem_after_move(obj);
135832b7cf51SThomas Hellström 	i915_gem_object_unlock(obj);
135932b7cf51SThomas Hellström 
1360213d5092SThomas Hellström 	return 0;
1361 }
1362 
1363 static const struct intel_memory_region_ops ttm_system_region_ops = {
1364 	.init_object = __i915_gem_ttm_object_init,
1365 	.release = intel_region_ttm_fini,
1366 };
1367 
1368 struct intel_memory_region *
i915_gem_ttm_system_setup(struct drm_i915_private * i915,u16 type,u16 instance)1369 i915_gem_ttm_system_setup(struct drm_i915_private *i915,
1370 			  u16 type, u16 instance)
1371 {
1372 	struct intel_memory_region *mr;
1373 
1374 	mr = intel_memory_region_create(i915, 0,
1375 					totalram_pages() << PAGE_SHIFT,
1376 					PAGE_SIZE, 0, 0,
1377 					type, instance,
1378 					&ttm_system_region_ops);
1379 	if (IS_ERR(mr))
1380 		return mr;
1381 
1382 	intel_memory_region_set_name(mr, "system-ttm");
1383 	return mr;
1384 }
1385