Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / vc4 / vc4_bo.c
blob469d1b4f2643fb1c04680f37dcd2a8fe4cd46a97
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright © 2015 Broadcom
4 */
6 /**
7 * DOC: VC4 GEM BO management support
9 * The VC4 GPU architecture (both scanout and rendering) has direct
10 * access to system memory with no MMU in between. To support it, we
11 * use the GEM CMA helper functions to allocate contiguous ranges of
12 * physical memory for our BOs.
14 * Since the CMA allocator is very slow, we keep a cache of recently
15 * freed BOs around so that the kernel's allocation of objects for 3D
16 * rendering can return quickly.
19 #include <linux/dma-buf.h>
21 #include "vc4_drv.h"
22 #include "uapi/drm/vc4_drm.h"
24 static vm_fault_t vc4_fault(struct vm_fault *vmf);
26 static const char * const bo_type_names[] = {
27 "kernel",
28 "V3D",
29 "V3D shader",
30 "dumb",
31 "binner",
32 "RCL",
33 "BCL",
34 "kernel BO cache",
37 static bool is_user_label(int label)
39 return label >= VC4_BO_TYPE_COUNT;
42 static void vc4_bo_stats_print(struct drm_printer *p, struct vc4_dev *vc4)
44 int i;
46 for (i = 0; i < vc4->num_labels; i++) {
47 if (!vc4->bo_labels[i].num_allocated)
48 continue;
50 drm_printf(p, "%30s: %6dkb BOs (%d)\n",
51 vc4->bo_labels[i].name,
52 vc4->bo_labels[i].size_allocated / 1024,
53 vc4->bo_labels[i].num_allocated);
56 mutex_lock(&vc4->purgeable.lock);
57 if (vc4->purgeable.num)
58 drm_printf(p, "%30s: %6zdkb BOs (%d)\n", "userspace BO cache",
59 vc4->purgeable.size / 1024, vc4->purgeable.num);
61 if (vc4->purgeable.purged_num)
62 drm_printf(p, "%30s: %6zdkb BOs (%d)\n", "total purged BO",
63 vc4->purgeable.purged_size / 1024,
64 vc4->purgeable.purged_num);
65 mutex_unlock(&vc4->purgeable.lock);
68 static int vc4_bo_stats_debugfs(struct seq_file *m, void *unused)
70 struct drm_info_node *node = (struct drm_info_node *)m->private;
71 struct drm_device *dev = node->minor->dev;
72 struct vc4_dev *vc4 = to_vc4_dev(dev);
73 struct drm_printer p = drm_seq_file_printer(m);
75 vc4_bo_stats_print(&p, vc4);
77 return 0;
80 /* Takes ownership of *name and returns the appropriate slot for it in
81 * the bo_labels[] array, extending it as necessary.
83 * This is inefficient and could use a hash table instead of walking
84 * an array and strcmp()ing. However, the assumption is that user
85 * labeling will be infrequent (scanout buffers and other long-lived
86 * objects, or debug driver builds), so we can live with it for now.
88 static int vc4_get_user_label(struct vc4_dev *vc4, const char *name)
90 int i;
91 int free_slot = -1;
93 for (i = 0; i < vc4->num_labels; i++) {
94 if (!vc4->bo_labels[i].name) {
95 free_slot = i;
96 } else if (strcmp(vc4->bo_labels[i].name, name) == 0) {
97 kfree(name);
98 return i;
102 if (free_slot != -1) {
103 WARN_ON(vc4->bo_labels[free_slot].num_allocated != 0);
104 vc4->bo_labels[free_slot].name = name;
105 return free_slot;
106 } else {
107 u32 new_label_count = vc4->num_labels + 1;
108 struct vc4_label *new_labels =
109 krealloc(vc4->bo_labels,
110 new_label_count * sizeof(*new_labels),
111 GFP_KERNEL);
113 if (!new_labels) {
114 kfree(name);
115 return -1;
118 free_slot = vc4->num_labels;
119 vc4->bo_labels = new_labels;
120 vc4->num_labels = new_label_count;
122 vc4->bo_labels[free_slot].name = name;
123 vc4->bo_labels[free_slot].num_allocated = 0;
124 vc4->bo_labels[free_slot].size_allocated = 0;
126 return free_slot;
130 static void vc4_bo_set_label(struct drm_gem_object *gem_obj, int label)
132 struct vc4_bo *bo = to_vc4_bo(gem_obj);
133 struct vc4_dev *vc4 = to_vc4_dev(gem_obj->dev);
135 lockdep_assert_held(&vc4->bo_lock);
137 if (label != -1) {
138 vc4->bo_labels[label].num_allocated++;
139 vc4->bo_labels[label].size_allocated += gem_obj->size;
142 vc4->bo_labels[bo->label].num_allocated--;
143 vc4->bo_labels[bo->label].size_allocated -= gem_obj->size;
145 if (vc4->bo_labels[bo->label].num_allocated == 0 &&
146 is_user_label(bo->label)) {
147 /* Free user BO label slots on last unreference.
148 * Slots are just where we track the stats for a given
149 * name, and once a name is unused we can reuse that
150 * slot.
152 kfree(vc4->bo_labels[bo->label].name);
153 vc4->bo_labels[bo->label].name = NULL;
156 bo->label = label;
159 static uint32_t bo_page_index(size_t size)
161 return (size / PAGE_SIZE) - 1;
164 static void vc4_bo_destroy(struct vc4_bo *bo)
166 struct drm_gem_object *obj = &bo->base.base;
167 struct vc4_dev *vc4 = to_vc4_dev(obj->dev);
169 lockdep_assert_held(&vc4->bo_lock);
171 vc4_bo_set_label(obj, -1);
173 if (bo->validated_shader) {
174 kfree(bo->validated_shader->uniform_addr_offsets);
175 kfree(bo->validated_shader->texture_samples);
176 kfree(bo->validated_shader);
177 bo->validated_shader = NULL;
180 drm_gem_cma_free_object(obj);
183 static void vc4_bo_remove_from_cache(struct vc4_bo *bo)
185 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
187 lockdep_assert_held(&vc4->bo_lock);
188 list_del(&bo->unref_head);
189 list_del(&bo->size_head);
192 static struct list_head *vc4_get_cache_list_for_size(struct drm_device *dev,
193 size_t size)
195 struct vc4_dev *vc4 = to_vc4_dev(dev);
196 uint32_t page_index = bo_page_index(size);
198 if (vc4->bo_cache.size_list_size <= page_index) {
199 uint32_t new_size = max(vc4->bo_cache.size_list_size * 2,
200 page_index + 1);
201 struct list_head *new_list;
202 uint32_t i;
204 new_list = kmalloc_array(new_size, sizeof(struct list_head),
205 GFP_KERNEL);
206 if (!new_list)
207 return NULL;
209 /* Rebase the old cached BO lists to their new list
210 * head locations.
212 for (i = 0; i < vc4->bo_cache.size_list_size; i++) {
213 struct list_head *old_list =
214 &vc4->bo_cache.size_list[i];
216 if (list_empty(old_list))
217 INIT_LIST_HEAD(&new_list[i]);
218 else
219 list_replace(old_list, &new_list[i]);
221 /* And initialize the brand new BO list heads. */
222 for (i = vc4->bo_cache.size_list_size; i < new_size; i++)
223 INIT_LIST_HEAD(&new_list[i]);
225 kfree(vc4->bo_cache.size_list);
226 vc4->bo_cache.size_list = new_list;
227 vc4->bo_cache.size_list_size = new_size;
230 return &vc4->bo_cache.size_list[page_index];
233 static void vc4_bo_cache_purge(struct drm_device *dev)
235 struct vc4_dev *vc4 = to_vc4_dev(dev);
237 mutex_lock(&vc4->bo_lock);
238 while (!list_empty(&vc4->bo_cache.time_list)) {
239 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
240 struct vc4_bo, unref_head);
241 vc4_bo_remove_from_cache(bo);
242 vc4_bo_destroy(bo);
244 mutex_unlock(&vc4->bo_lock);
247 void vc4_bo_add_to_purgeable_pool(struct vc4_bo *bo)
249 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
251 mutex_lock(&vc4->purgeable.lock);
252 list_add_tail(&bo->size_head, &vc4->purgeable.list);
253 vc4->purgeable.num++;
254 vc4->purgeable.size += bo->base.base.size;
255 mutex_unlock(&vc4->purgeable.lock);
258 static void vc4_bo_remove_from_purgeable_pool_locked(struct vc4_bo *bo)
260 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
262 /* list_del_init() is used here because the caller might release
263 * the purgeable lock in order to acquire the madv one and update the
264 * madv status.
265 * During this short period of time a user might decide to mark
266 * the BO as unpurgeable, and if bo->madv is set to
267 * VC4_MADV_DONTNEED it will try to remove the BO from the
268 * purgeable list which will fail if the ->next/prev fields
269 * are set to LIST_POISON1/LIST_POISON2 (which is what
270 * list_del() does).
271 * Re-initializing the list element guarantees that list_del()
272 * will work correctly even if it's a NOP.
274 list_del_init(&bo->size_head);
275 vc4->purgeable.num--;
276 vc4->purgeable.size -= bo->base.base.size;
279 void vc4_bo_remove_from_purgeable_pool(struct vc4_bo *bo)
281 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
283 mutex_lock(&vc4->purgeable.lock);
284 vc4_bo_remove_from_purgeable_pool_locked(bo);
285 mutex_unlock(&vc4->purgeable.lock);
288 static void vc4_bo_purge(struct drm_gem_object *obj)
290 struct vc4_bo *bo = to_vc4_bo(obj);
291 struct drm_device *dev = obj->dev;
293 WARN_ON(!mutex_is_locked(&bo->madv_lock));
294 WARN_ON(bo->madv != VC4_MADV_DONTNEED);
296 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
298 dma_free_wc(dev->dev, obj->size, bo->base.vaddr, bo->base.paddr);
299 bo->base.vaddr = NULL;
300 bo->madv = __VC4_MADV_PURGED;
303 static void vc4_bo_userspace_cache_purge(struct drm_device *dev)
305 struct vc4_dev *vc4 = to_vc4_dev(dev);
307 mutex_lock(&vc4->purgeable.lock);
308 while (!list_empty(&vc4->purgeable.list)) {
309 struct vc4_bo *bo = list_first_entry(&vc4->purgeable.list,
310 struct vc4_bo, size_head);
311 struct drm_gem_object *obj = &bo->base.base;
312 size_t purged_size = 0;
314 vc4_bo_remove_from_purgeable_pool_locked(bo);
316 /* Release the purgeable lock while we're purging the BO so
317 * that other people can continue inserting things in the
318 * purgeable pool without having to wait for all BOs to be
319 * purged.
321 mutex_unlock(&vc4->purgeable.lock);
322 mutex_lock(&bo->madv_lock);
324 /* Since we released the purgeable pool lock before acquiring
325 * the BO madv one, the user may have marked the BO as WILLNEED
326 * and re-used it in the meantime.
327 * Before purging the BO we need to make sure
328 * - it is still marked as DONTNEED
329 * - it has not been re-inserted in the purgeable list
330 * - it is not used by HW blocks
331 * If one of these conditions is not met, just skip the entry.
333 if (bo->madv == VC4_MADV_DONTNEED &&
334 list_empty(&bo->size_head) &&
335 !refcount_read(&bo->usecnt)) {
336 purged_size = bo->base.base.size;
337 vc4_bo_purge(obj);
339 mutex_unlock(&bo->madv_lock);
340 mutex_lock(&vc4->purgeable.lock);
342 if (purged_size) {
343 vc4->purgeable.purged_size += purged_size;
344 vc4->purgeable.purged_num++;
347 mutex_unlock(&vc4->purgeable.lock);
350 static struct vc4_bo *vc4_bo_get_from_cache(struct drm_device *dev,
351 uint32_t size,
352 enum vc4_kernel_bo_type type)
354 struct vc4_dev *vc4 = to_vc4_dev(dev);
355 uint32_t page_index = bo_page_index(size);
356 struct vc4_bo *bo = NULL;
358 size = roundup(size, PAGE_SIZE);
360 mutex_lock(&vc4->bo_lock);
361 if (page_index >= vc4->bo_cache.size_list_size)
362 goto out;
364 if (list_empty(&vc4->bo_cache.size_list[page_index]))
365 goto out;
367 bo = list_first_entry(&vc4->bo_cache.size_list[page_index],
368 struct vc4_bo, size_head);
369 vc4_bo_remove_from_cache(bo);
370 kref_init(&bo->base.base.refcount);
372 out:
373 if (bo)
374 vc4_bo_set_label(&bo->base.base, type);
375 mutex_unlock(&vc4->bo_lock);
376 return bo;
379 static const struct vm_operations_struct vc4_vm_ops = {
380 .fault = vc4_fault,
381 .open = drm_gem_vm_open,
382 .close = drm_gem_vm_close,
385 static const struct drm_gem_object_funcs vc4_gem_object_funcs = {
386 .free = vc4_free_object,
387 .export = vc4_prime_export,
388 .get_sg_table = drm_gem_cma_prime_get_sg_table,
389 .vmap = vc4_prime_vmap,
390 .vm_ops = &vc4_vm_ops,
394 * vc4_create_object - Implementation of driver->gem_create_object.
395 * @dev: DRM device
396 * @size: Size in bytes of the memory the object will reference
398 * This lets the CMA helpers allocate object structs for us, and keep
399 * our BO stats correct.
401 struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size)
403 struct vc4_dev *vc4 = to_vc4_dev(dev);
404 struct vc4_bo *bo;
406 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
407 if (!bo)
408 return ERR_PTR(-ENOMEM);
410 bo->madv = VC4_MADV_WILLNEED;
411 refcount_set(&bo->usecnt, 0);
412 mutex_init(&bo->madv_lock);
413 mutex_lock(&vc4->bo_lock);
414 bo->label = VC4_BO_TYPE_KERNEL;
415 vc4->bo_labels[VC4_BO_TYPE_KERNEL].num_allocated++;
416 vc4->bo_labels[VC4_BO_TYPE_KERNEL].size_allocated += size;
417 mutex_unlock(&vc4->bo_lock);
419 bo->base.base.funcs = &vc4_gem_object_funcs;
421 return &bo->base.base;
424 struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t unaligned_size,
425 bool allow_unzeroed, enum vc4_kernel_bo_type type)
427 size_t size = roundup(unaligned_size, PAGE_SIZE);
428 struct vc4_dev *vc4 = to_vc4_dev(dev);
429 struct drm_gem_cma_object *cma_obj;
430 struct vc4_bo *bo;
432 if (size == 0)
433 return ERR_PTR(-EINVAL);
435 /* First, try to get a vc4_bo from the kernel BO cache. */
436 bo = vc4_bo_get_from_cache(dev, size, type);
437 if (bo) {
438 if (!allow_unzeroed)
439 memset(bo->base.vaddr, 0, bo->base.base.size);
440 return bo;
443 cma_obj = drm_gem_cma_create(dev, size);
444 if (IS_ERR(cma_obj)) {
446 * If we've run out of CMA memory, kill the cache of
447 * CMA allocations we've got laying around and try again.
449 vc4_bo_cache_purge(dev);
450 cma_obj = drm_gem_cma_create(dev, size);
453 if (IS_ERR(cma_obj)) {
455 * Still not enough CMA memory, purge the userspace BO
456 * cache and retry.
457 * This is sub-optimal since we purge the whole userspace
458 * BO cache which forces user that want to re-use the BO to
459 * restore its initial content.
460 * Ideally, we should purge entries one by one and retry
461 * after each to see if CMA allocation succeeds. Or even
462 * better, try to find an entry with at least the same
463 * size.
465 vc4_bo_userspace_cache_purge(dev);
466 cma_obj = drm_gem_cma_create(dev, size);
469 if (IS_ERR(cma_obj)) {
470 struct drm_printer p = drm_info_printer(vc4->base.dev);
471 DRM_ERROR("Failed to allocate from CMA:\n");
472 vc4_bo_stats_print(&p, vc4);
473 return ERR_PTR(-ENOMEM);
475 bo = to_vc4_bo(&cma_obj->base);
477 /* By default, BOs do not support the MADV ioctl. This will be enabled
478 * only on BOs that are exposed to userspace (V3D, V3D_SHADER and DUMB
479 * BOs).
481 bo->madv = __VC4_MADV_NOTSUPP;
483 mutex_lock(&vc4->bo_lock);
484 vc4_bo_set_label(&cma_obj->base, type);
485 mutex_unlock(&vc4->bo_lock);
487 return bo;
490 int vc4_dumb_create(struct drm_file *file_priv,
491 struct drm_device *dev,
492 struct drm_mode_create_dumb *args)
494 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
495 struct vc4_bo *bo = NULL;
496 int ret;
498 if (args->pitch < min_pitch)
499 args->pitch = min_pitch;
501 if (args->size < args->pitch * args->height)
502 args->size = args->pitch * args->height;
504 bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_DUMB);
505 if (IS_ERR(bo))
506 return PTR_ERR(bo);
508 bo->madv = VC4_MADV_WILLNEED;
510 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
511 drm_gem_object_put(&bo->base.base);
513 return ret;
516 static void vc4_bo_cache_free_old(struct drm_device *dev)
518 struct vc4_dev *vc4 = to_vc4_dev(dev);
519 unsigned long expire_time = jiffies - msecs_to_jiffies(1000);
521 lockdep_assert_held(&vc4->bo_lock);
523 while (!list_empty(&vc4->bo_cache.time_list)) {
524 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
525 struct vc4_bo, unref_head);
526 if (time_before(expire_time, bo->free_time)) {
527 mod_timer(&vc4->bo_cache.time_timer,
528 round_jiffies_up(jiffies +
529 msecs_to_jiffies(1000)));
530 return;
533 vc4_bo_remove_from_cache(bo);
534 vc4_bo_destroy(bo);
538 /* Called on the last userspace/kernel unreference of the BO. Returns
539 * it to the BO cache if possible, otherwise frees it.
541 void vc4_free_object(struct drm_gem_object *gem_bo)
543 struct drm_device *dev = gem_bo->dev;
544 struct vc4_dev *vc4 = to_vc4_dev(dev);
545 struct vc4_bo *bo = to_vc4_bo(gem_bo);
546 struct list_head *cache_list;
548 /* Remove the BO from the purgeable list. */
549 mutex_lock(&bo->madv_lock);
550 if (bo->madv == VC4_MADV_DONTNEED && !refcount_read(&bo->usecnt))
551 vc4_bo_remove_from_purgeable_pool(bo);
552 mutex_unlock(&bo->madv_lock);
554 mutex_lock(&vc4->bo_lock);
555 /* If the object references someone else's memory, we can't cache it.
557 if (gem_bo->import_attach) {
558 vc4_bo_destroy(bo);
559 goto out;
562 /* Don't cache if it was publicly named. */
563 if (gem_bo->name) {
564 vc4_bo_destroy(bo);
565 goto out;
568 /* If this object was partially constructed but CMA allocation
569 * had failed, just free it. Can also happen when the BO has been
570 * purged.
572 if (!bo->base.vaddr) {
573 vc4_bo_destroy(bo);
574 goto out;
577 cache_list = vc4_get_cache_list_for_size(dev, gem_bo->size);
578 if (!cache_list) {
579 vc4_bo_destroy(bo);
580 goto out;
583 if (bo->validated_shader) {
584 kfree(bo->validated_shader->uniform_addr_offsets);
585 kfree(bo->validated_shader->texture_samples);
586 kfree(bo->validated_shader);
587 bo->validated_shader = NULL;
590 /* Reset madv and usecnt before adding the BO to the cache. */
591 bo->madv = __VC4_MADV_NOTSUPP;
592 refcount_set(&bo->usecnt, 0);
594 bo->t_format = false;
595 bo->free_time = jiffies;
596 list_add(&bo->size_head, cache_list);
597 list_add(&bo->unref_head, &vc4->bo_cache.time_list);
599 vc4_bo_set_label(&bo->base.base, VC4_BO_TYPE_KERNEL_CACHE);
601 vc4_bo_cache_free_old(dev);
603 out:
604 mutex_unlock(&vc4->bo_lock);
607 static void vc4_bo_cache_time_work(struct work_struct *work)
609 struct vc4_dev *vc4 =
610 container_of(work, struct vc4_dev, bo_cache.time_work);
611 struct drm_device *dev = &vc4->base;
613 mutex_lock(&vc4->bo_lock);
614 vc4_bo_cache_free_old(dev);
615 mutex_unlock(&vc4->bo_lock);
618 int vc4_bo_inc_usecnt(struct vc4_bo *bo)
620 int ret;
622 /* Fast path: if the BO is already retained by someone, no need to
623 * check the madv status.
625 if (refcount_inc_not_zero(&bo->usecnt))
626 return 0;
628 mutex_lock(&bo->madv_lock);
629 switch (bo->madv) {
630 case VC4_MADV_WILLNEED:
631 if (!refcount_inc_not_zero(&bo->usecnt))
632 refcount_set(&bo->usecnt, 1);
633 ret = 0;
634 break;
635 case VC4_MADV_DONTNEED:
636 /* We shouldn't use a BO marked as purgeable if at least
637 * someone else retained its content by incrementing usecnt.
638 * Luckily the BO hasn't been purged yet, but something wrong
639 * is happening here. Just throw an error instead of
640 * authorizing this use case.
642 case __VC4_MADV_PURGED:
643 /* We can't use a purged BO. */
644 default:
645 /* Invalid madv value. */
646 ret = -EINVAL;
647 break;
649 mutex_unlock(&bo->madv_lock);
651 return ret;
654 void vc4_bo_dec_usecnt(struct vc4_bo *bo)
656 /* Fast path: if the BO is still retained by someone, no need to test
657 * the madv value.
659 if (refcount_dec_not_one(&bo->usecnt))
660 return;
662 mutex_lock(&bo->madv_lock);
663 if (refcount_dec_and_test(&bo->usecnt) &&
664 bo->madv == VC4_MADV_DONTNEED)
665 vc4_bo_add_to_purgeable_pool(bo);
666 mutex_unlock(&bo->madv_lock);
669 static void vc4_bo_cache_time_timer(struct timer_list *t)
671 struct vc4_dev *vc4 = from_timer(vc4, t, bo_cache.time_timer);
673 schedule_work(&vc4->bo_cache.time_work);
676 struct dma_buf * vc4_prime_export(struct drm_gem_object *obj, int flags)
678 struct vc4_bo *bo = to_vc4_bo(obj);
679 struct dma_buf *dmabuf;
680 int ret;
682 if (bo->validated_shader) {
683 DRM_DEBUG("Attempting to export shader BO\n");
684 return ERR_PTR(-EINVAL);
687 /* Note: as soon as the BO is exported it becomes unpurgeable, because
688 * noone ever decrements the usecnt even if the reference held by the
689 * exported BO is released. This shouldn't be a problem since we don't
690 * expect exported BOs to be marked as purgeable.
692 ret = vc4_bo_inc_usecnt(bo);
693 if (ret) {
694 DRM_ERROR("Failed to increment BO usecnt\n");
695 return ERR_PTR(ret);
698 dmabuf = drm_gem_prime_export(obj, flags);
699 if (IS_ERR(dmabuf))
700 vc4_bo_dec_usecnt(bo);
702 return dmabuf;
705 static vm_fault_t vc4_fault(struct vm_fault *vmf)
707 struct vm_area_struct *vma = vmf->vma;
708 struct drm_gem_object *obj = vma->vm_private_data;
709 struct vc4_bo *bo = to_vc4_bo(obj);
711 /* The only reason we would end up here is when user-space accesses
712 * BO's memory after it's been purged.
714 mutex_lock(&bo->madv_lock);
715 WARN_ON(bo->madv != __VC4_MADV_PURGED);
716 mutex_unlock(&bo->madv_lock);
718 return VM_FAULT_SIGBUS;
721 int vc4_mmap(struct file *filp, struct vm_area_struct *vma)
723 struct drm_gem_object *gem_obj;
724 unsigned long vm_pgoff;
725 struct vc4_bo *bo;
726 int ret;
728 ret = drm_gem_mmap(filp, vma);
729 if (ret)
730 return ret;
732 gem_obj = vma->vm_private_data;
733 bo = to_vc4_bo(gem_obj);
735 if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
736 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
737 return -EINVAL;
740 if (bo->madv != VC4_MADV_WILLNEED) {
741 DRM_DEBUG("mmaping of %s BO not allowed\n",
742 bo->madv == VC4_MADV_DONTNEED ?
743 "purgeable" : "purged");
744 return -EINVAL;
748 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
749 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
750 * the whole buffer.
752 vma->vm_flags &= ~VM_PFNMAP;
754 /* This ->vm_pgoff dance is needed to make all parties happy:
755 * - dma_mmap_wc() uses ->vm_pgoff as an offset within the allocated
756 * mem-region, hence the need to set it to zero (the value set by
757 * the DRM core is a virtual offset encoding the GEM object-id)
758 * - the mmap() core logic needs ->vm_pgoff to be restored to its
759 * initial value before returning from this function because it
760 * encodes the offset of this GEM in the dev->anon_inode pseudo-file
761 * and this information will be used when we invalidate userspace
762 * mappings with drm_vma_node_unmap() (called from vc4_gem_purge()).
764 vm_pgoff = vma->vm_pgoff;
765 vma->vm_pgoff = 0;
766 ret = dma_mmap_wc(bo->base.base.dev->dev, vma, bo->base.vaddr,
767 bo->base.paddr, vma->vm_end - vma->vm_start);
768 vma->vm_pgoff = vm_pgoff;
770 if (ret)
771 drm_gem_vm_close(vma);
773 return ret;
776 int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
778 struct vc4_bo *bo = to_vc4_bo(obj);
780 if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
781 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
782 return -EINVAL;
785 return drm_gem_cma_prime_mmap(obj, vma);
788 int vc4_prime_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
790 struct vc4_bo *bo = to_vc4_bo(obj);
792 if (bo->validated_shader) {
793 DRM_DEBUG("mmaping of shader BOs not allowed.\n");
794 return -EINVAL;
797 return drm_gem_cma_prime_vmap(obj, map);
800 struct drm_gem_object *
801 vc4_prime_import_sg_table(struct drm_device *dev,
802 struct dma_buf_attachment *attach,
803 struct sg_table *sgt)
805 struct drm_gem_object *obj;
807 obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
808 if (IS_ERR(obj))
809 return obj;
811 return obj;
814 static int vc4_grab_bin_bo(struct vc4_dev *vc4, struct vc4_file *vc4file)
816 int ret;
818 if (!vc4->v3d)
819 return -ENODEV;
821 if (vc4file->bin_bo_used)
822 return 0;
824 ret = vc4_v3d_bin_bo_get(vc4, &vc4file->bin_bo_used);
825 if (ret)
826 return ret;
828 return 0;
831 int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
832 struct drm_file *file_priv)
834 struct drm_vc4_create_bo *args = data;
835 struct vc4_file *vc4file = file_priv->driver_priv;
836 struct vc4_dev *vc4 = to_vc4_dev(dev);
837 struct vc4_bo *bo = NULL;
838 int ret;
840 ret = vc4_grab_bin_bo(vc4, vc4file);
841 if (ret)
842 return ret;
845 * We can't allocate from the BO cache, because the BOs don't
846 * get zeroed, and that might leak data between users.
848 bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_V3D);
849 if (IS_ERR(bo))
850 return PTR_ERR(bo);
852 bo->madv = VC4_MADV_WILLNEED;
854 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
855 drm_gem_object_put(&bo->base.base);
857 return ret;
860 int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
861 struct drm_file *file_priv)
863 struct drm_vc4_mmap_bo *args = data;
864 struct drm_gem_object *gem_obj;
866 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
867 if (!gem_obj) {
868 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
869 return -EINVAL;
872 /* The mmap offset was set up at BO allocation time. */
873 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
875 drm_gem_object_put(gem_obj);
876 return 0;
880 vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
881 struct drm_file *file_priv)
883 struct drm_vc4_create_shader_bo *args = data;
884 struct vc4_file *vc4file = file_priv->driver_priv;
885 struct vc4_dev *vc4 = to_vc4_dev(dev);
886 struct vc4_bo *bo = NULL;
887 int ret;
889 if (args->size == 0)
890 return -EINVAL;
892 if (args->size % sizeof(u64) != 0)
893 return -EINVAL;
895 if (args->flags != 0) {
896 DRM_INFO("Unknown flags set: 0x%08x\n", args->flags);
897 return -EINVAL;
900 if (args->pad != 0) {
901 DRM_INFO("Pad set: 0x%08x\n", args->pad);
902 return -EINVAL;
905 ret = vc4_grab_bin_bo(vc4, vc4file);
906 if (ret)
907 return ret;
909 bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER);
910 if (IS_ERR(bo))
911 return PTR_ERR(bo);
913 bo->madv = VC4_MADV_WILLNEED;
915 if (copy_from_user(bo->base.vaddr,
916 (void __user *)(uintptr_t)args->data,
917 args->size)) {
918 ret = -EFAULT;
919 goto fail;
921 /* Clear the rest of the memory from allocating from the BO
922 * cache.
924 memset(bo->base.vaddr + args->size, 0,
925 bo->base.base.size - args->size);
927 bo->validated_shader = vc4_validate_shader(&bo->base);
928 if (!bo->validated_shader) {
929 ret = -EINVAL;
930 goto fail;
933 /* We have to create the handle after validation, to avoid
934 * races for users to do doing things like mmap the shader BO.
936 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
938 fail:
939 drm_gem_object_put(&bo->base.base);
941 return ret;
945 * vc4_set_tiling_ioctl() - Sets the tiling modifier for a BO.
946 * @dev: DRM device
947 * @data: ioctl argument
948 * @file_priv: DRM file for this fd
950 * The tiling state of the BO decides the default modifier of an fb if
951 * no specific modifier was set by userspace, and the return value of
952 * vc4_get_tiling_ioctl() (so that userspace can treat a BO it
953 * received from dmabuf as the same tiling format as the producer
954 * used).
956 int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
957 struct drm_file *file_priv)
959 struct drm_vc4_set_tiling *args = data;
960 struct drm_gem_object *gem_obj;
961 struct vc4_bo *bo;
962 bool t_format;
964 if (args->flags != 0)
965 return -EINVAL;
967 switch (args->modifier) {
968 case DRM_FORMAT_MOD_NONE:
969 t_format = false;
970 break;
971 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
972 t_format = true;
973 break;
974 default:
975 return -EINVAL;
978 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
979 if (!gem_obj) {
980 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
981 return -ENOENT;
983 bo = to_vc4_bo(gem_obj);
984 bo->t_format = t_format;
986 drm_gem_object_put(gem_obj);
988 return 0;
992 * vc4_get_tiling_ioctl() - Gets the tiling modifier for a BO.
993 * @dev: DRM device
994 * @data: ioctl argument
995 * @file_priv: DRM file for this fd
997 * Returns the tiling modifier for a BO as set by vc4_set_tiling_ioctl().
999 int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
1000 struct drm_file *file_priv)
1002 struct drm_vc4_get_tiling *args = data;
1003 struct drm_gem_object *gem_obj;
1004 struct vc4_bo *bo;
1006 if (args->flags != 0 || args->modifier != 0)
1007 return -EINVAL;
1009 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1010 if (!gem_obj) {
1011 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
1012 return -ENOENT;
1014 bo = to_vc4_bo(gem_obj);
1016 if (bo->t_format)
1017 args->modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
1018 else
1019 args->modifier = DRM_FORMAT_MOD_NONE;
1021 drm_gem_object_put(gem_obj);
1023 return 0;
1026 static void vc4_bo_cache_destroy(struct drm_device *dev, void *unused);
1027 int vc4_bo_cache_init(struct drm_device *dev)
1029 struct vc4_dev *vc4 = to_vc4_dev(dev);
1030 int i;
1032 /* Create the initial set of BO labels that the kernel will
1033 * use. This lets us avoid a bunch of string reallocation in
1034 * the kernel's draw and BO allocation paths.
1036 vc4->bo_labels = kcalloc(VC4_BO_TYPE_COUNT, sizeof(*vc4->bo_labels),
1037 GFP_KERNEL);
1038 if (!vc4->bo_labels)
1039 return -ENOMEM;
1040 vc4->num_labels = VC4_BO_TYPE_COUNT;
1042 BUILD_BUG_ON(ARRAY_SIZE(bo_type_names) != VC4_BO_TYPE_COUNT);
1043 for (i = 0; i < VC4_BO_TYPE_COUNT; i++)
1044 vc4->bo_labels[i].name = bo_type_names[i];
1046 mutex_init(&vc4->bo_lock);
1048 vc4_debugfs_add_file(dev, "bo_stats", vc4_bo_stats_debugfs, NULL);
1050 INIT_LIST_HEAD(&vc4->bo_cache.time_list);
1052 INIT_WORK(&vc4->bo_cache.time_work, vc4_bo_cache_time_work);
1053 timer_setup(&vc4->bo_cache.time_timer, vc4_bo_cache_time_timer, 0);
1055 return drmm_add_action_or_reset(dev, vc4_bo_cache_destroy, NULL);
1058 static void vc4_bo_cache_destroy(struct drm_device *dev, void *unused)
1060 struct vc4_dev *vc4 = to_vc4_dev(dev);
1061 int i;
1063 del_timer(&vc4->bo_cache.time_timer);
1064 cancel_work_sync(&vc4->bo_cache.time_work);
1066 vc4_bo_cache_purge(dev);
1068 for (i = 0; i < vc4->num_labels; i++) {
1069 if (vc4->bo_labels[i].num_allocated) {
1070 DRM_ERROR("Destroying BO cache with %d %s "
1071 "BOs still allocated\n",
1072 vc4->bo_labels[i].num_allocated,
1073 vc4->bo_labels[i].name);
1076 if (is_user_label(i))
1077 kfree(vc4->bo_labels[i].name);
1079 kfree(vc4->bo_labels);
1082 int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
1083 struct drm_file *file_priv)
1085 struct vc4_dev *vc4 = to_vc4_dev(dev);
1086 struct drm_vc4_label_bo *args = data;
1087 char *name;
1088 struct drm_gem_object *gem_obj;
1089 int ret = 0, label;
1091 if (!args->len)
1092 return -EINVAL;
1094 name = strndup_user(u64_to_user_ptr(args->name), args->len + 1);
1095 if (IS_ERR(name))
1096 return PTR_ERR(name);
1098 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1099 if (!gem_obj) {
1100 DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
1101 kfree(name);
1102 return -ENOENT;
1105 mutex_lock(&vc4->bo_lock);
1106 label = vc4_get_user_label(vc4, name);
1107 if (label != -1)
1108 vc4_bo_set_label(gem_obj, label);
1109 else
1110 ret = -ENOMEM;
1111 mutex_unlock(&vc4->bo_lock);
1113 drm_gem_object_put(gem_obj);
1115 return ret;