blob: 730093fed51250cdec9769093296a7c1a0e29141 [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 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#include "drmP.h"
27#include "drm.h"
28
Inki Dae2b358922012-03-16 18:47:05 +090029#include <linux/shmem_fs.h>
Inki Dae1c248b72011-10-04 19:19:01 +090030#include <drm/exynos_drm.h>
31
32#include "exynos_drm_drv.h"
33#include "exynos_drm_gem.h"
34#include "exynos_drm_buf.h"
35
36static unsigned int convert_to_vm_err_msg(int msg)
37{
38 unsigned int out_msg;
39
40 switch (msg) {
41 case 0:
42 case -ERESTARTSYS:
43 case -EINTR:
44 out_msg = VM_FAULT_NOPAGE;
45 break;
46
47 case -ENOMEM:
48 out_msg = VM_FAULT_OOM;
49 break;
50
51 default:
52 out_msg = VM_FAULT_SIGBUS;
53 break;
54 }
55
56 return out_msg;
57}
58
Inki Dae2b358922012-03-16 18:47:05 +090059static unsigned int mask_gem_flags(unsigned int flags)
60{
61 return flags &= EXYNOS_BO_NONCONTIG;
62}
63
64static struct page **exynos_gem_get_pages(struct drm_gem_object *obj,
65 gfp_t gfpmask)
66{
67 struct inode *inode;
68 struct address_space *mapping;
69 struct page *p, **pages;
70 int i, npages;
71
72 /* This is the shared memory object that backs the GEM resource */
73 inode = obj->filp->f_path.dentry->d_inode;
74 mapping = inode->i_mapping;
75
76 npages = obj->size >> PAGE_SHIFT;
77
78 pages = drm_malloc_ab(npages, sizeof(struct page *));
79 if (pages == NULL)
80 return ERR_PTR(-ENOMEM);
81
82 gfpmask |= mapping_gfp_mask(mapping);
83
84 for (i = 0; i < npages; i++) {
85 p = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
86 if (IS_ERR(p))
87 goto fail;
88 pages[i] = p;
89 }
90
91 return pages;
92
93fail:
94 while (i--)
95 page_cache_release(pages[i]);
96
97 drm_free_large(pages);
98 return ERR_PTR(PTR_ERR(p));
99}
100
101static void exynos_gem_put_pages(struct drm_gem_object *obj,
102 struct page **pages,
103 bool dirty, bool accessed)
104{
105 int i, npages;
106
107 npages = obj->size >> PAGE_SHIFT;
108
109 for (i = 0; i < npages; i++) {
110 if (dirty)
111 set_page_dirty(pages[i]);
112
113 if (accessed)
114 mark_page_accessed(pages[i]);
115
116 /* Undo the reference we took when populating the table */
117 page_cache_release(pages[i]);
118 }
119
120 drm_free_large(pages);
121}
122
123static int exynos_drm_gem_map_pages(struct drm_gem_object *obj,
124 struct vm_area_struct *vma,
125 unsigned long f_vaddr,
126 pgoff_t page_offset)
127{
128 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
129 struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer;
130 unsigned long pfn;
131
132 if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) {
133 unsigned long usize = buf->size;
134
135 if (!buf->pages)
136 return -EINTR;
137
138 while (usize > 0) {
139 pfn = page_to_pfn(buf->pages[page_offset++]);
140 vm_insert_mixed(vma, f_vaddr, pfn);
141 f_vaddr += PAGE_SIZE;
142 usize -= PAGE_SIZE;
143 }
144
145 return 0;
146 }
147
148 pfn = (buf->dma_addr >> PAGE_SHIFT) + page_offset;
149
150 return vm_insert_mixed(vma, f_vaddr, pfn);
151}
152
153static int exynos_drm_gem_get_pages(struct drm_gem_object *obj)
154{
155 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
156 struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer;
157 struct scatterlist *sgl;
158 struct page **pages;
159 unsigned int npages, i = 0;
160 int ret;
161
162 if (buf->pages) {
163 DRM_DEBUG_KMS("already allocated.\n");
164 return -EINVAL;
165 }
166
167 pages = exynos_gem_get_pages(obj, GFP_KERNEL);
168 if (IS_ERR(pages)) {
169 DRM_ERROR("failed to get pages.\n");
170 return PTR_ERR(pages);
171 }
172
173 npages = obj->size >> PAGE_SHIFT;
174
175 buf->sgt = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
176 if (!buf->sgt) {
177 DRM_ERROR("failed to allocate sg table.\n");
178 ret = -ENOMEM;
179 goto err;
180 }
181
182 ret = sg_alloc_table(buf->sgt, npages, GFP_KERNEL);
183 if (ret < 0) {
184 DRM_ERROR("failed to initialize sg table.\n");
185 ret = -EFAULT;
186 goto err1;
187 }
188
189 sgl = buf->sgt->sgl;
190
191 /* set all pages to sg list. */
192 while (i < npages) {
193 sg_set_page(sgl, pages[i], PAGE_SIZE, 0);
194 sg_dma_address(sgl) = page_to_phys(pages[i]);
195 i++;
196 sgl = sg_next(sgl);
197 }
198
199 /* add some codes for UNCACHED type here. TODO */
200
201 buf->pages = pages;
202 return ret;
203err1:
204 kfree(buf->sgt);
205 buf->sgt = NULL;
206err:
207 exynos_gem_put_pages(obj, pages, true, false);
208 return ret;
209
210}
211
212static void exynos_drm_gem_put_pages(struct drm_gem_object *obj)
213{
214 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
215 struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer;
216
217 /*
218 * if buffer typs is EXYNOS_BO_NONCONTIG then release all pages
219 * allocated at gem fault handler.
220 */
221 sg_free_table(buf->sgt);
222 kfree(buf->sgt);
223 buf->sgt = NULL;
224
225 exynos_gem_put_pages(obj, buf->pages, true, false);
226 buf->pages = NULL;
227
228 /* add some codes for UNCACHED type here. TODO */
229}
230
Joonyoung Shim23648392011-12-13 14:39:13 +0900231static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
232 struct drm_file *file_priv,
233 unsigned int *handle)
Inki Dae1c248b72011-10-04 19:19:01 +0900234{
Inki Dae1c248b72011-10-04 19:19:01 +0900235 int ret;
236
Inki Dae1c248b72011-10-04 19:19:01 +0900237 /*
238 * allocate a id of idr table where the obj is registered
239 * and handle has the id what user can see.
240 */
241 ret = drm_gem_handle_create(file_priv, obj, handle);
242 if (ret)
Joonyoung Shim23648392011-12-13 14:39:13 +0900243 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900244
245 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
246
247 /* drop reference from allocate - handle holds it now. */
248 drm_gem_object_unreference_unlocked(obj);
249
Joonyoung Shim23648392011-12-13 14:39:13 +0900250 return 0;
251}
Inki Dae1c248b72011-10-04 19:19:01 +0900252
Joonyoung Shim23648392011-12-13 14:39:13 +0900253void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj)
254{
255 struct drm_gem_object *obj;
256
257 DRM_DEBUG_KMS("%s\n", __FILE__);
258
259 if (!exynos_gem_obj)
260 return;
261
262 obj = &exynos_gem_obj->base;
263
264 DRM_DEBUG_KMS("handle count = %d\n", atomic_read(&obj->handle_count));
265
Inki Dae2b358922012-03-16 18:47:05 +0900266 if ((exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) &&
267 exynos_gem_obj->buffer->pages)
268 exynos_drm_gem_put_pages(obj);
269 else
270 exynos_drm_free_buf(obj->dev, exynos_gem_obj->flags,
271 exynos_gem_obj->buffer);
272
273 exynos_drm_fini_buf(obj->dev, exynos_gem_obj->buffer);
274 exynos_gem_obj->buffer = NULL;
Joonyoung Shim23648392011-12-13 14:39:13 +0900275
276 if (obj->map_list.map)
277 drm_gem_free_mmap_offset(obj);
278
279 /* release file pointer to gem object. */
Inki Dae1c248b72011-10-04 19:19:01 +0900280 drm_gem_object_release(obj);
281
Inki Dae1c248b72011-10-04 19:19:01 +0900282 kfree(exynos_gem_obj);
Inki Dae2b358922012-03-16 18:47:05 +0900283 exynos_gem_obj = NULL;
Joonyoung Shim23648392011-12-13 14:39:13 +0900284}
285
286static struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev,
287 unsigned long size)
288{
289 struct exynos_drm_gem_obj *exynos_gem_obj;
290 struct drm_gem_object *obj;
291 int ret;
292
293 exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL);
294 if (!exynos_gem_obj) {
295 DRM_ERROR("failed to allocate exynos gem object\n");
296 return NULL;
297 }
298
Inki Dae2b358922012-03-16 18:47:05 +0900299 exynos_gem_obj->size = size;
Joonyoung Shim23648392011-12-13 14:39:13 +0900300 obj = &exynos_gem_obj->base;
301
302 ret = drm_gem_object_init(dev, obj, size);
303 if (ret < 0) {
304 DRM_ERROR("failed to initialize gem object\n");
305 kfree(exynos_gem_obj);
306 return NULL;
307 }
308
309 DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);
310
311 return exynos_gem_obj;
Inki Dae1c248b72011-10-04 19:19:01 +0900312}
313
Inki Daef088d5a2011-11-12 14:51:23 +0900314struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
Inki Dae2b358922012-03-16 18:47:05 +0900315 unsigned int flags,
316 unsigned long size)
Inki Daef088d5a2011-11-12 14:51:23 +0900317{
Joonyoung Shim23648392011-12-13 14:39:13 +0900318 struct exynos_drm_gem_obj *exynos_gem_obj;
Inki Dae2b358922012-03-16 18:47:05 +0900319 struct exynos_drm_gem_buf *buf;
320 int ret;
Inki Daef088d5a2011-11-12 14:51:23 +0900321
322 size = roundup(size, PAGE_SIZE);
Inki Daef088d5a2011-11-12 14:51:23 +0900323 DRM_DEBUG_KMS("%s: size = 0x%lx\n", __FILE__, size);
324
Inki Dae2b358922012-03-16 18:47:05 +0900325 flags = mask_gem_flags(flags);
326
327 buf = exynos_drm_init_buf(dev, size);
328 if (!buf)
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900329 return ERR_PTR(-ENOMEM);
Inki Daef088d5a2011-11-12 14:51:23 +0900330
Joonyoung Shim23648392011-12-13 14:39:13 +0900331 exynos_gem_obj = exynos_drm_gem_init(dev, size);
332 if (!exynos_gem_obj) {
Inki Dae2b358922012-03-16 18:47:05 +0900333 ret = -ENOMEM;
334 goto err;
Inki Daef088d5a2011-11-12 14:51:23 +0900335 }
336
Inki Dae2b358922012-03-16 18:47:05 +0900337 exynos_gem_obj->buffer = buf;
338
339 /* set memory type and cache attribute from user side. */
340 exynos_gem_obj->flags = flags;
341
342 /*
343 * allocate all pages as desired size if user wants to allocate
344 * physically non-continuous memory.
345 */
346 if (flags & EXYNOS_BO_NONCONTIG) {
347 ret = exynos_drm_gem_get_pages(&exynos_gem_obj->base);
348 if (ret < 0) {
349 drm_gem_object_release(&exynos_gem_obj->base);
350 goto err;
351 }
352 } else {
353 ret = exynos_drm_alloc_buf(dev, buf, flags);
354 if (ret < 0) {
355 drm_gem_object_release(&exynos_gem_obj->base);
356 goto err;
357 }
358 }
Inki Daef088d5a2011-11-12 14:51:23 +0900359
360 return exynos_gem_obj;
Inki Dae2b358922012-03-16 18:47:05 +0900361err:
362 exynos_drm_fini_buf(dev, buf);
363 return ERR_PTR(ret);
Inki Daef088d5a2011-11-12 14:51:23 +0900364}
365
Inki Dae1c248b72011-10-04 19:19:01 +0900366int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900367 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900368{
369 struct drm_exynos_gem_create *args = data;
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900370 struct exynos_drm_gem_obj *exynos_gem_obj;
Joonyoung Shim23648392011-12-13 14:39:13 +0900371 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900372
Inki Daef088d5a2011-11-12 14:51:23 +0900373 DRM_DEBUG_KMS("%s\n", __FILE__);
Inki Dae1c248b72011-10-04 19:19:01 +0900374
Inki Dae2b358922012-03-16 18:47:05 +0900375 exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size);
Inki Dae1c248b72011-10-04 19:19:01 +0900376 if (IS_ERR(exynos_gem_obj))
377 return PTR_ERR(exynos_gem_obj);
378
Joonyoung Shim23648392011-12-13 14:39:13 +0900379 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
380 &args->handle);
381 if (ret) {
382 exynos_drm_gem_destroy(exynos_gem_obj);
383 return ret;
384 }
385
Inki Dae1c248b72011-10-04 19:19:01 +0900386 return 0;
387}
388
389int exynos_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900390 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900391{
392 struct drm_exynos_gem_map_off *args = data;
393
394 DRM_DEBUG_KMS("%s\n", __FILE__);
395
396 DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n",
397 args->handle, (unsigned long)args->offset);
398
399 if (!(dev->driver->driver_features & DRIVER_GEM)) {
400 DRM_ERROR("does not support GEM.\n");
401 return -ENODEV;
402 }
403
404 return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle,
405 &args->offset);
406}
407
408static int exynos_drm_gem_mmap_buffer(struct file *filp,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900409 struct vm_area_struct *vma)
Inki Dae1c248b72011-10-04 19:19:01 +0900410{
411 struct drm_gem_object *obj = filp->private_data;
412 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
Inki Dae2c871122011-11-12 15:23:32 +0900413 struct exynos_drm_gem_buf *buffer;
Inki Dae2b358922012-03-16 18:47:05 +0900414 unsigned long pfn, vm_size, usize, uaddr = vma->vm_start;
415 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900416
417 DRM_DEBUG_KMS("%s\n", __FILE__);
418
419 vma->vm_flags |= (VM_IO | VM_RESERVED);
420
Joonyoung Shim23648392011-12-13 14:39:13 +0900421 /* in case of direct mapping, always having non-cachable attribute */
Inki Dae1c248b72011-10-04 19:19:01 +0900422 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Inki Dae1c248b72011-10-04 19:19:01 +0900423
Inki Dae2b358922012-03-16 18:47:05 +0900424 vm_size = usize = vma->vm_end - vma->vm_start;
425
Inki Dae1c248b72011-10-04 19:19:01 +0900426 /*
Inki Dae2c871122011-11-12 15:23:32 +0900427 * a buffer contains information to physically continuous memory
Inki Dae1c248b72011-10-04 19:19:01 +0900428 * allocated by user request or at framebuffer creation.
429 */
Inki Dae2c871122011-11-12 15:23:32 +0900430 buffer = exynos_gem_obj->buffer;
Inki Dae1c248b72011-10-04 19:19:01 +0900431
432 /* check if user-requested size is valid. */
Inki Dae2c871122011-11-12 15:23:32 +0900433 if (vm_size > buffer->size)
Inki Dae1c248b72011-10-04 19:19:01 +0900434 return -EINVAL;
435
Inki Dae2b358922012-03-16 18:47:05 +0900436 if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) {
437 int i = 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900438
Inki Dae2b358922012-03-16 18:47:05 +0900439 if (!buffer->pages)
440 return -EINVAL;
Inki Dae1c248b72011-10-04 19:19:01 +0900441
Inki Dae2b358922012-03-16 18:47:05 +0900442 do {
443 ret = vm_insert_page(vma, uaddr, buffer->pages[i++]);
444 if (ret) {
445 DRM_ERROR("failed to remap user space.\n");
446 return ret;
447 }
448
449 uaddr += PAGE_SIZE;
450 usize -= PAGE_SIZE;
451 } while (usize > 0);
452 } else {
453 /*
454 * get page frame number to physical memory to be mapped
455 * to user space.
456 */
457 pfn = ((unsigned long)exynos_gem_obj->buffer->dma_addr) >>
458 PAGE_SHIFT;
459
460 DRM_DEBUG_KMS("pfn = 0x%lx\n", pfn);
461
462 if (remap_pfn_range(vma, vma->vm_start, pfn, vm_size,
463 vma->vm_page_prot)) {
464 DRM_ERROR("failed to remap pfn range.\n");
465 return -EAGAIN;
466 }
Inki Dae1c248b72011-10-04 19:19:01 +0900467 }
468
469 return 0;
470}
471
472static const struct file_operations exynos_drm_gem_fops = {
473 .mmap = exynos_drm_gem_mmap_buffer,
474};
475
476int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900477 struct drm_file *file_priv)
Inki Dae1c248b72011-10-04 19:19:01 +0900478{
479 struct drm_exynos_gem_mmap *args = data;
480 struct drm_gem_object *obj;
481 unsigned int addr;
482
483 DRM_DEBUG_KMS("%s\n", __FILE__);
484
485 if (!(dev->driver->driver_features & DRIVER_GEM)) {
486 DRM_ERROR("does not support GEM.\n");
487 return -ENODEV;
488 }
489
490 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
491 if (!obj) {
492 DRM_ERROR("failed to lookup gem object.\n");
493 return -EINVAL;
494 }
495
496 obj->filp->f_op = &exynos_drm_gem_fops;
497 obj->filp->private_data = obj;
498
499 down_write(&current->mm->mmap_sem);
500 addr = do_mmap(obj->filp, 0, args->size,
501 PROT_READ | PROT_WRITE, MAP_SHARED, 0);
502 up_write(&current->mm->mmap_sem);
503
504 drm_gem_object_unreference_unlocked(obj);
505
506 if (IS_ERR((void *)addr))
507 return PTR_ERR((void *)addr);
508
509 args->mapped = addr;
510
511 DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args->mapped);
512
513 return 0;
514}
515
516int exynos_drm_gem_init_object(struct drm_gem_object *obj)
517{
518 DRM_DEBUG_KMS("%s\n", __FILE__);
519
520 return 0;
521}
522
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900523void exynos_drm_gem_free_object(struct drm_gem_object *obj)
Inki Dae1c248b72011-10-04 19:19:01 +0900524{
Inki Dae1c248b72011-10-04 19:19:01 +0900525 DRM_DEBUG_KMS("%s\n", __FILE__);
526
Joonyoung Shim23648392011-12-13 14:39:13 +0900527 exynos_drm_gem_destroy(to_exynos_gem_obj(obj));
Inki Dae1c248b72011-10-04 19:19:01 +0900528}
529
530int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900531 struct drm_device *dev,
532 struct drm_mode_create_dumb *args)
Inki Dae1c248b72011-10-04 19:19:01 +0900533{
534 struct exynos_drm_gem_obj *exynos_gem_obj;
Joonyoung Shim23648392011-12-13 14:39:13 +0900535 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900536
537 DRM_DEBUG_KMS("%s\n", __FILE__);
538
539 /*
540 * alocate memory to be used for framebuffer.
541 * - this callback would be called by user application
542 * with DRM_IOCTL_MODE_CREATE_DUMB command.
543 */
544
545 args->pitch = args->width * args->bpp >> 3;
Inki Dae2b358922012-03-16 18:47:05 +0900546 args->size = PAGE_ALIGN(args->pitch * args->height);
Inki Dae1c248b72011-10-04 19:19:01 +0900547
Inki Dae2b358922012-03-16 18:47:05 +0900548 exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size);
Inki Dae1c248b72011-10-04 19:19:01 +0900549 if (IS_ERR(exynos_gem_obj))
550 return PTR_ERR(exynos_gem_obj);
551
Joonyoung Shim23648392011-12-13 14:39:13 +0900552 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
553 &args->handle);
554 if (ret) {
555 exynos_drm_gem_destroy(exynos_gem_obj);
556 return ret;
557 }
558
Inki Dae1c248b72011-10-04 19:19:01 +0900559 return 0;
560}
561
562int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900563 struct drm_device *dev, uint32_t handle,
564 uint64_t *offset)
Inki Dae1c248b72011-10-04 19:19:01 +0900565{
566 struct exynos_drm_gem_obj *exynos_gem_obj;
567 struct drm_gem_object *obj;
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900568 int ret = 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900569
570 DRM_DEBUG_KMS("%s\n", __FILE__);
571
572 mutex_lock(&dev->struct_mutex);
573
574 /*
575 * get offset of memory allocated for drm framebuffer.
576 * - this callback would be called by user application
577 * with DRM_IOCTL_MODE_MAP_DUMB command.
578 */
579
580 obj = drm_gem_object_lookup(dev, file_priv, handle);
581 if (!obj) {
582 DRM_ERROR("failed to lookup gem object.\n");
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900583 ret = -EINVAL;
584 goto unlock;
Inki Dae1c248b72011-10-04 19:19:01 +0900585 }
586
587 exynos_gem_obj = to_exynos_gem_obj(obj);
588
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900589 if (!exynos_gem_obj->base.map_list.map) {
590 ret = drm_gem_create_mmap_offset(&exynos_gem_obj->base);
591 if (ret)
592 goto out;
593 }
Inki Dae1c248b72011-10-04 19:19:01 +0900594
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900595 *offset = (u64)exynos_gem_obj->base.map_list.hash.key << PAGE_SHIFT;
Inki Dae1c248b72011-10-04 19:19:01 +0900596 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
597
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900598out:
599 drm_gem_object_unreference(obj);
600unlock:
Inki Dae1c248b72011-10-04 19:19:01 +0900601 mutex_unlock(&dev->struct_mutex);
Joonyoung Shim2d91cf12011-12-13 14:32:24 +0900602 return ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900603}
604
Joonyoung Shimee5e7702011-12-13 14:20:23 +0900605int exynos_drm_gem_dumb_destroy(struct drm_file *file_priv,
606 struct drm_device *dev,
607 unsigned int handle)
608{
609 int ret;
610
611 DRM_DEBUG_KMS("%s\n", __FILE__);
612
613 /*
614 * obj->refcount and obj->handle_count are decreased and
615 * if both them are 0 then exynos_drm_gem_free_object()
616 * would be called by callback to release resources.
617 */
618 ret = drm_gem_handle_delete(file_priv, handle);
619 if (ret < 0) {
620 DRM_ERROR("failed to delete drm_gem_handle.\n");
621 return ret;
622 }
623
624 return 0;
625}
626
Inki Dae1c248b72011-10-04 19:19:01 +0900627int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
628{
629 struct drm_gem_object *obj = vma->vm_private_data;
630 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
631 struct drm_device *dev = obj->dev;
Inki Dae2b358922012-03-16 18:47:05 +0900632 unsigned long f_vaddr;
Inki Dae1c248b72011-10-04 19:19:01 +0900633 pgoff_t page_offset;
634 int ret;
635
636 page_offset = ((unsigned long)vmf->virtual_address -
637 vma->vm_start) >> PAGE_SHIFT;
Inki Dae2b358922012-03-16 18:47:05 +0900638 f_vaddr = (unsigned long)vmf->virtual_address;
Inki Dae1c248b72011-10-04 19:19:01 +0900639
640 mutex_lock(&dev->struct_mutex);
641
Inki Dae2b358922012-03-16 18:47:05 +0900642 /*
643 * allocate all pages as desired size if user wants to allocate
644 * physically non-continuous memory.
645 */
646 if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) {
647 ret = exynos_drm_gem_get_pages(obj);
648 if (ret < 0)
649 goto err;
650 }
Inki Dae1c248b72011-10-04 19:19:01 +0900651
Inki Dae2b358922012-03-16 18:47:05 +0900652 ret = exynos_drm_gem_map_pages(obj, vma, f_vaddr, page_offset);
653 if (ret < 0)
654 DRM_ERROR("failed to map pages.\n");
Inki Dae1c248b72011-10-04 19:19:01 +0900655
Inki Dae2b358922012-03-16 18:47:05 +0900656err:
Inki Dae1c248b72011-10-04 19:19:01 +0900657 mutex_unlock(&dev->struct_mutex);
658
659 return convert_to_vm_err_msg(ret);
660}
661
662int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
663{
664 int ret;
665
666 DRM_DEBUG_KMS("%s\n", __FILE__);
667
668 /* set vm_area_struct. */
669 ret = drm_gem_mmap(filp, vma);
670 if (ret < 0) {
671 DRM_ERROR("failed to mmap.\n");
672 return ret;
673 }
674
675 vma->vm_flags &= ~VM_PFNMAP;
676 vma->vm_flags |= VM_MIXEDMAP;
677
678 return ret;
679}
680
Inki Dae1c248b72011-10-04 19:19:01 +0900681MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
682MODULE_DESCRIPTION("Samsung SoC DRM GEM Module");
683MODULE_LICENSE("GPL");