11c248b7dSInki Dae /* exynos_drm_gem.c
21c248b7dSInki Dae  *
31c248b7dSInki Dae  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
41c248b7dSInki Dae  * Author: Inki Dae <inki.dae@samsung.com>
51c248b7dSInki Dae  *
6d81aecb5SInki Dae  * This program is free software; you can redistribute  it and/or modify it
7d81aecb5SInki Dae  * under  the terms of  the GNU General  Public License as published by the
8d81aecb5SInki Dae  * Free Software Foundation;  either version 2 of the  License, or (at your
9d81aecb5SInki Dae  * option) any later version.
101c248b7dSInki Dae  */
111c248b7dSInki Dae 
12760285e7SDavid Howells #include <drm/drmP.h>
130de23977SDavid Herrmann #include <drm/drm_vma_manager.h>
141c248b7dSInki Dae 
152b35892eSInki Dae #include <linux/shmem_fs.h>
161c248b7dSInki Dae #include <drm/exynos_drm.h>
171c248b7dSInki Dae 
181c248b7dSInki Dae #include "exynos_drm_drv.h"
191c248b7dSInki Dae #include "exynos_drm_gem.h"
201c248b7dSInki Dae #include "exynos_drm_buf.h"
213fec4532SVikas Sajjan #include "exynos_drm_iommu.h"
221c248b7dSInki Dae 
231c248b7dSInki Dae static unsigned int convert_to_vm_err_msg(int msg)
241c248b7dSInki Dae {
251c248b7dSInki Dae 	unsigned int out_msg;
261c248b7dSInki Dae 
271c248b7dSInki Dae 	switch (msg) {
281c248b7dSInki Dae 	case 0:
291c248b7dSInki Dae 	case -ERESTARTSYS:
301c248b7dSInki Dae 	case -EINTR:
311c248b7dSInki Dae 		out_msg = VM_FAULT_NOPAGE;
321c248b7dSInki Dae 		break;
331c248b7dSInki Dae 
341c248b7dSInki Dae 	case -ENOMEM:
351c248b7dSInki Dae 		out_msg = VM_FAULT_OOM;
361c248b7dSInki Dae 		break;
371c248b7dSInki Dae 
381c248b7dSInki Dae 	default:
391c248b7dSInki Dae 		out_msg = VM_FAULT_SIGBUS;
401c248b7dSInki Dae 		break;
411c248b7dSInki Dae 	}
421c248b7dSInki Dae 
431c248b7dSInki Dae 	return out_msg;
441c248b7dSInki Dae }
451c248b7dSInki Dae 
46dcf9af82SInki Dae static int check_gem_flags(unsigned int flags)
472b35892eSInki Dae {
48dcf9af82SInki Dae 	if (flags & ~(EXYNOS_BO_MASK)) {
49dcf9af82SInki Dae 		DRM_ERROR("invalid flags.\n");
50dcf9af82SInki Dae 		return -EINVAL;
51dcf9af82SInki Dae 	}
52dcf9af82SInki Dae 
53dcf9af82SInki Dae 	return 0;
54dcf9af82SInki Dae }
55dcf9af82SInki Dae 
56c01d73faSInki Dae static void update_vm_cache_attr(struct exynos_drm_gem_obj *obj,
57c01d73faSInki Dae 					struct vm_area_struct *vma)
58c01d73faSInki Dae {
59c01d73faSInki Dae 	DRM_DEBUG_KMS("flags = 0x%x\n", obj->flags);
60c01d73faSInki Dae 
61c01d73faSInki Dae 	/* non-cachable as default. */
62c01d73faSInki Dae 	if (obj->flags & EXYNOS_BO_CACHABLE)
63c01d73faSInki Dae 		vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
64c01d73faSInki Dae 	else if (obj->flags & EXYNOS_BO_WC)
65c01d73faSInki Dae 		vma->vm_page_prot =
66c01d73faSInki Dae 			pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
67c01d73faSInki Dae 	else
68c01d73faSInki Dae 		vma->vm_page_prot =
69c01d73faSInki Dae 			pgprot_noncached(vm_get_page_prot(vma->vm_flags));
70c01d73faSInki Dae }
71c01d73faSInki Dae 
72dcf9af82SInki Dae static unsigned long roundup_gem_size(unsigned long size, unsigned int flags)
73dcf9af82SInki Dae {
740519f9a1SInki Dae 	/* TODO */
750519f9a1SInki Dae 
76dcf9af82SInki Dae 	return roundup(size, PAGE_SIZE);
772b35892eSInki Dae }
782b35892eSInki Dae 
790519f9a1SInki Dae static int exynos_drm_gem_map_buf(struct drm_gem_object *obj,
802b35892eSInki Dae 					struct vm_area_struct *vma,
812b35892eSInki Dae 					unsigned long f_vaddr,
822b35892eSInki Dae 					pgoff_t page_offset)
832b35892eSInki Dae {
842b35892eSInki Dae 	struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
852b35892eSInki Dae 	struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer;
862b35892eSInki Dae 	unsigned long pfn;
874ddc404bSRahul Sharma 
88465ed660SPrathyush K 	if (page_offset >= (buf->size >> PAGE_SHIFT)) {
89465ed660SPrathyush K 		DRM_ERROR("invalid page offset\n");
90465ed660SPrathyush K 		return -EINVAL;
91465ed660SPrathyush K 	}
92465ed660SPrathyush K 
938139951cSJoonyoung Shim 	pfn = page_to_pfn(buf->pages[page_offset]);
942b35892eSInki Dae 
952b35892eSInki Dae 	return vm_insert_mixed(vma, f_vaddr, pfn);
962b35892eSInki Dae }
972b35892eSInki Dae 
982364839aSJoonyoung Shim static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
992364839aSJoonyoung Shim 					struct drm_file *file_priv,
1002364839aSJoonyoung Shim 					unsigned int *handle)
1011c248b7dSInki Dae {
1021c248b7dSInki Dae 	int ret;
1031c248b7dSInki Dae 
1041c248b7dSInki Dae 	/*
1051c248b7dSInki Dae 	 * allocate a id of idr table where the obj is registered
1061c248b7dSInki Dae 	 * and handle has the id what user can see.
1071c248b7dSInki Dae 	 */
1081c248b7dSInki Dae 	ret = drm_gem_handle_create(file_priv, obj, handle);
1091c248b7dSInki Dae 	if (ret)
1102364839aSJoonyoung Shim 		return ret;
1111c248b7dSInki Dae 
1121c248b7dSInki Dae 	DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
1131c248b7dSInki Dae 
1141c248b7dSInki Dae 	/* drop reference from allocate - handle holds it now. */
1151c248b7dSInki Dae 	drm_gem_object_unreference_unlocked(obj);
1161c248b7dSInki Dae 
1172364839aSJoonyoung Shim 	return 0;
1182364839aSJoonyoung Shim }
1191c248b7dSInki Dae 
1202364839aSJoonyoung Shim void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj)
1212364839aSJoonyoung Shim {
1222364839aSJoonyoung Shim 	struct drm_gem_object *obj;
123c01d73faSInki Dae 	struct exynos_drm_gem_buf *buf;
1242364839aSJoonyoung Shim 
1252364839aSJoonyoung Shim 	obj = &exynos_gem_obj->base;
126c01d73faSInki Dae 	buf = exynos_gem_obj->buffer;
1272364839aSJoonyoung Shim 
128a8e11d1cSDaniel Vetter 	DRM_DEBUG_KMS("handle count = %d\n", obj->handle_count);
1292364839aSJoonyoung Shim 
130c374e731SInki Dae 	/*
131c374e731SInki Dae 	 * do not release memory region from exporter.
132c374e731SInki Dae 	 *
133c374e731SInki Dae 	 * the region will be released by exporter
134c374e731SInki Dae 	 * once dmabuf's refcount becomes 0.
135c374e731SInki Dae 	 */
136c374e731SInki Dae 	if (obj->import_attach)
137c374e731SInki Dae 		goto out;
138c374e731SInki Dae 
139c01d73faSInki Dae 	exynos_drm_free_buf(obj->dev, exynos_gem_obj->flags, buf);
1402b35892eSInki Dae 
141c374e731SInki Dae out:
142c01d73faSInki Dae 	exynos_drm_fini_buf(obj->dev, buf);
1432b35892eSInki Dae 	exynos_gem_obj->buffer = NULL;
1442364839aSJoonyoung Shim 
1452364839aSJoonyoung Shim 	drm_gem_free_mmap_offset(obj);
1462364839aSJoonyoung Shim 
1472364839aSJoonyoung Shim 	/* release file pointer to gem object. */
1481c248b7dSInki Dae 	drm_gem_object_release(obj);
1491c248b7dSInki Dae 
1501c248b7dSInki Dae 	kfree(exynos_gem_obj);
1512b35892eSInki Dae 	exynos_gem_obj = NULL;
1522364839aSJoonyoung Shim }
1532364839aSJoonyoung Shim 
154a4f19aaaSInki Dae unsigned long exynos_drm_gem_get_size(struct drm_device *dev,
155a4f19aaaSInki Dae 						unsigned int gem_handle,
156a4f19aaaSInki Dae 						struct drm_file *file_priv)
157a4f19aaaSInki Dae {
158a4f19aaaSInki Dae 	struct exynos_drm_gem_obj *exynos_gem_obj;
159a4f19aaaSInki Dae 	struct drm_gem_object *obj;
160a4f19aaaSInki Dae 
161a4f19aaaSInki Dae 	obj = drm_gem_object_lookup(dev, file_priv, gem_handle);
162a4f19aaaSInki Dae 	if (!obj) {
163a4f19aaaSInki Dae 		DRM_ERROR("failed to lookup gem object.\n");
164a4f19aaaSInki Dae 		return 0;
165a4f19aaaSInki Dae 	}
166a4f19aaaSInki Dae 
167a4f19aaaSInki Dae 	exynos_gem_obj = to_exynos_gem_obj(obj);
168a4f19aaaSInki Dae 
169a4f19aaaSInki Dae 	drm_gem_object_unreference_unlocked(obj);
170a4f19aaaSInki Dae 
171a4f19aaaSInki Dae 	return exynos_gem_obj->buffer->size;
172a4f19aaaSInki Dae }
173a4f19aaaSInki Dae 
174a4f19aaaSInki Dae 
175b2df26c1SInki Dae struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev,
1762364839aSJoonyoung Shim 						      unsigned long size)
1772364839aSJoonyoung Shim {
1782364839aSJoonyoung Shim 	struct exynos_drm_gem_obj *exynos_gem_obj;
1792364839aSJoonyoung Shim 	struct drm_gem_object *obj;
1802364839aSJoonyoung Shim 	int ret;
1812364839aSJoonyoung Shim 
1822364839aSJoonyoung Shim 	exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL);
18338bb5253SSachin Kamat 	if (!exynos_gem_obj)
1842364839aSJoonyoung Shim 		return NULL;
1852364839aSJoonyoung Shim 
1862b35892eSInki Dae 	exynos_gem_obj->size = size;
1872364839aSJoonyoung Shim 	obj = &exynos_gem_obj->base;
1882364839aSJoonyoung Shim 
1892364839aSJoonyoung Shim 	ret = drm_gem_object_init(dev, obj, size);
1902364839aSJoonyoung Shim 	if (ret < 0) {
1912364839aSJoonyoung Shim 		DRM_ERROR("failed to initialize gem object\n");
1922364839aSJoonyoung Shim 		kfree(exynos_gem_obj);
1932364839aSJoonyoung Shim 		return NULL;
1942364839aSJoonyoung Shim 	}
1952364839aSJoonyoung Shim 
1962364839aSJoonyoung Shim 	DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);
1972364839aSJoonyoung Shim 
1982364839aSJoonyoung Shim 	return exynos_gem_obj;
1991c248b7dSInki Dae }
2001c248b7dSInki Dae 
201f088d5a9SInki Dae struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
2022b35892eSInki Dae 						unsigned int flags,
203ee5e770eSJoonyoung Shim 						unsigned long size)
204f088d5a9SInki Dae {
2052364839aSJoonyoung Shim 	struct exynos_drm_gem_obj *exynos_gem_obj;
2062b35892eSInki Dae 	struct exynos_drm_gem_buf *buf;
2072b35892eSInki Dae 	int ret;
208f088d5a9SInki Dae 
209dcf9af82SInki Dae 	if (!size) {
210dcf9af82SInki Dae 		DRM_ERROR("invalid size.\n");
211dcf9af82SInki Dae 		return ERR_PTR(-EINVAL);
212dcf9af82SInki Dae 	}
213f088d5a9SInki Dae 
214dcf9af82SInki Dae 	size = roundup_gem_size(size, flags);
215dcf9af82SInki Dae 
216dcf9af82SInki Dae 	ret = check_gem_flags(flags);
217dcf9af82SInki Dae 	if (ret)
218dcf9af82SInki Dae 		return ERR_PTR(ret);
2192b35892eSInki Dae 
2202b35892eSInki Dae 	buf = exynos_drm_init_buf(dev, size);
2212b35892eSInki Dae 	if (!buf)
222ee5e770eSJoonyoung Shim 		return ERR_PTR(-ENOMEM);
223f088d5a9SInki Dae 
2242364839aSJoonyoung Shim 	exynos_gem_obj = exynos_drm_gem_init(dev, size);
2252364839aSJoonyoung Shim 	if (!exynos_gem_obj) {
2262b35892eSInki Dae 		ret = -ENOMEM;
227dcf9af82SInki Dae 		goto err_fini_buf;
228f088d5a9SInki Dae 	}
229f088d5a9SInki Dae 
2302b35892eSInki Dae 	exynos_gem_obj->buffer = buf;
2312b35892eSInki Dae 
2322b35892eSInki Dae 	/* set memory type and cache attribute from user side. */
2332b35892eSInki Dae 	exynos_gem_obj->flags = flags;
2342b35892eSInki Dae 
2352b35892eSInki Dae 	ret = exynos_drm_alloc_buf(dev, buf, flags);
236c58c1599SYoungJun Cho 	if (ret < 0)
237c58c1599SYoungJun Cho 		goto err_gem_fini;
238f088d5a9SInki Dae 
239f088d5a9SInki Dae 	return exynos_gem_obj;
240dcf9af82SInki Dae 
241c58c1599SYoungJun Cho err_gem_fini:
242c58c1599SYoungJun Cho 	drm_gem_object_release(&exynos_gem_obj->base);
243c58c1599SYoungJun Cho 	kfree(exynos_gem_obj);
244dcf9af82SInki Dae err_fini_buf:
2452b35892eSInki Dae 	exynos_drm_fini_buf(dev, buf);
2462b35892eSInki Dae 	return ERR_PTR(ret);
247f088d5a9SInki Dae }
248f088d5a9SInki Dae 
2491c248b7dSInki Dae int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
2501c248b7dSInki Dae 				struct drm_file *file_priv)
2511c248b7dSInki Dae {
2521c248b7dSInki Dae 	struct drm_exynos_gem_create *args = data;
253ee5e770eSJoonyoung Shim 	struct exynos_drm_gem_obj *exynos_gem_obj;
2542364839aSJoonyoung Shim 	int ret;
2551c248b7dSInki Dae 
2562b35892eSInki Dae 	exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size);
2571c248b7dSInki Dae 	if (IS_ERR(exynos_gem_obj))
2581c248b7dSInki Dae 		return PTR_ERR(exynos_gem_obj);
2591c248b7dSInki Dae 
2602364839aSJoonyoung Shim 	ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
2612364839aSJoonyoung Shim 			&args->handle);
2622364839aSJoonyoung Shim 	if (ret) {
2632364839aSJoonyoung Shim 		exynos_drm_gem_destroy(exynos_gem_obj);
2642364839aSJoonyoung Shim 		return ret;
2652364839aSJoonyoung Shim 	}
2662364839aSJoonyoung Shim 
2671c248b7dSInki Dae 	return 0;
2681c248b7dSInki Dae }
2691c248b7dSInki Dae 
270d87342c1SInki Dae dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev,
271f0b1bda7SInki Dae 					unsigned int gem_handle,
272d87342c1SInki Dae 					struct drm_file *filp)
273f0b1bda7SInki Dae {
274f0b1bda7SInki Dae 	struct exynos_drm_gem_obj *exynos_gem_obj;
275f0b1bda7SInki Dae 	struct drm_gem_object *obj;
276f0b1bda7SInki Dae 
277d87342c1SInki Dae 	obj = drm_gem_object_lookup(dev, filp, gem_handle);
278f0b1bda7SInki Dae 	if (!obj) {
279f0b1bda7SInki Dae 		DRM_ERROR("failed to lookup gem object.\n");
280f0b1bda7SInki Dae 		return ERR_PTR(-EINVAL);
281f0b1bda7SInki Dae 	}
282f0b1bda7SInki Dae 
283f0b1bda7SInki Dae 	exynos_gem_obj = to_exynos_gem_obj(obj);
284f0b1bda7SInki Dae 
285f0b1bda7SInki Dae 	return &exynos_gem_obj->buffer->dma_addr;
286f0b1bda7SInki Dae }
287f0b1bda7SInki Dae 
288f0b1bda7SInki Dae void exynos_drm_gem_put_dma_addr(struct drm_device *dev,
289f0b1bda7SInki Dae 					unsigned int gem_handle,
290d87342c1SInki Dae 					struct drm_file *filp)
291f0b1bda7SInki Dae {
292f0b1bda7SInki Dae 	struct drm_gem_object *obj;
293f0b1bda7SInki Dae 
294d87342c1SInki Dae 	obj = drm_gem_object_lookup(dev, filp, gem_handle);
295f0b1bda7SInki Dae 	if (!obj) {
296f0b1bda7SInki Dae 		DRM_ERROR("failed to lookup gem object.\n");
297f0b1bda7SInki Dae 		return;
298f0b1bda7SInki Dae 	}
299f0b1bda7SInki Dae 
300f0b1bda7SInki Dae 	drm_gem_object_unreference_unlocked(obj);
301f0b1bda7SInki Dae 
302f0b1bda7SInki Dae 	/*
303f0b1bda7SInki Dae 	 * decrease obj->refcount one more time because we has already
304f0b1bda7SInki Dae 	 * increased it at exynos_drm_gem_get_dma_addr().
305f0b1bda7SInki Dae 	 */
306f0b1bda7SInki Dae 	drm_gem_object_unreference_unlocked(obj);
307f0b1bda7SInki Dae }
308f0b1bda7SInki Dae 
309832316c7SInki Dae int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem_obj *exynos_gem_obj,
3101c248b7dSInki Dae 				      struct vm_area_struct *vma)
3111c248b7dSInki Dae {
312832316c7SInki Dae 	struct drm_device *drm_dev = exynos_gem_obj->base.dev;
3132c871127SInki Dae 	struct exynos_drm_gem_buf *buffer;
3140519f9a1SInki Dae 	unsigned long vm_size;
3155b07c660SInki Dae 	int ret;
3161c248b7dSInki Dae 
317832316c7SInki Dae 	vma->vm_flags &= ~VM_PFNMAP;
318832316c7SInki Dae 	vma->vm_pgoff = 0;
3191c248b7dSInki Dae 
3200519f9a1SInki Dae 	vm_size = vma->vm_end - vma->vm_start;
3212b35892eSInki Dae 
3221c248b7dSInki Dae 	/*
3232c871127SInki Dae 	 * a buffer contains information to physically continuous memory
3241c248b7dSInki Dae 	 * allocated by user request or at framebuffer creation.
3251c248b7dSInki Dae 	 */
3262c871127SInki Dae 	buffer = exynos_gem_obj->buffer;
3271c248b7dSInki Dae 
3281c248b7dSInki Dae 	/* check if user-requested size is valid. */
3292c871127SInki Dae 	if (vm_size > buffer->size)
3301c248b7dSInki Dae 		return -EINVAL;
3311c248b7dSInki Dae 
3324744ad24SInki Dae 	ret = dma_mmap_attrs(drm_dev->dev, vma, buffer->pages,
3330519f9a1SInki Dae 				buffer->dma_addr, buffer->size,
3340519f9a1SInki Dae 				&buffer->dma_attrs);
3355b07c660SInki Dae 	if (ret < 0) {
3365b07c660SInki Dae 		DRM_ERROR("failed to mmap.\n");
3375b07c660SInki Dae 		return ret;
3385b07c660SInki Dae 	}
3395b07c660SInki Dae 
3401c248b7dSInki Dae 	return 0;
3411c248b7dSInki Dae }
3421c248b7dSInki Dae 
34340cd7e0cSInki Dae int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data,
34440cd7e0cSInki Dae 				      struct drm_file *file_priv)
34540cd7e0cSInki Dae {	struct exynos_drm_gem_obj *exynos_gem_obj;
34640cd7e0cSInki Dae 	struct drm_exynos_gem_info *args = data;
34740cd7e0cSInki Dae 	struct drm_gem_object *obj;
34840cd7e0cSInki Dae 
34940cd7e0cSInki Dae 	mutex_lock(&dev->struct_mutex);
35040cd7e0cSInki Dae 
35140cd7e0cSInki Dae 	obj = drm_gem_object_lookup(dev, file_priv, args->handle);
35240cd7e0cSInki Dae 	if (!obj) {
35340cd7e0cSInki Dae 		DRM_ERROR("failed to lookup gem object.\n");
35440cd7e0cSInki Dae 		mutex_unlock(&dev->struct_mutex);
35540cd7e0cSInki Dae 		return -EINVAL;
35640cd7e0cSInki Dae 	}
35740cd7e0cSInki Dae 
35840cd7e0cSInki Dae 	exynos_gem_obj = to_exynos_gem_obj(obj);
35940cd7e0cSInki Dae 
36040cd7e0cSInki Dae 	args->flags = exynos_gem_obj->flags;
36140cd7e0cSInki Dae 	args->size = exynos_gem_obj->size;
36240cd7e0cSInki Dae 
36340cd7e0cSInki Dae 	drm_gem_object_unreference(obj);
36440cd7e0cSInki Dae 	mutex_unlock(&dev->struct_mutex);
36540cd7e0cSInki Dae 
36640cd7e0cSInki Dae 	return 0;
36740cd7e0cSInki Dae }
36840cd7e0cSInki Dae 
3692a3098ffSInki Dae struct vm_area_struct *exynos_gem_get_vma(struct vm_area_struct *vma)
3702a3098ffSInki Dae {
3712a3098ffSInki Dae 	struct vm_area_struct *vma_copy;
3722a3098ffSInki Dae 
3732a3098ffSInki Dae 	vma_copy = kmalloc(sizeof(*vma_copy), GFP_KERNEL);
3742a3098ffSInki Dae 	if (!vma_copy)
3752a3098ffSInki Dae 		return NULL;
3762a3098ffSInki Dae 
3772a3098ffSInki Dae 	if (vma->vm_ops && vma->vm_ops->open)
3782a3098ffSInki Dae 		vma->vm_ops->open(vma);
3792a3098ffSInki Dae 
3802a3098ffSInki Dae 	if (vma->vm_file)
3812a3098ffSInki Dae 		get_file(vma->vm_file);
3822a3098ffSInki Dae 
3832a3098ffSInki Dae 	memcpy(vma_copy, vma, sizeof(*vma));
3842a3098ffSInki Dae 
3852a3098ffSInki Dae 	vma_copy->vm_mm = NULL;
3862a3098ffSInki Dae 	vma_copy->vm_next = NULL;
3872a3098ffSInki Dae 	vma_copy->vm_prev = NULL;
3882a3098ffSInki Dae 
3892a3098ffSInki Dae 	return vma_copy;
3902a3098ffSInki Dae }
3912a3098ffSInki Dae 
3922a3098ffSInki Dae void exynos_gem_put_vma(struct vm_area_struct *vma)
3932a3098ffSInki Dae {
3942a3098ffSInki Dae 	if (!vma)
3952a3098ffSInki Dae 		return;
3962a3098ffSInki Dae 
3972a3098ffSInki Dae 	if (vma->vm_ops && vma->vm_ops->close)
3982a3098ffSInki Dae 		vma->vm_ops->close(vma);
3992a3098ffSInki Dae 
4002a3098ffSInki Dae 	if (vma->vm_file)
4012a3098ffSInki Dae 		fput(vma->vm_file);
4022a3098ffSInki Dae 
4032a3098ffSInki Dae 	kfree(vma);
4042a3098ffSInki Dae }
4052a3098ffSInki Dae 
4062a3098ffSInki Dae int exynos_gem_get_pages_from_userptr(unsigned long start,
4072a3098ffSInki Dae 						unsigned int npages,
4082a3098ffSInki Dae 						struct page **pages,
4092a3098ffSInki Dae 						struct vm_area_struct *vma)
4102a3098ffSInki Dae {
4112a3098ffSInki Dae 	int get_npages;
4122a3098ffSInki Dae 
4132a3098ffSInki Dae 	/* the memory region mmaped with VM_PFNMAP. */
4142a3098ffSInki Dae 	if (vma_is_io(vma)) {
4152a3098ffSInki Dae 		unsigned int i;
4162a3098ffSInki Dae 
4172a3098ffSInki Dae 		for (i = 0; i < npages; ++i, start += PAGE_SIZE) {
4182a3098ffSInki Dae 			unsigned long pfn;
4192a3098ffSInki Dae 			int ret = follow_pfn(vma, start, &pfn);
4202a3098ffSInki Dae 			if (ret)
4212a3098ffSInki Dae 				return ret;
4222a3098ffSInki Dae 
4232a3098ffSInki Dae 			pages[i] = pfn_to_page(pfn);
4242a3098ffSInki Dae 		}
4252a3098ffSInki Dae 
4262a3098ffSInki Dae 		if (i != npages) {
4272a3098ffSInki Dae 			DRM_ERROR("failed to get user_pages.\n");
4282a3098ffSInki Dae 			return -EINVAL;
4292a3098ffSInki Dae 		}
4302a3098ffSInki Dae 
4312a3098ffSInki Dae 		return 0;
4322a3098ffSInki Dae 	}
4332a3098ffSInki Dae 
4342a3098ffSInki Dae 	get_npages = get_user_pages(current, current->mm, start,
4352a3098ffSInki Dae 					npages, 1, 1, pages, NULL);
4362a3098ffSInki Dae 	get_npages = max(get_npages, 0);
4372a3098ffSInki Dae 	if (get_npages != npages) {
4382a3098ffSInki Dae 		DRM_ERROR("failed to get user_pages.\n");
4392a3098ffSInki Dae 		while (get_npages)
4402a3098ffSInki Dae 			put_page(pages[--get_npages]);
4412a3098ffSInki Dae 		return -EFAULT;
4422a3098ffSInki Dae 	}
4432a3098ffSInki Dae 
4442a3098ffSInki Dae 	return 0;
4452a3098ffSInki Dae }
4462a3098ffSInki Dae 
4472a3098ffSInki Dae void exynos_gem_put_pages_to_userptr(struct page **pages,
4482a3098ffSInki Dae 					unsigned int npages,
4492a3098ffSInki Dae 					struct vm_area_struct *vma)
4502a3098ffSInki Dae {
4512a3098ffSInki Dae 	if (!vma_is_io(vma)) {
4522a3098ffSInki Dae 		unsigned int i;
4532a3098ffSInki Dae 
4542a3098ffSInki Dae 		for (i = 0; i < npages; i++) {
4552a3098ffSInki Dae 			set_page_dirty_lock(pages[i]);
4562a3098ffSInki Dae 
4572a3098ffSInki Dae 			/*
4582a3098ffSInki Dae 			 * undo the reference we took when populating
4592a3098ffSInki Dae 			 * the table.
4602a3098ffSInki Dae 			 */
4612a3098ffSInki Dae 			put_page(pages[i]);
4622a3098ffSInki Dae 		}
4632a3098ffSInki Dae 	}
4642a3098ffSInki Dae }
4652a3098ffSInki Dae 
4662a3098ffSInki Dae int exynos_gem_map_sgt_with_dma(struct drm_device *drm_dev,
4672a3098ffSInki Dae 				struct sg_table *sgt,
4682a3098ffSInki Dae 				enum dma_data_direction dir)
4692a3098ffSInki Dae {
4702a3098ffSInki Dae 	int nents;
4712a3098ffSInki Dae 
4722a3098ffSInki Dae 	mutex_lock(&drm_dev->struct_mutex);
4732a3098ffSInki Dae 
4742a3098ffSInki Dae 	nents = dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
4752a3098ffSInki Dae 	if (!nents) {
4762a3098ffSInki Dae 		DRM_ERROR("failed to map sgl with dma.\n");
4772a3098ffSInki Dae 		mutex_unlock(&drm_dev->struct_mutex);
4782a3098ffSInki Dae 		return nents;
4792a3098ffSInki Dae 	}
4802a3098ffSInki Dae 
4812a3098ffSInki Dae 	mutex_unlock(&drm_dev->struct_mutex);
4822a3098ffSInki Dae 	return 0;
4832a3098ffSInki Dae }
4842a3098ffSInki Dae 
4852a3098ffSInki Dae void exynos_gem_unmap_sgt_from_dma(struct drm_device *drm_dev,
4862a3098ffSInki Dae 				struct sg_table *sgt,
4872a3098ffSInki Dae 				enum dma_data_direction dir)
4882a3098ffSInki Dae {
4892a3098ffSInki Dae 	dma_unmap_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
4902a3098ffSInki Dae }
4912a3098ffSInki Dae 
492ee5e770eSJoonyoung Shim void exynos_drm_gem_free_object(struct drm_gem_object *obj)
4931c248b7dSInki Dae {
494b2df26c1SInki Dae 	struct exynos_drm_gem_obj *exynos_gem_obj;
495b2df26c1SInki Dae 	struct exynos_drm_gem_buf *buf;
496b2df26c1SInki Dae 
497b2df26c1SInki Dae 	exynos_gem_obj = to_exynos_gem_obj(obj);
498b2df26c1SInki Dae 	buf = exynos_gem_obj->buffer;
499b2df26c1SInki Dae 
500b2df26c1SInki Dae 	if (obj->import_attach)
501b2df26c1SInki Dae 		drm_prime_gem_destroy(obj, buf->sgt);
502b2df26c1SInki Dae 
5032364839aSJoonyoung Shim 	exynos_drm_gem_destroy(to_exynos_gem_obj(obj));
5041c248b7dSInki Dae }
5051c248b7dSInki Dae 
5061c248b7dSInki Dae int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
507ee5e770eSJoonyoung Shim 			       struct drm_device *dev,
508ee5e770eSJoonyoung Shim 			       struct drm_mode_create_dumb *args)
5091c248b7dSInki Dae {
5101c248b7dSInki Dae 	struct exynos_drm_gem_obj *exynos_gem_obj;
5112364839aSJoonyoung Shim 	int ret;
5121c248b7dSInki Dae 
5131c248b7dSInki Dae 	/*
514c6b78bc8SMasanari Iida 	 * allocate memory to be used for framebuffer.
5151c248b7dSInki Dae 	 * - this callback would be called by user application
5161c248b7dSInki Dae 	 *	with DRM_IOCTL_MODE_CREATE_DUMB command.
5171c248b7dSInki Dae 	 */
5181c248b7dSInki Dae 
5193fd6b694SCooper Yuan 	args->pitch = args->width * ((args->bpp + 7) / 8);
5207da5907cSInki Dae 	args->size = args->pitch * args->height;
5211c248b7dSInki Dae 
522122beea8SRahul Sharma 	if (is_drm_iommu_supported(dev)) {
5233fec4532SVikas Sajjan 		exynos_gem_obj = exynos_drm_gem_create(dev,
5243fec4532SVikas Sajjan 			EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC,
5253fec4532SVikas Sajjan 			args->size);
526122beea8SRahul Sharma 	} else {
527122beea8SRahul Sharma 		exynos_gem_obj = exynos_drm_gem_create(dev,
528122beea8SRahul Sharma 			EXYNOS_BO_CONTIG | EXYNOS_BO_WC,
529122beea8SRahul Sharma 			args->size);
5303fec4532SVikas Sajjan 	}
5313fec4532SVikas Sajjan 
532122beea8SRahul Sharma 	if (IS_ERR(exynos_gem_obj)) {
533122beea8SRahul Sharma 		dev_warn(dev->dev, "FB allocation failed.\n");
5341c248b7dSInki Dae 		return PTR_ERR(exynos_gem_obj);
535122beea8SRahul Sharma 	}
5361c248b7dSInki Dae 
5372364839aSJoonyoung Shim 	ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
5382364839aSJoonyoung Shim 			&args->handle);
5392364839aSJoonyoung Shim 	if (ret) {
5402364839aSJoonyoung Shim 		exynos_drm_gem_destroy(exynos_gem_obj);
5412364839aSJoonyoung Shim 		return ret;
5422364839aSJoonyoung Shim 	}
5432364839aSJoonyoung Shim 
5441c248b7dSInki Dae 	return 0;
5451c248b7dSInki Dae }
5461c248b7dSInki Dae 
5471c248b7dSInki Dae int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
548ee5e770eSJoonyoung Shim 				   struct drm_device *dev, uint32_t handle,
549ee5e770eSJoonyoung Shim 				   uint64_t *offset)
5501c248b7dSInki Dae {
5511c248b7dSInki Dae 	struct drm_gem_object *obj;
5522d91cf17SJoonyoung Shim 	int ret = 0;
5531c248b7dSInki Dae 
5541c248b7dSInki Dae 	mutex_lock(&dev->struct_mutex);
5551c248b7dSInki Dae 
5561c248b7dSInki Dae 	/*
5571c248b7dSInki Dae 	 * get offset of memory allocated for drm framebuffer.
5581c248b7dSInki Dae 	 * - this callback would be called by user application
5591c248b7dSInki Dae 	 *	with DRM_IOCTL_MODE_MAP_DUMB command.
5601c248b7dSInki Dae 	 */
5611c248b7dSInki Dae 
5621c248b7dSInki Dae 	obj = drm_gem_object_lookup(dev, file_priv, handle);
5631c248b7dSInki Dae 	if (!obj) {
5641c248b7dSInki Dae 		DRM_ERROR("failed to lookup gem object.\n");
5652d91cf17SJoonyoung Shim 		ret = -EINVAL;
5662d91cf17SJoonyoung Shim 		goto unlock;
5671c248b7dSInki Dae 	}
5681c248b7dSInki Dae 
5696037bafaSLaurent Pinchart 	ret = drm_gem_create_mmap_offset(obj);
5702d91cf17SJoonyoung Shim 	if (ret)
5712d91cf17SJoonyoung Shim 		goto out;
5721c248b7dSInki Dae 
5730de23977SDavid Herrmann 	*offset = drm_vma_node_offset_addr(&obj->vma_node);
5741c248b7dSInki Dae 	DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
5751c248b7dSInki Dae 
5762d91cf17SJoonyoung Shim out:
5772d91cf17SJoonyoung Shim 	drm_gem_object_unreference(obj);
5782d91cf17SJoonyoung Shim unlock:
5791c248b7dSInki Dae 	mutex_unlock(&dev->struct_mutex);
5802d91cf17SJoonyoung Shim 	return ret;
5811c248b7dSInki Dae }
5821c248b7dSInki Dae 
5831c248b7dSInki Dae int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
5841c248b7dSInki Dae {
5851c248b7dSInki Dae 	struct drm_gem_object *obj = vma->vm_private_data;
5861c248b7dSInki Dae 	struct drm_device *dev = obj->dev;
5872b35892eSInki Dae 	unsigned long f_vaddr;
5881c248b7dSInki Dae 	pgoff_t page_offset;
5891c248b7dSInki Dae 	int ret;
5901c248b7dSInki Dae 
5911c248b7dSInki Dae 	page_offset = ((unsigned long)vmf->virtual_address -
5921c248b7dSInki Dae 			vma->vm_start) >> PAGE_SHIFT;
5932b35892eSInki Dae 	f_vaddr = (unsigned long)vmf->virtual_address;
5941c248b7dSInki Dae 
5951c248b7dSInki Dae 	mutex_lock(&dev->struct_mutex);
5961c248b7dSInki Dae 
5970519f9a1SInki Dae 	ret = exynos_drm_gem_map_buf(obj, vma, f_vaddr, page_offset);
5982b35892eSInki Dae 	if (ret < 0)
5990519f9a1SInki Dae 		DRM_ERROR("failed to map a buffer with user.\n");
6001c248b7dSInki Dae 
6011c248b7dSInki Dae 	mutex_unlock(&dev->struct_mutex);
6021c248b7dSInki Dae 
6031c248b7dSInki Dae 	return convert_to_vm_err_msg(ret);
6041c248b7dSInki Dae }
6051c248b7dSInki Dae 
6061c248b7dSInki Dae int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
6071c248b7dSInki Dae {
608c01d73faSInki Dae 	struct exynos_drm_gem_obj *exynos_gem_obj;
609c01d73faSInki Dae 	struct drm_gem_object *obj;
6101c248b7dSInki Dae 	int ret;
6111c248b7dSInki Dae 
6121c248b7dSInki Dae 	/* set vm_area_struct. */
6131c248b7dSInki Dae 	ret = drm_gem_mmap(filp, vma);
6141c248b7dSInki Dae 	if (ret < 0) {
6151c248b7dSInki Dae 		DRM_ERROR("failed to mmap.\n");
6161c248b7dSInki Dae 		return ret;
6171c248b7dSInki Dae 	}
6181c248b7dSInki Dae 
619c01d73faSInki Dae 	obj = vma->vm_private_data;
620c01d73faSInki Dae 	exynos_gem_obj = to_exynos_gem_obj(obj);
621c01d73faSInki Dae 
622c01d73faSInki Dae 	ret = check_gem_flags(exynos_gem_obj->flags);
623832316c7SInki Dae 	if (ret)
624832316c7SInki Dae 		goto err_close_vm;
6251c248b7dSInki Dae 
626c01d73faSInki Dae 	update_vm_cache_attr(exynos_gem_obj, vma);
627c01d73faSInki Dae 
628832316c7SInki Dae 	ret = exynos_drm_gem_mmap_buffer(exynos_gem_obj, vma);
629832316c7SInki Dae 	if (ret)
630832316c7SInki Dae 		goto err_close_vm;
631832316c7SInki Dae 
632832316c7SInki Dae 	return ret;
633832316c7SInki Dae 
634832316c7SInki Dae err_close_vm:
635832316c7SInki Dae 	drm_gem_vm_close(vma);
636832316c7SInki Dae 	drm_gem_free_mmap_offset(obj);
637832316c7SInki Dae 
6381c248b7dSInki Dae 	return ret;
6391c248b7dSInki Dae }
640