xref: /linux/drivers/gpu/drm/i915/gem/i915_gem_object.c (revision 164666fa66669d437bdcc8d5f1744a2aee73be41)
1 /*
2  * Copyright © 2017 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include <linux/sched/mm.h>
26 
27 #include "display/intel_frontbuffer.h"
28 #include "pxp/intel_pxp.h"
29 #include "i915_drv.h"
30 #include "i915_gem_clflush.h"
31 #include "i915_gem_context.h"
32 #include "i915_gem_mman.h"
33 #include "i915_gem_object.h"
34 #include "i915_gem_ttm.h"
35 #include "i915_memcpy.h"
36 #include "i915_trace.h"
37 
38 static struct kmem_cache *slab_objects;
39 
40 static const struct drm_gem_object_funcs i915_gem_object_funcs;
41 
42 struct drm_i915_gem_object *i915_gem_object_alloc(void)
43 {
44 	struct drm_i915_gem_object *obj;
45 
46 	obj = kmem_cache_zalloc(slab_objects, GFP_KERNEL);
47 	if (!obj)
48 		return NULL;
49 	obj->base.funcs = &i915_gem_object_funcs;
50 
51 	return obj;
52 }
53 
54 void i915_gem_object_free(struct drm_i915_gem_object *obj)
55 {
56 	return kmem_cache_free(slab_objects, obj);
57 }
58 
59 void i915_gem_object_init(struct drm_i915_gem_object *obj,
60 			  const struct drm_i915_gem_object_ops *ops,
61 			  struct lock_class_key *key, unsigned flags)
62 {
63 	/*
64 	 * A gem object is embedded both in a struct ttm_buffer_object :/ and
65 	 * in a drm_i915_gem_object. Make sure they are aliased.
66 	 */
67 	BUILD_BUG_ON(offsetof(typeof(*obj), base) !=
68 		     offsetof(typeof(*obj), __do_not_access.base));
69 
70 	spin_lock_init(&obj->vma.lock);
71 	INIT_LIST_HEAD(&obj->vma.list);
72 
73 	INIT_LIST_HEAD(&obj->mm.link);
74 
75 	INIT_LIST_HEAD(&obj->lut_list);
76 	spin_lock_init(&obj->lut_lock);
77 
78 	spin_lock_init(&obj->mmo.lock);
79 	obj->mmo.offsets = RB_ROOT;
80 
81 	init_rcu_head(&obj->rcu);
82 
83 	obj->ops = ops;
84 	GEM_BUG_ON(flags & ~I915_BO_ALLOC_FLAGS);
85 	obj->flags = flags;
86 
87 	obj->mm.madv = I915_MADV_WILLNEED;
88 	INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
89 	mutex_init(&obj->mm.get_page.lock);
90 	INIT_RADIX_TREE(&obj->mm.get_dma_page.radix, GFP_KERNEL | __GFP_NOWARN);
91 	mutex_init(&obj->mm.get_dma_page.lock);
92 }
93 
94 /**
95  * __i915_gem_object_fini - Clean up a GEM object initialization
96  * @obj: The gem object to cleanup
97  *
98  * This function cleans up gem object fields that are set up by
99  * drm_gem_private_object_init() and i915_gem_object_init().
100  * It's primarily intended as a helper for backends that need to
101  * clean up the gem object in separate steps.
102  */
103 void __i915_gem_object_fini(struct drm_i915_gem_object *obj)
104 {
105 	mutex_destroy(&obj->mm.get_page.lock);
106 	mutex_destroy(&obj->mm.get_dma_page.lock);
107 	dma_resv_fini(&obj->base._resv);
108 }
109 
110 /**
111  * i915_gem_object_set_cache_coherency - Mark up the object's coherency levels
112  * for a given cache_level
113  * @obj: #drm_i915_gem_object
114  * @cache_level: cache level
115  */
116 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
117 					 unsigned int cache_level)
118 {
119 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
120 
121 	obj->cache_level = cache_level;
122 
123 	if (cache_level != I915_CACHE_NONE)
124 		obj->cache_coherent = (I915_BO_CACHE_COHERENT_FOR_READ |
125 				       I915_BO_CACHE_COHERENT_FOR_WRITE);
126 	else if (HAS_LLC(i915))
127 		obj->cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ;
128 	else
129 		obj->cache_coherent = 0;
130 
131 	obj->cache_dirty =
132 		!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE) &&
133 		!IS_DGFX(i915);
134 }
135 
136 bool i915_gem_object_can_bypass_llc(struct drm_i915_gem_object *obj)
137 {
138 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
139 
140 	/*
141 	 * This is purely from a security perspective, so we simply don't care
142 	 * about non-userspace objects being able to bypass the LLC.
143 	 */
144 	if (!(obj->flags & I915_BO_ALLOC_USER))
145 		return false;
146 
147 	/*
148 	 * EHL and JSL add the 'Bypass LLC' MOCS entry, which should make it
149 	 * possible for userspace to bypass the GTT caching bits set by the
150 	 * kernel, as per the given object cache_level. This is troublesome
151 	 * since the heavy flush we apply when first gathering the pages is
152 	 * skipped if the kernel thinks the object is coherent with the GPU. As
153 	 * a result it might be possible to bypass the cache and read the
154 	 * contents of the page directly, which could be stale data. If it's
155 	 * just a case of userspace shooting themselves in the foot then so be
156 	 * it, but since i915 takes the stance of always zeroing memory before
157 	 * handing it to userspace, we need to prevent this.
158 	 */
159 	return IS_JSL_EHL(i915);
160 }
161 
162 static void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
163 {
164 	struct drm_i915_gem_object *obj = to_intel_bo(gem);
165 	struct drm_i915_file_private *fpriv = file->driver_priv;
166 	struct i915_lut_handle bookmark = {};
167 	struct i915_mmap_offset *mmo, *mn;
168 	struct i915_lut_handle *lut, *ln;
169 	LIST_HEAD(close);
170 
171 	spin_lock(&obj->lut_lock);
172 	list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
173 		struct i915_gem_context *ctx = lut->ctx;
174 
175 		if (ctx && ctx->file_priv == fpriv) {
176 			i915_gem_context_get(ctx);
177 			list_move(&lut->obj_link, &close);
178 		}
179 
180 		/* Break long locks, and carefully continue on from this spot */
181 		if (&ln->obj_link != &obj->lut_list) {
182 			list_add_tail(&bookmark.obj_link, &ln->obj_link);
183 			if (cond_resched_lock(&obj->lut_lock))
184 				list_safe_reset_next(&bookmark, ln, obj_link);
185 			__list_del_entry(&bookmark.obj_link);
186 		}
187 	}
188 	spin_unlock(&obj->lut_lock);
189 
190 	spin_lock(&obj->mmo.lock);
191 	rbtree_postorder_for_each_entry_safe(mmo, mn, &obj->mmo.offsets, offset)
192 		drm_vma_node_revoke(&mmo->vma_node, file);
193 	spin_unlock(&obj->mmo.lock);
194 
195 	list_for_each_entry_safe(lut, ln, &close, obj_link) {
196 		struct i915_gem_context *ctx = lut->ctx;
197 		struct i915_vma *vma;
198 
199 		/*
200 		 * We allow the process to have multiple handles to the same
201 		 * vma, in the same fd namespace, by virtue of flink/open.
202 		 */
203 
204 		mutex_lock(&ctx->lut_mutex);
205 		vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
206 		if (vma) {
207 			GEM_BUG_ON(vma->obj != obj);
208 			GEM_BUG_ON(!atomic_read(&vma->open_count));
209 			i915_vma_close(vma);
210 		}
211 		mutex_unlock(&ctx->lut_mutex);
212 
213 		i915_gem_context_put(lut->ctx);
214 		i915_lut_handle_free(lut);
215 		i915_gem_object_put(obj);
216 	}
217 }
218 
219 void __i915_gem_free_object_rcu(struct rcu_head *head)
220 {
221 	struct drm_i915_gem_object *obj =
222 		container_of(head, typeof(*obj), rcu);
223 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
224 
225 	i915_gem_object_free(obj);
226 
227 	GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
228 	atomic_dec(&i915->mm.free_count);
229 }
230 
231 static void __i915_gem_object_free_mmaps(struct drm_i915_gem_object *obj)
232 {
233 	/* Skip serialisation and waking the device if known to be not used. */
234 
235 	if (obj->userfault_count)
236 		i915_gem_object_release_mmap_gtt(obj);
237 
238 	if (!RB_EMPTY_ROOT(&obj->mmo.offsets)) {
239 		struct i915_mmap_offset *mmo, *mn;
240 
241 		i915_gem_object_release_mmap_offset(obj);
242 
243 		rbtree_postorder_for_each_entry_safe(mmo, mn,
244 						     &obj->mmo.offsets,
245 						     offset) {
246 			drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
247 					      &mmo->vma_node);
248 			kfree(mmo);
249 		}
250 		obj->mmo.offsets = RB_ROOT;
251 	}
252 }
253 
254 /**
255  * __i915_gem_object_pages_fini - Clean up pages use of a gem object
256  * @obj: The gem object to clean up
257  *
258  * This function cleans up usage of the object mm.pages member. It
259  * is intended for backends that need to clean up a gem object in
260  * separate steps and needs to be called when the object is idle before
261  * the object's backing memory is freed.
262  */
263 void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj)
264 {
265 	assert_object_held(obj);
266 
267 	if (!list_empty(&obj->vma.list)) {
268 		struct i915_vma *vma;
269 
270 		/*
271 		 * Note that the vma keeps an object reference while
272 		 * it is active, so it *should* not sleep while we
273 		 * destroy it. Our debug code errs insits it *might*.
274 		 * For the moment, play along.
275 		 */
276 		spin_lock(&obj->vma.lock);
277 		while ((vma = list_first_entry_or_null(&obj->vma.list,
278 						       struct i915_vma,
279 						       obj_link))) {
280 			GEM_BUG_ON(vma->obj != obj);
281 			spin_unlock(&obj->vma.lock);
282 
283 			__i915_vma_put(vma);
284 
285 			spin_lock(&obj->vma.lock);
286 		}
287 		spin_unlock(&obj->vma.lock);
288 	}
289 
290 	__i915_gem_object_free_mmaps(obj);
291 
292 	atomic_set(&obj->mm.pages_pin_count, 0);
293 	__i915_gem_object_put_pages(obj);
294 	GEM_BUG_ON(i915_gem_object_has_pages(obj));
295 }
296 
297 void __i915_gem_free_object(struct drm_i915_gem_object *obj)
298 {
299 	trace_i915_gem_object_destroy(obj);
300 
301 	GEM_BUG_ON(!list_empty(&obj->lut_list));
302 
303 	bitmap_free(obj->bit_17);
304 
305 	if (obj->base.import_attach)
306 		drm_prime_gem_destroy(&obj->base, NULL);
307 
308 	drm_gem_free_mmap_offset(&obj->base);
309 
310 	if (obj->ops->release)
311 		obj->ops->release(obj);
312 
313 	if (obj->mm.n_placements > 1)
314 		kfree(obj->mm.placements);
315 
316 	if (obj->shares_resv_from)
317 		i915_vm_resv_put(obj->shares_resv_from);
318 
319 	__i915_gem_object_fini(obj);
320 }
321 
322 static void __i915_gem_free_objects(struct drm_i915_private *i915,
323 				    struct llist_node *freed)
324 {
325 	struct drm_i915_gem_object *obj, *on;
326 
327 	llist_for_each_entry_safe(obj, on, freed, freed) {
328 		might_sleep();
329 		if (obj->ops->delayed_free) {
330 			obj->ops->delayed_free(obj);
331 			continue;
332 		}
333 
334 		if (!i915_gem_object_trylock(obj, NULL)) {
335 			/* busy, toss it back to the pile */
336 			if (llist_add(&obj->freed, &i915->mm.free_list))
337 				queue_delayed_work(i915->wq, &i915->mm.free_work, msecs_to_jiffies(10));
338 			continue;
339 		}
340 
341 		__i915_gem_object_pages_fini(obj);
342 		i915_gem_object_unlock(obj);
343 		__i915_gem_free_object(obj);
344 
345 		/* But keep the pointer alive for RCU-protected lookups */
346 		call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
347 		cond_resched();
348 	}
349 }
350 
351 void i915_gem_flush_free_objects(struct drm_i915_private *i915)
352 {
353 	struct llist_node *freed = llist_del_all(&i915->mm.free_list);
354 
355 	if (unlikely(freed))
356 		__i915_gem_free_objects(i915, freed);
357 }
358 
359 static void __i915_gem_free_work(struct work_struct *work)
360 {
361 	struct drm_i915_private *i915 =
362 		container_of(work, struct drm_i915_private, mm.free_work.work);
363 
364 	i915_gem_flush_free_objects(i915);
365 }
366 
367 static void i915_gem_free_object(struct drm_gem_object *gem_obj)
368 {
369 	struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
370 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
371 
372 	GEM_BUG_ON(i915_gem_object_is_framebuffer(obj));
373 
374 	/*
375 	 * Before we free the object, make sure any pure RCU-only
376 	 * read-side critical sections are complete, e.g.
377 	 * i915_gem_busy_ioctl(). For the corresponding synchronized
378 	 * lookup see i915_gem_object_lookup_rcu().
379 	 */
380 	atomic_inc(&i915->mm.free_count);
381 
382 	/*
383 	 * Since we require blocking on struct_mutex to unbind the freed
384 	 * object from the GPU before releasing resources back to the
385 	 * system, we can not do that directly from the RCU callback (which may
386 	 * be a softirq context), but must instead then defer that work onto a
387 	 * kthread. We use the RCU callback rather than move the freed object
388 	 * directly onto the work queue so that we can mix between using the
389 	 * worker and performing frees directly from subsequent allocations for
390 	 * crude but effective memory throttling.
391 	 */
392 
393 	if (llist_add(&obj->freed, &i915->mm.free_list))
394 		queue_delayed_work(i915->wq, &i915->mm.free_work, 0);
395 }
396 
397 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
398 					 enum fb_op_origin origin)
399 {
400 	struct intel_frontbuffer *front;
401 
402 	front = __intel_frontbuffer_get(obj);
403 	if (front) {
404 		intel_frontbuffer_flush(front, origin);
405 		intel_frontbuffer_put(front);
406 	}
407 }
408 
409 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
410 					      enum fb_op_origin origin)
411 {
412 	struct intel_frontbuffer *front;
413 
414 	front = __intel_frontbuffer_get(obj);
415 	if (front) {
416 		intel_frontbuffer_invalidate(front, origin);
417 		intel_frontbuffer_put(front);
418 	}
419 }
420 
421 static void
422 i915_gem_object_read_from_page_kmap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
423 {
424 	void *src_map;
425 	void *src_ptr;
426 
427 	src_map = kmap_atomic(i915_gem_object_get_page(obj, offset >> PAGE_SHIFT));
428 
429 	src_ptr = src_map + offset_in_page(offset);
430 	if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
431 		drm_clflush_virt_range(src_ptr, size);
432 	memcpy(dst, src_ptr, size);
433 
434 	kunmap_atomic(src_map);
435 }
436 
437 static void
438 i915_gem_object_read_from_page_iomap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
439 {
440 	void __iomem *src_map;
441 	void __iomem *src_ptr;
442 	dma_addr_t dma = i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT);
443 
444 	src_map = io_mapping_map_wc(&obj->mm.region->iomap,
445 				    dma - obj->mm.region->region.start,
446 				    PAGE_SIZE);
447 
448 	src_ptr = src_map + offset_in_page(offset);
449 	if (!i915_memcpy_from_wc(dst, (void __force *)src_ptr, size))
450 		memcpy_fromio(dst, src_ptr, size);
451 
452 	io_mapping_unmap(src_map);
453 }
454 
455 /**
456  * i915_gem_object_read_from_page - read data from the page of a GEM object
457  * @obj: GEM object to read from
458  * @offset: offset within the object
459  * @dst: buffer to store the read data
460  * @size: size to read
461  *
462  * Reads data from @obj at the specified offset. The requested region to read
463  * from can't cross a page boundary. The caller must ensure that @obj pages
464  * are pinned and that @obj is synced wrt. any related writes.
465  *
466  * Return: %0 on success or -ENODEV if the type of @obj's backing store is
467  * unsupported.
468  */
469 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
470 {
471 	GEM_BUG_ON(offset >= obj->base.size);
472 	GEM_BUG_ON(offset_in_page(offset) > PAGE_SIZE - size);
473 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
474 
475 	if (i915_gem_object_has_struct_page(obj))
476 		i915_gem_object_read_from_page_kmap(obj, offset, dst, size);
477 	else if (i915_gem_object_has_iomem(obj))
478 		i915_gem_object_read_from_page_iomap(obj, offset, dst, size);
479 	else
480 		return -ENODEV;
481 
482 	return 0;
483 }
484 
485 /**
486  * i915_gem_object_evictable - Whether object is likely evictable after unbind.
487  * @obj: The object to check
488  *
489  * This function checks whether the object is likely unvictable after unbind.
490  * If the object is not locked when checking, the result is only advisory.
491  * If the object is locked when checking, and the function returns true,
492  * then an eviction should indeed be possible. But since unlocked vma
493  * unpinning and unbinding is currently possible, the object can actually
494  * become evictable even if this function returns false.
495  *
496  * Return: true if the object may be evictable. False otherwise.
497  */
498 bool i915_gem_object_evictable(struct drm_i915_gem_object *obj)
499 {
500 	struct i915_vma *vma;
501 	int pin_count = atomic_read(&obj->mm.pages_pin_count);
502 
503 	if (!pin_count)
504 		return true;
505 
506 	spin_lock(&obj->vma.lock);
507 	list_for_each_entry(vma, &obj->vma.list, obj_link) {
508 		if (i915_vma_is_pinned(vma)) {
509 			spin_unlock(&obj->vma.lock);
510 			return false;
511 		}
512 		if (atomic_read(&vma->pages_count))
513 			pin_count--;
514 	}
515 	spin_unlock(&obj->vma.lock);
516 	GEM_WARN_ON(pin_count < 0);
517 
518 	return pin_count == 0;
519 }
520 
521 /**
522  * i915_gem_object_migratable - Whether the object is migratable out of the
523  * current region.
524  * @obj: Pointer to the object.
525  *
526  * Return: Whether the object is allowed to be resident in other
527  * regions than the current while pages are present.
528  */
529 bool i915_gem_object_migratable(struct drm_i915_gem_object *obj)
530 {
531 	struct intel_memory_region *mr = READ_ONCE(obj->mm.region);
532 
533 	if (!mr)
534 		return false;
535 
536 	return obj->mm.n_placements > 1;
537 }
538 
539 /**
540  * i915_gem_object_has_struct_page - Whether the object is page-backed
541  * @obj: The object to query.
542  *
543  * This function should only be called while the object is locked or pinned,
544  * otherwise the page backing may change under the caller.
545  *
546  * Return: True if page-backed, false otherwise.
547  */
548 bool i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
549 {
550 #ifdef CONFIG_LOCKDEP
551 	if (IS_DGFX(to_i915(obj->base.dev)) &&
552 	    i915_gem_object_evictable((void __force *)obj))
553 		assert_object_held_shared(obj);
554 #endif
555 	return obj->mem_flags & I915_BO_FLAG_STRUCT_PAGE;
556 }
557 
558 /**
559  * i915_gem_object_has_iomem - Whether the object is iomem-backed
560  * @obj: The object to query.
561  *
562  * This function should only be called while the object is locked or pinned,
563  * otherwise the iomem backing may change under the caller.
564  *
565  * Return: True if iomem-backed, false otherwise.
566  */
567 bool i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj)
568 {
569 #ifdef CONFIG_LOCKDEP
570 	if (IS_DGFX(to_i915(obj->base.dev)) &&
571 	    i915_gem_object_evictable((void __force *)obj))
572 		assert_object_held_shared(obj);
573 #endif
574 	return obj->mem_flags & I915_BO_FLAG_IOMEM;
575 }
576 
577 /**
578  * i915_gem_object_can_migrate - Whether an object likely can be migrated
579  *
580  * @obj: The object to migrate
581  * @id: The region intended to migrate to
582  *
583  * Check whether the object backend supports migration to the
584  * given region. Note that pinning may affect the ability to migrate as
585  * returned by this function.
586  *
587  * This function is primarily intended as a helper for checking the
588  * possibility to migrate objects and might be slightly less permissive
589  * than i915_gem_object_migrate() when it comes to objects with the
590  * I915_BO_ALLOC_USER flag set.
591  *
592  * Return: true if migration is possible, false otherwise.
593  */
594 bool i915_gem_object_can_migrate(struct drm_i915_gem_object *obj,
595 				 enum intel_region_id id)
596 {
597 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
598 	unsigned int num_allowed = obj->mm.n_placements;
599 	struct intel_memory_region *mr;
600 	unsigned int i;
601 
602 	GEM_BUG_ON(id >= INTEL_REGION_UNKNOWN);
603 	GEM_BUG_ON(obj->mm.madv != I915_MADV_WILLNEED);
604 
605 	mr = i915->mm.regions[id];
606 	if (!mr)
607 		return false;
608 
609 	if (obj->mm.region == mr)
610 		return true;
611 
612 	if (!i915_gem_object_evictable(obj))
613 		return false;
614 
615 	if (!obj->ops->migrate)
616 		return false;
617 
618 	if (!(obj->flags & I915_BO_ALLOC_USER))
619 		return true;
620 
621 	if (num_allowed == 0)
622 		return false;
623 
624 	for (i = 0; i < num_allowed; ++i) {
625 		if (mr == obj->mm.placements[i])
626 			return true;
627 	}
628 
629 	return false;
630 }
631 
632 /**
633  * i915_gem_object_migrate - Migrate an object to the desired region id
634  * @obj: The object to migrate.
635  * @ww: An optional struct i915_gem_ww_ctx. If NULL, the backend may
636  * not be successful in evicting other objects to make room for this object.
637  * @id: The region id to migrate to.
638  *
639  * Attempt to migrate the object to the desired memory region. The
640  * object backend must support migration and the object may not be
641  * pinned, (explicitly pinned pages or pinned vmas). The object must
642  * be locked.
643  * On successful completion, the object will have pages pointing to
644  * memory in the new region, but an async migration task may not have
645  * completed yet, and to accomplish that, i915_gem_object_wait_migration()
646  * must be called.
647  *
648  * Note: the @ww parameter is not used yet, but included to make sure
649  * callers put some effort into obtaining a valid ww ctx if one is
650  * available.
651  *
652  * Return: 0 on success. Negative error code on failure. In particular may
653  * return -ENXIO on lack of region space, -EDEADLK for deadlock avoidance
654  * if @ww is set, -EINTR or -ERESTARTSYS if signal pending, and
655  * -EBUSY if the object is pinned.
656  */
657 int i915_gem_object_migrate(struct drm_i915_gem_object *obj,
658 			    struct i915_gem_ww_ctx *ww,
659 			    enum intel_region_id id)
660 {
661 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
662 	struct intel_memory_region *mr;
663 
664 	GEM_BUG_ON(id >= INTEL_REGION_UNKNOWN);
665 	GEM_BUG_ON(obj->mm.madv != I915_MADV_WILLNEED);
666 	assert_object_held(obj);
667 
668 	mr = i915->mm.regions[id];
669 	GEM_BUG_ON(!mr);
670 
671 	if (!i915_gem_object_can_migrate(obj, id))
672 		return -EINVAL;
673 
674 	if (!obj->ops->migrate) {
675 		if (GEM_WARN_ON(obj->mm.region != mr))
676 			return -EINVAL;
677 		return 0;
678 	}
679 
680 	return obj->ops->migrate(obj, mr);
681 }
682 
683 /**
684  * i915_gem_object_placement_possible - Check whether the object can be
685  * placed at certain memory type
686  * @obj: Pointer to the object
687  * @type: The memory type to check
688  *
689  * Return: True if the object can be placed in @type. False otherwise.
690  */
691 bool i915_gem_object_placement_possible(struct drm_i915_gem_object *obj,
692 					enum intel_memory_type type)
693 {
694 	unsigned int i;
695 
696 	if (!obj->mm.n_placements) {
697 		switch (type) {
698 		case INTEL_MEMORY_LOCAL:
699 			return i915_gem_object_has_iomem(obj);
700 		case INTEL_MEMORY_SYSTEM:
701 			return i915_gem_object_has_pages(obj);
702 		default:
703 			/* Ignore stolen for now */
704 			GEM_BUG_ON(1);
705 			return false;
706 		}
707 	}
708 
709 	for (i = 0; i < obj->mm.n_placements; i++) {
710 		if (obj->mm.placements[i]->type == type)
711 			return true;
712 	}
713 
714 	return false;
715 }
716 
717 void i915_gem_init__objects(struct drm_i915_private *i915)
718 {
719 	INIT_DELAYED_WORK(&i915->mm.free_work, __i915_gem_free_work);
720 }
721 
722 void i915_objects_module_exit(void)
723 {
724 	kmem_cache_destroy(slab_objects);
725 }
726 
727 int __init i915_objects_module_init(void)
728 {
729 	slab_objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
730 	if (!slab_objects)
731 		return -ENOMEM;
732 
733 	return 0;
734 }
735 
736 static const struct drm_gem_object_funcs i915_gem_object_funcs = {
737 	.free = i915_gem_free_object,
738 	.close = i915_gem_close_object,
739 	.export = i915_gem_prime_export,
740 };
741 
742 /**
743  * i915_gem_object_get_moving_fence - Get the object's moving fence if any
744  * @obj: The object whose moving fence to get.
745  *
746  * A non-signaled moving fence means that there is an async operation
747  * pending on the object that needs to be waited on before setting up
748  * any GPU- or CPU PTEs to the object's pages.
749  *
750  * Return: A refcounted pointer to the object's moving fence if any,
751  * NULL otherwise.
752  */
753 struct dma_fence *
754 i915_gem_object_get_moving_fence(struct drm_i915_gem_object *obj)
755 {
756 	return dma_fence_get(i915_gem_to_ttm(obj)->moving);
757 }
758 
759 /**
760  * i915_gem_object_wait_moving_fence - Wait for the object's moving fence if any
761  * @obj: The object whose moving fence to wait for.
762  * @intr: Whether to wait interruptible.
763  *
764  * If the moving fence signaled without an error, it is detached from the
765  * object and put.
766  *
767  * Return: 0 if successful, -ERESTARTSYS if the wait was interrupted,
768  * negative error code if the async operation represented by the
769  * moving fence failed.
770  */
771 int i915_gem_object_wait_moving_fence(struct drm_i915_gem_object *obj,
772 				      bool intr)
773 {
774 	struct dma_fence *fence = i915_gem_to_ttm(obj)->moving;
775 	int ret;
776 
777 	assert_object_held(obj);
778 	if (!fence)
779 		return 0;
780 
781 	ret = dma_fence_wait(fence, intr);
782 	if (ret)
783 		return ret;
784 
785 	if (fence->error)
786 		return fence->error;
787 
788 	i915_gem_to_ttm(obj)->moving = NULL;
789 	dma_fence_put(fence);
790 	return 0;
791 }
792 
793 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
794 #include "selftests/huge_gem_object.c"
795 #include "selftests/huge_pages.c"
796 #include "selftests/i915_gem_migrate.c"
797 #include "selftests/i915_gem_object.c"
798 #include "selftests/i915_gem_coherency.c"
799 #endif
800