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