2 * Copyright © 2015 Broadcom
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.
10 * DOC: VC4 GEM BO management support
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.
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.
22 #include <linux/dma-buf.h>
25 #include "uapi/drm/vc4_drm.h"
27 static const char * const bo_type_names[] = {
38 static bool is_user_label(int label)
40 return label >= VC4_BO_TYPE_COUNT;
43 static void vc4_bo_stats_dump(struct vc4_dev *vc4)
47 for (i = 0; i < vc4->num_labels; i++) {
48 if (!vc4->bo_labels[i].num_allocated)
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);
58 #ifdef CONFIG_DEBUG_FS
59 int vc4_bo_stats_debugfs(struct seq_file *m, void *unused)
61 struct drm_info_node *node = (struct drm_info_node *)m->private;
62 struct drm_device *dev = node->minor->dev;
63 struct vc4_dev *vc4 = to_vc4_dev(dev);
66 mutex_lock(&vc4->bo_lock);
67 for (i = 0; i < vc4->num_labels; i++) {
68 if (!vc4->bo_labels[i].num_allocated)
71 seq_printf(m, "%30s: %6dkb BOs (%d)\n",
72 vc4->bo_labels[i].name,
73 vc4->bo_labels[i].size_allocated / 1024,
74 vc4->bo_labels[i].num_allocated);
76 mutex_unlock(&vc4->bo_lock);
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 reservation_object_fini(&bo->_resv);
184 drm_gem_cma_free_object(obj);
187 static void vc4_bo_remove_from_cache(struct vc4_bo *bo)
189 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
191 lockdep_assert_held(&vc4->bo_lock);
192 list_del(&bo->unref_head);
193 list_del(&bo->size_head);
196 static struct list_head *vc4_get_cache_list_for_size(struct drm_device *dev,
199 struct vc4_dev *vc4 = to_vc4_dev(dev);
200 uint32_t page_index = bo_page_index(size);
202 if (vc4->bo_cache.size_list_size <= page_index) {
203 uint32_t new_size = max(vc4->bo_cache.size_list_size * 2,
205 struct list_head *new_list;
208 new_list = kmalloc_array(new_size, sizeof(struct list_head),
213 /* Rebase the old cached BO lists to their new list
216 for (i = 0; i < vc4->bo_cache.size_list_size; i++) {
217 struct list_head *old_list =
218 &vc4->bo_cache.size_list[i];
220 if (list_empty(old_list))
221 INIT_LIST_HEAD(&new_list[i]);
223 list_replace(old_list, &new_list[i]);
225 /* And initialize the brand new BO list heads. */
226 for (i = vc4->bo_cache.size_list_size; i < new_size; i++)
227 INIT_LIST_HEAD(&new_list[i]);
229 kfree(vc4->bo_cache.size_list);
230 vc4->bo_cache.size_list = new_list;
231 vc4->bo_cache.size_list_size = new_size;
234 return &vc4->bo_cache.size_list[page_index];
237 static void vc4_bo_cache_purge(struct drm_device *dev)
239 struct vc4_dev *vc4 = to_vc4_dev(dev);
241 mutex_lock(&vc4->bo_lock);
242 while (!list_empty(&vc4->bo_cache.time_list)) {
243 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
244 struct vc4_bo, unref_head);
245 vc4_bo_remove_from_cache(bo);
248 mutex_unlock(&vc4->bo_lock);
251 static struct vc4_bo *vc4_bo_get_from_cache(struct drm_device *dev,
253 enum vc4_kernel_bo_type type)
255 struct vc4_dev *vc4 = to_vc4_dev(dev);
256 uint32_t page_index = bo_page_index(size);
257 struct vc4_bo *bo = NULL;
259 size = roundup(size, PAGE_SIZE);
261 mutex_lock(&vc4->bo_lock);
262 if (page_index >= vc4->bo_cache.size_list_size)
265 if (list_empty(&vc4->bo_cache.size_list[page_index]))
268 bo = list_first_entry(&vc4->bo_cache.size_list[page_index],
269 struct vc4_bo, size_head);
270 vc4_bo_remove_from_cache(bo);
271 kref_init(&bo->base.base.refcount);
275 vc4_bo_set_label(&bo->base.base, type);
276 mutex_unlock(&vc4->bo_lock);
281 * vc4_gem_create_object - Implementation of driver->gem_create_object.
283 * @size: Size in bytes of the memory the object will reference
285 * This lets the CMA helpers allocate object structs for us, and keep
286 * our BO stats correct.
288 struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size)
290 struct vc4_dev *vc4 = to_vc4_dev(dev);
293 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
297 mutex_lock(&vc4->bo_lock);
298 bo->label = VC4_BO_TYPE_KERNEL;
299 vc4->bo_labels[VC4_BO_TYPE_KERNEL].num_allocated++;
300 vc4->bo_labels[VC4_BO_TYPE_KERNEL].size_allocated += size;
301 mutex_unlock(&vc4->bo_lock);
302 bo->resv = &bo->_resv;
303 reservation_object_init(bo->resv);
305 return &bo->base.base;
308 struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t unaligned_size,
309 bool allow_unzeroed, enum vc4_kernel_bo_type type)
311 size_t size = roundup(unaligned_size, PAGE_SIZE);
312 struct vc4_dev *vc4 = to_vc4_dev(dev);
313 struct drm_gem_cma_object *cma_obj;
317 return ERR_PTR(-EINVAL);
319 /* First, try to get a vc4_bo from the kernel BO cache. */
320 bo = vc4_bo_get_from_cache(dev, size, type);
323 memset(bo->base.vaddr, 0, bo->base.base.size);
327 cma_obj = drm_gem_cma_create(dev, size);
328 if (IS_ERR(cma_obj)) {
330 * If we've run out of CMA memory, kill the cache of
331 * CMA allocations we've got laying around and try again.
333 vc4_bo_cache_purge(dev);
335 cma_obj = drm_gem_cma_create(dev, size);
336 if (IS_ERR(cma_obj)) {
337 DRM_ERROR("Failed to allocate from CMA:\n");
338 vc4_bo_stats_dump(vc4);
339 return ERR_PTR(-ENOMEM);
342 bo = to_vc4_bo(&cma_obj->base);
344 mutex_lock(&vc4->bo_lock);
345 vc4_bo_set_label(&cma_obj->base, type);
346 mutex_unlock(&vc4->bo_lock);
351 int vc4_dumb_create(struct drm_file *file_priv,
352 struct drm_device *dev,
353 struct drm_mode_create_dumb *args)
355 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
356 struct vc4_bo *bo = NULL;
359 if (args->pitch < min_pitch)
360 args->pitch = min_pitch;
362 if (args->size < args->pitch * args->height)
363 args->size = args->pitch * args->height;
365 bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_DUMB);
369 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
370 drm_gem_object_put_unlocked(&bo->base.base);
375 static void vc4_bo_cache_free_old(struct drm_device *dev)
377 struct vc4_dev *vc4 = to_vc4_dev(dev);
378 unsigned long expire_time = jiffies - msecs_to_jiffies(1000);
380 lockdep_assert_held(&vc4->bo_lock);
382 while (!list_empty(&vc4->bo_cache.time_list)) {
383 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
384 struct vc4_bo, unref_head);
385 if (time_before(expire_time, bo->free_time)) {
386 mod_timer(&vc4->bo_cache.time_timer,
387 round_jiffies_up(jiffies +
388 msecs_to_jiffies(1000)));
392 vc4_bo_remove_from_cache(bo);
397 /* Called on the last userspace/kernel unreference of the BO. Returns
398 * it to the BO cache if possible, otherwise frees it.
400 void vc4_free_object(struct drm_gem_object *gem_bo)
402 struct drm_device *dev = gem_bo->dev;
403 struct vc4_dev *vc4 = to_vc4_dev(dev);
404 struct vc4_bo *bo = to_vc4_bo(gem_bo);
405 struct list_head *cache_list;
407 mutex_lock(&vc4->bo_lock);
408 /* If the object references someone else's memory, we can't cache it.
410 if (gem_bo->import_attach) {
415 /* Don't cache if it was publicly named. */
421 /* If this object was partially constructed but CMA allocation
422 * had failed, just free it.
424 if (!bo->base.vaddr) {
429 cache_list = vc4_get_cache_list_for_size(dev, gem_bo->size);
435 if (bo->validated_shader) {
436 kfree(bo->validated_shader->uniform_addr_offsets);
437 kfree(bo->validated_shader->texture_samples);
438 kfree(bo->validated_shader);
439 bo->validated_shader = NULL;
442 bo->t_format = false;
443 bo->free_time = jiffies;
444 list_add(&bo->size_head, cache_list);
445 list_add(&bo->unref_head, &vc4->bo_cache.time_list);
447 vc4_bo_set_label(&bo->base.base, VC4_BO_TYPE_KERNEL_CACHE);
449 vc4_bo_cache_free_old(dev);
452 mutex_unlock(&vc4->bo_lock);
455 static void vc4_bo_cache_time_work(struct work_struct *work)
457 struct vc4_dev *vc4 =
458 container_of(work, struct vc4_dev, bo_cache.time_work);
459 struct drm_device *dev = vc4->dev;
461 mutex_lock(&vc4->bo_lock);
462 vc4_bo_cache_free_old(dev);
463 mutex_unlock(&vc4->bo_lock);
466 static void vc4_bo_cache_time_timer(unsigned long data)
468 struct drm_device *dev = (struct drm_device *)data;
469 struct vc4_dev *vc4 = to_vc4_dev(dev);
471 schedule_work(&vc4->bo_cache.time_work);
474 struct reservation_object *vc4_prime_res_obj(struct drm_gem_object *obj)
476 struct vc4_bo *bo = to_vc4_bo(obj);
482 vc4_prime_export(struct drm_device *dev, struct drm_gem_object *obj, int flags)
484 struct vc4_bo *bo = to_vc4_bo(obj);
486 if (bo->validated_shader) {
487 DRM_DEBUG("Attempting to export shader BO\n");
488 return ERR_PTR(-EINVAL);
491 return drm_gem_prime_export(dev, obj, flags);
494 int vc4_mmap(struct file *filp, struct vm_area_struct *vma)
496 struct drm_gem_object *gem_obj;
500 ret = drm_gem_mmap(filp, vma);
504 gem_obj = vma->vm_private_data;
505 bo = to_vc4_bo(gem_obj);
507 if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
508 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
513 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
514 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
517 vma->vm_flags &= ~VM_PFNMAP;
520 ret = dma_mmap_wc(bo->base.base.dev->dev, vma, bo->base.vaddr,
521 bo->base.paddr, vma->vm_end - vma->vm_start);
523 drm_gem_vm_close(vma);
528 int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
530 struct vc4_bo *bo = to_vc4_bo(obj);
532 if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
533 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
537 return drm_gem_cma_prime_mmap(obj, vma);
540 void *vc4_prime_vmap(struct drm_gem_object *obj)
542 struct vc4_bo *bo = to_vc4_bo(obj);
544 if (bo->validated_shader) {
545 DRM_DEBUG("mmaping of shader BOs not allowed.\n");
546 return ERR_PTR(-EINVAL);
549 return drm_gem_cma_prime_vmap(obj);
552 struct drm_gem_object *
553 vc4_prime_import_sg_table(struct drm_device *dev,
554 struct dma_buf_attachment *attach,
555 struct sg_table *sgt)
557 struct drm_gem_object *obj;
560 obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
565 bo->resv = attach->dmabuf->resv;
570 int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
571 struct drm_file *file_priv)
573 struct drm_vc4_create_bo *args = data;
574 struct vc4_bo *bo = NULL;
578 * We can't allocate from the BO cache, because the BOs don't
579 * get zeroed, and that might leak data between users.
581 bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_V3D);
585 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
586 drm_gem_object_put_unlocked(&bo->base.base);
591 int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
592 struct drm_file *file_priv)
594 struct drm_vc4_mmap_bo *args = data;
595 struct drm_gem_object *gem_obj;
597 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
599 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
603 /* The mmap offset was set up at BO allocation time. */
604 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
606 drm_gem_object_put_unlocked(gem_obj);
611 vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
612 struct drm_file *file_priv)
614 struct drm_vc4_create_shader_bo *args = data;
615 struct vc4_bo *bo = NULL;
621 if (args->size % sizeof(u64) != 0)
624 if (args->flags != 0) {
625 DRM_INFO("Unknown flags set: 0x%08x\n", args->flags);
629 if (args->pad != 0) {
630 DRM_INFO("Pad set: 0x%08x\n", args->pad);
634 bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER);
638 if (copy_from_user(bo->base.vaddr,
639 (void __user *)(uintptr_t)args->data,
644 /* Clear the rest of the memory from allocating from the BO
647 memset(bo->base.vaddr + args->size, 0,
648 bo->base.base.size - args->size);
650 bo->validated_shader = vc4_validate_shader(&bo->base);
651 if (!bo->validated_shader) {
656 /* We have to create the handle after validation, to avoid
657 * races for users to do doing things like mmap the shader BO.
659 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
662 drm_gem_object_put_unlocked(&bo->base.base);
668 * vc4_set_tiling_ioctl() - Sets the tiling modifier for a BO.
670 * @data: ioctl argument
671 * @file_priv: DRM file for this fd
673 * The tiling state of the BO decides the default modifier of an fb if
674 * no specific modifier was set by userspace, and the return value of
675 * vc4_get_tiling_ioctl() (so that userspace can treat a BO it
676 * received from dmabuf as the same tiling format as the producer
679 int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
680 struct drm_file *file_priv)
682 struct drm_vc4_set_tiling *args = data;
683 struct drm_gem_object *gem_obj;
687 if (args->flags != 0)
690 switch (args->modifier) {
691 case DRM_FORMAT_MOD_NONE:
694 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
701 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
703 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
706 bo = to_vc4_bo(gem_obj);
707 bo->t_format = t_format;
709 drm_gem_object_put_unlocked(gem_obj);
715 * vc4_get_tiling_ioctl() - Gets the tiling modifier for a BO.
717 * @data: ioctl argument
718 * @file_priv: DRM file for this fd
720 * Returns the tiling modifier for a BO as set by vc4_set_tiling_ioctl().
722 int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
723 struct drm_file *file_priv)
725 struct drm_vc4_get_tiling *args = data;
726 struct drm_gem_object *gem_obj;
729 if (args->flags != 0 || args->modifier != 0)
732 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
734 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
737 bo = to_vc4_bo(gem_obj);
740 args->modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
742 args->modifier = DRM_FORMAT_MOD_NONE;
744 drm_gem_object_put_unlocked(gem_obj);
749 int vc4_bo_cache_init(struct drm_device *dev)
751 struct vc4_dev *vc4 = to_vc4_dev(dev);
754 /* Create the initial set of BO labels that the kernel will
755 * use. This lets us avoid a bunch of string reallocation in
756 * the kernel's draw and BO allocation paths.
758 vc4->bo_labels = kcalloc(VC4_BO_TYPE_COUNT, sizeof(*vc4->bo_labels),
762 vc4->num_labels = VC4_BO_TYPE_COUNT;
764 BUILD_BUG_ON(ARRAY_SIZE(bo_type_names) != VC4_BO_TYPE_COUNT);
765 for (i = 0; i < VC4_BO_TYPE_COUNT; i++)
766 vc4->bo_labels[i].name = bo_type_names[i];
768 mutex_init(&vc4->bo_lock);
770 INIT_LIST_HEAD(&vc4->bo_cache.time_list);
772 INIT_WORK(&vc4->bo_cache.time_work, vc4_bo_cache_time_work);
773 setup_timer(&vc4->bo_cache.time_timer,
774 vc4_bo_cache_time_timer,
780 void vc4_bo_cache_destroy(struct drm_device *dev)
782 struct vc4_dev *vc4 = to_vc4_dev(dev);
785 del_timer(&vc4->bo_cache.time_timer);
786 cancel_work_sync(&vc4->bo_cache.time_work);
788 vc4_bo_cache_purge(dev);
790 for (i = 0; i < vc4->num_labels; i++) {
791 if (vc4->bo_labels[i].num_allocated) {
792 DRM_ERROR("Destroying BO cache with %d %s "
793 "BOs still allocated\n",
794 vc4->bo_labels[i].num_allocated,
795 vc4->bo_labels[i].name);
798 if (is_user_label(i))
799 kfree(vc4->bo_labels[i].name);
801 kfree(vc4->bo_labels);
804 int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
805 struct drm_file *file_priv)
807 struct vc4_dev *vc4 = to_vc4_dev(dev);
808 struct drm_vc4_label_bo *args = data;
810 struct drm_gem_object *gem_obj;
816 name = strndup_user(u64_to_user_ptr(args->name), args->len + 1);
818 return PTR_ERR(name);
820 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
822 DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
827 mutex_lock(&vc4->bo_lock);
828 label = vc4_get_user_label(vc4, name);
830 vc4_bo_set_label(gem_obj, label);
833 mutex_unlock(&vc4->bo_lock);
835 drm_gem_object_put_unlocked(gem_obj);