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