2 * Copyright © 2017 Intel Corporation
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:
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
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
25 #include <linux/sched/mm.h>
27 #include "display/intel_frontbuffer.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_globals.h"
34 #include "i915_memcpy.h"
35 #include "i915_trace.h"
37 static struct i915_global_object {
38 struct i915_global base;
39 struct kmem_cache *slab_objects;
42 static const struct drm_gem_object_funcs i915_gem_object_funcs;
44 struct drm_i915_gem_object *i915_gem_object_alloc(void)
46 struct drm_i915_gem_object *obj;
48 obj = kmem_cache_zalloc(global.slab_objects, GFP_KERNEL);
51 obj->base.funcs = &i915_gem_object_funcs;
56 void i915_gem_object_free(struct drm_i915_gem_object *obj)
58 return kmem_cache_free(global.slab_objects, obj);
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)
65 __mutex_init(&obj->mm.lock, ops->name ?: "obj->mm.lock", key);
67 spin_lock_init(&obj->vma.lock);
68 INIT_LIST_HEAD(&obj->vma.list);
70 INIT_LIST_HEAD(&obj->mm.link);
72 INIT_LIST_HEAD(&obj->lut_list);
73 spin_lock_init(&obj->lut_lock);
75 spin_lock_init(&obj->mmo.lock);
76 obj->mmo.offsets = RB_ROOT;
78 init_rcu_head(&obj->rcu);
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 INIT_RADIX_TREE(&obj->mm.get_dma_page.radix, GFP_KERNEL | __GFP_NOWARN);
86 mutex_init(&obj->mm.get_dma_page.lock);
88 if (IS_ENABLED(CONFIG_LOCKDEP) && i915_gem_object_is_shrinkable(obj))
89 i915_gem_shrinker_taints_mutex(to_i915(obj->base.dev),
94 * Mark up the object's coherency levels for a given cache_level
95 * @obj: #drm_i915_gem_object
96 * @cache_level: cache level
98 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
99 unsigned int cache_level)
101 obj->cache_level = cache_level;
103 if (cache_level != I915_CACHE_NONE)
104 obj->cache_coherent = (I915_BO_CACHE_COHERENT_FOR_READ |
105 I915_BO_CACHE_COHERENT_FOR_WRITE);
106 else if (HAS_LLC(to_i915(obj->base.dev)))
107 obj->cache_coherent = I915_BO_CACHE_COHERENT_FOR_READ;
109 obj->cache_coherent = 0;
112 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE);
115 static void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
117 struct drm_i915_gem_object *obj = to_intel_bo(gem);
118 struct drm_i915_file_private *fpriv = file->driver_priv;
119 struct i915_lut_handle bookmark = {};
120 struct i915_mmap_offset *mmo, *mn;
121 struct i915_lut_handle *lut, *ln;
124 spin_lock(&obj->lut_lock);
125 list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
126 struct i915_gem_context *ctx = lut->ctx;
128 if (ctx && ctx->file_priv == fpriv) {
129 i915_gem_context_get(ctx);
130 list_move(&lut->obj_link, &close);
133 /* Break long locks, and carefully continue on from this spot */
134 if (&ln->obj_link != &obj->lut_list) {
135 list_add_tail(&bookmark.obj_link, &ln->obj_link);
136 if (cond_resched_lock(&obj->lut_lock))
137 list_safe_reset_next(&bookmark, ln, obj_link);
138 __list_del_entry(&bookmark.obj_link);
141 spin_unlock(&obj->lut_lock);
143 spin_lock(&obj->mmo.lock);
144 rbtree_postorder_for_each_entry_safe(mmo, mn, &obj->mmo.offsets, offset)
145 drm_vma_node_revoke(&mmo->vma_node, file);
146 spin_unlock(&obj->mmo.lock);
148 list_for_each_entry_safe(lut, ln, &close, obj_link) {
149 struct i915_gem_context *ctx = lut->ctx;
150 struct i915_vma *vma;
153 * We allow the process to have multiple handles to the same
154 * vma, in the same fd namespace, by virtue of flink/open.
157 mutex_lock(&ctx->lut_mutex);
158 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
160 GEM_BUG_ON(vma->obj != obj);
161 GEM_BUG_ON(!atomic_read(&vma->open_count));
164 mutex_unlock(&ctx->lut_mutex);
166 i915_gem_context_put(lut->ctx);
167 i915_lut_handle_free(lut);
168 i915_gem_object_put(obj);
172 static void __i915_gem_free_object_rcu(struct rcu_head *head)
174 struct drm_i915_gem_object *obj =
175 container_of(head, typeof(*obj), rcu);
176 struct drm_i915_private *i915 = to_i915(obj->base.dev);
178 dma_resv_fini(&obj->base._resv);
179 i915_gem_object_free(obj);
181 GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
182 atomic_dec(&i915->mm.free_count);
185 static void __i915_gem_object_free_mmaps(struct drm_i915_gem_object *obj)
187 /* Skip serialisation and waking the device if known to be not used. */
189 if (obj->userfault_count)
190 i915_gem_object_release_mmap_gtt(obj);
192 if (!RB_EMPTY_ROOT(&obj->mmo.offsets)) {
193 struct i915_mmap_offset *mmo, *mn;
195 i915_gem_object_release_mmap_offset(obj);
197 rbtree_postorder_for_each_entry_safe(mmo, mn,
200 drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
204 obj->mmo.offsets = RB_ROOT;
208 static void __i915_gem_free_objects(struct drm_i915_private *i915,
209 struct llist_node *freed)
211 struct drm_i915_gem_object *obj, *on;
213 llist_for_each_entry_safe(obj, on, freed, freed) {
214 trace_i915_gem_object_destroy(obj);
216 if (!list_empty(&obj->vma.list)) {
217 struct i915_vma *vma;
220 * Note that the vma keeps an object reference while
221 * it is active, so it *should* not sleep while we
222 * destroy it. Our debug code errs insits it *might*.
223 * For the moment, play along.
225 spin_lock(&obj->vma.lock);
226 while ((vma = list_first_entry_or_null(&obj->vma.list,
229 GEM_BUG_ON(vma->obj != obj);
230 spin_unlock(&obj->vma.lock);
234 spin_lock(&obj->vma.lock);
236 spin_unlock(&obj->vma.lock);
239 __i915_gem_object_free_mmaps(obj);
241 GEM_BUG_ON(!list_empty(&obj->lut_list));
243 atomic_set(&obj->mm.pages_pin_count, 0);
244 __i915_gem_object_put_pages(obj);
245 GEM_BUG_ON(i915_gem_object_has_pages(obj));
246 bitmap_free(obj->bit_17);
248 if (obj->base.import_attach)
249 drm_prime_gem_destroy(&obj->base, NULL);
251 drm_gem_free_mmap_offset(&obj->base);
253 if (obj->ops->release)
254 obj->ops->release(obj);
256 /* But keep the pointer alive for RCU-protected lookups */
257 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
262 void i915_gem_flush_free_objects(struct drm_i915_private *i915)
264 struct llist_node *freed = llist_del_all(&i915->mm.free_list);
267 __i915_gem_free_objects(i915, freed);
270 static void __i915_gem_free_work(struct work_struct *work)
272 struct drm_i915_private *i915 =
273 container_of(work, struct drm_i915_private, mm.free_work);
275 i915_gem_flush_free_objects(i915);
278 static void i915_gem_free_object(struct drm_gem_object *gem_obj)
280 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
281 struct drm_i915_private *i915 = to_i915(obj->base.dev);
283 GEM_BUG_ON(i915_gem_object_is_framebuffer(obj));
286 * Before we free the object, make sure any pure RCU-only
287 * read-side critical sections are complete, e.g.
288 * i915_gem_busy_ioctl(). For the corresponding synchronized
289 * lookup see i915_gem_object_lookup_rcu().
291 atomic_inc(&i915->mm.free_count);
294 * This serializes freeing with the shrinker. Since the free
295 * is delayed, first by RCU then by the workqueue, we want the
296 * shrinker to be able to free pages of unreferenced objects,
297 * or else we may oom whilst there are plenty of deferred
300 i915_gem_object_make_unshrinkable(obj);
303 * Since we require blocking on struct_mutex to unbind the freed
304 * object from the GPU before releasing resources back to the
305 * system, we can not do that directly from the RCU callback (which may
306 * be a softirq context), but must instead then defer that work onto a
307 * kthread. We use the RCU callback rather than move the freed object
308 * directly onto the work queue so that we can mix between using the
309 * worker and performing frees directly from subsequent allocations for
310 * crude but effective memory throttling.
312 if (llist_add(&obj->freed, &i915->mm.free_list))
313 queue_work(i915->wq, &i915->mm.free_work);
316 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
317 enum fb_op_origin origin)
319 struct intel_frontbuffer *front;
321 front = __intel_frontbuffer_get(obj);
323 intel_frontbuffer_flush(front, origin);
324 intel_frontbuffer_put(front);
328 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
329 enum fb_op_origin origin)
331 struct intel_frontbuffer *front;
333 front = __intel_frontbuffer_get(obj);
335 intel_frontbuffer_invalidate(front, origin);
336 intel_frontbuffer_put(front);
341 i915_gem_object_read_from_page_kmap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
346 src_map = kmap_atomic(i915_gem_object_get_page(obj, offset >> PAGE_SHIFT));
348 src_ptr = src_map + offset_in_page(offset);
349 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
350 drm_clflush_virt_range(src_ptr, size);
351 memcpy(dst, src_ptr, size);
353 kunmap_atomic(src_map);
357 i915_gem_object_read_from_page_iomap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
359 void __iomem *src_map;
360 void __iomem *src_ptr;
361 dma_addr_t dma = i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT);
363 src_map = io_mapping_map_wc(&obj->mm.region->iomap,
364 dma - obj->mm.region->region.start,
367 src_ptr = src_map + offset_in_page(offset);
368 if (!i915_memcpy_from_wc(dst, (void __force *)src_ptr, size))
369 memcpy_fromio(dst, src_ptr, size);
371 io_mapping_unmap(src_map);
375 * i915_gem_object_read_from_page - read data from the page of a GEM object
376 * @obj: GEM object to read from
377 * @offset: offset within the object
378 * @dst: buffer to store the read data
379 * @size: size to read
381 * Reads data from @obj at the specified offset. The requested region to read
382 * from can't cross a page boundary. The caller must ensure that @obj pages
383 * are pinned and that @obj is synced wrt. any related writes.
385 * Returns 0 on success or -ENODEV if the type of @obj's backing store is
388 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
390 GEM_BUG_ON(offset >= obj->base.size);
391 GEM_BUG_ON(offset_in_page(offset) > PAGE_SIZE - size);
392 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
394 if (i915_gem_object_has_struct_page(obj))
395 i915_gem_object_read_from_page_kmap(obj, offset, dst, size);
396 else if (i915_gem_object_has_iomem(obj))
397 i915_gem_object_read_from_page_iomap(obj, offset, dst, size);
404 void i915_gem_init__objects(struct drm_i915_private *i915)
406 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
409 static void i915_global_objects_shrink(void)
411 kmem_cache_shrink(global.slab_objects);
414 static void i915_global_objects_exit(void)
416 kmem_cache_destroy(global.slab_objects);
419 static struct i915_global_object global = { {
420 .shrink = i915_global_objects_shrink,
421 .exit = i915_global_objects_exit,
424 int __init i915_global_objects_init(void)
426 global.slab_objects =
427 KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
428 if (!global.slab_objects)
431 i915_global_register(&global.base);
435 static const struct drm_gem_object_funcs i915_gem_object_funcs = {
436 .free = i915_gem_free_object,
437 .close = i915_gem_close_object,
438 .export = i915_gem_prime_export,
441 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
442 #include "selftests/huge_gem_object.c"
443 #include "selftests/huge_pages.c"
444 #include "selftests/i915_gem_object.c"
445 #include "selftests/i915_gem_coherency.c"