1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 MediaTek Inc.
4  */
5 
6 #include <linux/dma-buf.h>
7 
8 #include <drm/drm.h>
9 #include <drm/drm_device.h>
10 #include <drm/drm_gem.h>
11 #include <drm/drm_gem_dma_helper.h>
12 #include <drm/drm_prime.h>
13 
14 #include "mtk_drm_drv.h"
15 #include "mtk_drm_gem.h"
16 
17 static int mtk_drm_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
18 
19 static const struct vm_operations_struct vm_ops = {
20 	.open = drm_gem_vm_open,
21 	.close = drm_gem_vm_close,
22 };
23 
24 static const struct drm_gem_object_funcs mtk_drm_gem_object_funcs = {
25 	.free = mtk_drm_gem_free_object,
26 	.get_sg_table = mtk_gem_prime_get_sg_table,
27 	.vmap = mtk_drm_gem_prime_vmap,
28 	.vunmap = mtk_drm_gem_prime_vunmap,
29 	.mmap = mtk_drm_gem_object_mmap,
30 	.vm_ops = &vm_ops,
31 };
32 
33 static struct mtk_drm_gem_obj *mtk_drm_gem_init(struct drm_device *dev,
34 						unsigned long size)
35 {
36 	struct mtk_drm_gem_obj *mtk_gem_obj;
37 	int ret;
38 
39 	size = round_up(size, PAGE_SIZE);
40 
41 	if (size == 0)
42 		return ERR_PTR(-EINVAL);
43 
44 	mtk_gem_obj = kzalloc(sizeof(*mtk_gem_obj), GFP_KERNEL);
45 	if (!mtk_gem_obj)
46 		return ERR_PTR(-ENOMEM);
47 
48 	mtk_gem_obj->base.funcs = &mtk_drm_gem_object_funcs;
49 
50 	ret = drm_gem_object_init(dev, &mtk_gem_obj->base, size);
51 	if (ret < 0) {
52 		DRM_ERROR("failed to initialize gem object\n");
53 		kfree(mtk_gem_obj);
54 		return ERR_PTR(ret);
55 	}
56 
57 	return mtk_gem_obj;
58 }
59 
60 struct mtk_drm_gem_obj *mtk_drm_gem_create(struct drm_device *dev,
61 					   size_t size, bool alloc_kmap)
62 {
63 	struct mtk_drm_private *priv = dev->dev_private;
64 	struct mtk_drm_gem_obj *mtk_gem;
65 	struct drm_gem_object *obj;
66 	int ret;
67 
68 	mtk_gem = mtk_drm_gem_init(dev, size);
69 	if (IS_ERR(mtk_gem))
70 		return ERR_CAST(mtk_gem);
71 
72 	obj = &mtk_gem->base;
73 
74 	mtk_gem->dma_attrs = DMA_ATTR_WRITE_COMBINE;
75 
76 	if (!alloc_kmap)
77 		mtk_gem->dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
78 
79 	mtk_gem->cookie = dma_alloc_attrs(priv->dma_dev, obj->size,
80 					  &mtk_gem->dma_addr, GFP_KERNEL,
81 					  mtk_gem->dma_attrs);
82 	if (!mtk_gem->cookie) {
83 		DRM_ERROR("failed to allocate %zx byte dma buffer", obj->size);
84 		ret = -ENOMEM;
85 		goto err_gem_free;
86 	}
87 
88 	if (alloc_kmap)
89 		mtk_gem->kvaddr = mtk_gem->cookie;
90 
91 	DRM_DEBUG_DRIVER("cookie = %p dma_addr = %pad size = %zu\n",
92 			 mtk_gem->cookie, &mtk_gem->dma_addr,
93 			 size);
94 
95 	return mtk_gem;
96 
97 err_gem_free:
98 	drm_gem_object_release(obj);
99 	kfree(mtk_gem);
100 	return ERR_PTR(ret);
101 }
102 
103 void mtk_drm_gem_free_object(struct drm_gem_object *obj)
104 {
105 	struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
106 	struct mtk_drm_private *priv = obj->dev->dev_private;
107 
108 	if (mtk_gem->sg)
109 		drm_prime_gem_destroy(obj, mtk_gem->sg);
110 	else
111 		dma_free_attrs(priv->dma_dev, obj->size, mtk_gem->cookie,
112 			       mtk_gem->dma_addr, mtk_gem->dma_attrs);
113 
114 	/* release file pointer to gem object. */
115 	drm_gem_object_release(obj);
116 
117 	kfree(mtk_gem);
118 }
119 
120 int mtk_drm_gem_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
121 			    struct drm_mode_create_dumb *args)
122 {
123 	struct mtk_drm_gem_obj *mtk_gem;
124 	int ret;
125 
126 	args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
127 
128 	/*
129 	 * Multiply 2 variables of different types,
130 	 * for example: args->size = args->spacing * args->height;
131 	 * may cause coverity issue with unintentional overflow.
132 	 */
133 	args->size = args->pitch;
134 	args->size *= args->height;
135 
136 	mtk_gem = mtk_drm_gem_create(dev, args->size, false);
137 	if (IS_ERR(mtk_gem))
138 		return PTR_ERR(mtk_gem);
139 
140 	/*
141 	 * allocate a id of idr table where the obj is registered
142 	 * and handle has the id what user can see.
143 	 */
144 	ret = drm_gem_handle_create(file_priv, &mtk_gem->base, &args->handle);
145 	if (ret)
146 		goto err_handle_create;
147 
148 	/* drop reference from allocate - handle holds it now. */
149 	drm_gem_object_put(&mtk_gem->base);
150 
151 	return 0;
152 
153 err_handle_create:
154 	mtk_drm_gem_free_object(&mtk_gem->base);
155 	return ret;
156 }
157 
158 static int mtk_drm_gem_object_mmap(struct drm_gem_object *obj,
159 				   struct vm_area_struct *vma)
160 
161 {
162 	int ret;
163 	struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
164 	struct mtk_drm_private *priv = obj->dev->dev_private;
165 
166 	/*
167 	 * Set vm_pgoff (used as a fake buffer offset by DRM) to 0 and map the
168 	 * whole buffer from the start.
169 	 */
170 	vma->vm_pgoff = 0;
171 
172 	/*
173 	 * dma_alloc_attrs() allocated a struct page table for mtk_gem, so clear
174 	 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
175 	 */
176 	vm_flags_set(vma, VM_IO | VM_DONTEXPAND | VM_DONTDUMP);
177 	vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
178 	vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
179 
180 	ret = dma_mmap_attrs(priv->dma_dev, vma, mtk_gem->cookie,
181 			     mtk_gem->dma_addr, obj->size, mtk_gem->dma_attrs);
182 
183 	return ret;
184 }
185 
186 /*
187  * Allocate a sg_table for this GEM object.
188  * Note: Both the table's contents, and the sg_table itself must be freed by
189  *       the caller.
190  * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
191  */
192 struct sg_table *mtk_gem_prime_get_sg_table(struct drm_gem_object *obj)
193 {
194 	struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
195 	struct mtk_drm_private *priv = obj->dev->dev_private;
196 	struct sg_table *sgt;
197 	int ret;
198 
199 	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
200 	if (!sgt)
201 		return ERR_PTR(-ENOMEM);
202 
203 	ret = dma_get_sgtable_attrs(priv->dma_dev, sgt, mtk_gem->cookie,
204 				    mtk_gem->dma_addr, obj->size,
205 				    mtk_gem->dma_attrs);
206 	if (ret) {
207 		DRM_ERROR("failed to allocate sgt, %d\n", ret);
208 		kfree(sgt);
209 		return ERR_PTR(ret);
210 	}
211 
212 	return sgt;
213 }
214 
215 struct drm_gem_object *mtk_gem_prime_import_sg_table(struct drm_device *dev,
216 			struct dma_buf_attachment *attach, struct sg_table *sg)
217 {
218 	struct mtk_drm_gem_obj *mtk_gem;
219 
220 	/* check if the entries in the sg_table are contiguous */
221 	if (drm_prime_get_contiguous_size(sg) < attach->dmabuf->size) {
222 		DRM_ERROR("sg_table is not contiguous");
223 		return ERR_PTR(-EINVAL);
224 	}
225 
226 	mtk_gem = mtk_drm_gem_init(dev, attach->dmabuf->size);
227 	if (IS_ERR(mtk_gem))
228 		return ERR_CAST(mtk_gem);
229 
230 	mtk_gem->dma_addr = sg_dma_address(sg->sgl);
231 	mtk_gem->sg = sg;
232 
233 	return &mtk_gem->base;
234 }
235 
236 int mtk_drm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
237 {
238 	struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
239 	struct sg_table *sgt = NULL;
240 	unsigned int npages;
241 
242 	if (mtk_gem->kvaddr)
243 		goto out;
244 
245 	sgt = mtk_gem_prime_get_sg_table(obj);
246 	if (IS_ERR(sgt))
247 		return PTR_ERR(sgt);
248 
249 	npages = obj->size >> PAGE_SHIFT;
250 	mtk_gem->pages = kcalloc(npages, sizeof(*mtk_gem->pages), GFP_KERNEL);
251 	if (!mtk_gem->pages) {
252 		sg_free_table(sgt);
253 		kfree(sgt);
254 		return -ENOMEM;
255 	}
256 
257 	drm_prime_sg_to_page_array(sgt, mtk_gem->pages, npages);
258 
259 	mtk_gem->kvaddr = vmap(mtk_gem->pages, npages, VM_MAP,
260 			       pgprot_writecombine(PAGE_KERNEL));
261 	if (!mtk_gem->kvaddr) {
262 		sg_free_table(sgt);
263 		kfree(sgt);
264 		kfree(mtk_gem->pages);
265 		return -ENOMEM;
266 	}
267 	sg_free_table(sgt);
268 	kfree(sgt);
269 
270 out:
271 	iosys_map_set_vaddr(map, mtk_gem->kvaddr);
272 
273 	return 0;
274 }
275 
276 void mtk_drm_gem_prime_vunmap(struct drm_gem_object *obj,
277 			      struct iosys_map *map)
278 {
279 	struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
280 	void *vaddr = map->vaddr;
281 
282 	if (!mtk_gem->pages)
283 		return;
284 
285 	vunmap(vaddr);
286 	mtk_gem->kvaddr = NULL;
287 	kfree(mtk_gem->pages);
288 }
289