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