00d24000b5e8cddfceee8633f21840df0d167741
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / gem / i915_gem_object.c
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         INIT_RADIX_TREE(&obj->mm.get_dma_page.radix, GFP_KERNEL | __GFP_NOWARN);
86         mutex_init(&obj->mm.get_dma_page.lock);
87
88         if (IS_ENABLED(CONFIG_LOCKDEP) && i915_gem_object_is_shrinkable(obj))
89                 i915_gem_shrinker_taints_mutex(to_i915(obj->base.dev),
90                                                &obj->mm.lock);
91 }
92
93 /**
94  * Mark up the object's coherency levels for a given cache_level
95  * @obj: #drm_i915_gem_object
96  * @cache_level: cache level
97  */
98 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
99                                          unsigned int cache_level)
100 {
101         obj->cache_level = cache_level;
102
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;
108         else
109                 obj->cache_coherent = 0;
110
111         obj->cache_dirty =
112                 !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE);
113 }
114
115 static void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
116 {
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;
122         LIST_HEAD(close);
123
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;
127
128                 if (ctx && ctx->file_priv == fpriv) {
129                         i915_gem_context_get(ctx);
130                         list_move(&lut->obj_link, &close);
131                 }
132
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);
139                 }
140         }
141         spin_unlock(&obj->lut_lock);
142
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);
147
148         list_for_each_entry_safe(lut, ln, &close, obj_link) {
149                 struct i915_gem_context *ctx = lut->ctx;
150                 struct i915_vma *vma;
151
152                 /*
153                  * We allow the process to have multiple handles to the same
154                  * vma, in the same fd namespace, by virtue of flink/open.
155                  */
156
157                 mutex_lock(&ctx->lut_mutex);
158                 vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
159                 if (vma) {
160                         GEM_BUG_ON(vma->obj != obj);
161                         GEM_BUG_ON(!atomic_read(&vma->open_count));
162                         i915_vma_close(vma);
163                 }
164                 mutex_unlock(&ctx->lut_mutex);
165
166                 i915_gem_context_put(lut->ctx);
167                 i915_lut_handle_free(lut);
168                 i915_gem_object_put(obj);
169         }
170 }
171
172 static void __i915_gem_free_object_rcu(struct rcu_head *head)
173 {
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);
177
178         dma_resv_fini(&obj->base._resv);
179         i915_gem_object_free(obj);
180
181         GEM_BUG_ON(!atomic_read(&i915->mm.free_count));
182         atomic_dec(&i915->mm.free_count);
183 }
184
185 static void __i915_gem_object_free_mmaps(struct drm_i915_gem_object *obj)
186 {
187         /* Skip serialisation and waking the device if known to be not used. */
188
189         if (obj->userfault_count)
190                 i915_gem_object_release_mmap_gtt(obj);
191
192         if (!RB_EMPTY_ROOT(&obj->mmo.offsets)) {
193                 struct i915_mmap_offset *mmo, *mn;
194
195                 i915_gem_object_release_mmap_offset(obj);
196
197                 rbtree_postorder_for_each_entry_safe(mmo, mn,
198                                                      &obj->mmo.offsets,
199                                                      offset) {
200                         drm_vma_offset_remove(obj->base.dev->vma_offset_manager,
201                                               &mmo->vma_node);
202                         kfree(mmo);
203                 }
204                 obj->mmo.offsets = RB_ROOT;
205         }
206 }
207
208 static void __i915_gem_free_objects(struct drm_i915_private *i915,
209                                     struct llist_node *freed)
210 {
211         struct drm_i915_gem_object *obj, *on;
212
213         llist_for_each_entry_safe(obj, on, freed, freed) {
214                 trace_i915_gem_object_destroy(obj);
215
216                 if (!list_empty(&obj->vma.list)) {
217                         struct i915_vma *vma;
218
219                         /*
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.
224                          */
225                         spin_lock(&obj->vma.lock);
226                         while ((vma = list_first_entry_or_null(&obj->vma.list,
227                                                                struct i915_vma,
228                                                                obj_link))) {
229                                 GEM_BUG_ON(vma->obj != obj);
230                                 spin_unlock(&obj->vma.lock);
231
232                                 __i915_vma_put(vma);
233
234                                 spin_lock(&obj->vma.lock);
235                         }
236                         spin_unlock(&obj->vma.lock);
237                 }
238
239                 __i915_gem_object_free_mmaps(obj);
240
241                 GEM_BUG_ON(!list_empty(&obj->lut_list));
242
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);
247
248                 if (obj->base.import_attach)
249                         drm_prime_gem_destroy(&obj->base, NULL);
250
251                 drm_gem_free_mmap_offset(&obj->base);
252
253                 if (obj->ops->release)
254                         obj->ops->release(obj);
255
256                 /* But keep the pointer alive for RCU-protected lookups */
257                 call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
258                 cond_resched();
259         }
260 }
261
262 void i915_gem_flush_free_objects(struct drm_i915_private *i915)
263 {
264         struct llist_node *freed = llist_del_all(&i915->mm.free_list);
265
266         if (unlikely(freed))
267                 __i915_gem_free_objects(i915, freed);
268 }
269
270 static void __i915_gem_free_work(struct work_struct *work)
271 {
272         struct drm_i915_private *i915 =
273                 container_of(work, struct drm_i915_private, mm.free_work);
274
275         i915_gem_flush_free_objects(i915);
276 }
277
278 static void i915_gem_free_object(struct drm_gem_object *gem_obj)
279 {
280         struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
281         struct drm_i915_private *i915 = to_i915(obj->base.dev);
282
283         GEM_BUG_ON(i915_gem_object_is_framebuffer(obj));
284
285         /*
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().
290          */
291         atomic_inc(&i915->mm.free_count);
292
293         /*
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
298          * freed objects.
299          */
300         i915_gem_object_make_unshrinkable(obj);
301
302         /*
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.
311          */
312         if (llist_add(&obj->freed, &i915->mm.free_list))
313                 queue_work(i915->wq, &i915->mm.free_work);
314 }
315
316 static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
317 {
318         return !(obj->cache_level == I915_CACHE_NONE ||
319                  obj->cache_level == I915_CACHE_WT);
320 }
321
322 void
323 i915_gem_object_flush_write_domain(struct drm_i915_gem_object *obj,
324                                    unsigned int flush_domains)
325 {
326         struct i915_vma *vma;
327
328         assert_object_held(obj);
329
330         if (!(obj->write_domain & flush_domains))
331                 return;
332
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);
339                 }
340                 spin_unlock(&obj->vma.lock);
341
342                 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
343                 break;
344
345         case I915_GEM_DOMAIN_WC:
346                 wmb();
347                 break;
348
349         case I915_GEM_DOMAIN_CPU:
350                 i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
351                 break;
352
353         case I915_GEM_DOMAIN_RENDER:
354                 if (gpu_write_needs_clflush(obj))
355                         obj->cache_dirty = true;
356                 break;
357         }
358
359         obj->write_domain = 0;
360 }
361
362 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
363                                          enum fb_op_origin origin)
364 {
365         struct intel_frontbuffer *front;
366
367         front = __intel_frontbuffer_get(obj);
368         if (front) {
369                 intel_frontbuffer_flush(front, origin);
370                 intel_frontbuffer_put(front);
371         }
372 }
373
374 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
375                                               enum fb_op_origin origin)
376 {
377         struct intel_frontbuffer *front;
378
379         front = __intel_frontbuffer_get(obj);
380         if (front) {
381                 intel_frontbuffer_invalidate(front, origin);
382                 intel_frontbuffer_put(front);
383         }
384 }
385
386 void i915_gem_init__objects(struct drm_i915_private *i915)
387 {
388         INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
389 }
390
391 static void i915_global_objects_shrink(void)
392 {
393         kmem_cache_shrink(global.slab_objects);
394 }
395
396 static void i915_global_objects_exit(void)
397 {
398         kmem_cache_destroy(global.slab_objects);
399 }
400
401 static struct i915_global_object global = { {
402         .shrink = i915_global_objects_shrink,
403         .exit = i915_global_objects_exit,
404 } };
405
406 int __init i915_global_objects_init(void)
407 {
408         global.slab_objects =
409                 KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
410         if (!global.slab_objects)
411                 return -ENOMEM;
412
413         i915_global_register(&global.base);
414         return 0;
415 }
416
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,
421 };
422
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"
428 #endif