• 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 "gt/intel_gt.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_globals.h"
35 #include "i915_trace.h"
36 
37 static struct i915_global_object {
38 	struct i915_global base;
39 	struct kmem_cache *slab_objects;
40 } global;
41 
i915_gem_object_alloc(void)42 struct drm_i915_gem_object *i915_gem_object_alloc(void)
43 {
44 	return kmem_cache_zalloc(global.slab_objects, GFP_KERNEL);
45 }
46 
i915_gem_object_free(struct drm_i915_gem_object * obj)47 void i915_gem_object_free(struct drm_i915_gem_object *obj)
48 {
49 	return kmem_cache_free(global.slab_objects, obj);
50 }
51 
i915_gem_object_init(struct drm_i915_gem_object * obj,const struct drm_i915_gem_object_ops * ops,struct lock_class_key * key)52 void i915_gem_object_init(struct drm_i915_gem_object *obj,
53 			  const struct drm_i915_gem_object_ops *ops,
54 			  struct lock_class_key *key)
55 {
56 	__mutex_init(&obj->mm.lock, ops->name ?: "obj->mm.lock", key);
57 
58 	spin_lock_init(&obj->vma.lock);
59 	INIT_LIST_HEAD(&obj->vma.list);
60 
61 	INIT_LIST_HEAD(&obj->mm.link);
62 
63 	INIT_LIST_HEAD(&obj->lut_list);
64 	spin_lock_init(&obj->lut_lock);
65 
66 	spin_lock_init(&obj->mmo.lock);
67 	obj->mmo.offsets = RB_ROOT;
68 
69 	init_rcu_head(&obj->rcu);
70 
71 	obj->ops = ops;
72 
73 	obj->mm.madv = I915_MADV_WILLNEED;
74 	INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
75 	mutex_init(&obj->mm.get_page.lock);
76 
77 	if (IS_ENABLED(CONFIG_LOCKDEP) && i915_gem_object_is_shrinkable(obj))
78 		i915_gem_shrinker_taints_mutex(to_i915(obj->base.dev),
79 					       &obj->mm.lock);
80 }
81 
82 /**
83  * Mark up the object's coherency levels for a given cache_level
84  * @obj: #drm_i915_gem_object
85  * @cache_level: cache level
86  */
i915_gem_object_set_cache_coherency(struct drm_i915_gem_object * obj,unsigned int cache_level)87 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
88 					 unsigned int cache_level)
89 {
90 	obj->cache_level = cache_level;
91 
92 	if (cache_level != I915_CACHE_NONE)
93 		obj->cache_coherent = (I915_BO_CACHE_COHERENT_FOR_READ |
94 				       I915_BO_CACHE_COHERENT_FOR_WRITE);
95 	else if (HAS_LLC(to_i915(obj->base.dev)))
96 		obj->cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ;
97 	else
98 		obj->cache_coherent = 0;
99 
100 	obj->cache_dirty =
101 		!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE);
102 }
103 
i915_gem_close_object(struct drm_gem_object * gem,struct drm_file * file)104 void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
105 {
106 	struct drm_i915_gem_object *obj = to_intel_bo(gem);
107 	struct drm_i915_file_private *fpriv = file->driver_priv;
108 	struct i915_lut_handle bookmark = {};
109 	struct i915_mmap_offset *mmo, *mn;
110 	struct i915_lut_handle *lut, *ln;
111 	LIST_HEAD(close);
112 
113 	spin_lock(&obj->lut_lock);
114 	list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
115 		struct i915_gem_context *ctx = lut->ctx;
116 
117 		if (ctx && ctx->file_priv == fpriv) {
118 			i915_gem_context_get(ctx);
119 			list_move(&lut->obj_link, &close);
120 		}
121 
122 		/* Break long locks, and carefully continue on from this spot */
123 		if (&ln->obj_link != &obj->lut_list) {
124 			list_add_tail(&bookmark.obj_link, &ln->obj_link);
125 			if (cond_resched_lock(&obj->lut_lock))
126 				list_safe_reset_next(&bookmark, ln, obj_link);
127 			__list_del_entry(&bookmark.obj_link);
128 		}
129 	}
130 	spin_unlock(&obj->lut_lock);
131 
132 	spin_lock(&obj->mmo.lock);
133 	rbtree_postorder_for_each_entry_safe(mmo, mn, &obj->mmo.offsets, offset)
134 		drm_vma_node_revoke(&mmo->vma_node, file);
135 	spin_unlock(&obj->mmo.lock);
136 
137 	list_for_each_entry_safe(lut, ln, &close, obj_link) {
138 		struct i915_gem_context *ctx = lut->ctx;
139 		struct i915_vma *vma;
140 
141 		/*
142 		 * We allow the process to have multiple handles to the same
143 		 * vma, in the same fd namespace, by virtue of flink/open.
144 		 */
145 
146 		mutex_lock(&ctx->lut_mutex);
147 		vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
148 		if (vma) {
149 			GEM_BUG_ON(vma->obj != obj);
150 			GEM_BUG_ON(!atomic_read(&vma->open_count));
151 			i915_vma_close(vma);
152 		}
153 		mutex_unlock(&ctx->lut_mutex);
154 
155 		i915_gem_context_put(lut->ctx);
156 		i915_lut_handle_free(lut);
157 		i915_gem_object_put(obj);
158 	}
159 }
160 
__i915_gem_free_object_rcu(struct rcu_head * head)161 static void __i915_gem_free_object_rcu(struct rcu_head *head)
162 {
163 	struct drm_i915_gem_object *obj =
164 		container_of(head, typeof(*obj), rcu);
165 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
166 
167 	dma_resv_fini(&obj->base._resv);
168 	i915_gem_object_free(obj);
169 
170 	GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
171 	atomic_dec(&i915->mm.free_count);
172 }
173 
__i915_gem_object_free_mmaps(struct drm_i915_gem_object * obj)174 static void __i915_gem_object_free_mmaps(struct drm_i915_gem_object *obj)
175 {
176 	/* Skip serialisation and waking the device if known to be not used. */
177 
178 	if (obj->userfault_count)
179 		i915_gem_object_release_mmap_gtt(obj);
180 
181 	if (!RB_EMPTY_ROOT(&obj->mmo.offsets)) {
182 		struct i915_mmap_offset *mmo, *mn;
183 
184 		i915_gem_object_release_mmap_offset(obj);
185 
186 		rbtree_postorder_for_each_entry_safe(mmo, mn,
187 						     &obj->mmo.offsets,
188 						     offset) {
189 			drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
190 					      &mmo->vma_node);
191 			kfree(mmo);
192 		}
193 		obj->mmo.offsets = RB_ROOT;
194 	}
195 }
196 
__i915_gem_free_objects(struct drm_i915_private * i915,struct llist_node * freed)197 static void __i915_gem_free_objects(struct drm_i915_private *i915,
198 				    struct llist_node *freed)
199 {
200 	struct drm_i915_gem_object *obj, *on;
201 
202 	llist_for_each_entry_safe(obj, on, freed, freed) {
203 		trace_i915_gem_object_destroy(obj);
204 
205 		if (!list_empty(&obj->vma.list)) {
206 			struct i915_vma *vma;
207 
208 			/*
209 			 * Note that the vma keeps an object reference while
210 			 * it is active, so it *should* not sleep while we
211 			 * destroy it. Our debug code errs insits it *might*.
212 			 * For the moment, play along.
213 			 */
214 			spin_lock(&obj->vma.lock);
215 			while ((vma = list_first_entry_or_null(&obj->vma.list,
216 							       struct i915_vma,
217 							       obj_link))) {
218 				GEM_BUG_ON(vma->obj != obj);
219 				spin_unlock(&obj->vma.lock);
220 
221 				__i915_vma_put(vma);
222 
223 				spin_lock(&obj->vma.lock);
224 			}
225 			spin_unlock(&obj->vma.lock);
226 		}
227 
228 		__i915_gem_object_free_mmaps(obj);
229 
230 		GEM_BUG_ON(!list_empty(&obj->lut_list));
231 
232 		atomic_set(&obj->mm.pages_pin_count, 0);
233 		__i915_gem_object_put_pages(obj);
234 		GEM_BUG_ON(i915_gem_object_has_pages(obj));
235 		bitmap_free(obj->bit_17);
236 
237 		if (obj->base.import_attach)
238 			drm_prime_gem_destroy(&obj->base, NULL);
239 
240 		drm_gem_free_mmap_offset(&obj->base);
241 
242 		if (obj->ops->release)
243 			obj->ops->release(obj);
244 
245 		/* But keep the pointer alive for RCU-protected lookups */
246 		call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
247 		cond_resched();
248 	}
249 }
250 
i915_gem_flush_free_objects(struct drm_i915_private * i915)251 void i915_gem_flush_free_objects(struct drm_i915_private *i915)
252 {
253 	struct llist_node *freed = llist_del_all(&i915->mm.free_list);
254 
255 	if (unlikely(freed))
256 		__i915_gem_free_objects(i915, freed);
257 }
258 
__i915_gem_free_work(struct work_struct * work)259 static void __i915_gem_free_work(struct work_struct *work)
260 {
261 	struct drm_i915_private *i915 =
262 		container_of(work, struct drm_i915_private, mm.free_work);
263 
264 	i915_gem_flush_free_objects(i915);
265 }
266 
i915_gem_free_object(struct drm_gem_object * gem_obj)267 void i915_gem_free_object(struct drm_gem_object *gem_obj)
268 {
269 	struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
270 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
271 
272 	GEM_BUG_ON(i915_gem_object_is_framebuffer(obj));
273 
274 	/*
275 	 * Before we free the object, make sure any pure RCU-only
276 	 * read-side critical sections are complete, e.g.
277 	 * i915_gem_busy_ioctl(). For the corresponding synchronized
278 	 * lookup see i915_gem_object_lookup_rcu().
279 	 */
280 	atomic_inc(&i915->mm.free_count);
281 
282 	/*
283 	 * This serializes freeing with the shrinker. Since the free
284 	 * is delayed, first by RCU then by the workqueue, we want the
285 	 * shrinker to be able to free pages of unreferenced objects,
286 	 * or else we may oom whilst there are plenty of deferred
287 	 * freed objects.
288 	 */
289 	i915_gem_object_make_unshrinkable(obj);
290 
291 	/*
292 	 * Since we require blocking on struct_mutex to unbind the freed
293 	 * object from the GPU before releasing resources back to the
294 	 * system, we can not do that directly from the RCU callback (which may
295 	 * be a softirq context), but must instead then defer that work onto a
296 	 * kthread. We use the RCU callback rather than move the freed object
297 	 * directly onto the work queue so that we can mix between using the
298 	 * worker and performing frees directly from subsequent allocations for
299 	 * crude but effective memory throttling.
300 	 */
301 	if (llist_add(&obj->freed, &i915->mm.free_list))
302 		queue_work(i915->wq, &i915->mm.free_work);
303 }
304 
gpu_write_needs_clflush(struct drm_i915_gem_object * obj)305 static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
306 {
307 	return !(obj->cache_level == I915_CACHE_NONE ||
308 		 obj->cache_level == I915_CACHE_WT);
309 }
310 
311 void
i915_gem_object_flush_write_domain(struct drm_i915_gem_object * obj,unsigned int flush_domains)312 i915_gem_object_flush_write_domain(struct drm_i915_gem_object *obj,
313 				   unsigned int flush_domains)
314 {
315 	struct i915_vma *vma;
316 
317 	assert_object_held(obj);
318 
319 	if (!(obj->write_domain & flush_domains))
320 		return;
321 
322 	switch (obj->write_domain) {
323 	case I915_GEM_DOMAIN_GTT:
324 		spin_lock(&obj->vma.lock);
325 		for_each_ggtt_vma(vma, obj) {
326 			if (i915_vma_unset_ggtt_write(vma))
327 				intel_gt_flush_ggtt_writes(vma->vm->gt);
328 		}
329 		spin_unlock(&obj->vma.lock);
330 
331 		i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
332 		break;
333 
334 	case I915_GEM_DOMAIN_WC:
335 		wmb();
336 		break;
337 
338 	case I915_GEM_DOMAIN_CPU:
339 		i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
340 		break;
341 
342 	case I915_GEM_DOMAIN_RENDER:
343 		if (gpu_write_needs_clflush(obj))
344 			obj->cache_dirty = true;
345 		break;
346 	}
347 
348 	obj->write_domain = 0;
349 }
350 
__i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object * obj,enum fb_op_origin origin)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 
__i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object * obj,enum fb_op_origin origin)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 
i915_gem_init__objects(struct drm_i915_private * i915)375 void i915_gem_init__objects(struct drm_i915_private *i915)
376 {
377 	INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
378 }
379 
i915_global_objects_shrink(void)380 static void i915_global_objects_shrink(void)
381 {
382 	kmem_cache_shrink(global.slab_objects);
383 }
384 
i915_global_objects_exit(void)385 static void i915_global_objects_exit(void)
386 {
387 	kmem_cache_destroy(global.slab_objects);
388 }
389 
390 static struct i915_global_object global = { {
391 	.shrink = i915_global_objects_shrink,
392 	.exit = i915_global_objects_exit,
393 } };
394 
i915_global_objects_init(void)395 int __init i915_global_objects_init(void)
396 {
397 	global.slab_objects =
398 		KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
399 	if (!global.slab_objects)
400 		return -ENOMEM;
401 
402 	i915_global_register(&global.base);
403 	return 0;
404 }
405 
406 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
407 #include "selftests/huge_gem_object.c"
408 #include "selftests/huge_pages.c"
409 #include "selftests/i915_gem_object.c"
410 #include "selftests/i915_gem_coherency.c"
411 #endif
412