xref: /openbsd/sys/dev/pci/drm/i915/gem/i915_gem_object.c (revision 3bef86f7)
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/highmem.h>
26 #include <linux/sched/mm.h>
27 
28 #include <drm/drm_cache.h>
29 
30 #include "display/intel_frontbuffer.h"
31 #include "pxp/intel_pxp.h"
32 
33 #include "i915_drv.h"
34 #include "i915_file_private.h"
35 #include "i915_gem_clflush.h"
36 #include "i915_gem_context.h"
37 #include "i915_gem_dmabuf.h"
38 #include "i915_gem_mman.h"
39 #include "i915_gem_object.h"
40 #include "i915_gem_ttm.h"
41 #include "i915_memcpy.h"
42 #include "i915_trace.h"
43 
44 static struct pool slab_objects;
45 
46 static const struct drm_gem_object_funcs i915_gem_object_funcs;
47 
48 unsigned int i915_gem_get_pat_index(struct drm_i915_private *i915,
49 				    enum i915_cache_level level)
50 {
51 	if (drm_WARN_ON(&i915->drm, level >= I915_MAX_CACHE_LEVEL))
52 		return 0;
53 
54 	return INTEL_INFO(i915)->cachelevel_to_pat[level];
55 }
56 
57 bool i915_gem_object_has_cache_level(const struct drm_i915_gem_object *obj,
58 				     enum i915_cache_level lvl)
59 {
60 	/*
61 	 * In case the pat_index is set by user space, this kernel mode
62 	 * driver should leave the coherency to be managed by user space,
63 	 * simply return true here.
64 	 */
65 	if (obj->pat_set_by_user)
66 		return true;
67 
68 	/*
69 	 * Otherwise the pat_index should have been converted from cache_level
70 	 * so that the following comparison is valid.
71 	 */
72 	return obj->pat_index == i915_gem_get_pat_index(obj_to_i915(obj), lvl);
73 }
74 
75 struct drm_i915_gem_object *i915_gem_object_alloc(void)
76 {
77 	struct drm_i915_gem_object *obj;
78 
79 #ifdef __linux__
80 	obj = kmem_cache_zalloc(slab_objects, GFP_KERNEL);
81 #else
82 	obj = pool_get(&slab_objects, PR_WAITOK | PR_ZERO);
83 #endif
84 	if (!obj)
85 		return NULL;
86 	obj->base.funcs = &i915_gem_object_funcs;
87 
88 	return obj;
89 }
90 
91 void i915_gem_object_free(struct drm_i915_gem_object *obj)
92 {
93 #ifdef __linux__
94 	return kmem_cache_free(slab_objects, obj);
95 #else
96 	pool_put(&slab_objects, obj);
97 #endif
98 }
99 
100 void i915_gem_object_init(struct drm_i915_gem_object *obj,
101 			  const struct drm_i915_gem_object_ops *ops,
102 			  struct lock_class_key *key, unsigned flags)
103 {
104 	/*
105 	 * A gem object is embedded both in a struct ttm_buffer_object :/ and
106 	 * in a drm_i915_gem_object. Make sure they are aliased.
107 	 */
108 	BUILD_BUG_ON(offsetof(typeof(*obj), base) !=
109 		     offsetof(typeof(*obj), __do_not_access.base));
110 
111 	mtx_init(&obj->vma.lock, IPL_NONE);
112 	INIT_LIST_HEAD(&obj->vma.list);
113 
114 	INIT_LIST_HEAD(&obj->mm.link);
115 
116 	INIT_LIST_HEAD(&obj->lut_list);
117 	mtx_init(&obj->lut_lock, IPL_NONE);
118 
119 	mtx_init(&obj->mmo.lock, IPL_NONE);
120 	obj->mmo.offsets = RB_ROOT;
121 
122 	init_rcu_head(&obj->rcu);
123 
124 	obj->ops = ops;
125 	GEM_BUG_ON(flags & ~I915_BO_ALLOC_FLAGS);
126 	obj->flags = flags;
127 
128 	obj->mm.madv = I915_MADV_WILLNEED;
129 	INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
130 	rw_init(&obj->mm.get_page.lock, "mmget");
131 	INIT_RADIX_TREE(&obj->mm.get_dma_page.radix, GFP_KERNEL | __GFP_NOWARN);
132 	rw_init(&obj->mm.get_dma_page.lock, "mmgetd");
133 }
134 
135 /**
136  * __i915_gem_object_fini - Clean up a GEM object initialization
137  * @obj: The gem object to cleanup
138  *
139  * This function cleans up gem object fields that are set up by
140  * drm_gem_private_object_init() and i915_gem_object_init().
141  * It's primarily intended as a helper for backends that need to
142  * clean up the gem object in separate steps.
143  */
144 void __i915_gem_object_fini(struct drm_i915_gem_object *obj)
145 {
146 	mutex_destroy(&obj->mm.get_page.lock);
147 	mutex_destroy(&obj->mm.get_dma_page.lock);
148 	dma_resv_fini(&obj->base._resv);
149 }
150 
151 /**
152  * i915_gem_object_set_cache_coherency - Mark up the object's coherency levels
153  * for a given cache_level
154  * @obj: #drm_i915_gem_object
155  * @cache_level: cache level
156  */
157 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
158 					 unsigned int cache_level)
159 {
160 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
161 
162 	obj->pat_index = i915_gem_get_pat_index(i915, cache_level);
163 
164 	if (cache_level != I915_CACHE_NONE)
165 		obj->cache_coherent = (I915_BO_CACHE_COHERENT_FOR_READ |
166 				       I915_BO_CACHE_COHERENT_FOR_WRITE);
167 	else if (HAS_LLC(i915))
168 		obj->cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ;
169 	else
170 		obj->cache_coherent = 0;
171 
172 	obj->cache_dirty =
173 		!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE) &&
174 		!IS_DGFX(i915);
175 }
176 
177 /**
178  * i915_gem_object_set_pat_index - set PAT index to be used in PTE encode
179  * @obj: #drm_i915_gem_object
180  * @pat_index: PAT index
181  *
182  * This is a clone of i915_gem_object_set_cache_coherency taking pat index
183  * instead of cache_level as its second argument.
184  */
185 void i915_gem_object_set_pat_index(struct drm_i915_gem_object *obj,
186 				   unsigned int pat_index)
187 {
188 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
189 
190 	if (obj->pat_index == pat_index)
191 		return;
192 
193 	obj->pat_index = pat_index;
194 
195 	if (pat_index != i915_gem_get_pat_index(i915, I915_CACHE_NONE))
196 		obj->cache_coherent = (I915_BO_CACHE_COHERENT_FOR_READ |
197 				       I915_BO_CACHE_COHERENT_FOR_WRITE);
198 	else if (HAS_LLC(i915))
199 		obj->cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ;
200 	else
201 		obj->cache_coherent = 0;
202 
203 	obj->cache_dirty =
204 		!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE) &&
205 		!IS_DGFX(i915);
206 }
207 
208 bool i915_gem_object_can_bypass_llc(struct drm_i915_gem_object *obj)
209 {
210 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
211 
212 	/*
213 	 * This is purely from a security perspective, so we simply don't care
214 	 * about non-userspace objects being able to bypass the LLC.
215 	 */
216 	if (!(obj->flags & I915_BO_ALLOC_USER))
217 		return false;
218 
219 	/*
220 	 * Always flush cache for UMD objects at creation time.
221 	 */
222 	if (obj->pat_set_by_user)
223 		return true;
224 
225 	/*
226 	 * EHL and JSL add the 'Bypass LLC' MOCS entry, which should make it
227 	 * possible for userspace to bypass the GTT caching bits set by the
228 	 * kernel, as per the given object cache_level. This is troublesome
229 	 * since the heavy flush we apply when first gathering the pages is
230 	 * skipped if the kernel thinks the object is coherent with the GPU. As
231 	 * a result it might be possible to bypass the cache and read the
232 	 * contents of the page directly, which could be stale data. If it's
233 	 * just a case of userspace shooting themselves in the foot then so be
234 	 * it, but since i915 takes the stance of always zeroing memory before
235 	 * handing it to userspace, we need to prevent this.
236 	 */
237 	return (IS_JASPERLAKE(i915) || IS_ELKHARTLAKE(i915));
238 }
239 
240 static void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
241 {
242 	struct drm_i915_gem_object *obj = to_intel_bo(gem);
243 	struct drm_i915_file_private *fpriv = file->driver_priv;
244 	struct i915_lut_handle bookmark = {};
245 	struct i915_mmap_offset *mmo, *mn;
246 	struct i915_lut_handle *lut, *ln;
247 	DRM_LIST_HEAD(close);
248 
249 	spin_lock(&obj->lut_lock);
250 	list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
251 		struct i915_gem_context *ctx = lut->ctx;
252 
253 		if (ctx && ctx->file_priv == fpriv) {
254 			i915_gem_context_get(ctx);
255 			list_move(&lut->obj_link, &close);
256 		}
257 
258 		/* Break long locks, and carefully continue on from this spot */
259 		if (&ln->obj_link != &obj->lut_list) {
260 			list_add_tail(&bookmark.obj_link, &ln->obj_link);
261 			if (cond_resched_lock(&obj->lut_lock))
262 				list_safe_reset_next(&bookmark, ln, obj_link);
263 			__list_del_entry(&bookmark.obj_link);
264 		}
265 	}
266 	spin_unlock(&obj->lut_lock);
267 
268 	spin_lock(&obj->mmo.lock);
269 	rbtree_postorder_for_each_entry_safe(mmo, mn, &obj->mmo.offsets, offset)
270 		drm_vma_node_revoke(&mmo->vma_node, file);
271 	spin_unlock(&obj->mmo.lock);
272 
273 	list_for_each_entry_safe(lut, ln, &close, obj_link) {
274 		struct i915_gem_context *ctx = lut->ctx;
275 		struct i915_vma *vma;
276 
277 		/*
278 		 * We allow the process to have multiple handles to the same
279 		 * vma, in the same fd namespace, by virtue of flink/open.
280 		 */
281 
282 		mutex_lock(&ctx->lut_mutex);
283 		vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
284 		if (vma) {
285 			GEM_BUG_ON(vma->obj != obj);
286 			GEM_BUG_ON(!atomic_read(&vma->open_count));
287 			i915_vma_close(vma);
288 		}
289 		mutex_unlock(&ctx->lut_mutex);
290 
291 		i915_gem_context_put(lut->ctx);
292 		i915_lut_handle_free(lut);
293 		i915_gem_object_put(obj);
294 	}
295 }
296 
297 void __i915_gem_free_object_rcu(struct rcu_head *head)
298 {
299 	struct drm_i915_gem_object *obj =
300 		container_of(head, typeof(*obj), rcu);
301 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
302 
303 #ifdef __OpenBSD__
304 	if (obj->base.uao)
305 		uao_detach(obj->base.uao);
306 #endif
307 
308 	i915_gem_object_free(obj);
309 
310 	GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
311 	atomic_dec(&i915->mm.free_count);
312 }
313 
314 static void __i915_gem_object_free_mmaps(struct drm_i915_gem_object *obj)
315 {
316 	/* Skip serialisation and waking the device if known to be not used. */
317 
318 	if (obj->userfault_count && !IS_DGFX(to_i915(obj->base.dev)))
319 		i915_gem_object_release_mmap_gtt(obj);
320 
321 	if (!RB_EMPTY_ROOT(&obj->mmo.offsets)) {
322 		struct i915_mmap_offset *mmo, *mn;
323 
324 		i915_gem_object_release_mmap_offset(obj);
325 
326 		rbtree_postorder_for_each_entry_safe(mmo, mn,
327 						     &obj->mmo.offsets,
328 						     offset) {
329 			drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
330 					      &mmo->vma_node);
331 			kfree(mmo);
332 		}
333 		obj->mmo.offsets = RB_ROOT;
334 	}
335 }
336 
337 /**
338  * __i915_gem_object_pages_fini - Clean up pages use of a gem object
339  * @obj: The gem object to clean up
340  *
341  * This function cleans up usage of the object mm.pages member. It
342  * is intended for backends that need to clean up a gem object in
343  * separate steps and needs to be called when the object is idle before
344  * the object's backing memory is freed.
345  */
346 void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj)
347 {
348 	assert_object_held_shared(obj);
349 
350 	if (!list_empty(&obj->vma.list)) {
351 		struct i915_vma *vma;
352 
353 		spin_lock(&obj->vma.lock);
354 		while ((vma = list_first_entry_or_null(&obj->vma.list,
355 						       struct i915_vma,
356 						       obj_link))) {
357 			GEM_BUG_ON(vma->obj != obj);
358 			spin_unlock(&obj->vma.lock);
359 
360 			i915_vma_destroy(vma);
361 
362 			spin_lock(&obj->vma.lock);
363 		}
364 		spin_unlock(&obj->vma.lock);
365 	}
366 
367 	__i915_gem_object_free_mmaps(obj);
368 
369 	atomic_set(&obj->mm.pages_pin_count, 0);
370 
371 	/*
372 	 * dma_buf_unmap_attachment() requires reservation to be
373 	 * locked. The imported GEM shouldn't share reservation lock
374 	 * and ttm_bo_cleanup_memtype_use() shouldn't be invoked for
375 	 * dma-buf, so it's safe to take the lock.
376 	 */
377 	if (obj->base.import_attach)
378 		i915_gem_object_lock(obj, NULL);
379 
380 	__i915_gem_object_put_pages(obj);
381 
382 	if (obj->base.import_attach)
383 		i915_gem_object_unlock(obj);
384 
385 	GEM_BUG_ON(i915_gem_object_has_pages(obj));
386 }
387 
388 void __i915_gem_free_object(struct drm_i915_gem_object *obj)
389 {
390 	trace_i915_gem_object_destroy(obj);
391 
392 	GEM_BUG_ON(!list_empty(&obj->lut_list));
393 
394 	bitmap_free(obj->bit_17);
395 
396 	if (obj->base.import_attach)
397 		drm_prime_gem_destroy(&obj->base, NULL);
398 
399 	drm_gem_free_mmap_offset(&obj->base);
400 
401 	if (obj->ops->release)
402 		obj->ops->release(obj);
403 
404 	if (obj->mm.n_placements > 1)
405 		kfree(obj->mm.placements);
406 
407 	if (obj->shares_resv_from)
408 		i915_vm_resv_put(obj->shares_resv_from);
409 
410 	__i915_gem_object_fini(obj);
411 }
412 
413 static void __i915_gem_free_objects(struct drm_i915_private *i915,
414 				    struct llist_node *freed)
415 {
416 	struct drm_i915_gem_object *obj, *on;
417 
418 	llist_for_each_entry_safe(obj, on, freed, freed) {
419 		might_sleep();
420 		if (obj->ops->delayed_free) {
421 			obj->ops->delayed_free(obj);
422 			continue;
423 		}
424 
425 		__i915_gem_object_pages_fini(obj);
426 		__i915_gem_free_object(obj);
427 
428 		/* But keep the pointer alive for RCU-protected lookups */
429 		call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
430 		cond_resched();
431 	}
432 }
433 
434 void i915_gem_flush_free_objects(struct drm_i915_private *i915)
435 {
436 	struct llist_node *freed = llist_del_all(&i915->mm.free_list);
437 
438 	if (unlikely(freed))
439 		__i915_gem_free_objects(i915, freed);
440 }
441 
442 static void __i915_gem_free_work(struct work_struct *work)
443 {
444 	struct drm_i915_private *i915 =
445 		container_of(work, struct drm_i915_private, mm.free_work);
446 
447 	i915_gem_flush_free_objects(i915);
448 }
449 
450 static void i915_gem_free_object(struct drm_gem_object *gem_obj)
451 {
452 	struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
453 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
454 
455 	GEM_BUG_ON(i915_gem_object_is_framebuffer(obj));
456 
457 	/*
458 	 * Before we free the object, make sure any pure RCU-only
459 	 * read-side critical sections are complete, e.g.
460 	 * i915_gem_busy_ioctl(). For the corresponding synchronized
461 	 * lookup see i915_gem_object_lookup_rcu().
462 	 */
463 	atomic_inc(&i915->mm.free_count);
464 
465 	/*
466 	 * Since we require blocking on struct_mutex to unbind the freed
467 	 * object from the GPU before releasing resources back to the
468 	 * system, we can not do that directly from the RCU callback (which may
469 	 * be a softirq context), but must instead then defer that work onto a
470 	 * kthread. We use the RCU callback rather than move the freed object
471 	 * directly onto the work queue so that we can mix between using the
472 	 * worker and performing frees directly from subsequent allocations for
473 	 * crude but effective memory throttling.
474 	 */
475 
476 	if (llist_add(&obj->freed, &i915->mm.free_list))
477 		queue_work(i915->wq, &i915->mm.free_work);
478 }
479 
480 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
481 					 enum fb_op_origin origin)
482 {
483 	struct intel_frontbuffer *front;
484 
485 	front = i915_gem_object_get_frontbuffer(obj);
486 	if (front) {
487 		intel_frontbuffer_flush(front, origin);
488 		intel_frontbuffer_put(front);
489 	}
490 }
491 
492 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
493 					      enum fb_op_origin origin)
494 {
495 	struct intel_frontbuffer *front;
496 
497 	front = i915_gem_object_get_frontbuffer(obj);
498 	if (front) {
499 		intel_frontbuffer_invalidate(front, origin);
500 		intel_frontbuffer_put(front);
501 	}
502 }
503 
504 static void
505 i915_gem_object_read_from_page_kmap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
506 {
507 	pgoff_t idx = offset >> PAGE_SHIFT;
508 	void *src_map;
509 	void *src_ptr;
510 
511 	src_map = kmap_atomic(i915_gem_object_get_page(obj, idx));
512 
513 	src_ptr = src_map + offset_in_page(offset);
514 	if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
515 		drm_clflush_virt_range(src_ptr, size);
516 	memcpy(dst, src_ptr, size);
517 
518 	kunmap_atomic(src_map);
519 }
520 
521 static void
522 i915_gem_object_read_from_page_iomap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
523 {
524 	STUB();
525 #ifdef notyet
526 	pgoff_t idx = offset >> PAGE_SHIFT;
527 	dma_addr_t dma = i915_gem_object_get_dma_address(obj, idx);
528 	void __iomem *src_map;
529 	void __iomem *src_ptr;
530 
531 	src_map = io_mapping_map_wc(&obj->mm.region->iomap,
532 				    dma - obj->mm.region->region.start,
533 				    PAGE_SIZE);
534 
535 	src_ptr = src_map + offset_in_page(offset);
536 	if (!i915_memcpy_from_wc(dst, (void __force *)src_ptr, size))
537 		memcpy_fromio(dst, src_ptr, size);
538 
539 	io_mapping_unmap(src_map);
540 #endif
541 }
542 
543 static bool object_has_mappable_iomem(struct drm_i915_gem_object *obj)
544 {
545 	GEM_BUG_ON(!i915_gem_object_has_iomem(obj));
546 
547 	if (IS_DGFX(to_i915(obj->base.dev)))
548 		return i915_ttm_resource_mappable(i915_gem_to_ttm(obj)->resource);
549 
550 	return true;
551 }
552 
553 /**
554  * i915_gem_object_read_from_page - read data from the page of a GEM object
555  * @obj: GEM object to read from
556  * @offset: offset within the object
557  * @dst: buffer to store the read data
558  * @size: size to read
559  *
560  * Reads data from @obj at the specified offset. The requested region to read
561  * from can't cross a page boundary. The caller must ensure that @obj pages
562  * are pinned and that @obj is synced wrt. any related writes.
563  *
564  * Return: %0 on success or -ENODEV if the type of @obj's backing store is
565  * unsupported.
566  */
567 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
568 {
569 	GEM_BUG_ON(overflows_type(offset >> PAGE_SHIFT, pgoff_t));
570 	GEM_BUG_ON(offset >= obj->base.size);
571 	GEM_BUG_ON(offset_in_page(offset) > PAGE_SIZE - size);
572 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
573 
574 	if (i915_gem_object_has_struct_page(obj))
575 		i915_gem_object_read_from_page_kmap(obj, offset, dst, size);
576 	else if (i915_gem_object_has_iomem(obj) && object_has_mappable_iomem(obj))
577 		i915_gem_object_read_from_page_iomap(obj, offset, dst, size);
578 	else
579 		return -ENODEV;
580 
581 	return 0;
582 }
583 
584 /**
585  * i915_gem_object_evictable - Whether object is likely evictable after unbind.
586  * @obj: The object to check
587  *
588  * This function checks whether the object is likely unvictable after unbind.
589  * If the object is not locked when checking, the result is only advisory.
590  * If the object is locked when checking, and the function returns true,
591  * then an eviction should indeed be possible. But since unlocked vma
592  * unpinning and unbinding is currently possible, the object can actually
593  * become evictable even if this function returns false.
594  *
595  * Return: true if the object may be evictable. False otherwise.
596  */
597 bool i915_gem_object_evictable(struct drm_i915_gem_object *obj)
598 {
599 	struct i915_vma *vma;
600 	int pin_count = atomic_read(&obj->mm.pages_pin_count);
601 
602 	if (!pin_count)
603 		return true;
604 
605 	spin_lock(&obj->vma.lock);
606 	list_for_each_entry(vma, &obj->vma.list, obj_link) {
607 		if (i915_vma_is_pinned(vma)) {
608 			spin_unlock(&obj->vma.lock);
609 			return false;
610 		}
611 		if (atomic_read(&vma->pages_count))
612 			pin_count--;
613 	}
614 	spin_unlock(&obj->vma.lock);
615 	GEM_WARN_ON(pin_count < 0);
616 
617 	return pin_count == 0;
618 }
619 
620 /**
621  * i915_gem_object_migratable - Whether the object is migratable out of the
622  * current region.
623  * @obj: Pointer to the object.
624  *
625  * Return: Whether the object is allowed to be resident in other
626  * regions than the current while pages are present.
627  */
628 bool i915_gem_object_migratable(struct drm_i915_gem_object *obj)
629 {
630 	struct intel_memory_region *mr = READ_ONCE(obj->mm.region);
631 
632 	if (!mr)
633 		return false;
634 
635 	return obj->mm.n_placements > 1;
636 }
637 
638 /**
639  * i915_gem_object_has_struct_page - Whether the object is page-backed
640  * @obj: The object to query.
641  *
642  * This function should only be called while the object is locked or pinned,
643  * otherwise the page backing may change under the caller.
644  *
645  * Return: True if page-backed, false otherwise.
646  */
647 bool i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
648 {
649 #ifdef CONFIG_LOCKDEP
650 	if (IS_DGFX(to_i915(obj->base.dev)) &&
651 	    i915_gem_object_evictable((void __force *)obj))
652 		assert_object_held_shared(obj);
653 #endif
654 	return obj->mem_flags & I915_BO_FLAG_STRUCT_PAGE;
655 }
656 
657 /**
658  * i915_gem_object_has_iomem - Whether the object is iomem-backed
659  * @obj: The object to query.
660  *
661  * This function should only be called while the object is locked or pinned,
662  * otherwise the iomem backing may change under the caller.
663  *
664  * Return: True if iomem-backed, false otherwise.
665  */
666 bool i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj)
667 {
668 #ifdef CONFIG_LOCKDEP
669 	if (IS_DGFX(to_i915(obj->base.dev)) &&
670 	    i915_gem_object_evictable((void __force *)obj))
671 		assert_object_held_shared(obj);
672 #endif
673 	return obj->mem_flags & I915_BO_FLAG_IOMEM;
674 }
675 
676 /**
677  * i915_gem_object_can_migrate - Whether an object likely can be migrated
678  *
679  * @obj: The object to migrate
680  * @id: The region intended to migrate to
681  *
682  * Check whether the object backend supports migration to the
683  * given region. Note that pinning may affect the ability to migrate as
684  * returned by this function.
685  *
686  * This function is primarily intended as a helper for checking the
687  * possibility to migrate objects and might be slightly less permissive
688  * than i915_gem_object_migrate() when it comes to objects with the
689  * I915_BO_ALLOC_USER flag set.
690  *
691  * Return: true if migration is possible, false otherwise.
692  */
693 bool i915_gem_object_can_migrate(struct drm_i915_gem_object *obj,
694 				 enum intel_region_id id)
695 {
696 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
697 	unsigned int num_allowed = obj->mm.n_placements;
698 	struct intel_memory_region *mr;
699 	unsigned int i;
700 
701 	GEM_BUG_ON(id >= INTEL_REGION_UNKNOWN);
702 	GEM_BUG_ON(obj->mm.madv != I915_MADV_WILLNEED);
703 
704 	mr = i915->mm.regions[id];
705 	if (!mr)
706 		return false;
707 
708 	if (!IS_ALIGNED(obj->base.size, mr->min_page_size))
709 		return false;
710 
711 	if (obj->mm.region == mr)
712 		return true;
713 
714 	if (!i915_gem_object_evictable(obj))
715 		return false;
716 
717 	if (!obj->ops->migrate)
718 		return false;
719 
720 	if (!(obj->flags & I915_BO_ALLOC_USER))
721 		return true;
722 
723 	if (num_allowed == 0)
724 		return false;
725 
726 	for (i = 0; i < num_allowed; ++i) {
727 		if (mr == obj->mm.placements[i])
728 			return true;
729 	}
730 
731 	return false;
732 }
733 
734 /**
735  * i915_gem_object_migrate - Migrate an object to the desired region id
736  * @obj: The object to migrate.
737  * @ww: An optional struct i915_gem_ww_ctx. If NULL, the backend may
738  * not be successful in evicting other objects to make room for this object.
739  * @id: The region id to migrate to.
740  *
741  * Attempt to migrate the object to the desired memory region. The
742  * object backend must support migration and the object may not be
743  * pinned, (explicitly pinned pages or pinned vmas). The object must
744  * be locked.
745  * On successful completion, the object will have pages pointing to
746  * memory in the new region, but an async migration task may not have
747  * completed yet, and to accomplish that, i915_gem_object_wait_migration()
748  * must be called.
749  *
750  * Note: the @ww parameter is not used yet, but included to make sure
751  * callers put some effort into obtaining a valid ww ctx if one is
752  * available.
753  *
754  * Return: 0 on success. Negative error code on failure. In particular may
755  * return -ENXIO on lack of region space, -EDEADLK for deadlock avoidance
756  * if @ww is set, -EINTR or -ERESTARTSYS if signal pending, and
757  * -EBUSY if the object is pinned.
758  */
759 int i915_gem_object_migrate(struct drm_i915_gem_object *obj,
760 			    struct i915_gem_ww_ctx *ww,
761 			    enum intel_region_id id)
762 {
763 	return __i915_gem_object_migrate(obj, ww, id, obj->flags);
764 }
765 
766 /**
767  * __i915_gem_object_migrate - Migrate an object to the desired region id, with
768  * control of the extra flags
769  * @obj: The object to migrate.
770  * @ww: An optional struct i915_gem_ww_ctx. If NULL, the backend may
771  * not be successful in evicting other objects to make room for this object.
772  * @id: The region id to migrate to.
773  * @flags: The object flags. Normally just obj->flags.
774  *
775  * Attempt to migrate the object to the desired memory region. The
776  * object backend must support migration and the object may not be
777  * pinned, (explicitly pinned pages or pinned vmas). The object must
778  * be locked.
779  * On successful completion, the object will have pages pointing to
780  * memory in the new region, but an async migration task may not have
781  * completed yet, and to accomplish that, i915_gem_object_wait_migration()
782  * must be called.
783  *
784  * Note: the @ww parameter is not used yet, but included to make sure
785  * callers put some effort into obtaining a valid ww ctx if one is
786  * available.
787  *
788  * Return: 0 on success. Negative error code on failure. In particular may
789  * return -ENXIO on lack of region space, -EDEADLK for deadlock avoidance
790  * if @ww is set, -EINTR or -ERESTARTSYS if signal pending, and
791  * -EBUSY if the object is pinned.
792  */
793 int __i915_gem_object_migrate(struct drm_i915_gem_object *obj,
794 			      struct i915_gem_ww_ctx *ww,
795 			      enum intel_region_id id,
796 			      unsigned int flags)
797 {
798 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
799 	struct intel_memory_region *mr;
800 
801 	GEM_BUG_ON(id >= INTEL_REGION_UNKNOWN);
802 	GEM_BUG_ON(obj->mm.madv != I915_MADV_WILLNEED);
803 	assert_object_held(obj);
804 
805 	mr = i915->mm.regions[id];
806 	GEM_BUG_ON(!mr);
807 
808 	if (!i915_gem_object_can_migrate(obj, id))
809 		return -EINVAL;
810 
811 	if (!obj->ops->migrate) {
812 		if (GEM_WARN_ON(obj->mm.region != mr))
813 			return -EINVAL;
814 		return 0;
815 	}
816 
817 	return obj->ops->migrate(obj, mr, flags);
818 }
819 
820 /**
821  * i915_gem_object_placement_possible - Check whether the object can be
822  * placed at certain memory type
823  * @obj: Pointer to the object
824  * @type: The memory type to check
825  *
826  * Return: True if the object can be placed in @type. False otherwise.
827  */
828 bool i915_gem_object_placement_possible(struct drm_i915_gem_object *obj,
829 					enum intel_memory_type type)
830 {
831 	unsigned int i;
832 
833 	if (!obj->mm.n_placements) {
834 		switch (type) {
835 		case INTEL_MEMORY_LOCAL:
836 			return i915_gem_object_has_iomem(obj);
837 		case INTEL_MEMORY_SYSTEM:
838 			return i915_gem_object_has_pages(obj);
839 		default:
840 			/* Ignore stolen for now */
841 			GEM_BUG_ON(1);
842 			return false;
843 		}
844 	}
845 
846 	for (i = 0; i < obj->mm.n_placements; i++) {
847 		if (obj->mm.placements[i]->type == type)
848 			return true;
849 	}
850 
851 	return false;
852 }
853 
854 /**
855  * i915_gem_object_needs_ccs_pages - Check whether the object requires extra
856  * pages when placed in system-memory, in order to save and later restore the
857  * flat-CCS aux state when the object is moved between local-memory and
858  * system-memory
859  * @obj: Pointer to the object
860  *
861  * Return: True if the object needs extra ccs pages. False otherwise.
862  */
863 bool i915_gem_object_needs_ccs_pages(struct drm_i915_gem_object *obj)
864 {
865 	bool lmem_placement = false;
866 	int i;
867 
868 	if (!HAS_FLAT_CCS(to_i915(obj->base.dev)))
869 		return false;
870 
871 	if (obj->flags & I915_BO_ALLOC_CCS_AUX)
872 		return true;
873 
874 	for (i = 0; i < obj->mm.n_placements; i++) {
875 		/* Compression is not allowed for the objects with smem placement */
876 		if (obj->mm.placements[i]->type == INTEL_MEMORY_SYSTEM)
877 			return false;
878 		if (!lmem_placement &&
879 		    obj->mm.placements[i]->type == INTEL_MEMORY_LOCAL)
880 			lmem_placement = true;
881 	}
882 
883 	return lmem_placement;
884 }
885 
886 void i915_gem_init__objects(struct drm_i915_private *i915)
887 {
888 	INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
889 }
890 
891 void i915_objects_module_exit(void)
892 {
893 #ifdef __linux__
894 	kmem_cache_destroy(slab_objects);
895 #else
896 	pool_destroy(&slab_objects);
897 #endif
898 }
899 
900 int __init i915_objects_module_init(void)
901 {
902 #ifdef __linux__
903 	slab_objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
904 	if (!slab_objects)
905 		return -ENOMEM;
906 #else
907 	pool_init(&slab_objects, sizeof(struct drm_i915_gem_object),
908 	    CACHELINESIZE, IPL_NONE, 0, "drmobj", NULL);
909 #endif
910 
911 	return 0;
912 }
913 
914 static const struct drm_gem_object_funcs i915_gem_object_funcs = {
915 	.free = i915_gem_free_object,
916 	.close = i915_gem_close_object,
917 	.export = i915_gem_prime_export,
918 };
919 
920 /**
921  * i915_gem_object_get_moving_fence - Get the object's moving fence if any
922  * @obj: The object whose moving fence to get.
923  * @fence: The resulting fence
924  *
925  * A non-signaled moving fence means that there is an async operation
926  * pending on the object that needs to be waited on before setting up
927  * any GPU- or CPU PTEs to the object's pages.
928  *
929  * Return: Negative error code or 0 for success.
930  */
931 int i915_gem_object_get_moving_fence(struct drm_i915_gem_object *obj,
932 				     struct dma_fence **fence)
933 {
934 	return dma_resv_get_singleton(obj->base.resv, DMA_RESV_USAGE_KERNEL,
935 				      fence);
936 }
937 
938 /**
939  * i915_gem_object_wait_moving_fence - Wait for the object's moving fence if any
940  * @obj: The object whose moving fence to wait for.
941  * @intr: Whether to wait interruptible.
942  *
943  * If the moving fence signaled without an error, it is detached from the
944  * object and put.
945  *
946  * Return: 0 if successful, -ERESTARTSYS if the wait was interrupted,
947  * negative error code if the async operation represented by the
948  * moving fence failed.
949  */
950 int i915_gem_object_wait_moving_fence(struct drm_i915_gem_object *obj,
951 				      bool intr)
952 {
953 	long ret;
954 
955 	assert_object_held(obj);
956 
957 	ret = dma_resv_wait_timeout(obj->base. resv, DMA_RESV_USAGE_KERNEL,
958 				    intr, MAX_SCHEDULE_TIMEOUT);
959 	if (!ret)
960 		ret = -ETIME;
961 	else if (ret > 0 && i915_gem_object_has_unknown_state(obj))
962 		ret = -EIO;
963 
964 	return ret < 0 ? ret : 0;
965 }
966 
967 /*
968  * i915_gem_object_has_unknown_state - Return true if the object backing pages are
969  * in an unknown_state. This means that userspace must NEVER be allowed to touch
970  * the pages, with either the GPU or CPU.
971  *
972  * ONLY valid to be called after ensuring that all kernel fences have signalled
973  * (in particular the fence for moving/clearing the object).
974  */
975 bool i915_gem_object_has_unknown_state(struct drm_i915_gem_object *obj)
976 {
977 	/*
978 	 * The below barrier pairs with the dma_fence_signal() in
979 	 * __memcpy_work(). We should only sample the unknown_state after all
980 	 * the kernel fences have signalled.
981 	 */
982 	smp_rmb();
983 	return obj->mm.unknown_state;
984 }
985 
986 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
987 #include "selftests/huge_gem_object.c"
988 #include "selftests/huge_pages.c"
989 #include "selftests/i915_gem_migrate.c"
990 #include "selftests/i915_gem_object.c"
991 #include "selftests/i915_gem_coherency.c"
992 #endif
993