1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* exynos_drm_gem.c
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5  * Author: Inki Dae <inki.dae@samsung.com>
6  */
7 
8 
9 #include <linux/dma-buf.h>
10 #include <linux/pfn_t.h>
11 #include <linux/shmem_fs.h>
12 
13 #include <drm/drm_prime.h>
14 #include <drm/drm_vma_manager.h>
15 #include <drm/exynos_drm.h>
16 
17 #include "exynos_drm_drv.h"
18 #include "exynos_drm_gem.h"
19 
20 static int exynos_drm_alloc_buf(struct exynos_drm_gem *exynos_gem, bool kvmap)
21 {
22 	struct drm_device *dev = exynos_gem->base.dev;
23 	unsigned long attr = 0;
24 
25 	if (exynos_gem->dma_addr) {
26 		DRM_DEV_DEBUG_KMS(to_dma_dev(dev), "already allocated.\n");
27 		return 0;
28 	}
29 
30 	/*
31 	 * if EXYNOS_BO_CONTIG, fully physically contiguous memory
32 	 * region will be allocated else physically contiguous
33 	 * as possible.
34 	 */
35 	if (!(exynos_gem->flags & EXYNOS_BO_NONCONTIG))
36 		attr |= DMA_ATTR_FORCE_CONTIGUOUS;
37 
38 	/*
39 	 * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping
40 	 * else cachable mapping.
41 	 */
42 	if (exynos_gem->flags & EXYNOS_BO_WC ||
43 			!(exynos_gem->flags & EXYNOS_BO_CACHABLE))
44 		attr |= DMA_ATTR_WRITE_COMBINE;
45 
46 	/* FBDev emulation requires kernel mapping */
47 	if (!kvmap)
48 		attr |= DMA_ATTR_NO_KERNEL_MAPPING;
49 
50 	exynos_gem->dma_attrs = attr;
51 	exynos_gem->cookie = dma_alloc_attrs(to_dma_dev(dev), exynos_gem->size,
52 					     &exynos_gem->dma_addr, GFP_KERNEL,
53 					     exynos_gem->dma_attrs);
54 	if (!exynos_gem->cookie) {
55 		DRM_DEV_ERROR(to_dma_dev(dev), "failed to allocate buffer.\n");
56 		return -ENOMEM;
57 	}
58 
59 	if (kvmap)
60 		exynos_gem->kvaddr = exynos_gem->cookie;
61 
62 	DRM_DEV_DEBUG_KMS(to_dma_dev(dev), "dma_addr(0x%lx), size(0x%lx)\n",
63 			(unsigned long)exynos_gem->dma_addr, exynos_gem->size);
64 	return 0;
65 }
66 
67 static void exynos_drm_free_buf(struct exynos_drm_gem *exynos_gem)
68 {
69 	struct drm_device *dev = exynos_gem->base.dev;
70 
71 	if (!exynos_gem->dma_addr) {
72 		DRM_DEV_DEBUG_KMS(dev->dev, "dma_addr is invalid.\n");
73 		return;
74 	}
75 
76 	DRM_DEV_DEBUG_KMS(dev->dev, "dma_addr(0x%lx), size(0x%lx)\n",
77 			(unsigned long)exynos_gem->dma_addr, exynos_gem->size);
78 
79 	dma_free_attrs(to_dma_dev(dev), exynos_gem->size, exynos_gem->cookie,
80 			(dma_addr_t)exynos_gem->dma_addr,
81 			exynos_gem->dma_attrs);
82 }
83 
84 static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
85 					struct drm_file *file_priv,
86 					unsigned int *handle)
87 {
88 	int ret;
89 
90 	/*
91 	 * allocate a id of idr table where the obj is registered
92 	 * and handle has the id what user can see.
93 	 */
94 	ret = drm_gem_handle_create(file_priv, obj, handle);
95 	if (ret)
96 		return ret;
97 
98 	DRM_DEV_DEBUG_KMS(to_dma_dev(obj->dev), "gem handle = 0x%x\n", *handle);
99 
100 	/* drop reference from allocate - handle holds it now. */
101 	drm_gem_object_put(obj);
102 
103 	return 0;
104 }
105 
106 void exynos_drm_gem_destroy(struct exynos_drm_gem *exynos_gem)
107 {
108 	struct drm_gem_object *obj = &exynos_gem->base;
109 
110 	DRM_DEV_DEBUG_KMS(to_dma_dev(obj->dev), "handle count = %d\n",
111 			  obj->handle_count);
112 
113 	/*
114 	 * do not release memory region from exporter.
115 	 *
116 	 * the region will be released by exporter
117 	 * once dmabuf's refcount becomes 0.
118 	 */
119 	if (obj->import_attach)
120 		drm_prime_gem_destroy(obj, exynos_gem->sgt);
121 	else
122 		exynos_drm_free_buf(exynos_gem);
123 
124 	/* release file pointer to gem object. */
125 	drm_gem_object_release(obj);
126 
127 	kfree(exynos_gem);
128 }
129 
130 static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
131 	.open = drm_gem_vm_open,
132 	.close = drm_gem_vm_close,
133 };
134 
135 static const struct drm_gem_object_funcs exynos_drm_gem_object_funcs = {
136 	.free = exynos_drm_gem_free_object,
137 	.get_sg_table = exynos_drm_gem_prime_get_sg_table,
138 	.vm_ops = &exynos_drm_gem_vm_ops,
139 };
140 
141 static struct exynos_drm_gem *exynos_drm_gem_init(struct drm_device *dev,
142 						  unsigned long size)
143 {
144 	struct exynos_drm_gem *exynos_gem;
145 	struct drm_gem_object *obj;
146 	int ret;
147 
148 	exynos_gem = kzalloc(sizeof(*exynos_gem), GFP_KERNEL);
149 	if (!exynos_gem)
150 		return ERR_PTR(-ENOMEM);
151 
152 	exynos_gem->size = size;
153 	obj = &exynos_gem->base;
154 
155 	obj->funcs = &exynos_drm_gem_object_funcs;
156 
157 	ret = drm_gem_object_init(dev, obj, size);
158 	if (ret < 0) {
159 		DRM_DEV_ERROR(dev->dev, "failed to initialize gem object\n");
160 		kfree(exynos_gem);
161 		return ERR_PTR(ret);
162 	}
163 
164 	ret = drm_gem_create_mmap_offset(obj);
165 	if (ret < 0) {
166 		drm_gem_object_release(obj);
167 		kfree(exynos_gem);
168 		return ERR_PTR(ret);
169 	}
170 
171 	DRM_DEV_DEBUG_KMS(dev->dev, "created file object = %pK\n", obj->filp);
172 
173 	return exynos_gem;
174 }
175 
176 struct exynos_drm_gem *exynos_drm_gem_create(struct drm_device *dev,
177 					     unsigned int flags,
178 					     unsigned long size,
179 					     bool kvmap)
180 {
181 	struct exynos_drm_gem *exynos_gem;
182 	int ret;
183 
184 	if (flags & ~(EXYNOS_BO_MASK)) {
185 		DRM_DEV_ERROR(dev->dev,
186 			      "invalid GEM buffer flags: %u\n", flags);
187 		return ERR_PTR(-EINVAL);
188 	}
189 
190 	if (!size) {
191 		DRM_DEV_ERROR(dev->dev, "invalid GEM buffer size: %lu\n", size);
192 		return ERR_PTR(-EINVAL);
193 	}
194 
195 	size = roundup(size, PAGE_SIZE);
196 
197 	exynos_gem = exynos_drm_gem_init(dev, size);
198 	if (IS_ERR(exynos_gem))
199 		return exynos_gem;
200 
201 	if (!is_drm_iommu_supported(dev) && (flags & EXYNOS_BO_NONCONTIG)) {
202 		/*
203 		 * when no IOMMU is available, all allocated buffers are
204 		 * contiguous anyway, so drop EXYNOS_BO_NONCONTIG flag
205 		 */
206 		flags &= ~EXYNOS_BO_NONCONTIG;
207 		DRM_WARN("Non-contiguous allocation is not supported without IOMMU, falling back to contiguous buffer\n");
208 	}
209 
210 	/* set memory type and cache attribute from user side. */
211 	exynos_gem->flags = flags;
212 
213 	ret = exynos_drm_alloc_buf(exynos_gem, kvmap);
214 	if (ret < 0) {
215 		drm_gem_object_release(&exynos_gem->base);
216 		kfree(exynos_gem);
217 		return ERR_PTR(ret);
218 	}
219 
220 	return exynos_gem;
221 }
222 
223 int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
224 				struct drm_file *file_priv)
225 {
226 	struct drm_exynos_gem_create *args = data;
227 	struct exynos_drm_gem *exynos_gem;
228 	int ret;
229 
230 	exynos_gem = exynos_drm_gem_create(dev, args->flags, args->size, false);
231 	if (IS_ERR(exynos_gem))
232 		return PTR_ERR(exynos_gem);
233 
234 	ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv,
235 					   &args->handle);
236 	if (ret) {
237 		exynos_drm_gem_destroy(exynos_gem);
238 		return ret;
239 	}
240 
241 	return 0;
242 }
243 
244 int exynos_drm_gem_map_ioctl(struct drm_device *dev, void *data,
245 			     struct drm_file *file_priv)
246 {
247 	struct drm_exynos_gem_map *args = data;
248 
249 	return drm_gem_dumb_map_offset(file_priv, dev, args->handle,
250 				       &args->offset);
251 }
252 
253 struct exynos_drm_gem *exynos_drm_gem_get(struct drm_file *filp,
254 					  unsigned int gem_handle)
255 {
256 	struct drm_gem_object *obj;
257 
258 	obj = drm_gem_object_lookup(filp, gem_handle);
259 	if (!obj)
260 		return NULL;
261 	return to_exynos_gem(obj);
262 }
263 
264 static int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem *exynos_gem,
265 				      struct vm_area_struct *vma)
266 {
267 	struct drm_device *drm_dev = exynos_gem->base.dev;
268 	unsigned long vm_size;
269 	int ret;
270 
271 	vma->vm_flags &= ~VM_PFNMAP;
272 	vma->vm_pgoff = 0;
273 
274 	vm_size = vma->vm_end - vma->vm_start;
275 
276 	/* check if user-requested size is valid. */
277 	if (vm_size > exynos_gem->size)
278 		return -EINVAL;
279 
280 	ret = dma_mmap_attrs(to_dma_dev(drm_dev), vma, exynos_gem->cookie,
281 			     exynos_gem->dma_addr, exynos_gem->size,
282 			     exynos_gem->dma_attrs);
283 	if (ret < 0) {
284 		DRM_ERROR("failed to mmap.\n");
285 		return ret;
286 	}
287 
288 	return 0;
289 }
290 
291 int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data,
292 				      struct drm_file *file_priv)
293 {
294 	struct exynos_drm_gem *exynos_gem;
295 	struct drm_exynos_gem_info *args = data;
296 	struct drm_gem_object *obj;
297 
298 	obj = drm_gem_object_lookup(file_priv, args->handle);
299 	if (!obj) {
300 		DRM_DEV_ERROR(dev->dev, "failed to lookup gem object.\n");
301 		return -EINVAL;
302 	}
303 
304 	exynos_gem = to_exynos_gem(obj);
305 
306 	args->flags = exynos_gem->flags;
307 	args->size = exynos_gem->size;
308 
309 	drm_gem_object_put(obj);
310 
311 	return 0;
312 }
313 
314 void exynos_drm_gem_free_object(struct drm_gem_object *obj)
315 {
316 	exynos_drm_gem_destroy(to_exynos_gem(obj));
317 }
318 
319 int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
320 			       struct drm_device *dev,
321 			       struct drm_mode_create_dumb *args)
322 {
323 	struct exynos_drm_gem *exynos_gem;
324 	unsigned int flags;
325 	int ret;
326 
327 	/*
328 	 * allocate memory to be used for framebuffer.
329 	 * - this callback would be called by user application
330 	 *	with DRM_IOCTL_MODE_CREATE_DUMB command.
331 	 */
332 
333 	args->pitch = args->width * ((args->bpp + 7) / 8);
334 	args->size = args->pitch * args->height;
335 
336 	if (is_drm_iommu_supported(dev))
337 		flags = EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC;
338 	else
339 		flags = EXYNOS_BO_CONTIG | EXYNOS_BO_WC;
340 
341 	exynos_gem = exynos_drm_gem_create(dev, flags, args->size, false);
342 	if (IS_ERR(exynos_gem)) {
343 		dev_warn(dev->dev, "FB allocation failed.\n");
344 		return PTR_ERR(exynos_gem);
345 	}
346 
347 	ret = exynos_drm_gem_handle_create(&exynos_gem->base, file_priv,
348 					   &args->handle);
349 	if (ret) {
350 		exynos_drm_gem_destroy(exynos_gem);
351 		return ret;
352 	}
353 
354 	return 0;
355 }
356 
357 static int exynos_drm_gem_mmap_obj(struct drm_gem_object *obj,
358 				   struct vm_area_struct *vma)
359 {
360 	struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj);
361 	int ret;
362 
363 	DRM_DEV_DEBUG_KMS(to_dma_dev(obj->dev), "flags = 0x%x\n",
364 			  exynos_gem->flags);
365 
366 	/* non-cachable as default. */
367 	if (exynos_gem->flags & EXYNOS_BO_CACHABLE)
368 		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
369 	else if (exynos_gem->flags & EXYNOS_BO_WC)
370 		vma->vm_page_prot =
371 			pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
372 	else
373 		vma->vm_page_prot =
374 			pgprot_noncached(vm_get_page_prot(vma->vm_flags));
375 
376 	ret = exynos_drm_gem_mmap_buffer(exynos_gem, vma);
377 	if (ret)
378 		goto err_close_vm;
379 
380 	return ret;
381 
382 err_close_vm:
383 	drm_gem_vm_close(vma);
384 
385 	return ret;
386 }
387 
388 int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
389 {
390 	struct drm_gem_object *obj;
391 	int ret;
392 
393 	/* set vm_area_struct. */
394 	ret = drm_gem_mmap(filp, vma);
395 	if (ret < 0) {
396 		DRM_ERROR("failed to mmap.\n");
397 		return ret;
398 	}
399 
400 	obj = vma->vm_private_data;
401 
402 	if (obj->import_attach)
403 		return dma_buf_mmap(obj->dma_buf, vma, 0);
404 
405 	return exynos_drm_gem_mmap_obj(obj, vma);
406 }
407 
408 /* low-level interface prime helpers */
409 struct drm_gem_object *exynos_drm_gem_prime_import(struct drm_device *dev,
410 					    struct dma_buf *dma_buf)
411 {
412 	return drm_gem_prime_import_dev(dev, dma_buf, to_dma_dev(dev));
413 }
414 
415 struct sg_table *exynos_drm_gem_prime_get_sg_table(struct drm_gem_object *obj)
416 {
417 	struct exynos_drm_gem *exynos_gem = to_exynos_gem(obj);
418 	struct drm_device *drm_dev = obj->dev;
419 	struct sg_table *sgt;
420 	int ret;
421 
422 	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
423 	if (!sgt)
424 		return ERR_PTR(-ENOMEM);
425 
426 	ret = dma_get_sgtable_attrs(to_dma_dev(drm_dev), sgt, exynos_gem->cookie,
427 				    exynos_gem->dma_addr, exynos_gem->size,
428 				    exynos_gem->dma_attrs);
429 	if (ret) {
430 		DRM_ERROR("failed to get sgtable, %d\n", ret);
431 		kfree(sgt);
432 		return ERR_PTR(ret);
433 	}
434 
435 	return sgt;
436 }
437 
438 struct drm_gem_object *
439 exynos_drm_gem_prime_import_sg_table(struct drm_device *dev,
440 				     struct dma_buf_attachment *attach,
441 				     struct sg_table *sgt)
442 {
443 	struct exynos_drm_gem *exynos_gem;
444 
445 	/* check if the entries in the sg_table are contiguous */
446 	if (drm_prime_get_contiguous_size(sgt) < attach->dmabuf->size) {
447 		DRM_ERROR("buffer chunks must be mapped contiguously");
448 		return ERR_PTR(-EINVAL);
449 	}
450 
451 	exynos_gem = exynos_drm_gem_init(dev, attach->dmabuf->size);
452 	if (IS_ERR(exynos_gem))
453 		return ERR_CAST(exynos_gem);
454 
455 	/*
456 	 * Buffer has been mapped as contiguous into DMA address space,
457 	 * but if there is IOMMU, it can be either CONTIG or NONCONTIG.
458 	 * We assume a simplified logic below:
459 	 */
460 	if (is_drm_iommu_supported(dev))
461 		exynos_gem->flags |= EXYNOS_BO_NONCONTIG;
462 	else
463 		exynos_gem->flags |= EXYNOS_BO_CONTIG;
464 
465 	exynos_gem->dma_addr = sg_dma_address(sgt->sgl);
466 	exynos_gem->sgt = sgt;
467 	return &exynos_gem->base;
468 }
469 
470 int exynos_drm_gem_prime_mmap(struct drm_gem_object *obj,
471 			      struct vm_area_struct *vma)
472 {
473 	int ret;
474 
475 	ret = drm_gem_mmap_obj(obj, obj->size, vma);
476 	if (ret < 0)
477 		return ret;
478 
479 	return exynos_drm_gem_mmap_obj(obj, vma);
480 }
481