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->texture_samples);
177 kfree(bo->validated_shader);
178 bo->validated_shader = NULL;
181 reservation_object_fini(&bo->_resv);
183 drm_gem_cma_free_object(obj);
186 static void vc4_bo_remove_from_cache(struct vc4_bo *bo)
188 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
190 lockdep_assert_held(&vc4->bo_lock);
191 list_del(&bo->unref_head);
192 list_del(&bo->size_head);
195 static struct list_head *vc4_get_cache_list_for_size(struct drm_device *dev,
198 struct vc4_dev *vc4 = to_vc4_dev(dev);
199 uint32_t page_index = bo_page_index(size);
201 if (vc4->bo_cache.size_list_size <= page_index) {
202 uint32_t new_size = max(vc4->bo_cache.size_list_size * 2,
204 struct list_head *new_list;
207 new_list = kmalloc_array(new_size, sizeof(struct list_head),
212 /* Rebase the old cached BO lists to their new list
215 for (i = 0; i < vc4->bo_cache.size_list_size; i++) {
216 struct list_head *old_list =
217 &vc4->bo_cache.size_list[i];
219 if (list_empty(old_list))
220 INIT_LIST_HEAD(&new_list[i]);
222 list_replace(old_list, &new_list[i]);
224 /* And initialize the brand new BO list heads. */
225 for (i = vc4->bo_cache.size_list_size; i < new_size; i++)
226 INIT_LIST_HEAD(&new_list[i]);
228 kfree(vc4->bo_cache.size_list);
229 vc4->bo_cache.size_list = new_list;
230 vc4->bo_cache.size_list_size = new_size;
233 return &vc4->bo_cache.size_list[page_index];
236 static void vc4_bo_cache_purge(struct drm_device *dev)
238 struct vc4_dev *vc4 = to_vc4_dev(dev);
240 mutex_lock(&vc4->bo_lock);
241 while (!list_empty(&vc4->bo_cache.time_list)) {
242 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
243 struct vc4_bo, unref_head);
244 vc4_bo_remove_from_cache(bo);
247 mutex_unlock(&vc4->bo_lock);
250 static struct vc4_bo *vc4_bo_get_from_cache(struct drm_device *dev,
252 enum vc4_kernel_bo_type type)
254 struct vc4_dev *vc4 = to_vc4_dev(dev);
255 uint32_t page_index = bo_page_index(size);
256 struct vc4_bo *bo = NULL;
258 size = roundup(size, PAGE_SIZE);
260 mutex_lock(&vc4->bo_lock);
261 if (page_index >= vc4->bo_cache.size_list_size)
264 if (list_empty(&vc4->bo_cache.size_list[page_index]))
267 bo = list_first_entry(&vc4->bo_cache.size_list[page_index],
268 struct vc4_bo, size_head);
269 vc4_bo_remove_from_cache(bo);
270 kref_init(&bo->base.base.refcount);
274 vc4_bo_set_label(&bo->base.base, type);
275 mutex_unlock(&vc4->bo_lock);
280 * vc4_gem_create_object - Implementation of driver->gem_create_object.
282 * @size: Size in bytes of the memory the object will reference
284 * This lets the CMA helpers allocate object structs for us, and keep
285 * our BO stats correct.
287 struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size)
289 struct vc4_dev *vc4 = to_vc4_dev(dev);
292 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
294 return ERR_PTR(-ENOMEM);
296 mutex_lock(&vc4->bo_lock);
297 bo->label = VC4_BO_TYPE_KERNEL;
298 vc4->bo_labels[VC4_BO_TYPE_KERNEL].num_allocated++;
299 vc4->bo_labels[VC4_BO_TYPE_KERNEL].size_allocated += size;
300 mutex_unlock(&vc4->bo_lock);
301 bo->resv = &bo->_resv;
302 reservation_object_init(bo->resv);
304 return &bo->base.base;
307 struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t unaligned_size,
308 bool allow_unzeroed, enum vc4_kernel_bo_type type)
310 size_t size = roundup(unaligned_size, PAGE_SIZE);
311 struct vc4_dev *vc4 = to_vc4_dev(dev);
312 struct drm_gem_cma_object *cma_obj;
316 return ERR_PTR(-EINVAL);
318 /* First, try to get a vc4_bo from the kernel BO cache. */
319 bo = vc4_bo_get_from_cache(dev, size, type);
322 memset(bo->base.vaddr, 0, bo->base.base.size);
326 cma_obj = drm_gem_cma_create(dev, size);
327 if (IS_ERR(cma_obj)) {
329 * If we've run out of CMA memory, kill the cache of
330 * CMA allocations we've got laying around and try again.
332 vc4_bo_cache_purge(dev);
334 cma_obj = drm_gem_cma_create(dev, size);
335 if (IS_ERR(cma_obj)) {
336 DRM_ERROR("Failed to allocate from CMA:\n");
337 vc4_bo_stats_dump(vc4);
338 return ERR_PTR(-ENOMEM);
341 bo = to_vc4_bo(&cma_obj->base);
343 mutex_lock(&vc4->bo_lock);
344 vc4_bo_set_label(&cma_obj->base, type);
345 mutex_unlock(&vc4->bo_lock);
350 int vc4_dumb_create(struct drm_file *file_priv,
351 struct drm_device *dev,
352 struct drm_mode_create_dumb *args)
354 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
355 struct vc4_bo *bo = NULL;
358 if (args->pitch < min_pitch)
359 args->pitch = min_pitch;
361 if (args->size < args->pitch * args->height)
362 args->size = args->pitch * args->height;
364 bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_DUMB);
368 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
369 drm_gem_object_put_unlocked(&bo->base.base);
374 static void vc4_bo_cache_free_old(struct drm_device *dev)
376 struct vc4_dev *vc4 = to_vc4_dev(dev);
377 unsigned long expire_time = jiffies - msecs_to_jiffies(1000);
379 lockdep_assert_held(&vc4->bo_lock);
381 while (!list_empty(&vc4->bo_cache.time_list)) {
382 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
383 struct vc4_bo, unref_head);
384 if (time_before(expire_time, bo->free_time)) {
385 mod_timer(&vc4->bo_cache.time_timer,
386 round_jiffies_up(jiffies +
387 msecs_to_jiffies(1000)));
391 vc4_bo_remove_from_cache(bo);
396 /* Called on the last userspace/kernel unreference of the BO. Returns
397 * it to the BO cache if possible, otherwise frees it.
399 void vc4_free_object(struct drm_gem_object *gem_bo)
401 struct drm_device *dev = gem_bo->dev;
402 struct vc4_dev *vc4 = to_vc4_dev(dev);
403 struct vc4_bo *bo = to_vc4_bo(gem_bo);
404 struct list_head *cache_list;
406 mutex_lock(&vc4->bo_lock);
407 /* If the object references someone else's memory, we can't cache it.
409 if (gem_bo->import_attach) {
414 /* Don't cache if it was publicly named. */
420 /* If this object was partially constructed but CMA allocation
421 * had failed, just free it.
423 if (!bo->base.vaddr) {
428 cache_list = vc4_get_cache_list_for_size(dev, gem_bo->size);
434 if (bo->validated_shader) {
435 kfree(bo->validated_shader->texture_samples);
436 kfree(bo->validated_shader);
437 bo->validated_shader = NULL;
440 bo->t_format = false;
441 bo->free_time = jiffies;
442 list_add(&bo->size_head, cache_list);
443 list_add(&bo->unref_head, &vc4->bo_cache.time_list);
445 vc4_bo_set_label(&bo->base.base, VC4_BO_TYPE_KERNEL_CACHE);
447 vc4_bo_cache_free_old(dev);
450 mutex_unlock(&vc4->bo_lock);
453 static void vc4_bo_cache_time_work(struct work_struct *work)
455 struct vc4_dev *vc4 =
456 container_of(work, struct vc4_dev, bo_cache.time_work);
457 struct drm_device *dev = vc4->dev;
459 mutex_lock(&vc4->bo_lock);
460 vc4_bo_cache_free_old(dev);
461 mutex_unlock(&vc4->bo_lock);
464 static void vc4_bo_cache_time_timer(unsigned long data)
466 struct drm_device *dev = (struct drm_device *)data;
467 struct vc4_dev *vc4 = to_vc4_dev(dev);
469 schedule_work(&vc4->bo_cache.time_work);
472 struct reservation_object *vc4_prime_res_obj(struct drm_gem_object *obj)
474 struct vc4_bo *bo = to_vc4_bo(obj);
480 vc4_prime_export(struct drm_device *dev, struct drm_gem_object *obj, int flags)
482 struct vc4_bo *bo = to_vc4_bo(obj);
484 if (bo->validated_shader) {
485 DRM_DEBUG("Attempting to export shader BO\n");
486 return ERR_PTR(-EINVAL);
489 return drm_gem_prime_export(dev, obj, flags);
492 int vc4_mmap(struct file *filp, struct vm_area_struct *vma)
494 struct drm_gem_object *gem_obj;
498 ret = drm_gem_mmap(filp, vma);
502 gem_obj = vma->vm_private_data;
503 bo = to_vc4_bo(gem_obj);
505 if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
506 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
511 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
512 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
515 vma->vm_flags &= ~VM_PFNMAP;
518 ret = dma_mmap_wc(bo->base.base.dev->dev, vma, bo->base.vaddr,
519 bo->base.paddr, vma->vm_end - vma->vm_start);
521 drm_gem_vm_close(vma);
526 int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
528 struct vc4_bo *bo = to_vc4_bo(obj);
530 if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
531 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
535 return drm_gem_cma_prime_mmap(obj, vma);
538 void *vc4_prime_vmap(struct drm_gem_object *obj)
540 struct vc4_bo *bo = to_vc4_bo(obj);
542 if (bo->validated_shader) {
543 DRM_DEBUG("mmaping of shader BOs not allowed.\n");
544 return ERR_PTR(-EINVAL);
547 return drm_gem_cma_prime_vmap(obj);
550 struct drm_gem_object *
551 vc4_prime_import_sg_table(struct drm_device *dev,
552 struct dma_buf_attachment *attach,
553 struct sg_table *sgt)
555 struct drm_gem_object *obj;
558 obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
563 bo->resv = attach->dmabuf->resv;
568 int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
569 struct drm_file *file_priv)
571 struct drm_vc4_create_bo *args = data;
572 struct vc4_bo *bo = NULL;
576 * We can't allocate from the BO cache, because the BOs don't
577 * get zeroed, and that might leak data between users.
579 bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_V3D);
583 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
584 drm_gem_object_put_unlocked(&bo->base.base);
589 int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
590 struct drm_file *file_priv)
592 struct drm_vc4_mmap_bo *args = data;
593 struct drm_gem_object *gem_obj;
595 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
597 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
601 /* The mmap offset was set up at BO allocation time. */
602 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
604 drm_gem_object_put_unlocked(gem_obj);
609 vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
610 struct drm_file *file_priv)
612 struct drm_vc4_create_shader_bo *args = data;
613 struct vc4_bo *bo = NULL;
619 if (args->size % sizeof(u64) != 0)
622 if (args->flags != 0) {
623 DRM_INFO("Unknown flags set: 0x%08x\n", args->flags);
627 if (args->pad != 0) {
628 DRM_INFO("Pad set: 0x%08x\n", args->pad);
632 bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER);
636 if (copy_from_user(bo->base.vaddr,
637 (void __user *)(uintptr_t)args->data,
642 /* Clear the rest of the memory from allocating from the BO
645 memset(bo->base.vaddr + args->size, 0,
646 bo->base.base.size - args->size);
648 bo->validated_shader = vc4_validate_shader(&bo->base);
649 if (!bo->validated_shader) {
654 /* We have to create the handle after validation, to avoid
655 * races for users to do doing things like mmap the shader BO.
657 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
660 drm_gem_object_put_unlocked(&bo->base.base);
666 * vc4_set_tiling_ioctl() - Sets the tiling modifier for a BO.
668 * @data: ioctl argument
669 * @file_priv: DRM file for this fd
671 * The tiling state of the BO decides the default modifier of an fb if
672 * no specific modifier was set by userspace, and the return value of
673 * vc4_get_tiling_ioctl() (so that userspace can treat a BO it
674 * received from dmabuf as the same tiling format as the producer
677 int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
678 struct drm_file *file_priv)
680 struct drm_vc4_set_tiling *args = data;
681 struct drm_gem_object *gem_obj;
685 if (args->flags != 0)
688 switch (args->modifier) {
689 case DRM_FORMAT_MOD_NONE:
692 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
699 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
701 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
704 bo = to_vc4_bo(gem_obj);
705 bo->t_format = t_format;
707 drm_gem_object_put_unlocked(gem_obj);
713 * vc4_get_tiling_ioctl() - Gets the tiling modifier for a BO.
715 * @data: ioctl argument
716 * @file_priv: DRM file for this fd
718 * Returns the tiling modifier for a BO as set by vc4_set_tiling_ioctl().
720 int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
721 struct drm_file *file_priv)
723 struct drm_vc4_get_tiling *args = data;
724 struct drm_gem_object *gem_obj;
727 if (args->flags != 0 || args->modifier != 0)
730 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
732 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
735 bo = to_vc4_bo(gem_obj);
738 args->modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
740 args->modifier = DRM_FORMAT_MOD_NONE;
742 drm_gem_object_put_unlocked(gem_obj);
747 int vc4_bo_cache_init(struct drm_device *dev)
749 struct vc4_dev *vc4 = to_vc4_dev(dev);
752 /* Create the initial set of BO labels that the kernel will
753 * use. This lets us avoid a bunch of string reallocation in
754 * the kernel's draw and BO allocation paths.
756 vc4->bo_labels = kcalloc(VC4_BO_TYPE_COUNT, sizeof(*vc4->bo_labels),
760 vc4->num_labels = VC4_BO_TYPE_COUNT;
762 BUILD_BUG_ON(ARRAY_SIZE(bo_type_names) != VC4_BO_TYPE_COUNT);
763 for (i = 0; i < VC4_BO_TYPE_COUNT; i++)
764 vc4->bo_labels[i].name = bo_type_names[i];
766 mutex_init(&vc4->bo_lock);
768 INIT_LIST_HEAD(&vc4->bo_cache.time_list);
770 INIT_WORK(&vc4->bo_cache.time_work, vc4_bo_cache_time_work);
771 setup_timer(&vc4->bo_cache.time_timer,
772 vc4_bo_cache_time_timer,
778 void vc4_bo_cache_destroy(struct drm_device *dev)
780 struct vc4_dev *vc4 = to_vc4_dev(dev);
783 del_timer(&vc4->bo_cache.time_timer);
784 cancel_work_sync(&vc4->bo_cache.time_work);
786 vc4_bo_cache_purge(dev);
788 for (i = 0; i < vc4->num_labels; i++) {
789 if (vc4->bo_labels[i].num_allocated) {
790 DRM_ERROR("Destroying BO cache with %d %s "
791 "BOs still allocated\n",
792 vc4->bo_labels[i].num_allocated,
793 vc4->bo_labels[i].name);
796 if (is_user_label(i))
797 kfree(vc4->bo_labels[i].name);
799 kfree(vc4->bo_labels);
802 int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
803 struct drm_file *file_priv)
805 struct vc4_dev *vc4 = to_vc4_dev(dev);
806 struct drm_vc4_label_bo *args = data;
808 struct drm_gem_object *gem_obj;
814 name = strndup_user(u64_to_user_ptr(args->name), args->len + 1);
816 return PTR_ERR(name);
818 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
820 DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
825 mutex_lock(&vc4->bo_lock);
826 label = vc4_get_user_label(vc4, name);
828 vc4_bo_set_label(gem_obj, label);
831 mutex_unlock(&vc4->bo_lock);
833 drm_gem_object_put_unlocked(gem_obj);