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