3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
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:
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
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.
29 #include <linux/shmem_fs.h>
30 #include <drm/exynos_drm.h>
32 #include "exynos_drm_drv.h"
33 #include "exynos_drm_gem.h"
34 #include "exynos_drm_buf.h"
36 static unsigned int convert_to_vm_err_msg(int msg)
44 out_msg = VM_FAULT_NOPAGE;
48 out_msg = VM_FAULT_OOM;
52 out_msg = VM_FAULT_SIGBUS;
59 static unsigned int mask_gem_flags(unsigned int flags)
61 return flags &= EXYNOS_BO_NONCONTIG;
64 static struct page **exynos_gem_get_pages(struct drm_gem_object *obj,
68 struct address_space *mapping;
69 struct page *p, **pages;
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;
76 npages = obj->size >> PAGE_SHIFT;
78 pages = drm_malloc_ab(npages, sizeof(struct page *));
80 return ERR_PTR(-ENOMEM);
82 gfpmask |= mapping_gfp_mask(mapping);
84 for (i = 0; i < npages; i++) {
85 p = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
95 page_cache_release(pages[i]);
97 drm_free_large(pages);
98 return ERR_PTR(PTR_ERR(p));
101 static void exynos_gem_put_pages(struct drm_gem_object *obj,
103 bool dirty, bool accessed)
107 npages = obj->size >> PAGE_SHIFT;
109 for (i = 0; i < npages; i++) {
111 set_page_dirty(pages[i]);
114 mark_page_accessed(pages[i]);
116 /* Undo the reference we took when populating the table */
117 page_cache_release(pages[i]);
120 drm_free_large(pages);
123 static int exynos_drm_gem_map_pages(struct drm_gem_object *obj,
124 struct vm_area_struct *vma,
125 unsigned long f_vaddr,
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;
132 if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) {
133 unsigned long usize = buf->size;
139 pfn = page_to_pfn(buf->pages[page_offset++]);
140 vm_insert_mixed(vma, f_vaddr, pfn);
141 f_vaddr += PAGE_SIZE;
148 pfn = (buf->dma_addr >> PAGE_SHIFT) + page_offset;
150 return vm_insert_mixed(vma, f_vaddr, pfn);
153 static int exynos_drm_gem_get_pages(struct drm_gem_object *obj)
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;
159 unsigned int npages, i = 0;
163 DRM_DEBUG_KMS("already allocated.\n");
167 pages = exynos_gem_get_pages(obj, GFP_KERNEL);
169 DRM_ERROR("failed to get pages.\n");
170 return PTR_ERR(pages);
173 npages = obj->size >> PAGE_SHIFT;
175 buf->sgt = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
177 DRM_ERROR("failed to allocate sg table.\n");
182 ret = sg_alloc_table(buf->sgt, npages, GFP_KERNEL);
184 DRM_ERROR("failed to initialize sg table.\n");
191 /* set all pages to sg list. */
193 sg_set_page(sgl, pages[i], PAGE_SIZE, 0);
194 sg_dma_address(sgl) = page_to_phys(pages[i]);
199 /* add some codes for UNCACHED type here. TODO */
207 exynos_gem_put_pages(obj, pages, true, false);
212 static void exynos_drm_gem_put_pages(struct drm_gem_object *obj)
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;
218 * if buffer typs is EXYNOS_BO_NONCONTIG then release all pages
219 * allocated at gem fault handler.
221 sg_free_table(buf->sgt);
225 exynos_gem_put_pages(obj, buf->pages, true, false);
228 /* add some codes for UNCACHED type here. TODO */
231 static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
232 struct drm_file *file_priv,
233 unsigned int *handle)
238 * allocate a id of idr table where the obj is registered
239 * and handle has the id what user can see.
241 ret = drm_gem_handle_create(file_priv, obj, handle);
245 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
247 /* drop reference from allocate - handle holds it now. */
248 drm_gem_object_unreference_unlocked(obj);
253 void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj)
255 struct drm_gem_object *obj;
257 DRM_DEBUG_KMS("%s\n", __FILE__);
262 obj = &exynos_gem_obj->base;
264 DRM_DEBUG_KMS("handle count = %d\n", atomic_read(&obj->handle_count));
266 if ((exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) &&
267 exynos_gem_obj->buffer->pages)
268 exynos_drm_gem_put_pages(obj);
270 exynos_drm_free_buf(obj->dev, exynos_gem_obj->flags,
271 exynos_gem_obj->buffer);
273 exynos_drm_fini_buf(obj->dev, exynos_gem_obj->buffer);
274 exynos_gem_obj->buffer = NULL;
276 if (obj->map_list.map)
277 drm_gem_free_mmap_offset(obj);
279 /* release file pointer to gem object. */
280 drm_gem_object_release(obj);
282 kfree(exynos_gem_obj);
283 exynos_gem_obj = NULL;
286 static struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev,
289 struct exynos_drm_gem_obj *exynos_gem_obj;
290 struct drm_gem_object *obj;
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");
299 exynos_gem_obj->size = size;
300 obj = &exynos_gem_obj->base;
302 ret = drm_gem_object_init(dev, obj, size);
304 DRM_ERROR("failed to initialize gem object\n");
305 kfree(exynos_gem_obj);
309 DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);
311 return exynos_gem_obj;
314 struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
318 struct exynos_drm_gem_obj *exynos_gem_obj;
319 struct exynos_drm_gem_buf *buf;
322 size = roundup(size, PAGE_SIZE);
323 DRM_DEBUG_KMS("%s: size = 0x%lx\n", __FILE__, size);
325 flags = mask_gem_flags(flags);
327 buf = exynos_drm_init_buf(dev, size);
329 return ERR_PTR(-ENOMEM);
331 exynos_gem_obj = exynos_drm_gem_init(dev, size);
332 if (!exynos_gem_obj) {
337 exynos_gem_obj->buffer = buf;
339 /* set memory type and cache attribute from user side. */
340 exynos_gem_obj->flags = flags;
343 * allocate all pages as desired size if user wants to allocate
344 * physically non-continuous memory.
346 if (flags & EXYNOS_BO_NONCONTIG) {
347 ret = exynos_drm_gem_get_pages(&exynos_gem_obj->base);
349 drm_gem_object_release(&exynos_gem_obj->base);
353 ret = exynos_drm_alloc_buf(dev, buf, flags);
355 drm_gem_object_release(&exynos_gem_obj->base);
360 return exynos_gem_obj;
362 exynos_drm_fini_buf(dev, buf);
366 int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
367 struct drm_file *file_priv)
369 struct drm_exynos_gem_create *args = data;
370 struct exynos_drm_gem_obj *exynos_gem_obj;
373 DRM_DEBUG_KMS("%s\n", __FILE__);
375 exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size);
376 if (IS_ERR(exynos_gem_obj))
377 return PTR_ERR(exynos_gem_obj);
379 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
382 exynos_drm_gem_destroy(exynos_gem_obj);
389 void *exynos_drm_gem_get_dma_addr(struct drm_device *dev,
390 unsigned int gem_handle,
391 struct drm_file *file_priv)
393 struct exynos_drm_gem_obj *exynos_gem_obj;
394 struct drm_gem_object *obj;
396 obj = drm_gem_object_lookup(dev, file_priv, gem_handle);
398 DRM_ERROR("failed to lookup gem object.\n");
399 return ERR_PTR(-EINVAL);
402 exynos_gem_obj = to_exynos_gem_obj(obj);
404 if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) {
405 DRM_DEBUG_KMS("not support NONCONTIG type.\n");
406 drm_gem_object_unreference_unlocked(obj);
409 return ERR_PTR(-EINVAL);
412 return &exynos_gem_obj->buffer->dma_addr;
415 void exynos_drm_gem_put_dma_addr(struct drm_device *dev,
416 unsigned int gem_handle,
417 struct drm_file *file_priv)
419 struct exynos_drm_gem_obj *exynos_gem_obj;
420 struct drm_gem_object *obj;
422 obj = drm_gem_object_lookup(dev, file_priv, gem_handle);
424 DRM_ERROR("failed to lookup gem object.\n");
428 exynos_gem_obj = to_exynos_gem_obj(obj);
430 if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) {
431 DRM_DEBUG_KMS("not support NONCONTIG type.\n");
432 drm_gem_object_unreference_unlocked(obj);
438 drm_gem_object_unreference_unlocked(obj);
441 * decrease obj->refcount one more time because we has already
442 * increased it at exynos_drm_gem_get_dma_addr().
444 drm_gem_object_unreference_unlocked(obj);
447 int exynos_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data,
448 struct drm_file *file_priv)
450 struct drm_exynos_gem_map_off *args = data;
452 DRM_DEBUG_KMS("%s\n", __FILE__);
454 DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n",
455 args->handle, (unsigned long)args->offset);
457 if (!(dev->driver->driver_features & DRIVER_GEM)) {
458 DRM_ERROR("does not support GEM.\n");
462 return exynos_drm_gem_dumb_map_offset(file_priv, dev, args->handle,
466 static int exynos_drm_gem_mmap_buffer(struct file *filp,
467 struct vm_area_struct *vma)
469 struct drm_gem_object *obj = filp->private_data;
470 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
471 struct exynos_drm_gem_buf *buffer;
472 unsigned long pfn, vm_size, usize, uaddr = vma->vm_start;
475 DRM_DEBUG_KMS("%s\n", __FILE__);
477 vma->vm_flags |= (VM_IO | VM_RESERVED);
479 /* in case of direct mapping, always having non-cachable attribute */
480 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
482 vm_size = usize = vma->vm_end - vma->vm_start;
485 * a buffer contains information to physically continuous memory
486 * allocated by user request or at framebuffer creation.
488 buffer = exynos_gem_obj->buffer;
490 /* check if user-requested size is valid. */
491 if (vm_size > buffer->size)
494 if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) {
501 ret = vm_insert_page(vma, uaddr, buffer->pages[i++]);
503 DRM_ERROR("failed to remap user space.\n");
512 * get page frame number to physical memory to be mapped
515 pfn = ((unsigned long)exynos_gem_obj->buffer->dma_addr) >>
518 DRM_DEBUG_KMS("pfn = 0x%lx\n", pfn);
520 if (remap_pfn_range(vma, vma->vm_start, pfn, vm_size,
521 vma->vm_page_prot)) {
522 DRM_ERROR("failed to remap pfn range.\n");
530 static const struct file_operations exynos_drm_gem_fops = {
531 .mmap = exynos_drm_gem_mmap_buffer,
534 int exynos_drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
535 struct drm_file *file_priv)
537 struct drm_exynos_gem_mmap *args = data;
538 struct drm_gem_object *obj;
541 DRM_DEBUG_KMS("%s\n", __FILE__);
543 if (!(dev->driver->driver_features & DRIVER_GEM)) {
544 DRM_ERROR("does not support GEM.\n");
548 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
550 DRM_ERROR("failed to lookup gem object.\n");
554 obj->filp->f_op = &exynos_drm_gem_fops;
555 obj->filp->private_data = obj;
557 down_write(¤t->mm->mmap_sem);
558 addr = do_mmap(obj->filp, 0, args->size,
559 PROT_READ | PROT_WRITE, MAP_SHARED, 0);
560 up_write(¤t->mm->mmap_sem);
562 drm_gem_object_unreference_unlocked(obj);
564 if (IS_ERR((void *)addr))
565 return PTR_ERR((void *)addr);
569 DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args->mapped);
574 int exynos_drm_gem_init_object(struct drm_gem_object *obj)
576 DRM_DEBUG_KMS("%s\n", __FILE__);
581 void exynos_drm_gem_free_object(struct drm_gem_object *obj)
583 DRM_DEBUG_KMS("%s\n", __FILE__);
585 exynos_drm_gem_destroy(to_exynos_gem_obj(obj));
588 int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
589 struct drm_device *dev,
590 struct drm_mode_create_dumb *args)
592 struct exynos_drm_gem_obj *exynos_gem_obj;
595 DRM_DEBUG_KMS("%s\n", __FILE__);
598 * alocate memory to be used for framebuffer.
599 * - this callback would be called by user application
600 * with DRM_IOCTL_MODE_CREATE_DUMB command.
603 args->pitch = args->width * args->bpp >> 3;
604 args->size = PAGE_ALIGN(args->pitch * args->height);
606 exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size);
607 if (IS_ERR(exynos_gem_obj))
608 return PTR_ERR(exynos_gem_obj);
610 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
613 exynos_drm_gem_destroy(exynos_gem_obj);
620 int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
621 struct drm_device *dev, uint32_t handle,
624 struct exynos_drm_gem_obj *exynos_gem_obj;
625 struct drm_gem_object *obj;
628 DRM_DEBUG_KMS("%s\n", __FILE__);
630 mutex_lock(&dev->struct_mutex);
633 * get offset of memory allocated for drm framebuffer.
634 * - this callback would be called by user application
635 * with DRM_IOCTL_MODE_MAP_DUMB command.
638 obj = drm_gem_object_lookup(dev, file_priv, handle);
640 DRM_ERROR("failed to lookup gem object.\n");
645 exynos_gem_obj = to_exynos_gem_obj(obj);
647 if (!exynos_gem_obj->base.map_list.map) {
648 ret = drm_gem_create_mmap_offset(&exynos_gem_obj->base);
653 *offset = (u64)exynos_gem_obj->base.map_list.hash.key << PAGE_SHIFT;
654 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
657 drm_gem_object_unreference(obj);
659 mutex_unlock(&dev->struct_mutex);
663 int exynos_drm_gem_dumb_destroy(struct drm_file *file_priv,
664 struct drm_device *dev,
669 DRM_DEBUG_KMS("%s\n", __FILE__);
672 * obj->refcount and obj->handle_count are decreased and
673 * if both them are 0 then exynos_drm_gem_free_object()
674 * would be called by callback to release resources.
676 ret = drm_gem_handle_delete(file_priv, handle);
678 DRM_ERROR("failed to delete drm_gem_handle.\n");
685 int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
687 struct drm_gem_object *obj = vma->vm_private_data;
688 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
689 struct drm_device *dev = obj->dev;
690 unsigned long f_vaddr;
694 page_offset = ((unsigned long)vmf->virtual_address -
695 vma->vm_start) >> PAGE_SHIFT;
696 f_vaddr = (unsigned long)vmf->virtual_address;
698 mutex_lock(&dev->struct_mutex);
701 * allocate all pages as desired size if user wants to allocate
702 * physically non-continuous memory.
704 if (exynos_gem_obj->flags & EXYNOS_BO_NONCONTIG) {
705 ret = exynos_drm_gem_get_pages(obj);
710 ret = exynos_drm_gem_map_pages(obj, vma, f_vaddr, page_offset);
712 DRM_ERROR("failed to map pages.\n");
715 mutex_unlock(&dev->struct_mutex);
717 return convert_to_vm_err_msg(ret);
720 int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
724 DRM_DEBUG_KMS("%s\n", __FILE__);
726 /* set vm_area_struct. */
727 ret = drm_gem_mmap(filp, vma);
729 DRM_ERROR("failed to mmap.\n");
733 vma->vm_flags &= ~VM_PFNMAP;
734 vma->vm_flags |= VM_MIXEDMAP;