1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2015 Broadcom
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 <drm/drm_fourcc.h>
24 #include "uapi/drm/vc4_drm.h"
26 static const struct drm_gem_object_funcs vc4_gem_object_funcs;
28 static const char * const bo_type_names[] = {
39 static bool is_user_label(int label)
41 return label >= VC4_BO_TYPE_COUNT;
44 static void vc4_bo_stats_print(struct drm_printer *p, struct vc4_dev *vc4)
48 for (i = 0; i < vc4->num_labels; i++) {
49 if (!vc4->bo_labels[i].num_allocated)
52 drm_printf(p, "%30s: %6dkb BOs (%d)\n",
53 vc4->bo_labels[i].name,
54 vc4->bo_labels[i].size_allocated / 1024,
55 vc4->bo_labels[i].num_allocated);
58 mutex_lock(&vc4->purgeable.lock);
59 if (vc4->purgeable.num)
60 drm_printf(p, "%30s: %6zdkb BOs (%d)\n", "userspace BO cache",
61 vc4->purgeable.size / 1024, vc4->purgeable.num);
63 if (vc4->purgeable.purged_num)
64 drm_printf(p, "%30s: %6zdkb BOs (%d)\n", "total purged BO",
65 vc4->purgeable.purged_size / 1024,
66 vc4->purgeable.purged_num);
67 mutex_unlock(&vc4->purgeable.lock);
70 static int vc4_bo_stats_debugfs(struct seq_file *m, void *unused)
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 struct drm_printer p = drm_seq_file_printer(m);
77 vc4_bo_stats_print(&p, vc4);
82 /* Takes ownership of *name and returns the appropriate slot for it in
83 * the bo_labels[] array, extending it as necessary.
85 * This is inefficient and could use a hash table instead of walking
86 * an array and strcmp()ing. However, the assumption is that user
87 * labeling will be infrequent (scanout buffers and other long-lived
88 * objects, or debug driver builds), so we can live with it for now.
90 static int vc4_get_user_label(struct vc4_dev *vc4, const char *name)
95 for (i = 0; i < vc4->num_labels; i++) {
96 if (!vc4->bo_labels[i].name) {
98 } else if (strcmp(vc4->bo_labels[i].name, name) == 0) {
104 if (free_slot != -1) {
105 WARN_ON(vc4->bo_labels[free_slot].num_allocated != 0);
106 vc4->bo_labels[free_slot].name = name;
109 u32 new_label_count = vc4->num_labels + 1;
110 struct vc4_label *new_labels =
111 krealloc(vc4->bo_labels,
112 new_label_count * sizeof(*new_labels),
120 free_slot = vc4->num_labels;
121 vc4->bo_labels = new_labels;
122 vc4->num_labels = new_label_count;
124 vc4->bo_labels[free_slot].name = name;
125 vc4->bo_labels[free_slot].num_allocated = 0;
126 vc4->bo_labels[free_slot].size_allocated = 0;
132 static void vc4_bo_set_label(struct drm_gem_object *gem_obj, int label)
134 struct vc4_bo *bo = to_vc4_bo(gem_obj);
135 struct vc4_dev *vc4 = to_vc4_dev(gem_obj->dev);
137 lockdep_assert_held(&vc4->bo_lock);
140 vc4->bo_labels[label].num_allocated++;
141 vc4->bo_labels[label].size_allocated += gem_obj->size;
144 vc4->bo_labels[bo->label].num_allocated--;
145 vc4->bo_labels[bo->label].size_allocated -= gem_obj->size;
147 if (vc4->bo_labels[bo->label].num_allocated == 0 &&
148 is_user_label(bo->label)) {
149 /* Free user BO label slots on last unreference.
150 * Slots are just where we track the stats for a given
151 * name, and once a name is unused we can reuse that
154 kfree(vc4->bo_labels[bo->label].name);
155 vc4->bo_labels[bo->label].name = NULL;
161 static uint32_t bo_page_index(size_t size)
163 return (size / PAGE_SIZE) - 1;
166 static void vc4_bo_destroy(struct vc4_bo *bo)
168 struct drm_gem_object *obj = &bo->base.base;
169 struct vc4_dev *vc4 = to_vc4_dev(obj->dev);
171 lockdep_assert_held(&vc4->bo_lock);
173 vc4_bo_set_label(obj, -1);
175 if (bo->validated_shader) {
176 kfree(bo->validated_shader->uniform_addr_offsets);
177 kfree(bo->validated_shader->texture_samples);
178 kfree(bo->validated_shader);
179 bo->validated_shader = NULL;
182 drm_gem_cma_free(&bo->base);
185 static void vc4_bo_remove_from_cache(struct vc4_bo *bo)
187 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
189 lockdep_assert_held(&vc4->bo_lock);
190 list_del(&bo->unref_head);
191 list_del(&bo->size_head);
194 static struct list_head *vc4_get_cache_list_for_size(struct drm_device *dev,
197 struct vc4_dev *vc4 = to_vc4_dev(dev);
198 uint32_t page_index = bo_page_index(size);
200 if (vc4->bo_cache.size_list_size <= page_index) {
201 uint32_t new_size = max(vc4->bo_cache.size_list_size * 2,
203 struct list_head *new_list;
206 new_list = kmalloc_array(new_size, sizeof(struct list_head),
211 /* Rebase the old cached BO lists to their new list
214 for (i = 0; i < vc4->bo_cache.size_list_size; i++) {
215 struct list_head *old_list =
216 &vc4->bo_cache.size_list[i];
218 if (list_empty(old_list))
219 INIT_LIST_HEAD(&new_list[i]);
221 list_replace(old_list, &new_list[i]);
223 /* And initialize the brand new BO list heads. */
224 for (i = vc4->bo_cache.size_list_size; i < new_size; i++)
225 INIT_LIST_HEAD(&new_list[i]);
227 kfree(vc4->bo_cache.size_list);
228 vc4->bo_cache.size_list = new_list;
229 vc4->bo_cache.size_list_size = new_size;
232 return &vc4->bo_cache.size_list[page_index];
235 static void vc4_bo_cache_purge(struct drm_device *dev)
237 struct vc4_dev *vc4 = to_vc4_dev(dev);
239 mutex_lock(&vc4->bo_lock);
240 while (!list_empty(&vc4->bo_cache.time_list)) {
241 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
242 struct vc4_bo, unref_head);
243 vc4_bo_remove_from_cache(bo);
246 mutex_unlock(&vc4->bo_lock);
249 void vc4_bo_add_to_purgeable_pool(struct vc4_bo *bo)
251 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
253 if (WARN_ON_ONCE(vc4->is_vc5))
256 mutex_lock(&vc4->purgeable.lock);
257 list_add_tail(&bo->size_head, &vc4->purgeable.list);
258 vc4->purgeable.num++;
259 vc4->purgeable.size += bo->base.base.size;
260 mutex_unlock(&vc4->purgeable.lock);
263 static void vc4_bo_remove_from_purgeable_pool_locked(struct vc4_bo *bo)
265 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
267 if (WARN_ON_ONCE(vc4->is_vc5))
270 /* list_del_init() is used here because the caller might release
271 * the purgeable lock in order to acquire the madv one and update the
273 * During this short period of time a user might decide to mark
274 * the BO as unpurgeable, and if bo->madv is set to
275 * VC4_MADV_DONTNEED it will try to remove the BO from the
276 * purgeable list which will fail if the ->next/prev fields
277 * are set to LIST_POISON1/LIST_POISON2 (which is what
279 * Re-initializing the list element guarantees that list_del()
280 * will work correctly even if it's a NOP.
282 list_del_init(&bo->size_head);
283 vc4->purgeable.num--;
284 vc4->purgeable.size -= bo->base.base.size;
287 void vc4_bo_remove_from_purgeable_pool(struct vc4_bo *bo)
289 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
291 mutex_lock(&vc4->purgeable.lock);
292 vc4_bo_remove_from_purgeable_pool_locked(bo);
293 mutex_unlock(&vc4->purgeable.lock);
296 static void vc4_bo_purge(struct drm_gem_object *obj)
298 struct vc4_bo *bo = to_vc4_bo(obj);
299 struct drm_device *dev = obj->dev;
301 WARN_ON(!mutex_is_locked(&bo->madv_lock));
302 WARN_ON(bo->madv != VC4_MADV_DONTNEED);
304 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
306 dma_free_wc(dev->dev, obj->size, bo->base.vaddr, bo->base.paddr);
307 bo->base.vaddr = NULL;
308 bo->madv = __VC4_MADV_PURGED;
311 static void vc4_bo_userspace_cache_purge(struct drm_device *dev)
313 struct vc4_dev *vc4 = to_vc4_dev(dev);
315 mutex_lock(&vc4->purgeable.lock);
316 while (!list_empty(&vc4->purgeable.list)) {
317 struct vc4_bo *bo = list_first_entry(&vc4->purgeable.list,
318 struct vc4_bo, size_head);
319 struct drm_gem_object *obj = &bo->base.base;
320 size_t purged_size = 0;
322 vc4_bo_remove_from_purgeable_pool_locked(bo);
324 /* Release the purgeable lock while we're purging the BO so
325 * that other people can continue inserting things in the
326 * purgeable pool without having to wait for all BOs to be
329 mutex_unlock(&vc4->purgeable.lock);
330 mutex_lock(&bo->madv_lock);
332 /* Since we released the purgeable pool lock before acquiring
333 * the BO madv one, the user may have marked the BO as WILLNEED
334 * and re-used it in the meantime.
335 * Before purging the BO we need to make sure
336 * - it is still marked as DONTNEED
337 * - it has not been re-inserted in the purgeable list
338 * - it is not used by HW blocks
339 * If one of these conditions is not met, just skip the entry.
341 if (bo->madv == VC4_MADV_DONTNEED &&
342 list_empty(&bo->size_head) &&
343 !refcount_read(&bo->usecnt)) {
344 purged_size = bo->base.base.size;
347 mutex_unlock(&bo->madv_lock);
348 mutex_lock(&vc4->purgeable.lock);
351 vc4->purgeable.purged_size += purged_size;
352 vc4->purgeable.purged_num++;
355 mutex_unlock(&vc4->purgeable.lock);
358 static struct vc4_bo *vc4_bo_get_from_cache(struct drm_device *dev,
360 enum vc4_kernel_bo_type type)
362 struct vc4_dev *vc4 = to_vc4_dev(dev);
363 uint32_t page_index = bo_page_index(size);
364 struct vc4_bo *bo = NULL;
366 mutex_lock(&vc4->bo_lock);
367 if (page_index >= vc4->bo_cache.size_list_size)
370 if (list_empty(&vc4->bo_cache.size_list[page_index]))
373 bo = list_first_entry(&vc4->bo_cache.size_list[page_index],
374 struct vc4_bo, size_head);
375 vc4_bo_remove_from_cache(bo);
376 kref_init(&bo->base.base.refcount);
380 vc4_bo_set_label(&bo->base.base, type);
381 mutex_unlock(&vc4->bo_lock);
386 * vc4_create_object - Implementation of driver->gem_create_object.
388 * @size: Size in bytes of the memory the object will reference
390 * This lets the CMA helpers allocate object structs for us, and keep
391 * our BO stats correct.
393 struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size)
395 struct vc4_dev *vc4 = to_vc4_dev(dev);
398 if (WARN_ON_ONCE(vc4->is_vc5))
399 return ERR_PTR(-ENODEV);
401 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
403 return ERR_PTR(-ENOMEM);
405 bo->madv = VC4_MADV_WILLNEED;
406 refcount_set(&bo->usecnt, 0);
407 mutex_init(&bo->madv_lock);
408 mutex_lock(&vc4->bo_lock);
409 bo->label = VC4_BO_TYPE_KERNEL;
410 vc4->bo_labels[VC4_BO_TYPE_KERNEL].num_allocated++;
411 vc4->bo_labels[VC4_BO_TYPE_KERNEL].size_allocated += size;
412 mutex_unlock(&vc4->bo_lock);
414 bo->base.base.funcs = &vc4_gem_object_funcs;
416 return &bo->base.base;
419 struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t unaligned_size,
420 bool allow_unzeroed, enum vc4_kernel_bo_type type)
422 size_t size = roundup(unaligned_size, PAGE_SIZE);
423 struct vc4_dev *vc4 = to_vc4_dev(dev);
424 struct drm_gem_cma_object *cma_obj;
427 if (WARN_ON_ONCE(vc4->is_vc5))
428 return ERR_PTR(-ENODEV);
431 return ERR_PTR(-EINVAL);
433 /* First, try to get a vc4_bo from the kernel BO cache. */
434 bo = vc4_bo_get_from_cache(dev, size, type);
437 memset(bo->base.vaddr, 0, bo->base.base.size);
441 cma_obj = drm_gem_cma_create(dev, size);
442 if (IS_ERR(cma_obj)) {
444 * If we've run out of CMA memory, kill the cache of
445 * CMA allocations we've got laying around and try again.
447 vc4_bo_cache_purge(dev);
448 cma_obj = drm_gem_cma_create(dev, size);
451 if (IS_ERR(cma_obj)) {
453 * Still not enough CMA memory, purge the userspace BO
455 * This is sub-optimal since we purge the whole userspace
456 * BO cache which forces user that want to re-use the BO to
457 * restore its initial content.
458 * Ideally, we should purge entries one by one and retry
459 * after each to see if CMA allocation succeeds. Or even
460 * better, try to find an entry with at least the same
463 vc4_bo_userspace_cache_purge(dev);
464 cma_obj = drm_gem_cma_create(dev, size);
467 if (IS_ERR(cma_obj)) {
468 struct drm_printer p = drm_info_printer(vc4->base.dev);
469 DRM_ERROR("Failed to allocate from CMA:\n");
470 vc4_bo_stats_print(&p, vc4);
471 return ERR_PTR(-ENOMEM);
473 bo = to_vc4_bo(&cma_obj->base);
475 /* By default, BOs do not support the MADV ioctl. This will be enabled
476 * only on BOs that are exposed to userspace (V3D, V3D_SHADER and DUMB
479 bo->madv = __VC4_MADV_NOTSUPP;
481 mutex_lock(&vc4->bo_lock);
482 vc4_bo_set_label(&cma_obj->base, type);
483 mutex_unlock(&vc4->bo_lock);
488 int vc4_bo_dumb_create(struct drm_file *file_priv,
489 struct drm_device *dev,
490 struct drm_mode_create_dumb *args)
492 struct vc4_dev *vc4 = to_vc4_dev(dev);
493 struct vc4_bo *bo = NULL;
496 if (WARN_ON_ONCE(vc4->is_vc5))
499 ret = vc4_dumb_fixup_args(args);
503 bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_DUMB);
507 bo->madv = VC4_MADV_WILLNEED;
509 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
510 drm_gem_object_put(&bo->base.base);
515 static void vc4_bo_cache_free_old(struct drm_device *dev)
517 struct vc4_dev *vc4 = to_vc4_dev(dev);
518 unsigned long expire_time = jiffies - msecs_to_jiffies(1000);
520 lockdep_assert_held(&vc4->bo_lock);
522 while (!list_empty(&vc4->bo_cache.time_list)) {
523 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
524 struct vc4_bo, unref_head);
525 if (time_before(expire_time, bo->free_time)) {
526 mod_timer(&vc4->bo_cache.time_timer,
527 round_jiffies_up(jiffies +
528 msecs_to_jiffies(1000)));
532 vc4_bo_remove_from_cache(bo);
537 /* Called on the last userspace/kernel unreference of the BO. Returns
538 * it to the BO cache if possible, otherwise frees it.
540 static void vc4_free_object(struct drm_gem_object *gem_bo)
542 struct drm_device *dev = gem_bo->dev;
543 struct vc4_dev *vc4 = to_vc4_dev(dev);
544 struct vc4_bo *bo = to_vc4_bo(gem_bo);
545 struct list_head *cache_list;
547 /* Remove the BO from the purgeable list. */
548 mutex_lock(&bo->madv_lock);
549 if (bo->madv == VC4_MADV_DONTNEED && !refcount_read(&bo->usecnt))
550 vc4_bo_remove_from_purgeable_pool(bo);
551 mutex_unlock(&bo->madv_lock);
553 mutex_lock(&vc4->bo_lock);
554 /* If the object references someone else's memory, we can't cache it.
556 if (gem_bo->import_attach) {
561 /* Don't cache if it was publicly named. */
567 /* If this object was partially constructed but CMA allocation
568 * had failed, just free it. Can also happen when the BO has been
571 if (!bo->base.vaddr) {
576 cache_list = vc4_get_cache_list_for_size(dev, gem_bo->size);
582 if (bo->validated_shader) {
583 kfree(bo->validated_shader->uniform_addr_offsets);
584 kfree(bo->validated_shader->texture_samples);
585 kfree(bo->validated_shader);
586 bo->validated_shader = NULL;
589 /* Reset madv and usecnt before adding the BO to the cache. */
590 bo->madv = __VC4_MADV_NOTSUPP;
591 refcount_set(&bo->usecnt, 0);
593 bo->t_format = false;
594 bo->free_time = jiffies;
595 list_add(&bo->size_head, cache_list);
596 list_add(&bo->unref_head, &vc4->bo_cache.time_list);
598 vc4_bo_set_label(&bo->base.base, VC4_BO_TYPE_KERNEL_CACHE);
600 vc4_bo_cache_free_old(dev);
603 mutex_unlock(&vc4->bo_lock);
606 static void vc4_bo_cache_time_work(struct work_struct *work)
608 struct vc4_dev *vc4 =
609 container_of(work, struct vc4_dev, bo_cache.time_work);
610 struct drm_device *dev = &vc4->base;
612 mutex_lock(&vc4->bo_lock);
613 vc4_bo_cache_free_old(dev);
614 mutex_unlock(&vc4->bo_lock);
617 int vc4_bo_inc_usecnt(struct vc4_bo *bo)
619 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
622 if (WARN_ON_ONCE(vc4->is_vc5))
625 /* Fast path: if the BO is already retained by someone, no need to
626 * check the madv status.
628 if (refcount_inc_not_zero(&bo->usecnt))
631 mutex_lock(&bo->madv_lock);
633 case VC4_MADV_WILLNEED:
634 if (!refcount_inc_not_zero(&bo->usecnt))
635 refcount_set(&bo->usecnt, 1);
638 case VC4_MADV_DONTNEED:
639 /* We shouldn't use a BO marked as purgeable if at least
640 * someone else retained its content by incrementing usecnt.
641 * Luckily the BO hasn't been purged yet, but something wrong
642 * is happening here. Just throw an error instead of
643 * authorizing this use case.
645 case __VC4_MADV_PURGED:
646 /* We can't use a purged BO. */
648 /* Invalid madv value. */
652 mutex_unlock(&bo->madv_lock);
657 void vc4_bo_dec_usecnt(struct vc4_bo *bo)
659 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
661 if (WARN_ON_ONCE(vc4->is_vc5))
664 /* Fast path: if the BO is still retained by someone, no need to test
667 if (refcount_dec_not_one(&bo->usecnt))
670 mutex_lock(&bo->madv_lock);
671 if (refcount_dec_and_test(&bo->usecnt) &&
672 bo->madv == VC4_MADV_DONTNEED)
673 vc4_bo_add_to_purgeable_pool(bo);
674 mutex_unlock(&bo->madv_lock);
677 static void vc4_bo_cache_time_timer(struct timer_list *t)
679 struct vc4_dev *vc4 = from_timer(vc4, t, bo_cache.time_timer);
681 schedule_work(&vc4->bo_cache.time_work);
684 static struct dma_buf *vc4_prime_export(struct drm_gem_object *obj, int flags)
686 struct vc4_bo *bo = to_vc4_bo(obj);
687 struct dma_buf *dmabuf;
690 if (bo->validated_shader) {
691 DRM_DEBUG("Attempting to export shader BO\n");
692 return ERR_PTR(-EINVAL);
695 /* Note: as soon as the BO is exported it becomes unpurgeable, because
696 * noone ever decrements the usecnt even if the reference held by the
697 * exported BO is released. This shouldn't be a problem since we don't
698 * expect exported BOs to be marked as purgeable.
700 ret = vc4_bo_inc_usecnt(bo);
702 DRM_ERROR("Failed to increment BO usecnt\n");
706 dmabuf = drm_gem_prime_export(obj, flags);
708 vc4_bo_dec_usecnt(bo);
713 static vm_fault_t vc4_fault(struct vm_fault *vmf)
715 struct vm_area_struct *vma = vmf->vma;
716 struct drm_gem_object *obj = vma->vm_private_data;
717 struct vc4_bo *bo = to_vc4_bo(obj);
719 /* The only reason we would end up here is when user-space accesses
720 * BO's memory after it's been purged.
722 mutex_lock(&bo->madv_lock);
723 WARN_ON(bo->madv != __VC4_MADV_PURGED);
724 mutex_unlock(&bo->madv_lock);
726 return VM_FAULT_SIGBUS;
729 static int vc4_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
731 struct vc4_bo *bo = to_vc4_bo(obj);
733 if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
734 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
738 if (bo->madv != VC4_MADV_WILLNEED) {
739 DRM_DEBUG("mmaping of %s BO not allowed\n",
740 bo->madv == VC4_MADV_DONTNEED ?
741 "purgeable" : "purged");
745 return drm_gem_cma_mmap(&bo->base, vma);
748 static const struct vm_operations_struct vc4_vm_ops = {
750 .open = drm_gem_vm_open,
751 .close = drm_gem_vm_close,
754 static const struct drm_gem_object_funcs vc4_gem_object_funcs = {
755 .free = vc4_free_object,
756 .export = vc4_prime_export,
757 .get_sg_table = drm_gem_cma_object_get_sg_table,
758 .vmap = drm_gem_cma_object_vmap,
759 .mmap = vc4_gem_object_mmap,
760 .vm_ops = &vc4_vm_ops,
763 static int vc4_grab_bin_bo(struct vc4_dev *vc4, struct vc4_file *vc4file)
768 if (vc4file->bin_bo_used)
771 return vc4_v3d_bin_bo_get(vc4, &vc4file->bin_bo_used);
774 int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
775 struct drm_file *file_priv)
777 struct drm_vc4_create_bo *args = data;
778 struct vc4_file *vc4file = file_priv->driver_priv;
779 struct vc4_dev *vc4 = to_vc4_dev(dev);
780 struct vc4_bo *bo = NULL;
783 if (WARN_ON_ONCE(vc4->is_vc5))
786 ret = vc4_grab_bin_bo(vc4, vc4file);
791 * We can't allocate from the BO cache, because the BOs don't
792 * get zeroed, and that might leak data between users.
794 bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_V3D);
798 bo->madv = VC4_MADV_WILLNEED;
800 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
801 drm_gem_object_put(&bo->base.base);
806 int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
807 struct drm_file *file_priv)
809 struct vc4_dev *vc4 = to_vc4_dev(dev);
810 struct drm_vc4_mmap_bo *args = data;
811 struct drm_gem_object *gem_obj;
813 if (WARN_ON_ONCE(vc4->is_vc5))
816 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
818 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
822 /* The mmap offset was set up at BO allocation time. */
823 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
825 drm_gem_object_put(gem_obj);
830 vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
831 struct drm_file *file_priv)
833 struct drm_vc4_create_shader_bo *args = data;
834 struct vc4_file *vc4file = file_priv->driver_priv;
835 struct vc4_dev *vc4 = to_vc4_dev(dev);
836 struct vc4_bo *bo = NULL;
839 if (WARN_ON_ONCE(vc4->is_vc5))
845 if (args->size % sizeof(u64) != 0)
848 if (args->flags != 0) {
849 DRM_INFO("Unknown flags set: 0x%08x\n", args->flags);
853 if (args->pad != 0) {
854 DRM_INFO("Pad set: 0x%08x\n", args->pad);
858 ret = vc4_grab_bin_bo(vc4, vc4file);
862 bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER);
866 bo->madv = VC4_MADV_WILLNEED;
868 if (copy_from_user(bo->base.vaddr,
869 (void __user *)(uintptr_t)args->data,
874 /* Clear the rest of the memory from allocating from the BO
877 memset(bo->base.vaddr + args->size, 0,
878 bo->base.base.size - args->size);
880 bo->validated_shader = vc4_validate_shader(&bo->base);
881 if (!bo->validated_shader) {
886 /* We have to create the handle after validation, to avoid
887 * races for users to do doing things like mmap the shader BO.
889 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
892 drm_gem_object_put(&bo->base.base);
898 * vc4_set_tiling_ioctl() - Sets the tiling modifier for a BO.
900 * @data: ioctl argument
901 * @file_priv: DRM file for this fd
903 * The tiling state of the BO decides the default modifier of an fb if
904 * no specific modifier was set by userspace, and the return value of
905 * vc4_get_tiling_ioctl() (so that userspace can treat a BO it
906 * received from dmabuf as the same tiling format as the producer
909 int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
910 struct drm_file *file_priv)
912 struct vc4_dev *vc4 = to_vc4_dev(dev);
913 struct drm_vc4_set_tiling *args = data;
914 struct drm_gem_object *gem_obj;
918 if (WARN_ON_ONCE(vc4->is_vc5))
921 if (args->flags != 0)
924 switch (args->modifier) {
925 case DRM_FORMAT_MOD_NONE:
928 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
935 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
937 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
940 bo = to_vc4_bo(gem_obj);
941 bo->t_format = t_format;
943 drm_gem_object_put(gem_obj);
949 * vc4_get_tiling_ioctl() - Gets the tiling modifier for a BO.
951 * @data: ioctl argument
952 * @file_priv: DRM file for this fd
954 * Returns the tiling modifier for a BO as set by vc4_set_tiling_ioctl().
956 int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
957 struct drm_file *file_priv)
959 struct vc4_dev *vc4 = to_vc4_dev(dev);
960 struct drm_vc4_get_tiling *args = data;
961 struct drm_gem_object *gem_obj;
964 if (WARN_ON_ONCE(vc4->is_vc5))
967 if (args->flags != 0 || args->modifier != 0)
970 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
972 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
975 bo = to_vc4_bo(gem_obj);
978 args->modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
980 args->modifier = DRM_FORMAT_MOD_NONE;
982 drm_gem_object_put(gem_obj);
987 static void vc4_bo_cache_destroy(struct drm_device *dev, void *unused);
988 int vc4_bo_cache_init(struct drm_device *dev)
990 struct vc4_dev *vc4 = to_vc4_dev(dev);
993 if (WARN_ON_ONCE(vc4->is_vc5))
996 /* Create the initial set of BO labels that the kernel will
997 * use. This lets us avoid a bunch of string reallocation in
998 * the kernel's draw and BO allocation paths.
1000 vc4->bo_labels = kcalloc(VC4_BO_TYPE_COUNT, sizeof(*vc4->bo_labels),
1002 if (!vc4->bo_labels)
1004 vc4->num_labels = VC4_BO_TYPE_COUNT;
1006 BUILD_BUG_ON(ARRAY_SIZE(bo_type_names) != VC4_BO_TYPE_COUNT);
1007 for (i = 0; i < VC4_BO_TYPE_COUNT; i++)
1008 vc4->bo_labels[i].name = bo_type_names[i];
1010 mutex_init(&vc4->bo_lock);
1012 vc4_debugfs_add_file(dev, "bo_stats", vc4_bo_stats_debugfs, NULL);
1014 INIT_LIST_HEAD(&vc4->bo_cache.time_list);
1016 INIT_WORK(&vc4->bo_cache.time_work, vc4_bo_cache_time_work);
1017 timer_setup(&vc4->bo_cache.time_timer, vc4_bo_cache_time_timer, 0);
1019 return drmm_add_action_or_reset(dev, vc4_bo_cache_destroy, NULL);
1022 static void vc4_bo_cache_destroy(struct drm_device *dev, void *unused)
1024 struct vc4_dev *vc4 = to_vc4_dev(dev);
1027 del_timer(&vc4->bo_cache.time_timer);
1028 cancel_work_sync(&vc4->bo_cache.time_work);
1030 vc4_bo_cache_purge(dev);
1032 for (i = 0; i < vc4->num_labels; i++) {
1033 if (vc4->bo_labels[i].num_allocated) {
1034 DRM_ERROR("Destroying BO cache with %d %s "
1035 "BOs still allocated\n",
1036 vc4->bo_labels[i].num_allocated,
1037 vc4->bo_labels[i].name);
1040 if (is_user_label(i))
1041 kfree(vc4->bo_labels[i].name);
1043 kfree(vc4->bo_labels);
1046 int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
1047 struct drm_file *file_priv)
1049 struct vc4_dev *vc4 = to_vc4_dev(dev);
1050 struct drm_vc4_label_bo *args = data;
1052 struct drm_gem_object *gem_obj;
1055 if (WARN_ON_ONCE(vc4->is_vc5))
1061 name = strndup_user(u64_to_user_ptr(args->name), args->len + 1);
1063 return PTR_ERR(name);
1065 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1067 DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
1072 mutex_lock(&vc4->bo_lock);
1073 label = vc4_get_user_label(vc4, name);
1075 vc4_bo_set_label(gem_obj, label);
1078 mutex_unlock(&vc4->bo_lock);
1080 drm_gem_object_put(gem_obj);