1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
5 #include <linux/slab.h>
6 #include <linux/dma-buf.h>
7 #include <linux/dma-mapping.h>
9 #include <drm/panfrost_drm.h>
10 #include "panfrost_device.h"
11 #include "panfrost_gem.h"
12 #include "panfrost_mmu.h"
14 /* Called DRM core on the last userspace/kernel unreference of the
17 static void panfrost_gem_free_object(struct drm_gem_object *obj)
19 struct panfrost_gem_object *bo = to_panfrost_bo(obj);
20 struct panfrost_device *pfdev = obj->dev->dev_private;
24 int n_sgt = bo->base.base.size / SZ_2M;
26 for (i = 0; i < n_sgt; i++) {
27 if (bo->sgts[i].sgl) {
28 dma_unmap_sg(pfdev->dev, bo->sgts[i].sgl,
29 bo->sgts[i].nents, DMA_BIDIRECTIONAL);
30 sg_free_table(&bo->sgts[i]);
36 mutex_lock(&pfdev->shrinker_lock);
37 if (!list_empty(&bo->base.madv_list))
38 list_del(&bo->base.madv_list);
39 mutex_unlock(&pfdev->shrinker_lock);
41 drm_gem_shmem_free_object(obj);
44 static int panfrost_gem_open(struct drm_gem_object *obj, struct drm_file *file_priv)
47 size_t size = obj->size;
49 struct panfrost_gem_object *bo = to_panfrost_bo(obj);
50 unsigned long color = bo->noexec ? PANFROST_BO_NOEXEC : 0;
51 struct panfrost_file_priv *priv = file_priv->driver_priv;
54 * Executable buffers cannot cross a 16MB boundary as the program
55 * counter is 24-bits. We assume executable buffers will be less than
56 * 16MB and aligning executable buffers to their size will avoid
57 * crossing a 16MB boundary.
60 align = size >> PAGE_SHIFT;
62 align = size >= SZ_2M ? SZ_2M >> PAGE_SHIFT : 0;
65 spin_lock(&priv->mm_lock);
66 ret = drm_mm_insert_node_generic(&priv->mm, &bo->node,
67 size >> PAGE_SHIFT, align, color, 0);
68 spin_unlock(&priv->mm_lock);
73 ret = panfrost_mmu_map(bo);
75 spin_lock(&priv->mm_lock);
76 drm_mm_remove_node(&bo->node);
77 spin_unlock(&priv->mm_lock);
83 static void panfrost_gem_close(struct drm_gem_object *obj, struct drm_file *file_priv)
85 struct panfrost_gem_object *bo = to_panfrost_bo(obj);
86 struct panfrost_file_priv *priv = file_priv->driver_priv;
89 panfrost_mmu_unmap(bo);
91 spin_lock(&priv->mm_lock);
92 if (drm_mm_node_allocated(&bo->node))
93 drm_mm_remove_node(&bo->node);
94 spin_unlock(&priv->mm_lock);
97 static int panfrost_gem_pin(struct drm_gem_object *obj)
99 if (to_panfrost_bo(obj)->is_heap)
102 return drm_gem_shmem_pin(obj);
105 static const struct drm_gem_object_funcs panfrost_gem_funcs = {
106 .free = panfrost_gem_free_object,
107 .open = panfrost_gem_open,
108 .close = panfrost_gem_close,
109 .print_info = drm_gem_shmem_print_info,
110 .pin = panfrost_gem_pin,
111 .unpin = drm_gem_shmem_unpin,
112 .get_sg_table = drm_gem_shmem_get_sg_table,
113 .vmap = drm_gem_shmem_vmap,
114 .vunmap = drm_gem_shmem_vunmap,
115 .mmap = drm_gem_shmem_mmap,
119 * panfrost_gem_create_object - Implementation of driver->gem_create_object.
121 * @size: Size in bytes of the memory the object will reference
123 * This lets the GEM helpers allocate object structs for us, and keep
124 * our BO stats correct.
126 struct drm_gem_object *panfrost_gem_create_object(struct drm_device *dev, size_t size)
128 struct panfrost_gem_object *obj;
130 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
134 obj->base.base.funcs = &panfrost_gem_funcs;
136 return &obj->base.base;
139 struct panfrost_gem_object *
140 panfrost_gem_create_with_handle(struct drm_file *file_priv,
141 struct drm_device *dev, size_t size,
146 struct drm_gem_shmem_object *shmem;
147 struct panfrost_gem_object *bo;
149 /* Round up heap allocations to 2MB to keep fault handling simple */
150 if (flags & PANFROST_BO_HEAP)
151 size = roundup(size, SZ_2M);
153 shmem = drm_gem_shmem_create(dev, size);
155 return ERR_CAST(shmem);
157 bo = to_panfrost_bo(&shmem->base);
158 bo->noexec = !!(flags & PANFROST_BO_NOEXEC);
159 bo->is_heap = !!(flags & PANFROST_BO_HEAP);
162 * Allocate an id of idr table where the obj is registered
163 * and handle has the id what user can see.
165 ret = drm_gem_handle_create(file_priv, &shmem->base, handle);
166 /* drop reference from allocate - handle holds it now. */
167 drm_gem_object_put_unlocked(&shmem->base);
174 struct drm_gem_object *
175 panfrost_gem_prime_import_sg_table(struct drm_device *dev,
176 struct dma_buf_attachment *attach,
177 struct sg_table *sgt)
179 struct drm_gem_object *obj;
180 struct panfrost_gem_object *bo;
182 obj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt);
184 return ERR_CAST(obj);
186 bo = to_panfrost_bo(obj);