Merge tag 'batadv-next-pullrequest-20210408' of git://git.open-mesh.org/linux-merge
[linux-2.6-block.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 "i915_drv.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"
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 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
317                                          enum fb_op_origin origin)
318 {
319         struct intel_frontbuffer *front;
320
321         front = __intel_frontbuffer_get(obj);
322         if (front) {
323                 intel_frontbuffer_flush(front, origin);
324                 intel_frontbuffer_put(front);
325         }
326 }
327
328 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
329                                               enum fb_op_origin origin)
330 {
331         struct intel_frontbuffer *front;
332
333         front = __intel_frontbuffer_get(obj);
334         if (front) {
335                 intel_frontbuffer_invalidate(front, origin);
336                 intel_frontbuffer_put(front);
337         }
338 }
339
340 static void
341 i915_gem_object_read_from_page_kmap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
342 {
343         void *src_map;
344         void *src_ptr;
345
346         src_map = kmap_atomic(i915_gem_object_get_page(obj, offset >> PAGE_SHIFT));
347
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);
352
353         kunmap_atomic(src_map);
354 }
355
356 static void
357 i915_gem_object_read_from_page_iomap(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
358 {
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);
362
363         src_map = io_mapping_map_wc(&obj->mm.region->iomap,
364                                     dma - obj->mm.region->region.start,
365                                     PAGE_SIZE);
366
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);
370
371         io_mapping_unmap(src_map);
372 }
373
374 /**
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
380  *
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.
384  *
385  * Returns 0 on success or -ENODEV if the type of @obj's backing store is
386  * unsupported.
387  */
388 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size)
389 {
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));
393
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);
398         else
399                 return -ENODEV;
400
401         return 0;
402 }
403
404 void i915_gem_init__objects(struct drm_i915_private *i915)
405 {
406         INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
407 }
408
409 static void i915_global_objects_shrink(void)
410 {
411         kmem_cache_shrink(global.slab_objects);
412 }
413
414 static void i915_global_objects_exit(void)
415 {
416         kmem_cache_destroy(global.slab_objects);
417 }
418
419 static struct i915_global_object global = { {
420         .shrink = i915_global_objects_shrink,
421         .exit = i915_global_objects_exit,
422 } };
423
424 int __init i915_global_objects_init(void)
425 {
426         global.slab_objects =
427                 KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
428         if (!global.slab_objects)
429                 return -ENOMEM;
430
431         i915_global_register(&global.base);
432         return 0;
433 }
434
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,
439 };
440
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"
446 #endif