drm/vc4: fix wrong filp usage in gem_info debugfs node
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / vc4 / vc4_bo.c
1 /*
2  *  Copyright © 2015 Broadcom
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 /**
10  * DOC: VC4 GEM BO management support
11  *
12  * The VC4 GPU architecture (both scanout and rendering) has direct
13  * access to system memory with no MMU in between.  To support it, we
14  * use the GEM CMA helper functions to allocate contiguous ranges of
15  * physical memory for our BOs.
16  *
17  * Since the CMA allocator is very slow, we keep a cache of recently
18  * freed BOs around so that the kernel's allocation of objects for 3D
19  * rendering can return quickly.
20  */
21
22 #include <linux/dma-buf.h>
23
24 #include "vc4_drv.h"
25 #include "uapi/drm/vc4_drm.h"
26
27 static const char * const bo_type_names[] = {
28         "kernel",
29         "V3D",
30         "V3D shader",
31         "dumb",
32         "binner",
33         "RCL",
34         "BCL",
35         "kernel BO cache",
36 };
37
38 static bool is_user_label(int label)
39 {
40         return label >= VC4_BO_TYPE_COUNT;
41 }
42
43 static void vc4_bo_stats_dump(struct vc4_dev *vc4)
44 {
45         int i;
46
47         for (i = 0; i < vc4->num_labels; i++) {
48                 if (!vc4->bo_labels[i].num_allocated)
49                         continue;
50
51                 DRM_INFO("%30s: %6dkb BOs (%d)\n",
52                          vc4->bo_labels[i].name,
53                          vc4->bo_labels[i].size_allocated / 1024,
54                          vc4->bo_labels[i].num_allocated);
55         }
56
57         mutex_lock(&vc4->purgeable.lock);
58         if (vc4->purgeable.num)
59                 DRM_INFO("%30s: %6zdkb BOs (%d)\n", "userspace BO cache",
60                          vc4->purgeable.size / 1024, vc4->purgeable.num);
61
62         if (vc4->purgeable.purged_num)
63                 DRM_INFO("%30s: %6zdkb BOs (%d)\n", "total purged BO",
64                          vc4->purgeable.purged_size / 1024,
65                          vc4->purgeable.purged_num);
66         mutex_unlock(&vc4->purgeable.lock);
67 }
68
69 #ifdef CONFIG_DEBUG_FS
70 int vc4_bo_stats_debugfs(struct seq_file *m, void *unused)
71 {
72         struct drm_info_node *node = (struct drm_info_node *)m->private;
73         struct drm_device *dev = node->minor->dev;
74         struct vc4_dev *vc4 = to_vc4_dev(dev);
75         int i;
76
77         mutex_lock(&vc4->bo_lock);
78         for (i = 0; i < vc4->num_labels; i++) {
79                 if (!vc4->bo_labels[i].num_allocated)
80                         continue;
81
82                 seq_printf(m, "%30s: %6dkb BOs (%d)\n",
83                            vc4->bo_labels[i].name,
84                            vc4->bo_labels[i].size_allocated / 1024,
85                            vc4->bo_labels[i].num_allocated);
86         }
87         mutex_unlock(&vc4->bo_lock);
88
89         mutex_lock(&vc4->purgeable.lock);
90         if (vc4->purgeable.num)
91                 seq_printf(m, "%30s: %6zdkb BOs (%d)\n", "userspace BO cache",
92                            vc4->purgeable.size / 1024, vc4->purgeable.num);
93
94         if (vc4->purgeable.purged_num)
95                 seq_printf(m, "%30s: %6zdkb BOs (%d)\n", "total purged BO",
96                            vc4->purgeable.purged_size / 1024,
97                            vc4->purgeable.purged_num);
98         mutex_unlock(&vc4->purgeable.lock);
99
100         return 0;
101 }
102
103 struct vc4_gem_info_data {
104         struct drm_file *filp;
105         struct seq_file *m;
106 };
107
108 static int vc4_gem_one_info(int id, void *ptr, void *data)
109 {
110         struct drm_gem_object *obj = (struct drm_gem_object *)ptr;
111         struct vc4_gem_info_data *gem_info_data = data;
112         struct vc4_file *vc4file = gem_info_data->filp->driver_priv;
113         struct drm_vc4_file_private *file_priv = &vc4file->priv;
114
115         if (!obj) {
116                 DRM_ERROR("failed to get drm_gem_object\n");
117                 return -EFAULT;
118         }
119
120         drm_gem_object_reference(obj);
121
122         seq_printf(gem_info_data->m,
123                         "%5d\t%5d\t%4d\t%4d\t\t%4d\t0x%08zx\t0x%x\t%4d\t%4d\t\t"
124                         "%4d\t\t0x%p\t%6d\n",
125                         file_priv->pid,
126                         file_priv->tgid,
127                         id,
128                         kref_read(&obj->refcount) - 1,
129                         obj->handle_count,
130                         obj->size,
131                         0,
132                         0,
133                         obj->dma_buf ? 1 : 0,
134                         obj->import_attach ? 1 : 0,
135                         obj,
136                         obj->name);
137
138         drm_gem_object_unreference(obj);
139
140         return 0;
141 }
142
143 int vc4_debugfs_gem_info(struct seq_file *m, void *data)
144 {
145         struct drm_info_node *node = (struct drm_info_node *)m->private;
146         struct drm_minor *minor = node->minor;
147         struct drm_device *drm_dev = minor->dev;
148         struct vc4_gem_info_data gem_info_data;
149         struct drm_file *filp;
150
151         gem_info_data.m = m;
152
153         seq_puts(gem_info_data.m,
154                         "pid\ttgid\thandle\trefcount\thcount\tsize\t\tflags\t"
155                         "pfnmap\texport_to_fd\timport_from_fd\tobj_addr\t\t"
156                         "name\n");
157
158         mutex_lock(&drm_dev->struct_mutex);
159         list_for_each_entry(filp, &drm_dev->filelist, lhead) {
160                 gem_info_data.filp = filp;
161
162                 spin_lock(&filp->table_lock);
163                 idr_for_each(&filp->object_idr, vc4_gem_one_info,
164                                 &gem_info_data);
165                 spin_unlock(&filp->table_lock);
166         }
167         mutex_unlock(&drm_dev->struct_mutex);
168
169         return 0;
170 }
171
172 #endif
173
174 /* Takes ownership of *name and returns the appropriate slot for it in
175  * the bo_labels[] array, extending it as necessary.
176  *
177  * This is inefficient and could use a hash table instead of walking
178  * an array and strcmp()ing.  However, the assumption is that user
179  * labeling will be infrequent (scanout buffers and other long-lived
180  * objects, or debug driver builds), so we can live with it for now.
181  */
182 static int vc4_get_user_label(struct vc4_dev *vc4, const char *name)
183 {
184         int i;
185         int free_slot = -1;
186
187         for (i = 0; i < vc4->num_labels; i++) {
188                 if (!vc4->bo_labels[i].name) {
189                         free_slot = i;
190                 } else if (strcmp(vc4->bo_labels[i].name, name) == 0) {
191                         kfree(name);
192                         return i;
193                 }
194         }
195
196         if (free_slot != -1) {
197                 WARN_ON(vc4->bo_labels[free_slot].num_allocated != 0);
198                 vc4->bo_labels[free_slot].name = name;
199                 return free_slot;
200         } else {
201                 u32 new_label_count = vc4->num_labels + 1;
202                 struct vc4_label *new_labels =
203                         krealloc(vc4->bo_labels,
204                                  new_label_count * sizeof(*new_labels),
205                                  GFP_KERNEL);
206
207                 if (!new_labels) {
208                         kfree(name);
209                         return -1;
210                 }
211
212                 free_slot = vc4->num_labels;
213                 vc4->bo_labels = new_labels;
214                 vc4->num_labels = new_label_count;
215
216                 vc4->bo_labels[free_slot].name = name;
217                 vc4->bo_labels[free_slot].num_allocated = 0;
218                 vc4->bo_labels[free_slot].size_allocated = 0;
219
220                 return free_slot;
221         }
222 }
223
224 static void vc4_bo_set_label(struct drm_gem_object *gem_obj, int label)
225 {
226         struct vc4_bo *bo = to_vc4_bo(gem_obj);
227         struct vc4_dev *vc4 = to_vc4_dev(gem_obj->dev);
228
229         lockdep_assert_held(&vc4->bo_lock);
230
231         if (label != -1) {
232                 vc4->bo_labels[label].num_allocated++;
233                 vc4->bo_labels[label].size_allocated += gem_obj->size;
234         }
235
236         vc4->bo_labels[bo->label].num_allocated--;
237         vc4->bo_labels[bo->label].size_allocated -= gem_obj->size;
238
239         if (vc4->bo_labels[bo->label].num_allocated == 0 &&
240             is_user_label(bo->label)) {
241                 /* Free user BO label slots on last unreference.
242                  * Slots are just where we track the stats for a given
243                  * name, and once a name is unused we can reuse that
244                  * slot.
245                  */
246                 kfree(vc4->bo_labels[bo->label].name);
247                 vc4->bo_labels[bo->label].name = NULL;
248         }
249
250         bo->label = label;
251 }
252
253 static uint32_t bo_page_index(size_t size)
254 {
255         return (size / PAGE_SIZE) - 1;
256 }
257
258 static void vc4_bo_destroy(struct vc4_bo *bo)
259 {
260         struct drm_gem_object *obj = &bo->base.base;
261         struct vc4_dev *vc4 = to_vc4_dev(obj->dev);
262
263         lockdep_assert_held(&vc4->bo_lock);
264
265         vc4_bo_set_label(obj, -1);
266
267         if (bo->validated_shader) {
268                 kfree(bo->validated_shader->uniform_addr_offsets);
269                 kfree(bo->validated_shader->texture_samples);
270                 kfree(bo->validated_shader);
271                 bo->validated_shader = NULL;
272         }
273
274         reservation_object_fini(&bo->_resv);
275
276         drm_gem_cma_free_object(obj);
277 }
278
279 static void vc4_bo_remove_from_cache(struct vc4_bo *bo)
280 {
281         struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
282
283         lockdep_assert_held(&vc4->bo_lock);
284         list_del(&bo->unref_head);
285         list_del(&bo->size_head);
286 }
287
288 static struct list_head *vc4_get_cache_list_for_size(struct drm_device *dev,
289                                                      size_t size)
290 {
291         struct vc4_dev *vc4 = to_vc4_dev(dev);
292         uint32_t page_index = bo_page_index(size);
293
294         if (vc4->bo_cache.size_list_size <= page_index) {
295                 uint32_t new_size = max(vc4->bo_cache.size_list_size * 2,
296                                         page_index + 1);
297                 struct list_head *new_list;
298                 uint32_t i;
299
300                 new_list = kmalloc_array(new_size, sizeof(struct list_head),
301                                          GFP_KERNEL);
302                 if (!new_list)
303                         return NULL;
304
305                 /* Rebase the old cached BO lists to their new list
306                  * head locations.
307                  */
308                 for (i = 0; i < vc4->bo_cache.size_list_size; i++) {
309                         struct list_head *old_list =
310                                 &vc4->bo_cache.size_list[i];
311
312                         if (list_empty(old_list))
313                                 INIT_LIST_HEAD(&new_list[i]);
314                         else
315                                 list_replace(old_list, &new_list[i]);
316                 }
317                 /* And initialize the brand new BO list heads. */
318                 for (i = vc4->bo_cache.size_list_size; i < new_size; i++)
319                         INIT_LIST_HEAD(&new_list[i]);
320
321                 kfree(vc4->bo_cache.size_list);
322                 vc4->bo_cache.size_list = new_list;
323                 vc4->bo_cache.size_list_size = new_size;
324         }
325
326         return &vc4->bo_cache.size_list[page_index];
327 }
328
329 static void vc4_bo_cache_purge(struct drm_device *dev)
330 {
331         struct vc4_dev *vc4 = to_vc4_dev(dev);
332
333         mutex_lock(&vc4->bo_lock);
334         while (!list_empty(&vc4->bo_cache.time_list)) {
335                 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
336                                                     struct vc4_bo, unref_head);
337                 vc4_bo_remove_from_cache(bo);
338                 vc4_bo_destroy(bo);
339         }
340         mutex_unlock(&vc4->bo_lock);
341 }
342
343 void vc4_bo_add_to_purgeable_pool(struct vc4_bo *bo)
344 {
345         struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
346
347         mutex_lock(&vc4->purgeable.lock);
348         list_add_tail(&bo->size_head, &vc4->purgeable.list);
349         vc4->purgeable.num++;
350         vc4->purgeable.size += bo->base.base.size;
351         mutex_unlock(&vc4->purgeable.lock);
352 }
353
354 static void vc4_bo_remove_from_purgeable_pool_locked(struct vc4_bo *bo)
355 {
356         struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
357
358         /* list_del_init() is used here because the caller might release
359          * the purgeable lock in order to acquire the madv one and update the
360          * madv status.
361          * During this short period of time a user might decide to mark
362          * the BO as unpurgeable, and if bo->madv is set to
363          * VC4_MADV_DONTNEED it will try to remove the BO from the
364          * purgeable list which will fail if the ->next/prev fields
365          * are set to LIST_POISON1/LIST_POISON2 (which is what
366          * list_del() does).
367          * Re-initializing the list element guarantees that list_del()
368          * will work correctly even if it's a NOP.
369          */
370         list_del_init(&bo->size_head);
371         vc4->purgeable.num--;
372         vc4->purgeable.size -= bo->base.base.size;
373 }
374
375 void vc4_bo_remove_from_purgeable_pool(struct vc4_bo *bo)
376 {
377         struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
378
379         mutex_lock(&vc4->purgeable.lock);
380         vc4_bo_remove_from_purgeable_pool_locked(bo);
381         mutex_unlock(&vc4->purgeable.lock);
382 }
383
384 static void vc4_bo_purge(struct drm_gem_object *obj)
385 {
386         struct vc4_bo *bo = to_vc4_bo(obj);
387         struct drm_device *dev = obj->dev;
388
389         WARN_ON(!mutex_is_locked(&bo->madv_lock));
390         WARN_ON(bo->madv != VC4_MADV_DONTNEED);
391
392         drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
393
394         dma_free_wc(dev->dev, obj->size, bo->base.vaddr, bo->base.paddr);
395         bo->base.vaddr = NULL;
396         bo->madv = __VC4_MADV_PURGED;
397 }
398
399 static void vc4_bo_userspace_cache_purge(struct drm_device *dev)
400 {
401         struct vc4_dev *vc4 = to_vc4_dev(dev);
402
403         mutex_lock(&vc4->purgeable.lock);
404         while (!list_empty(&vc4->purgeable.list)) {
405                 struct vc4_bo *bo = list_first_entry(&vc4->purgeable.list,
406                                                      struct vc4_bo, size_head);
407                 struct drm_gem_object *obj = &bo->base.base;
408                 size_t purged_size = 0;
409
410                 vc4_bo_remove_from_purgeable_pool_locked(bo);
411
412                 /* Release the purgeable lock while we're purging the BO so
413                  * that other people can continue inserting things in the
414                  * purgeable pool without having to wait for all BOs to be
415                  * purged.
416                  */
417                 mutex_unlock(&vc4->purgeable.lock);
418                 mutex_lock(&bo->madv_lock);
419
420                 /* Since we released the purgeable pool lock before acquiring
421                  * the BO madv one, the user may have marked the BO as WILLNEED
422                  * and re-used it in the meantime.
423                  * Before purging the BO we need to make sure
424                  * - it is still marked as DONTNEED
425                  * - it has not been re-inserted in the purgeable list
426                  * - it is not used by HW blocks
427                  * If one of these conditions is not met, just skip the entry.
428                  */
429                 if (bo->madv == VC4_MADV_DONTNEED &&
430                     list_empty(&bo->size_head) &&
431                     !refcount_read(&bo->usecnt)) {
432                         purged_size = bo->base.base.size;
433                         vc4_bo_purge(obj);
434                 }
435                 mutex_unlock(&bo->madv_lock);
436                 mutex_lock(&vc4->purgeable.lock);
437
438                 if (purged_size) {
439                         vc4->purgeable.purged_size += purged_size;
440                         vc4->purgeable.purged_num++;
441                 }
442         }
443         mutex_unlock(&vc4->purgeable.lock);
444 }
445
446 static struct vc4_bo *vc4_bo_get_from_cache(struct drm_device *dev,
447                                             uint32_t size,
448                                             enum vc4_kernel_bo_type type)
449 {
450         struct vc4_dev *vc4 = to_vc4_dev(dev);
451         uint32_t page_index = bo_page_index(size);
452         struct vc4_bo *bo = NULL;
453
454         size = roundup(size, PAGE_SIZE);
455
456         mutex_lock(&vc4->bo_lock);
457         if (page_index >= vc4->bo_cache.size_list_size)
458                 goto out;
459
460         if (list_empty(&vc4->bo_cache.size_list[page_index]))
461                 goto out;
462
463         bo = list_first_entry(&vc4->bo_cache.size_list[page_index],
464                               struct vc4_bo, size_head);
465         vc4_bo_remove_from_cache(bo);
466         kref_init(&bo->base.base.refcount);
467
468 out:
469         if (bo)
470                 vc4_bo_set_label(&bo->base.base, type);
471         mutex_unlock(&vc4->bo_lock);
472         return bo;
473 }
474
475 /**
476  * vc4_gem_create_object - Implementation of driver->gem_create_object.
477  * @dev: DRM device
478  * @size: Size in bytes of the memory the object will reference
479  *
480  * This lets the CMA helpers allocate object structs for us, and keep
481  * our BO stats correct.
482  */
483 struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size)
484 {
485         struct vc4_dev *vc4 = to_vc4_dev(dev);
486         struct vc4_bo *bo;
487
488         bo = kzalloc(sizeof(*bo), GFP_KERNEL);
489         if (!bo)
490                 return ERR_PTR(-ENOMEM);
491
492         bo->madv = VC4_MADV_WILLNEED;
493         refcount_set(&bo->usecnt, 0);
494         mutex_init(&bo->madv_lock);
495         mutex_lock(&vc4->bo_lock);
496         bo->label = VC4_BO_TYPE_KERNEL;
497         vc4->bo_labels[VC4_BO_TYPE_KERNEL].num_allocated++;
498         vc4->bo_labels[VC4_BO_TYPE_KERNEL].size_allocated += size;
499         mutex_unlock(&vc4->bo_lock);
500         bo->resv = &bo->_resv;
501         reservation_object_init(bo->resv);
502
503         return &bo->base.base;
504 }
505
506 struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t unaligned_size,
507                              bool allow_unzeroed, enum vc4_kernel_bo_type type)
508 {
509         size_t size = roundup(unaligned_size, PAGE_SIZE);
510         struct vc4_dev *vc4 = to_vc4_dev(dev);
511         struct drm_gem_cma_object *cma_obj;
512         struct vc4_bo *bo;
513
514         if (size == 0)
515                 return ERR_PTR(-EINVAL);
516
517         /* First, try to get a vc4_bo from the kernel BO cache. */
518         bo = vc4_bo_get_from_cache(dev, size, type);
519         if (bo) {
520                 if (!allow_unzeroed)
521                         memset(bo->base.vaddr, 0, bo->base.base.size);
522                 return bo;
523         }
524
525         cma_obj = drm_gem_cma_create(dev, size);
526         if (IS_ERR(cma_obj)) {
527                 /*
528                  * If we've run out of CMA memory, kill the cache of
529                  * CMA allocations we've got laying around and try again.
530                  */
531                 vc4_bo_cache_purge(dev);
532                 cma_obj = drm_gem_cma_create(dev, size);
533         }
534
535         if (IS_ERR(cma_obj)) {
536                 /*
537                  * Still not enough CMA memory, purge the userspace BO
538                  * cache and retry.
539                  * This is sub-optimal since we purge the whole userspace
540                  * BO cache which forces user that want to re-use the BO to
541                  * restore its initial content.
542                  * Ideally, we should purge entries one by one and retry
543                  * after each to see if CMA allocation succeeds. Or even
544                  * better, try to find an entry with at least the same
545                  * size.
546                  */
547                 vc4_bo_userspace_cache_purge(dev);
548                 cma_obj = drm_gem_cma_create(dev, size);
549         }
550
551         if (IS_ERR(cma_obj)) {
552                 DRM_ERROR("Failed to allocate from CMA:\n");
553                 vc4_bo_stats_dump(vc4);
554                 return ERR_PTR(-ENOMEM);
555         }
556         bo = to_vc4_bo(&cma_obj->base);
557
558         /* By default, BOs do not support the MADV ioctl. This will be enabled
559          * only on BOs that are exposed to userspace (V3D, V3D_SHADER and DUMB
560          * BOs).
561          */
562         bo->madv = __VC4_MADV_NOTSUPP;
563
564         mutex_lock(&vc4->bo_lock);
565         vc4_bo_set_label(&cma_obj->base, type);
566         mutex_unlock(&vc4->bo_lock);
567
568         return bo;
569 }
570
571 static void vc4_gem_register_pid(struct drm_file *file_priv)
572 {
573         struct vc4_file *vc4file = file_priv->driver_priv;
574         struct drm_vc4_file_private *driver_priv = &vc4file->priv;
575
576         if (!driver_priv->pid && !driver_priv->tgid) {
577                 driver_priv->pid = task_pid_nr(current);
578                 driver_priv->tgid = task_tgid_nr(current);
579         } else {
580                 if (driver_priv->pid != task_pid_nr(current))
581                         DRM_DEBUG_KMS("wrong pid: %ld, %ld\n",
582                                         (unsigned long)driver_priv->pid,
583                                         (unsigned long)task_pid_nr(current));
584                 if (driver_priv->tgid != task_tgid_nr(current))
585                         DRM_DEBUG_KMS("wrong tgid: %ld, %ld\n",
586                                         (unsigned long)driver_priv->tgid,
587                                         (unsigned long)task_tgid_nr(current));
588         }
589 }
590
591 int vc4_dumb_create(struct drm_file *file_priv,
592                     struct drm_device *dev,
593                     struct drm_mode_create_dumb *args)
594 {
595         int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
596         struct vc4_bo *bo = NULL;
597         int ret;
598
599         if (args->pitch < min_pitch)
600                 args->pitch = min_pitch;
601
602         if (args->size < args->pitch * args->height)
603                 args->size = args->pitch * args->height;
604
605         bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_DUMB);
606         if (IS_ERR(bo))
607                 return PTR_ERR(bo);
608
609         bo->madv = VC4_MADV_WILLNEED;
610
611         ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
612         drm_gem_object_put_unlocked(&bo->base.base);
613
614         if (!ret)
615                 vc4_gem_register_pid(file_priv);
616
617         return ret;
618 }
619
620 static void vc4_bo_cache_free_old(struct drm_device *dev)
621 {
622         struct vc4_dev *vc4 = to_vc4_dev(dev);
623         unsigned long expire_time = jiffies - msecs_to_jiffies(1000);
624
625         lockdep_assert_held(&vc4->bo_lock);
626
627         while (!list_empty(&vc4->bo_cache.time_list)) {
628                 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
629                                                     struct vc4_bo, unref_head);
630                 if (time_before(expire_time, bo->free_time)) {
631                         mod_timer(&vc4->bo_cache.time_timer,
632                                   round_jiffies_up(jiffies +
633                                                    msecs_to_jiffies(1000)));
634                         return;
635                 }
636
637                 vc4_bo_remove_from_cache(bo);
638                 vc4_bo_destroy(bo);
639         }
640 }
641
642 /* Called on the last userspace/kernel unreference of the BO.  Returns
643  * it to the BO cache if possible, otherwise frees it.
644  */
645 void vc4_free_object(struct drm_gem_object *gem_bo)
646 {
647         struct drm_device *dev = gem_bo->dev;
648         struct vc4_dev *vc4 = to_vc4_dev(dev);
649         struct vc4_bo *bo = to_vc4_bo(gem_bo);
650         struct list_head *cache_list;
651
652         /* Remove the BO from the purgeable list. */
653         mutex_lock(&bo->madv_lock);
654         if (bo->madv == VC4_MADV_DONTNEED && !refcount_read(&bo->usecnt))
655                 vc4_bo_remove_from_purgeable_pool(bo);
656         mutex_unlock(&bo->madv_lock);
657
658         mutex_lock(&vc4->bo_lock);
659         /* If the object references someone else's memory, we can't cache it.
660          */
661         if (gem_bo->import_attach) {
662                 vc4_bo_destroy(bo);
663                 goto out;
664         }
665
666         /* Don't cache if it was publicly named. */
667         if (gem_bo->name) {
668                 vc4_bo_destroy(bo);
669                 goto out;
670         }
671
672         /* If this object was partially constructed but CMA allocation
673          * had failed, just free it. Can also happen when the BO has been
674          * purged.
675          */
676         if (!bo->base.vaddr) {
677                 vc4_bo_destroy(bo);
678                 goto out;
679         }
680
681         cache_list = vc4_get_cache_list_for_size(dev, gem_bo->size);
682         if (!cache_list) {
683                 vc4_bo_destroy(bo);
684                 goto out;
685         }
686
687         if (bo->validated_shader) {
688                 kfree(bo->validated_shader->uniform_addr_offsets);
689                 kfree(bo->validated_shader->texture_samples);
690                 kfree(bo->validated_shader);
691                 bo->validated_shader = NULL;
692         }
693
694         /* Reset madv and usecnt before adding the BO to the cache. */
695         bo->madv = __VC4_MADV_NOTSUPP;
696         refcount_set(&bo->usecnt, 0);
697
698         bo->t_format = false;
699         bo->free_time = jiffies;
700         list_add(&bo->size_head, cache_list);
701         list_add(&bo->unref_head, &vc4->bo_cache.time_list);
702
703         vc4_bo_set_label(&bo->base.base, VC4_BO_TYPE_KERNEL_CACHE);
704
705         vc4_bo_cache_free_old(dev);
706
707 out:
708         mutex_unlock(&vc4->bo_lock);
709 }
710
711 static void vc4_bo_cache_time_work(struct work_struct *work)
712 {
713         struct vc4_dev *vc4 =
714                 container_of(work, struct vc4_dev, bo_cache.time_work);
715         struct drm_device *dev = vc4->dev;
716
717         mutex_lock(&vc4->bo_lock);
718         vc4_bo_cache_free_old(dev);
719         mutex_unlock(&vc4->bo_lock);
720 }
721
722 int vc4_bo_inc_usecnt(struct vc4_bo *bo)
723 {
724         int ret;
725
726         /* Fast path: if the BO is already retained by someone, no need to
727          * check the madv status.
728          */
729         if (refcount_inc_not_zero(&bo->usecnt))
730                 return 0;
731
732         mutex_lock(&bo->madv_lock);
733         switch (bo->madv) {
734         case VC4_MADV_WILLNEED:
735                 if (!refcount_inc_not_zero(&bo->usecnt))
736                         refcount_set(&bo->usecnt, 1);
737                 ret = 0;
738                 break;
739         case VC4_MADV_DONTNEED:
740                 /* We shouldn't use a BO marked as purgeable if at least
741                  * someone else retained its content by incrementing usecnt.
742                  * Luckily the BO hasn't been purged yet, but something wrong
743                  * is happening here. Just throw an error instead of
744                  * authorizing this use case.
745                  */
746         case __VC4_MADV_PURGED:
747                 /* We can't use a purged BO. */
748         default:
749                 /* Invalid madv value. */
750                 ret = -EINVAL;
751                 break;
752         }
753         mutex_unlock(&bo->madv_lock);
754
755         return ret;
756 }
757
758 void vc4_bo_dec_usecnt(struct vc4_bo *bo)
759 {
760         /* Fast path: if the BO is still retained by someone, no need to test
761          * the madv value.
762          */
763         if (refcount_dec_not_one(&bo->usecnt))
764                 return;
765
766         mutex_lock(&bo->madv_lock);
767         if (refcount_dec_and_test(&bo->usecnt) &&
768             bo->madv == VC4_MADV_DONTNEED)
769                 vc4_bo_add_to_purgeable_pool(bo);
770         mutex_unlock(&bo->madv_lock);
771 }
772
773 static void vc4_bo_cache_time_timer(struct timer_list *t)
774 {
775         struct vc4_dev *vc4 = from_timer(vc4, t, bo_cache.time_timer);
776
777         schedule_work(&vc4->bo_cache.time_work);
778 }
779
780 struct reservation_object *vc4_prime_res_obj(struct drm_gem_object *obj)
781 {
782         struct vc4_bo *bo = to_vc4_bo(obj);
783
784         return bo->resv;
785 }
786
787 struct dma_buf *
788 vc4_prime_export(struct drm_device *dev, struct drm_gem_object *obj, int flags)
789 {
790         struct vc4_bo *bo = to_vc4_bo(obj);
791         struct dma_buf *dmabuf;
792         int ret;
793
794         if (bo->validated_shader) {
795                 DRM_DEBUG("Attempting to export shader BO\n");
796                 return ERR_PTR(-EINVAL);
797         }
798
799         /* Note: as soon as the BO is exported it becomes unpurgeable, because
800          * noone ever decrements the usecnt even if the reference held by the
801          * exported BO is released. This shouldn't be a problem since we don't
802          * expect exported BOs to be marked as purgeable.
803          */
804         ret = vc4_bo_inc_usecnt(bo);
805         if (ret) {
806                 DRM_ERROR("Failed to increment BO usecnt\n");
807                 return ERR_PTR(ret);
808         }
809
810         dmabuf = drm_gem_prime_export(dev, obj, flags);
811         if (IS_ERR(dmabuf))
812                 vc4_bo_dec_usecnt(bo);
813
814         return dmabuf;
815 }
816
817 vm_fault_t vc4_fault(struct vm_fault *vmf)
818 {
819         struct vm_area_struct *vma = vmf->vma;
820         struct drm_gem_object *obj = vma->vm_private_data;
821         struct vc4_bo *bo = to_vc4_bo(obj);
822
823         /* The only reason we would end up here is when user-space accesses
824          * BO's memory after it's been purged.
825          */
826         mutex_lock(&bo->madv_lock);
827         WARN_ON(bo->madv != __VC4_MADV_PURGED);
828         mutex_unlock(&bo->madv_lock);
829
830         return VM_FAULT_SIGBUS;
831 }
832
833 int vc4_drm_gem_prime_fd_to_handle(struct drm_device *dev,
834                 struct drm_file *file_priv, int prime_fd, uint32_t *handle)
835 {
836         int ret;
837
838         ret = drm_gem_prime_fd_to_handle(dev, file_priv, prime_fd, handle);
839         if (ret < 0)
840                 goto out;
841
842         vc4_gem_register_pid(file_priv);
843
844 out:
845         return ret;
846 }
847
848 int vc4_mmap(struct file *filp, struct vm_area_struct *vma)
849 {
850         struct drm_gem_object *gem_obj;
851         unsigned long vm_pgoff;
852         struct vc4_bo *bo;
853         int ret;
854
855         ret = drm_gem_mmap(filp, vma);
856         if (ret)
857                 return ret;
858
859         gem_obj = vma->vm_private_data;
860         bo = to_vc4_bo(gem_obj);
861
862         if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
863                 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
864                 return -EINVAL;
865         }
866
867         if (bo->madv != VC4_MADV_WILLNEED) {
868                 DRM_DEBUG("mmaping of %s BO not allowed\n",
869                           bo->madv == VC4_MADV_DONTNEED ?
870                           "purgeable" : "purged");
871                 return -EINVAL;
872         }
873
874         /*
875          * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
876          * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
877          * the whole buffer.
878          */
879         vma->vm_flags &= ~VM_PFNMAP;
880
881         /* This ->vm_pgoff dance is needed to make all parties happy:
882          * - dma_mmap_wc() uses ->vm_pgoff as an offset within the allocated
883          *   mem-region, hence the need to set it to zero (the value set by
884          *   the DRM core is a virtual offset encoding the GEM object-id)
885          * - the mmap() core logic needs ->vm_pgoff to be restored to its
886          *   initial value before returning from this function because it
887          *   encodes the  offset of this GEM in the dev->anon_inode pseudo-file
888          *   and this information will be used when we invalidate userspace
889          *   mappings  with drm_vma_node_unmap() (called from vc4_gem_purge()).
890          */
891         vm_pgoff = vma->vm_pgoff;
892         vma->vm_pgoff = 0;
893         ret = dma_mmap_wc(bo->base.base.dev->dev, vma, bo->base.vaddr,
894                           bo->base.paddr, vma->vm_end - vma->vm_start);
895         vma->vm_pgoff = vm_pgoff;
896
897         if (ret)
898                 drm_gem_vm_close(vma);
899
900         return ret;
901 }
902
903 int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
904 {
905         struct vc4_bo *bo = to_vc4_bo(obj);
906
907         if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
908                 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
909                 return -EINVAL;
910         }
911
912         return drm_gem_cma_prime_mmap(obj, vma);
913 }
914
915 void *vc4_prime_vmap(struct drm_gem_object *obj)
916 {
917         struct vc4_bo *bo = to_vc4_bo(obj);
918
919         if (bo->validated_shader) {
920                 DRM_DEBUG("mmaping of shader BOs not allowed.\n");
921                 return ERR_PTR(-EINVAL);
922         }
923
924         return drm_gem_cma_prime_vmap(obj);
925 }
926
927 struct drm_gem_object *
928 vc4_prime_import_sg_table(struct drm_device *dev,
929                           struct dma_buf_attachment *attach,
930                           struct sg_table *sgt)
931 {
932         struct drm_gem_object *obj;
933         struct vc4_bo *bo;
934
935         obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
936         if (IS_ERR(obj))
937                 return obj;
938
939         bo = to_vc4_bo(obj);
940         bo->resv = attach->dmabuf->resv;
941
942         return obj;
943 }
944
945 int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
946                         struct drm_file *file_priv)
947 {
948         struct drm_vc4_create_bo *args = data;
949         struct vc4_bo *bo = NULL;
950         int ret;
951
952         /*
953          * We can't allocate from the BO cache, because the BOs don't
954          * get zeroed, and that might leak data between users.
955          */
956         bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_V3D);
957         if (IS_ERR(bo))
958                 return PTR_ERR(bo);
959
960         bo->madv = VC4_MADV_WILLNEED;
961
962         ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
963         drm_gem_object_put_unlocked(&bo->base.base);
964
965         if (!ret)
966                 vc4_gem_register_pid(file_priv);
967
968         return ret;
969 }
970
971 int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
972                       struct drm_file *file_priv)
973 {
974         struct drm_vc4_mmap_bo *args = data;
975         struct drm_gem_object *gem_obj;
976
977         gem_obj = drm_gem_object_lookup(file_priv, args->handle);
978         if (!gem_obj) {
979                 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
980                 return -EINVAL;
981         }
982
983         /* The mmap offset was set up at BO allocation time. */
984         args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
985
986         drm_gem_object_put_unlocked(gem_obj);
987         return 0;
988 }
989
990 int
991 vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
992                            struct drm_file *file_priv)
993 {
994         struct drm_vc4_create_shader_bo *args = data;
995         struct vc4_bo *bo = NULL;
996         int ret;
997
998         if (args->size == 0)
999                 return -EINVAL;
1000
1001         if (args->size % sizeof(u64) != 0)
1002                 return -EINVAL;
1003
1004         if (args->flags != 0) {
1005                 DRM_INFO("Unknown flags set: 0x%08x\n", args->flags);
1006                 return -EINVAL;
1007         }
1008
1009         if (args->pad != 0) {
1010                 DRM_INFO("Pad set: 0x%08x\n", args->pad);
1011                 return -EINVAL;
1012         }
1013
1014         bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER);
1015         if (IS_ERR(bo))
1016                 return PTR_ERR(bo);
1017
1018         bo->madv = VC4_MADV_WILLNEED;
1019
1020         if (copy_from_user(bo->base.vaddr,
1021                              (void __user *)(uintptr_t)args->data,
1022                              args->size)) {
1023                 ret = -EFAULT;
1024                 goto fail;
1025         }
1026         /* Clear the rest of the memory from allocating from the BO
1027          * cache.
1028          */
1029         memset(bo->base.vaddr + args->size, 0,
1030                bo->base.base.size - args->size);
1031
1032         bo->validated_shader = vc4_validate_shader(&bo->base);
1033         if (!bo->validated_shader) {
1034                 ret = -EINVAL;
1035                 goto fail;
1036         }
1037
1038         /* We have to create the handle after validation, to avoid
1039          * races for users to do doing things like mmap the shader BO.
1040          */
1041         ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
1042
1043         if (!ret)
1044                 vc4_gem_register_pid(file_priv);
1045
1046  fail:
1047         drm_gem_object_put_unlocked(&bo->base.base);
1048
1049         return ret;
1050 }
1051
1052 /**
1053  * vc4_set_tiling_ioctl() - Sets the tiling modifier for a BO.
1054  * @dev: DRM device
1055  * @data: ioctl argument
1056  * @file_priv: DRM file for this fd
1057  *
1058  * The tiling state of the BO decides the default modifier of an fb if
1059  * no specific modifier was set by userspace, and the return value of
1060  * vc4_get_tiling_ioctl() (so that userspace can treat a BO it
1061  * received from dmabuf as the same tiling format as the producer
1062  * used).
1063  */
1064 int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
1065                          struct drm_file *file_priv)
1066 {
1067         struct drm_vc4_set_tiling *args = data;
1068         struct drm_gem_object *gem_obj;
1069         struct vc4_bo *bo;
1070         bool t_format;
1071
1072         if (args->flags != 0)
1073                 return -EINVAL;
1074
1075         switch (args->modifier) {
1076         case DRM_FORMAT_MOD_NONE:
1077                 t_format = false;
1078                 break;
1079         case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
1080                 t_format = true;
1081                 break;
1082         default:
1083                 return -EINVAL;
1084         }
1085
1086         gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1087         if (!gem_obj) {
1088                 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
1089                 return -ENOENT;
1090         }
1091         bo = to_vc4_bo(gem_obj);
1092         bo->t_format = t_format;
1093
1094         drm_gem_object_put_unlocked(gem_obj);
1095
1096         return 0;
1097 }
1098
1099 /**
1100  * vc4_get_tiling_ioctl() - Gets the tiling modifier for a BO.
1101  * @dev: DRM device
1102  * @data: ioctl argument
1103  * @file_priv: DRM file for this fd
1104  *
1105  * Returns the tiling modifier for a BO as set by vc4_set_tiling_ioctl().
1106  */
1107 int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
1108                          struct drm_file *file_priv)
1109 {
1110         struct drm_vc4_get_tiling *args = data;
1111         struct drm_gem_object *gem_obj;
1112         struct vc4_bo *bo;
1113
1114         if (args->flags != 0 || args->modifier != 0)
1115                 return -EINVAL;
1116
1117         gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1118         if (!gem_obj) {
1119                 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
1120                 return -ENOENT;
1121         }
1122         bo = to_vc4_bo(gem_obj);
1123
1124         if (bo->t_format)
1125                 args->modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
1126         else
1127                 args->modifier = DRM_FORMAT_MOD_NONE;
1128
1129         drm_gem_object_put_unlocked(gem_obj);
1130
1131         return 0;
1132 }
1133
1134 int vc4_bo_cache_init(struct drm_device *dev)
1135 {
1136         struct vc4_dev *vc4 = to_vc4_dev(dev);
1137         int i;
1138
1139         /* Create the initial set of BO labels that the kernel will
1140          * use.  This lets us avoid a bunch of string reallocation in
1141          * the kernel's draw and BO allocation paths.
1142          */
1143         vc4->bo_labels = kcalloc(VC4_BO_TYPE_COUNT, sizeof(*vc4->bo_labels),
1144                                  GFP_KERNEL);
1145         if (!vc4->bo_labels)
1146                 return -ENOMEM;
1147         vc4->num_labels = VC4_BO_TYPE_COUNT;
1148
1149         BUILD_BUG_ON(ARRAY_SIZE(bo_type_names) != VC4_BO_TYPE_COUNT);
1150         for (i = 0; i < VC4_BO_TYPE_COUNT; i++)
1151                 vc4->bo_labels[i].name = bo_type_names[i];
1152
1153         mutex_init(&vc4->bo_lock);
1154
1155         INIT_LIST_HEAD(&vc4->bo_cache.time_list);
1156
1157         INIT_WORK(&vc4->bo_cache.time_work, vc4_bo_cache_time_work);
1158         timer_setup(&vc4->bo_cache.time_timer, vc4_bo_cache_time_timer, 0);
1159
1160         return 0;
1161 }
1162
1163 void vc4_bo_cache_destroy(struct drm_device *dev)
1164 {
1165         struct vc4_dev *vc4 = to_vc4_dev(dev);
1166         int i;
1167
1168         del_timer(&vc4->bo_cache.time_timer);
1169         cancel_work_sync(&vc4->bo_cache.time_work);
1170
1171         vc4_bo_cache_purge(dev);
1172
1173         for (i = 0; i < vc4->num_labels; i++) {
1174                 if (vc4->bo_labels[i].num_allocated) {
1175                         DRM_ERROR("Destroying BO cache with %d %s "
1176                                   "BOs still allocated\n",
1177                                   vc4->bo_labels[i].num_allocated,
1178                                   vc4->bo_labels[i].name);
1179                 }
1180
1181                 if (is_user_label(i))
1182                         kfree(vc4->bo_labels[i].name);
1183         }
1184         kfree(vc4->bo_labels);
1185 }
1186
1187 int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
1188                        struct drm_file *file_priv)
1189 {
1190         struct vc4_dev *vc4 = to_vc4_dev(dev);
1191         struct drm_vc4_label_bo *args = data;
1192         char *name;
1193         struct drm_gem_object *gem_obj;
1194         int ret = 0, label;
1195
1196         if (!args->len)
1197                 return -EINVAL;
1198
1199         name = strndup_user(u64_to_user_ptr(args->name), args->len + 1);
1200         if (IS_ERR(name))
1201                 return PTR_ERR(name);
1202
1203         gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1204         if (!gem_obj) {
1205                 DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
1206                 kfree(name);
1207                 return -ENOENT;
1208         }
1209
1210         mutex_lock(&vc4->bo_lock);
1211         label = vc4_get_user_label(vc4, name);
1212         if (label != -1)
1213                 vc4_bo_set_label(gem_obj, label);
1214         else
1215                 ret = -ENOMEM;
1216         mutex_unlock(&vc4->bo_lock);
1217
1218         drm_gem_object_put_unlocked(gem_obj);
1219
1220         return ret;
1221 }