blob: 49b8c9b2290225dcf9cdf5993075c30ea0fd475e [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* exynos_drm_gem.c
2 *
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
5 *
Inki Daed81aecb2012-12-18 02:30:17 +09006 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
Inki Dae1c248b72011-10-04 19:19:01 +090010 */
11
David Howells760285e2012-10-02 18:01:07 +010012#include <drm/drmP.h>
David Herrmann0de23972013-07-24 21:07:52 +020013#include <drm/drm_vma_manager.h>
Inki Dae1c248b72011-10-04 19:19:01 +090014
Inki Dae2b358922012-03-16 18:47:05 +090015#include <linux/shmem_fs.h>
Inki Dae1c248b72011-10-04 19:19:01 +090016#include <drm/exynos_drm.h>
17
18#include "exynos_drm_drv.h"
19#include "exynos_drm_gem.h"
20#include "exynos_drm_buf.h"
Vikas Sajjan3fec4532013-08-23 12:05:06 +053021#include "exynos_drm_iommu.h"
Inki Dae1c248b72011-10-04 19:19:01 +090022
23static unsigned int convert_to_vm_err_msg(int msg)
24{
25 unsigned int out_msg;
26
27 switch (msg) {
28 case 0:
29 case -ERESTARTSYS:
30 case -EINTR:
31 out_msg = VM_FAULT_NOPAGE;
32 break;
33
34 case -ENOMEM:
35 out_msg = VM_FAULT_OOM;
36 break;
37
38 default:
39 out_msg = VM_FAULT_SIGBUS;
40 break;
41 }
42
43 return out_msg;
44}
45
Inki Daedcf9af82012-04-03 21:27:58 +090046static int check_gem_flags(unsigned int flags)
Inki Dae2b358922012-03-16 18:47:05 +090047{
Inki Daedcf9af82012-04-03 21:27:58 +090048 if (flags & ~(EXYNOS_BO_MASK)) {
49 DRM_ERROR("invalid flags.\n");
50 return -EINVAL;
51 }
52
53 return 0;
54}
55
Inki Daec01d73fa2012-04-23 19:26:34 +090056static void update_vm_cache_attr(struct exynos_drm_gem_obj *obj,
57 struct vm_area_struct *vma)
58{
59 DRM_DEBUG_KMS("flags = 0x%x\n", obj->flags);
60
61 /* non-cachable as default. */
62 if (obj->flags & EXYNOS_BO_CACHABLE)
63 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
64 else if (obj->flags & EXYNOS_BO_WC)
65 vma->vm_page_prot =
66 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
67 else
68 vma->vm_page_prot =
69 pgprot_noncached(vm_get_page_prot(vma->vm_flags));
70}
71
Inki Daedcf9af82012-04-03 21:27:58 +090072static unsigned long roundup_gem_size(unsigned long size, unsigned int flags)
73{
Inki Dae0519f9a2012-10-20 07:53:42 -070074 /* TODO */
75
Inki Daedcf9af82012-04-03 21:27:58 +090076 return roundup(size, PAGE_SIZE);
Inki Dae2b358922012-03-16 18:47:05 +090077}
78
Inki Dae0519f9a2012-10-20 07:53:42 -070079static int exynos_drm_gem_map_buf(struct drm_gem_object *obj,
Inki Dae2b358922012-03-16 18:47:05 +090080 struct vm_area_struct *vma,
81 unsigned long f_vaddr,
82 pgoff_t page_offset)
83{
84 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
85 struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer;
Rahul Sharma4ddc4042012-11-05 21:04:29 +053086 struct scatterlist *sgl;
Inki Dae2b358922012-03-16 18:47:05 +090087 unsigned long pfn;
Rahul Sharma4ddc4042012-11-05 21:04:29 +053088 int i;
Inki Dae2b358922012-03-16 18:47:05 +090089
Prathyush K465ed662012-11-20 19:32:56 +090090 if (!buf->sgt)
91 return -EINTR;
Rahul Sharma4ddc4042012-11-05 21:04:29 +053092
Prathyush K465ed662012-11-20 19:32:56 +090093 if (page_offset >= (buf->size >> PAGE_SHIFT)) {
94 DRM_ERROR("invalid page offset\n");
95 return -EINVAL;
Rahul Sharma4ddc4042012-11-05 21:04:29 +053096 }
Inki Dae2b358922012-03-16 18:47:05 +090097
Prathyush K465ed662012-11-20 19:32:56 +090098 sgl = buf->sgt->sgl;
99 for_each_sg(buf->sgt->sgl, sgl, buf->sgt->nents, i) {
100 if (page_offset < (sgl->length >> PAGE_SHIFT))
101 break;
102 page_offset -= (sgl->length >> PAGE_SHIFT);
103 }
104
105 pfn = __phys_to_pfn(sg_phys(sgl)) + page_offset;
106
Inki Dae2b358922012-03-16 18:47:05 +0900107 return vm_insert_mixed(vma, f_vaddr, pfn);
108}
109
Joonyoung Shim23648392011-12-13 14:39:13 +0900110static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
111 struct drm_file *file_priv,
112 unsigned int *handle)
Inki Dae1c248b72011-10-04 19:19:01 +0900113{
Inki Dae1c248b72011-10-04 19:19:01 +0900114 int ret;
115
Inki Dae1c248b72011-10-04 19:19:01 +0900116 /*
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, obj, handle);
121 if (ret)
Joonyoung Shim23648392011-12-13 14:39:13 +0900122 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900123
124 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
125
126 /* drop reference from allocate - handle holds it now. */
127 drm_gem_object_unreference_unlocked(obj);
128
Joonyoung Shim23648392011-12-13 14:39:13 +0900129 return 0;
130}
Inki Dae1c248b72011-10-04 19:19:01 +0900131
Joonyoung Shim23648392011-12-13 14:39:13 +0900132void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj)
133{
134 struct drm_gem_object *obj;
Inki Daec01d73fa2012-04-23 19:26:34 +0900135 struct exynos_drm_gem_buf *buf;
Joonyoung Shim23648392011-12-13 14:39:13 +0900136
Joonyoung Shim23648392011-12-13 14:39:13 +0900137 obj = &exynos_gem_obj->base;
Inki Daec01d73fa2012-04-23 19:26:34 +0900138 buf = exynos_gem_obj->buffer;
Joonyoung Shim23648392011-12-13 14:39:13 +0900139
Daniel Vettera8e11d12013-08-15 00:02:37 +0200140 DRM_DEBUG_KMS("handle count = %d\n", obj->handle_count);
Joonyoung Shim23648392011-12-13 14:39:13 +0900141
Inki Daec374e732012-06-12 16:52:54 +0900142 /*
143 * do not release memory region from exporter.
144 *
145 * the region will be released by exporter
146 * once dmabuf's refcount becomes 0.
147 */
148 if (obj->import_attach)
149 goto out;
150
Inki Dae0519f9a2012-10-20 07:53:42 -0700151 exynos_drm_free_buf(obj->dev, exynos_gem_obj->flags, buf);
Inki Dae2b358922012-03-16 18:47:05 +0900152
Inki Daec374e732012-06-12 16:52:54 +0900153out:
Inki Daec01d73fa2012-04-23 19:26:34 +0900154 exynos_drm_fini_buf(obj->dev, buf);
Inki Dae2b358922012-03-16 18:47:05 +0900155 exynos_gem_obj->buffer = NULL;
Joonyoung Shim23648392011-12-13 14:39:13 +0900156
David Herrmann0de23972013-07-24 21:07:52 +0200157 drm_gem_free_mmap_offset(obj);
Joonyoung Shim23648392011-12-13 14:39:13 +0900158
159 /* release file pointer to gem object. */
Inki Dae1c248b72011-10-04 19:19:01 +0900160 drm_gem_object_release(obj);
161
Inki Dae1c248b72011-10-04 19:19:01 +0900162 kfree(exynos_gem_obj);
Inki Dae2b358922012-03-16 18:47:05 +0900163 exynos_gem_obj = NULL;
Joonyoung Shim23648392011-12-13 14:39:13 +0900164}
165
Inki Daea4f19aa2013-03-11 21:15:59 +0900166unsigned long exynos_drm_gem_get_size(struct drm_device *dev,
167 unsigned int gem_handle,
168 struct drm_file *file_priv)
169{
170 struct exynos_drm_gem_obj *exynos_gem_obj;
171 struct drm_gem_object *obj;
172
173 obj = drm_gem_object_lookup(dev, file_priv, gem_handle);
174 if (!obj) {
175 DRM_ERROR("failed to lookup gem object.\n");
176 return 0;
177 }
178
179 exynos_gem_obj = to_exynos_gem_obj(obj);
180
181 drm_gem_object_unreference_unlocked(obj);
182
183 return exynos_gem_obj->buffer->size;
184}
185
186
Inki Daeb2df26c2012-04-23 21:01:28 +0900187struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev,
Joonyoung Shim23648392011-12-13 14:39:13 +0900188 unsigned long size)
189{
190 struct exynos_drm_gem_obj *exynos_gem_obj;
191 struct drm_gem_object *obj;
192 int ret;
193
194 exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900195 if (!exynos_gem_obj)
Joonyoung Shim23648392011-12-13 14:39:13 +0900196 return NULL;
Joonyoung Shim23648392011-12-13 14:39:13 +0900197
Inki Dae2b358922012-03-16 18:47:05 +0900198 exynos_gem_obj->size = size;
Joonyoung Shim23648392011-12-13 14:39:13 +0900199 obj = &exynos_gem_obj->base;
200
201 ret = drm_gem_object_init(dev, obj, size);
202 if (ret < 0) {
203 DRM_ERROR("failed to initialize gem object\n");
204 kfree(exynos_gem_obj);
205 return NULL;
206 }
207
208 DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);
209
210 return exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900211}
212
Inki Daef088d5a2011-11-12 14:51:23 +0900213struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
Inki Dae2b358922012-03-16 18:47:05 +0900214 unsigned int flags,
215 unsigned long size)
Inki Daef088d5a2011-11-12 14:51:23 +0900216{
Joonyoung Shim23648392011-12-13 14:39:13 +0900217 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae2b358922012-03-16 18:47:05 +0900218 struct exynos_drm_gem_buf *buf;
219 int ret;
Inki Daef088d5a2011-11-12 14:51:23 +0900220
Inki Daedcf9af82012-04-03 21:27:58 +0900221 if (!size) {
222 DRM_ERROR("invalid size.\n");
223 return ERR_PTR(-EINVAL);
224 }
Inki Daef088d5a2011-11-12 14:51:23 +0900225
Inki Daedcf9af82012-04-03 21:27:58 +0900226 size = roundup_gem_size(size, flags);
Inki Daedcf9af82012-04-03 21:27:58 +0900227
228 ret = check_gem_flags(flags);
229 if (ret)
230 return ERR_PTR(ret);
Inki Dae2b358922012-03-16 18:47:05 +0900231
232 buf = exynos_drm_init_buf(dev, size);
233 if (!buf)
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900234 return ERR_PTR(-ENOMEM);
Inki Daef088d5a2011-11-12 14:51:23 +0900235
Joonyoung Shim23648392011-12-13 14:39:13 +0900236 exynos_gem_obj = exynos_drm_gem_init(dev, size);
237 if (!exynos_gem_obj) {
Inki Dae2b358922012-03-16 18:47:05 +0900238 ret = -ENOMEM;
Inki Daedcf9af82012-04-03 21:27:58 +0900239 goto err_fini_buf;
Inki Daef088d5a2011-11-12 14:51:23 +0900240 }
241
Inki Dae2b358922012-03-16 18:47:05 +0900242 exynos_gem_obj->buffer = buf;
243
244 /* set memory type and cache attribute from user side. */
245 exynos_gem_obj->flags = flags;
246
Inki Dae0519f9a2012-10-20 07:53:42 -0700247 ret = exynos_drm_alloc_buf(dev, buf, flags);
YoungJun Choc58c1592013-07-01 11:17:12 +0900248 if (ret < 0)
249 goto err_gem_fini;
Inki Daef088d5a2011-11-12 14:51:23 +0900250
251 return exynos_gem_obj;
Inki Daedcf9af82012-04-03 21:27:58 +0900252
YoungJun Choc58c1592013-07-01 11:17:12 +0900253err_gem_fini:
254 drm_gem_object_release(&exynos_gem_obj->base);
255 kfree(exynos_gem_obj);
Inki Daedcf9af82012-04-03 21:27:58 +0900256err_fini_buf:
Inki Dae2b358922012-03-16 18:47:05 +0900257 exynos_drm_fini_buf(dev, buf);
258 return ERR_PTR(ret);
Inki Daef088d5a2011-11-12 14:51:23 +0900259}
260
Inki Dae1c248b72011-10-04 19:19:01 +0900261int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900262 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900263{
264 struct drm_exynos_gem_create *args = data;
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900265 struct exynos_drm_gem_obj *exynos_gem_obj;
Joonyoung Shim23648392011-12-13 14:39:13 +0900266 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900267
Inki Dae2b358922012-03-16 18:47:05 +0900268 exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size);
Inki Dae1c248b72011-10-04 19:19:01 +0900269 if (IS_ERR(exynos_gem_obj))
270 return PTR_ERR(exynos_gem_obj);
271
Joonyoung Shim23648392011-12-13 14:39:13 +0900272 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
273 &args->handle);
274 if (ret) {
275 exynos_drm_gem_destroy(exynos_gem_obj);
276 return ret;
277 }
278
Inki Dae1c248b72011-10-04 19:19:01 +0900279 return 0;
280}
281
Inki Daed87342c2012-11-03 21:53:24 -0700282dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev,
Inki Daef0b1bda2012-03-16 18:47:06 +0900283 unsigned int gem_handle,
Inki Daed87342c2012-11-03 21:53:24 -0700284 struct drm_file *filp)
Inki Daef0b1bda2012-03-16 18:47:06 +0900285{
286 struct exynos_drm_gem_obj *exynos_gem_obj;
287 struct drm_gem_object *obj;
288
Inki Daed87342c2012-11-03 21:53:24 -0700289 obj = drm_gem_object_lookup(dev, filp, gem_handle);
Inki Daef0b1bda2012-03-16 18:47:06 +0900290 if (!obj) {
291 DRM_ERROR("failed to lookup gem object.\n");
292 return ERR_PTR(-EINVAL);
293 }
294
295 exynos_gem_obj = to_exynos_gem_obj(obj);
296
Inki Daef0b1bda2012-03-16 18:47:06 +0900297 return &exynos_gem_obj->buffer->dma_addr;
298}
299
300void exynos_drm_gem_put_dma_addr(struct drm_device *dev,
301 unsigned int gem_handle,
Inki Daed87342c2012-11-03 21:53:24 -0700302 struct drm_file *filp)
Inki Daef0b1bda2012-03-16 18:47:06 +0900303{
304 struct exynos_drm_gem_obj *exynos_gem_obj;
305 struct drm_gem_object *obj;
306
Inki Daed87342c2012-11-03 21:53:24 -0700307 obj = drm_gem_object_lookup(dev, filp, gem_handle);
Inki Daef0b1bda2012-03-16 18:47:06 +0900308 if (!obj) {
309 DRM_ERROR("failed to lookup gem object.\n");
310 return;
311 }
312
313 exynos_gem_obj = to_exynos_gem_obj(obj);
314
Inki Daef0b1bda2012-03-16 18:47:06 +0900315 drm_gem_object_unreference_unlocked(obj);
316
317 /*
318 * decrease obj->refcount one more time because we has already
319 * increased it at exynos_drm_gem_get_dma_addr().
320 */
321 drm_gem_object_unreference_unlocked(obj);
322}
323
Inki Dae1c248b72011-10-04 19:19:01 +0900324int exynos_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900325 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900326{
327 struct drm_exynos_gem_map_off *args = data;
328
Inki Dae1c248b72011-10-04 19:19:01 +0900329 DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n",
330 args->handle, (unsigned long)args->offset);
331
332 if (!(dev->driver->driver_features & DRIVER_GEM)) {
333 DRM_ERROR("does not support GEM.\n");
334 return -ENODEV;
335 }
336
337 return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle,
338 &args->offset);
339}
340
Inki Dae96f54212013-12-20 19:16:24 +0900341int exynos_drm_gem_mmap_buffer(struct file *filp,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900342 struct vm_area_struct *vma)
Inki Dae1c248b72011-10-04 19:19:01 +0900343{
344 struct drm_gem_object *obj = filp->private_data;
345 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
Inki Dae5b07c662012-11-08 21:52:54 +0900346 struct drm_device *drm_dev = obj->dev;
Inki Dae2c871122011-11-12 15:23:32 +0900347 struct exynos_drm_gem_buf *buffer;
Inki Dae0519f9a2012-10-20 07:53:42 -0700348 unsigned long vm_size;
Inki Dae5b07c662012-11-08 21:52:54 +0900349 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900350
Inki Dae96f54212013-12-20 19:16:24 +0900351 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
352
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700353 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
Inki Dae5b07c662012-11-08 21:52:54 +0900354 vma->vm_private_data = obj;
355 vma->vm_ops = drm_dev->driver->gem_vm_ops;
356
Inki Daec01d73fa2012-04-23 19:26:34 +0900357 update_vm_cache_attr(exynos_gem_obj, vma);
Inki Dae1c248b72011-10-04 19:19:01 +0900358
Inki Dae0519f9a2012-10-20 07:53:42 -0700359 vm_size = vma->vm_end - vma->vm_start;
Inki Dae2b358922012-03-16 18:47:05 +0900360
Inki Dae1c248b72011-10-04 19:19:01 +0900361 /*
Inki Dae2c871122011-11-12 15:23:32 +0900362 * a buffer contains information to physically continuous memory
Inki Dae1c248b72011-10-04 19:19:01 +0900363 * allocated by user request or at framebuffer creation.
364 */
Inki Dae2c871122011-11-12 15:23:32 +0900365 buffer = exynos_gem_obj->buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900366
367 /* check if user-requested size is valid. */
Inki Dae2c871122011-11-12 15:23:32 +0900368 if (vm_size > buffer->size)
Inki Dae1c248b72011-10-04 19:19:01 +0900369 return -EINVAL;
370
Inki Dae4744ad22012-12-07 17:51:27 +0900371 ret = dma_mmap_attrs(drm_dev->dev, vma, buffer->pages,
Inki Dae0519f9a2012-10-20 07:53:42 -0700372 buffer->dma_addr, buffer->size,
373 &buffer->dma_attrs);
Inki Dae5b07c662012-11-08 21:52:54 +0900374 if (ret < 0) {
375 DRM_ERROR("failed to mmap.\n");
376 return ret;
377 }
378
379 /*
380 * take a reference to this mapping of the object. And this reference
381 * is unreferenced by the corresponding vm_close call.
382 */
383 drm_gem_object_reference(obj);
384
Inki Dae5b07c662012-11-08 21:52:54 +0900385 drm_vm_open_locked(drm_dev, vma);
Inki Dae5b07c662012-11-08 21:52:54 +0900386
387 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900388}
389
Inki Dae1c248b72011-10-04 19:19:01 +0900390int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900391 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900392{
Inki Dae96f54212013-12-20 19:16:24 +0900393 struct drm_exynos_file_private *exynos_file_priv;
Inki Dae1c248b72011-10-04 19:19:01 +0900394 struct drm_exynos_gem_mmap *args = data;
395 struct drm_gem_object *obj;
Inki Dae96f54212013-12-20 19:16:24 +0900396 struct file *anon_filp;
Inki Dae23f340e2013-06-14 17:54:27 +0900397 unsigned long addr;
Inki Dae1c248b72011-10-04 19:19:01 +0900398
Inki Dae1c248b72011-10-04 19:19:01 +0900399 if (!(dev->driver->driver_features & DRIVER_GEM)) {
400 DRM_ERROR("does not support GEM.\n");
401 return -ENODEV;
402 }
403
Inki Dae96f54212013-12-20 19:16:24 +0900404 mutex_lock(&dev->struct_mutex);
405
Inki Dae1c248b72011-10-04 19:19:01 +0900406 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
407 if (!obj) {
408 DRM_ERROR("failed to lookup gem object.\n");
Inki Dae96f54212013-12-20 19:16:24 +0900409 mutex_unlock(&dev->struct_mutex);
Inki Dae1c248b72011-10-04 19:19:01 +0900410 return -EINVAL;
411 }
412
Inki Dae96f54212013-12-20 19:16:24 +0900413 exynos_file_priv = file_priv->driver_priv;
414 anon_filp = exynos_file_priv->anon_filp;
415 anon_filp->private_data = obj;
YoungJun Cho5af9f112013-02-07 16:17:54 +0900416
Inki Dae96f54212013-12-20 19:16:24 +0900417 addr = vm_mmap(anon_filp, 0, args->size, PROT_READ | PROT_WRITE,
418 MAP_SHARED, 0);
Inki Dae1c248b72011-10-04 19:19:01 +0900419
YoungJun Cho5af9f112013-02-07 16:17:54 +0900420 drm_gem_object_unreference(obj);
Inki Dae1c248b72011-10-04 19:19:01 +0900421
Inki Dae23f340e2013-06-14 17:54:27 +0900422 if (IS_ERR_VALUE(addr)) {
YoungJun Cho5af9f112013-02-07 16:17:54 +0900423 mutex_unlock(&dev->struct_mutex);
Inki Dae23f340e2013-06-14 17:54:27 +0900424 return (int)addr;
Inki Dae5b07c662012-11-08 21:52:54 +0900425 }
Inki Dae1c248b72011-10-04 19:19:01 +0900426
YoungJun Cho5af9f112013-02-07 16:17:54 +0900427 mutex_unlock(&dev->struct_mutex);
428
Inki Dae1c248b72011-10-04 19:19:01 +0900429 args->mapped = addr;
430
431 DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args->mapped);
432
433 return 0;
434}
435
Inki Dae40cd7e02012-05-04 15:51:17 +0900436int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data,
437 struct drm_file *file_priv)
438{ struct exynos_drm_gem_obj *exynos_gem_obj;
439 struct drm_exynos_gem_info *args = data;
440 struct drm_gem_object *obj;
441
442 mutex_lock(&dev->struct_mutex);
443
444 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
445 if (!obj) {
446 DRM_ERROR("failed to lookup gem object.\n");
447 mutex_unlock(&dev->struct_mutex);
448 return -EINVAL;
449 }
450
451 exynos_gem_obj = to_exynos_gem_obj(obj);
452
453 args->flags = exynos_gem_obj->flags;
454 args->size = exynos_gem_obj->size;
455
456 drm_gem_object_unreference(obj);
457 mutex_unlock(&dev->struct_mutex);
458
459 return 0;
460}
461
Inki Dae2a3098f2012-11-04 05:48:52 -0800462struct vm_area_struct *exynos_gem_get_vma(struct vm_area_struct *vma)
463{
464 struct vm_area_struct *vma_copy;
465
466 vma_copy = kmalloc(sizeof(*vma_copy), GFP_KERNEL);
467 if (!vma_copy)
468 return NULL;
469
470 if (vma->vm_ops && vma->vm_ops->open)
471 vma->vm_ops->open(vma);
472
473 if (vma->vm_file)
474 get_file(vma->vm_file);
475
476 memcpy(vma_copy, vma, sizeof(*vma));
477
478 vma_copy->vm_mm = NULL;
479 vma_copy->vm_next = NULL;
480 vma_copy->vm_prev = NULL;
481
482 return vma_copy;
483}
484
485void exynos_gem_put_vma(struct vm_area_struct *vma)
486{
487 if (!vma)
488 return;
489
490 if (vma->vm_ops && vma->vm_ops->close)
491 vma->vm_ops->close(vma);
492
493 if (vma->vm_file)
494 fput(vma->vm_file);
495
496 kfree(vma);
497}
498
499int exynos_gem_get_pages_from_userptr(unsigned long start,
500 unsigned int npages,
501 struct page **pages,
502 struct vm_area_struct *vma)
503{
504 int get_npages;
505
506 /* the memory region mmaped with VM_PFNMAP. */
507 if (vma_is_io(vma)) {
508 unsigned int i;
509
510 for (i = 0; i < npages; ++i, start += PAGE_SIZE) {
511 unsigned long pfn;
512 int ret = follow_pfn(vma, start, &pfn);
513 if (ret)
514 return ret;
515
516 pages[i] = pfn_to_page(pfn);
517 }
518
519 if (i != npages) {
520 DRM_ERROR("failed to get user_pages.\n");
521 return -EINVAL;
522 }
523
524 return 0;
525 }
526
527 get_npages = get_user_pages(current, current->mm, start,
528 npages, 1, 1, pages, NULL);
529 get_npages = max(get_npages, 0);
530 if (get_npages != npages) {
531 DRM_ERROR("failed to get user_pages.\n");
532 while (get_npages)
533 put_page(pages[--get_npages]);
534 return -EFAULT;
535 }
536
537 return 0;
538}
539
540void exynos_gem_put_pages_to_userptr(struct page **pages,
541 unsigned int npages,
542 struct vm_area_struct *vma)
543{
544 if (!vma_is_io(vma)) {
545 unsigned int i;
546
547 for (i = 0; i < npages; i++) {
548 set_page_dirty_lock(pages[i]);
549
550 /*
551 * undo the reference we took when populating
552 * the table.
553 */
554 put_page(pages[i]);
555 }
556 }
557}
558
559int exynos_gem_map_sgt_with_dma(struct drm_device *drm_dev,
560 struct sg_table *sgt,
561 enum dma_data_direction dir)
562{
563 int nents;
564
565 mutex_lock(&drm_dev->struct_mutex);
566
567 nents = dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
568 if (!nents) {
569 DRM_ERROR("failed to map sgl with dma.\n");
570 mutex_unlock(&drm_dev->struct_mutex);
571 return nents;
572 }
573
574 mutex_unlock(&drm_dev->struct_mutex);
575 return 0;
576}
577
578void exynos_gem_unmap_sgt_from_dma(struct drm_device *drm_dev,
579 struct sg_table *sgt,
580 enum dma_data_direction dir)
581{
582 dma_unmap_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
583}
584
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900585void exynos_drm_gem_free_object(struct drm_gem_object *obj)
Inki Dae1c248b72011-10-04 19:19:01 +0900586{
Inki Daeb2df26c2012-04-23 21:01:28 +0900587 struct exynos_drm_gem_obj *exynos_gem_obj;
588 struct exynos_drm_gem_buf *buf;
589
Inki Daeb2df26c2012-04-23 21:01:28 +0900590 exynos_gem_obj = to_exynos_gem_obj(obj);
591 buf = exynos_gem_obj->buffer;
592
593 if (obj->import_attach)
594 drm_prime_gem_destroy(obj, buf->sgt);
595
Joonyoung Shim23648392011-12-13 14:39:13 +0900596 exynos_drm_gem_destroy(to_exynos_gem_obj(obj));
Inki Dae1c248b72011-10-04 19:19:01 +0900597}
598
599int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900600 struct drm_device *dev,
601 struct drm_mode_create_dumb *args)
Inki Dae1c248b72011-10-04 19:19:01 +0900602{
603 struct exynos_drm_gem_obj *exynos_gem_obj;
Joonyoung Shim23648392011-12-13 14:39:13 +0900604 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900605
Inki Dae1c248b72011-10-04 19:19:01 +0900606 /*
607 * alocate memory to be used for framebuffer.
608 * - this callback would be called by user application
609 * with DRM_IOCTL_MODE_CREATE_DUMB command.
610 */
611
Cooper Yuan3fd6b692012-06-29 11:49:45 +0900612 args->pitch = args->width * ((args->bpp + 7) / 8);
Inki Dae7da59072012-08-17 15:24:03 +0900613 args->size = args->pitch * args->height;
Inki Dae1c248b72011-10-04 19:19:01 +0900614
Inki Dae69961d82013-04-18 14:04:17 +0900615 exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG |
616 EXYNOS_BO_WC, args->size);
Vikas Sajjan3fec4532013-08-23 12:05:06 +0530617 /*
618 * If physically contiguous memory allocation fails and if IOMMU is
619 * supported then try to get buffer from non physically contiguous
620 * memory area.
621 */
622 if (IS_ERR(exynos_gem_obj) && is_drm_iommu_supported(dev)) {
623 dev_warn(dev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
624 exynos_gem_obj = exynos_drm_gem_create(dev,
625 EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC,
626 args->size);
627 }
628
Inki Dae1c248b72011-10-04 19:19:01 +0900629 if (IS_ERR(exynos_gem_obj))
630 return PTR_ERR(exynos_gem_obj);
631
Joonyoung Shim23648392011-12-13 14:39:13 +0900632 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
633 &args->handle);
634 if (ret) {
635 exynos_drm_gem_destroy(exynos_gem_obj);
636 return ret;
637 }
638
Inki Dae1c248b72011-10-04 19:19:01 +0900639 return 0;
640}
641
642int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900643 struct drm_device *dev, uint32_t handle,
644 uint64_t *offset)
Inki Dae1c248b72011-10-04 19:19:01 +0900645{
Inki Dae1c248b72011-10-04 19:19:01 +0900646 struct drm_gem_object *obj;
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900647 int ret = 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900648
Inki Dae1c248b72011-10-04 19:19:01 +0900649 mutex_lock(&dev->struct_mutex);
650
651 /*
652 * get offset of memory allocated for drm framebuffer.
653 * - this callback would be called by user application
654 * with DRM_IOCTL_MODE_MAP_DUMB command.
655 */
656
657 obj = drm_gem_object_lookup(dev, file_priv, handle);
658 if (!obj) {
659 DRM_ERROR("failed to lookup gem object.\n");
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900660 ret = -EINVAL;
661 goto unlock;
Inki Dae1c248b72011-10-04 19:19:01 +0900662 }
663
David Herrmann0de23972013-07-24 21:07:52 +0200664 ret = drm_gem_create_mmap_offset(obj);
665 if (ret)
666 goto out;
Inki Dae1c248b72011-10-04 19:19:01 +0900667
David Herrmann0de23972013-07-24 21:07:52 +0200668 *offset = drm_vma_node_offset_addr(&obj->vma_node);
Inki Dae1c248b72011-10-04 19:19:01 +0900669 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
670
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900671out:
672 drm_gem_object_unreference(obj);
673unlock:
Inki Dae1c248b72011-10-04 19:19:01 +0900674 mutex_unlock(&dev->struct_mutex);
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900675 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900676}
677
678int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
679{
680 struct drm_gem_object *obj = vma->vm_private_data;
Inki Dae1c248b72011-10-04 19:19:01 +0900681 struct drm_device *dev = obj->dev;
Inki Dae2b358922012-03-16 18:47:05 +0900682 unsigned long f_vaddr;
Inki Dae1c248b72011-10-04 19:19:01 +0900683 pgoff_t page_offset;
684 int ret;
685
686 page_offset = ((unsigned long)vmf->virtual_address -
687 vma->vm_start) >> PAGE_SHIFT;
Inki Dae2b358922012-03-16 18:47:05 +0900688 f_vaddr = (unsigned long)vmf->virtual_address;
Inki Dae1c248b72011-10-04 19:19:01 +0900689
690 mutex_lock(&dev->struct_mutex);
691
Inki Dae0519f9a2012-10-20 07:53:42 -0700692 ret = exynos_drm_gem_map_buf(obj, vma, f_vaddr, page_offset);
Inki Dae2b358922012-03-16 18:47:05 +0900693 if (ret < 0)
Inki Dae0519f9a2012-10-20 07:53:42 -0700694 DRM_ERROR("failed to map a buffer with user.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900695
696 mutex_unlock(&dev->struct_mutex);
697
698 return convert_to_vm_err_msg(ret);
699}
700
701int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
702{
Inki Daec01d73fa2012-04-23 19:26:34 +0900703 struct exynos_drm_gem_obj *exynos_gem_obj;
704 struct drm_gem_object *obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900705 int ret;
706
Inki Dae1c248b72011-10-04 19:19:01 +0900707 /* set vm_area_struct. */
708 ret = drm_gem_mmap(filp, vma);
709 if (ret < 0) {
710 DRM_ERROR("failed to mmap.\n");
711 return ret;
712 }
713
Inki Daec01d73fa2012-04-23 19:26:34 +0900714 obj = vma->vm_private_data;
715 exynos_gem_obj = to_exynos_gem_obj(obj);
716
717 ret = check_gem_flags(exynos_gem_obj->flags);
718 if (ret) {
719 drm_gem_vm_close(vma);
720 drm_gem_free_mmap_offset(obj);
721 return ret;
722 }
723
Inki Dae1c248b72011-10-04 19:19:01 +0900724 vma->vm_flags &= ~VM_PFNMAP;
725 vma->vm_flags |= VM_MIXEDMAP;
726
Inki Daec01d73fa2012-04-23 19:26:34 +0900727 update_vm_cache_attr(exynos_gem_obj, vma);
728
Inki Dae1c248b72011-10-04 19:19:01 +0900729 return ret;
730}