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