1 /*
2  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3  * Author:Mark Yao <mark.yao@rock-chips.com>
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 
15 #include <drm/drm.h>
16 #include <drm/drmP.h>
17 #include <drm/drm_gem.h>
18 #include <drm/drm_vma_manager.h>
19 
20 #include <linux/dma-attrs.h>
21 
22 #include "rockchip_drm_drv.h"
23 #include "rockchip_drm_gem.h"
24 
25 static int rockchip_gem_alloc_buf(struct rockchip_gem_object *rk_obj,
26 				  bool alloc_kmap)
27 {
28 	struct drm_gem_object *obj = &rk_obj->base;
29 	struct drm_device *drm = obj->dev;
30 
31 	init_dma_attrs(&rk_obj->dma_attrs);
32 	dma_set_attr(DMA_ATTR_WRITE_COMBINE, &rk_obj->dma_attrs);
33 
34 	if (!alloc_kmap)
35 		dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs);
36 
37 	rk_obj->kvaddr = dma_alloc_attrs(drm->dev, obj->size,
38 					 &rk_obj->dma_addr, GFP_KERNEL,
39 					 &rk_obj->dma_attrs);
40 	if (!rk_obj->kvaddr) {
41 		DRM_ERROR("failed to allocate %#x byte dma buffer", obj->size);
42 		return -ENOMEM;
43 	}
44 
45 	return 0;
46 }
47 
48 static void rockchip_gem_free_buf(struct rockchip_gem_object *rk_obj)
49 {
50 	struct drm_gem_object *obj = &rk_obj->base;
51 	struct drm_device *drm = obj->dev;
52 
53 	dma_free_attrs(drm->dev, obj->size, rk_obj->kvaddr, rk_obj->dma_addr,
54 		       &rk_obj->dma_attrs);
55 }
56 
57 int rockchip_gem_mmap_buf(struct drm_gem_object *obj,
58 			  struct vm_area_struct *vma)
59 {
60 	struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
61 	struct drm_device *drm = obj->dev;
62 	unsigned long vm_size;
63 
64 	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
65 	vm_size = vma->vm_end - vma->vm_start;
66 
67 	if (vm_size > obj->size)
68 		return -EINVAL;
69 
70 	return dma_mmap_attrs(drm->dev, vma, rk_obj->kvaddr, rk_obj->dma_addr,
71 			     obj->size, &rk_obj->dma_attrs);
72 }
73 
74 /* drm driver mmap file operations */
75 int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma)
76 {
77 	struct drm_file *priv = filp->private_data;
78 	struct drm_device *dev = priv->minor->dev;
79 	struct drm_gem_object *obj;
80 	struct drm_vma_offset_node *node;
81 	int ret;
82 
83 	if (drm_device_is_unplugged(dev))
84 		return -ENODEV;
85 
86 	mutex_lock(&dev->struct_mutex);
87 
88 	node = drm_vma_offset_exact_lookup(dev->vma_offset_manager,
89 					   vma->vm_pgoff,
90 					   vma_pages(vma));
91 	if (!node) {
92 		mutex_unlock(&dev->struct_mutex);
93 		DRM_ERROR("failed to find vma node.\n");
94 		return -EINVAL;
95 	} else if (!drm_vma_node_is_allowed(node, filp)) {
96 		mutex_unlock(&dev->struct_mutex);
97 		return -EACCES;
98 	}
99 
100 	obj = container_of(node, struct drm_gem_object, vma_node);
101 	ret = rockchip_gem_mmap_buf(obj, vma);
102 
103 	mutex_unlock(&dev->struct_mutex);
104 
105 	return ret;
106 }
107 
108 struct rockchip_gem_object *
109 	rockchip_gem_create_object(struct drm_device *drm, unsigned int size,
110 				   bool alloc_kmap)
111 {
112 	struct rockchip_gem_object *rk_obj;
113 	struct drm_gem_object *obj;
114 	int ret;
115 
116 	size = round_up(size, PAGE_SIZE);
117 
118 	rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
119 	if (!rk_obj)
120 		return ERR_PTR(-ENOMEM);
121 
122 	obj = &rk_obj->base;
123 
124 	drm_gem_private_object_init(drm, obj, size);
125 
126 	ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap);
127 	if (ret)
128 		goto err_free_rk_obj;
129 
130 	return rk_obj;
131 
132 err_free_rk_obj:
133 	kfree(rk_obj);
134 	return ERR_PTR(ret);
135 }
136 
137 /*
138  * rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback
139  * function
140  */
141 void rockchip_gem_free_object(struct drm_gem_object *obj)
142 {
143 	struct rockchip_gem_object *rk_obj;
144 
145 	drm_gem_free_mmap_offset(obj);
146 
147 	rk_obj = to_rockchip_obj(obj);
148 
149 	rockchip_gem_free_buf(rk_obj);
150 
151 	kfree(rk_obj);
152 }
153 
154 /*
155  * rockchip_gem_create_with_handle - allocate an object with the given
156  * size and create a gem handle on it
157  *
158  * returns a struct rockchip_gem_object* on success or ERR_PTR values
159  * on failure.
160  */
161 static struct rockchip_gem_object *
162 rockchip_gem_create_with_handle(struct drm_file *file_priv,
163 				struct drm_device *drm, unsigned int size,
164 				unsigned int *handle)
165 {
166 	struct rockchip_gem_object *rk_obj;
167 	struct drm_gem_object *obj;
168 	int ret;
169 
170 	rk_obj = rockchip_gem_create_object(drm, size, false);
171 	if (IS_ERR(rk_obj))
172 		return ERR_CAST(rk_obj);
173 
174 	obj = &rk_obj->base;
175 
176 	/*
177 	 * allocate a id of idr table where the obj is registered
178 	 * and handle has the id what user can see.
179 	 */
180 	ret = drm_gem_handle_create(file_priv, obj, handle);
181 	if (ret)
182 		goto err_handle_create;
183 
184 	/* drop reference from allocate - handle holds it now. */
185 	drm_gem_object_unreference_unlocked(obj);
186 
187 	return rk_obj;
188 
189 err_handle_create:
190 	rockchip_gem_free_object(obj);
191 
192 	return ERR_PTR(ret);
193 }
194 
195 int rockchip_gem_dumb_map_offset(struct drm_file *file_priv,
196 				 struct drm_device *dev, uint32_t handle,
197 				 uint64_t *offset)
198 {
199 	struct drm_gem_object *obj;
200 	int ret;
201 
202 	mutex_lock(&dev->struct_mutex);
203 
204 	obj = drm_gem_object_lookup(dev, file_priv, handle);
205 	if (!obj) {
206 		DRM_ERROR("failed to lookup gem object.\n");
207 		ret = -EINVAL;
208 		goto unlock;
209 	}
210 
211 	ret = drm_gem_create_mmap_offset(obj);
212 	if (ret)
213 		goto out;
214 
215 	*offset = drm_vma_node_offset_addr(&obj->vma_node);
216 	DRM_DEBUG_KMS("offset = 0x%llx\n", *offset);
217 
218 out:
219 	drm_gem_object_unreference(obj);
220 unlock:
221 	mutex_unlock(&dev->struct_mutex);
222 	return ret;
223 }
224 
225 /*
226  * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
227  * function
228  *
229  * This aligns the pitch and size arguments to the minimum required. wrap
230  * this into your own function if you need bigger alignment.
231  */
232 int rockchip_gem_dumb_create(struct drm_file *file_priv,
233 			     struct drm_device *dev,
234 			     struct drm_mode_create_dumb *args)
235 {
236 	struct rockchip_gem_object *rk_obj;
237 	int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
238 
239 	/*
240 	 * align to 64 bytes since Mali requires it.
241 	 */
242 	min_pitch = ALIGN(min_pitch, 64);
243 
244 	if (args->pitch < min_pitch)
245 		args->pitch = min_pitch;
246 
247 	if (args->size < args->pitch * args->height)
248 		args->size = args->pitch * args->height;
249 
250 	rk_obj = rockchip_gem_create_with_handle(file_priv, dev, args->size,
251 						 &args->handle);
252 
253 	return PTR_ERR_OR_ZERO(rk_obj);
254 }
255 
256 /*
257  * Allocate a sg_table for this GEM object.
258  * Note: Both the table's contents, and the sg_table itself must be freed by
259  *       the caller.
260  * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
261  */
262 struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj)
263 {
264 	struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
265 	struct drm_device *drm = obj->dev;
266 	struct sg_table *sgt;
267 	int ret;
268 
269 	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
270 	if (!sgt)
271 		return ERR_PTR(-ENOMEM);
272 
273 	ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr,
274 				    rk_obj->dma_addr, obj->size,
275 				    &rk_obj->dma_attrs);
276 	if (ret) {
277 		DRM_ERROR("failed to allocate sgt, %d\n", ret);
278 		kfree(sgt);
279 		return ERR_PTR(ret);
280 	}
281 
282 	return sgt;
283 }
284 
285 void *rockchip_gem_prime_vmap(struct drm_gem_object *obj)
286 {
287 	struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
288 
289 	if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs))
290 		return NULL;
291 
292 	return rk_obj->kvaddr;
293 }
294 
295 void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
296 {
297 	/* Nothing to do */
298 }
299