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