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"
28 #include "gt/intel_gt.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"
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 static bool gpu_write_needs_clflush(struct drm_i915_gem_object
*obj
)
318 return !(obj
->cache_level
== I915_CACHE_NONE
||
319 obj
->cache_level
== I915_CACHE_WT
);
323 i915_gem_object_flush_write_domain(struct drm_i915_gem_object
*obj
,
324 unsigned int flush_domains
)
326 struct i915_vma
*vma
;
328 assert_object_held(obj
);
330 if (!(obj
->write_domain
& flush_domains
))
333 switch (obj
->write_domain
) {
334 case I915_GEM_DOMAIN_GTT
:
335 spin_lock(&obj
->vma
.lock
);
336 for_each_ggtt_vma(vma
, obj
) {
337 if (i915_vma_unset_ggtt_write(vma
))
338 intel_gt_flush_ggtt_writes(vma
->vm
->gt
);
340 spin_unlock(&obj
->vma
.lock
);
342 i915_gem_object_flush_frontbuffer(obj
, ORIGIN_CPU
);
345 case I915_GEM_DOMAIN_WC
:
349 case I915_GEM_DOMAIN_CPU
:
350 i915_gem_clflush_object(obj
, I915_CLFLUSH_SYNC
);
353 case I915_GEM_DOMAIN_RENDER
:
354 if (gpu_write_needs_clflush(obj
))
355 obj
->cache_dirty
= true;
359 obj
->write_domain
= 0;
362 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object
*obj
,
363 enum fb_op_origin origin
)
365 struct intel_frontbuffer
*front
;
367 front
= __intel_frontbuffer_get(obj
);
369 intel_frontbuffer_flush(front
, origin
);
370 intel_frontbuffer_put(front
);
374 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object
*obj
,
375 enum fb_op_origin origin
)
377 struct intel_frontbuffer
*front
;
379 front
= __intel_frontbuffer_get(obj
);
381 intel_frontbuffer_invalidate(front
, origin
);
382 intel_frontbuffer_put(front
);
386 void i915_gem_init__objects(struct drm_i915_private
*i915
)
388 INIT_WORK(&i915
->mm
.free_work
, __i915_gem_free_work
);
391 static void i915_global_objects_shrink(void)
393 kmem_cache_shrink(global
.slab_objects
);
396 static void i915_global_objects_exit(void)
398 kmem_cache_destroy(global
.slab_objects
);
401 static struct i915_global_object global
= { {
402 .shrink
= i915_global_objects_shrink
,
403 .exit
= i915_global_objects_exit
,
406 int __init
i915_global_objects_init(void)
408 global
.slab_objects
=
409 KMEM_CACHE(drm_i915_gem_object
, SLAB_HWCACHE_ALIGN
);
410 if (!global
.slab_objects
)
413 i915_global_register(&global
.base
);
417 static const struct drm_gem_object_funcs i915_gem_object_funcs
= {
418 .free
= i915_gem_free_object
,
419 .close
= i915_gem_close_object
,
420 .export
= i915_gem_prime_export
,
423 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
424 #include "selftests/huge_gem_object.c"
425 #include "selftests/huge_pages.c"
426 #include "selftests/i915_gem_object.c"
427 #include "selftests/i915_gem_coherency.c"