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