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