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