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 int check_gem_flags(unsigned int flags
)
61 if (flags
& ~(EXYNOS_BO_MASK
)) {
62 DRM_ERROR("invalid flags.\n");
69 static void update_vm_cache_attr(struct exynos_drm_gem_obj
*obj
,
70 struct vm_area_struct
*vma
)
72 DRM_DEBUG_KMS("flags = 0x%x\n", obj
->flags
);
74 /* non-cachable as default. */
75 if (obj
->flags
& EXYNOS_BO_CACHABLE
)
76 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
77 else if (obj
->flags
& EXYNOS_BO_WC
)
79 pgprot_writecombine(vm_get_page_prot(vma
->vm_flags
));
82 pgprot_noncached(vm_get_page_prot(vma
->vm_flags
));
85 static unsigned long roundup_gem_size(unsigned long size
, unsigned int flags
)
87 if (!IS_NONCONTIG_BUFFER(flags
)) {
89 return roundup(size
, SECTION_SIZE
);
90 else if (size
>= SZ_64K
)
91 return roundup(size
, SZ_64K
);
96 return roundup(size
, PAGE_SIZE
);
99 struct page
**exynos_gem_get_pages(struct drm_gem_object
*obj
,
103 struct address_space
*mapping
;
104 struct page
*p
, **pages
;
107 /* This is the shared memory object that backs the GEM resource */
108 inode
= obj
->filp
->f_path
.dentry
->d_inode
;
109 mapping
= inode
->i_mapping
;
111 npages
= obj
->size
>> PAGE_SHIFT
;
113 pages
= drm_malloc_ab(npages
, sizeof(struct page
*));
115 return ERR_PTR(-ENOMEM
);
117 gfpmask
|= mapping_gfp_mask(mapping
);
119 for (i
= 0; i
< npages
; i
++) {
120 p
= shmem_read_mapping_page_gfp(mapping
, i
, gfpmask
);
130 page_cache_release(pages
[i
]);
132 drm_free_large(pages
);
133 return ERR_PTR(PTR_ERR(p
));
136 static void exynos_gem_put_pages(struct drm_gem_object
*obj
,
138 bool dirty
, bool accessed
)
142 npages
= obj
->size
>> PAGE_SHIFT
;
144 for (i
= 0; i
< npages
; i
++) {
146 set_page_dirty(pages
[i
]);
149 mark_page_accessed(pages
[i
]);
151 /* Undo the reference we took when populating the table */
152 page_cache_release(pages
[i
]);
155 drm_free_large(pages
);
158 static int exynos_drm_gem_map_pages(struct drm_gem_object
*obj
,
159 struct vm_area_struct
*vma
,
160 unsigned long f_vaddr
,
163 struct exynos_drm_gem_obj
*exynos_gem_obj
= to_exynos_gem_obj(obj
);
164 struct exynos_drm_gem_buf
*buf
= exynos_gem_obj
->buffer
;
167 if (exynos_gem_obj
->flags
& EXYNOS_BO_NONCONTIG
) {
171 pfn
= page_to_pfn(buf
->pages
[page_offset
++]);
173 pfn
= (buf
->dma_addr
>> PAGE_SHIFT
) + page_offset
;
175 return vm_insert_mixed(vma
, f_vaddr
, pfn
);
178 static int exynos_drm_gem_get_pages(struct drm_gem_object
*obj
)
180 struct exynos_drm_gem_obj
*exynos_gem_obj
= to_exynos_gem_obj(obj
);
181 struct exynos_drm_gem_buf
*buf
= exynos_gem_obj
->buffer
;
182 struct scatterlist
*sgl
;
184 unsigned int npages
, i
= 0;
188 DRM_DEBUG_KMS("already allocated.\n");
192 pages
= exynos_gem_get_pages(obj
, GFP_KERNEL
);
194 DRM_ERROR("failed to get pages.\n");
195 return PTR_ERR(pages
);
198 npages
= obj
->size
>> PAGE_SHIFT
;
199 buf
->page_size
= PAGE_SIZE
;
201 buf
->sgt
= kzalloc(sizeof(struct sg_table
), GFP_KERNEL
);
203 DRM_ERROR("failed to allocate sg table.\n");
208 ret
= sg_alloc_table(buf
->sgt
, npages
, GFP_KERNEL
);
210 DRM_ERROR("failed to initialize sg table.\n");
217 /* set all pages to sg list. */
219 sg_set_page(sgl
, pages
[i
], PAGE_SIZE
, 0);
220 sg_dma_address(sgl
) = page_to_phys(pages
[i
]);
225 /* add some codes for UNCACHED type here. TODO */
233 exynos_gem_put_pages(obj
, pages
, true, false);
238 static void exynos_drm_gem_put_pages(struct drm_gem_object
*obj
)
240 struct exynos_drm_gem_obj
*exynos_gem_obj
= to_exynos_gem_obj(obj
);
241 struct exynos_drm_gem_buf
*buf
= exynos_gem_obj
->buffer
;
244 * if buffer typs is EXYNOS_BO_NONCONTIG then release all pages
245 * allocated at gem fault handler.
247 sg_free_table(buf
->sgt
);
251 exynos_gem_put_pages(obj
, buf
->pages
, true, false);
254 /* add some codes for UNCACHED type here. TODO */
257 static int exynos_drm_gem_handle_create(struct drm_gem_object
*obj
,
258 struct drm_file
*file_priv
,
259 unsigned int *handle
)
264 * allocate a id of idr table where the obj is registered
265 * and handle has the id what user can see.
267 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
271 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle
);
273 /* drop reference from allocate - handle holds it now. */
274 drm_gem_object_unreference_unlocked(obj
);
279 void exynos_drm_gem_destroy(struct exynos_drm_gem_obj
*exynos_gem_obj
)
281 struct drm_gem_object
*obj
;
282 struct exynos_drm_gem_buf
*buf
;
284 DRM_DEBUG_KMS("%s\n", __FILE__
);
286 obj
= &exynos_gem_obj
->base
;
287 buf
= exynos_gem_obj
->buffer
;
289 DRM_DEBUG_KMS("handle count = %d\n", atomic_read(&obj
->handle_count
));
294 if (exynos_gem_obj
->flags
& EXYNOS_BO_NONCONTIG
)
295 exynos_drm_gem_put_pages(obj
);
297 exynos_drm_free_buf(obj
->dev
, exynos_gem_obj
->flags
, buf
);
299 exynos_drm_fini_buf(obj
->dev
, buf
);
300 exynos_gem_obj
->buffer
= NULL
;
302 if (obj
->map_list
.map
)
303 drm_gem_free_mmap_offset(obj
);
305 /* release file pointer to gem object. */
306 drm_gem_object_release(obj
);
308 kfree(exynos_gem_obj
);
309 exynos_gem_obj
= NULL
;
312 struct exynos_drm_gem_obj
*exynos_drm_gem_init(struct drm_device
*dev
,
315 struct exynos_drm_gem_obj
*exynos_gem_obj
;
316 struct drm_gem_object
*obj
;
319 exynos_gem_obj
= kzalloc(sizeof(*exynos_gem_obj
), GFP_KERNEL
);
320 if (!exynos_gem_obj
) {
321 DRM_ERROR("failed to allocate exynos gem object\n");
325 exynos_gem_obj
->size
= size
;
326 obj
= &exynos_gem_obj
->base
;
328 ret
= drm_gem_object_init(dev
, obj
, size
);
330 DRM_ERROR("failed to initialize gem object\n");
331 kfree(exynos_gem_obj
);
335 DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj
->filp
);
337 return exynos_gem_obj
;
340 struct exynos_drm_gem_obj
*exynos_drm_gem_create(struct drm_device
*dev
,
344 struct exynos_drm_gem_obj
*exynos_gem_obj
;
345 struct exynos_drm_gem_buf
*buf
;
349 DRM_ERROR("invalid size.\n");
350 return ERR_PTR(-EINVAL
);
353 size
= roundup_gem_size(size
, flags
);
354 DRM_DEBUG_KMS("%s\n", __FILE__
);
356 ret
= check_gem_flags(flags
);
360 buf
= exynos_drm_init_buf(dev
, size
);
362 return ERR_PTR(-ENOMEM
);
364 exynos_gem_obj
= exynos_drm_gem_init(dev
, size
);
365 if (!exynos_gem_obj
) {
370 exynos_gem_obj
->buffer
= buf
;
372 /* set memory type and cache attribute from user side. */
373 exynos_gem_obj
->flags
= flags
;
376 * allocate all pages as desired size if user wants to allocate
377 * physically non-continuous memory.
379 if (flags
& EXYNOS_BO_NONCONTIG
) {
380 ret
= exynos_drm_gem_get_pages(&exynos_gem_obj
->base
);
382 drm_gem_object_release(&exynos_gem_obj
->base
);
386 ret
= exynos_drm_alloc_buf(dev
, buf
, flags
);
388 drm_gem_object_release(&exynos_gem_obj
->base
);
393 return exynos_gem_obj
;
396 exynos_drm_fini_buf(dev
, buf
);
400 int exynos_drm_gem_create_ioctl(struct drm_device
*dev
, void *data
,
401 struct drm_file
*file_priv
)
403 struct drm_exynos_gem_create
*args
= data
;
404 struct exynos_drm_gem_obj
*exynos_gem_obj
;
407 DRM_DEBUG_KMS("%s\n", __FILE__
);
409 exynos_gem_obj
= exynos_drm_gem_create(dev
, args
->flags
, args
->size
);
410 if (IS_ERR(exynos_gem_obj
))
411 return PTR_ERR(exynos_gem_obj
);
413 ret
= exynos_drm_gem_handle_create(&exynos_gem_obj
->base
, file_priv
,
416 exynos_drm_gem_destroy(exynos_gem_obj
);
423 void *exynos_drm_gem_get_dma_addr(struct drm_device
*dev
,
424 unsigned int gem_handle
,
425 struct drm_file
*file_priv
)
427 struct exynos_drm_gem_obj
*exynos_gem_obj
;
428 struct drm_gem_object
*obj
;
430 obj
= drm_gem_object_lookup(dev
, file_priv
, gem_handle
);
432 DRM_ERROR("failed to lookup gem object.\n");
433 return ERR_PTR(-EINVAL
);
436 exynos_gem_obj
= to_exynos_gem_obj(obj
);
438 if (exynos_gem_obj
->flags
& EXYNOS_BO_NONCONTIG
) {
439 DRM_DEBUG_KMS("not support NONCONTIG type.\n");
440 drm_gem_object_unreference_unlocked(obj
);
443 return ERR_PTR(-EINVAL
);
446 return &exynos_gem_obj
->buffer
->dma_addr
;
449 void exynos_drm_gem_put_dma_addr(struct drm_device
*dev
,
450 unsigned int gem_handle
,
451 struct drm_file
*file_priv
)
453 struct exynos_drm_gem_obj
*exynos_gem_obj
;
454 struct drm_gem_object
*obj
;
456 obj
= drm_gem_object_lookup(dev
, file_priv
, gem_handle
);
458 DRM_ERROR("failed to lookup gem object.\n");
462 exynos_gem_obj
= to_exynos_gem_obj(obj
);
464 if (exynos_gem_obj
->flags
& EXYNOS_BO_NONCONTIG
) {
465 DRM_DEBUG_KMS("not support NONCONTIG type.\n");
466 drm_gem_object_unreference_unlocked(obj
);
472 drm_gem_object_unreference_unlocked(obj
);
475 * decrease obj->refcount one more time because we has already
476 * increased it at exynos_drm_gem_get_dma_addr().
478 drm_gem_object_unreference_unlocked(obj
);
481 int exynos_drm_gem_map_offset_ioctl(struct drm_device
*dev
, void *data
,
482 struct drm_file
*file_priv
)
484 struct drm_exynos_gem_map_off
*args
= data
;
486 DRM_DEBUG_KMS("%s\n", __FILE__
);
488 DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n",
489 args
->handle
, (unsigned long)args
->offset
);
491 if (!(dev
->driver
->driver_features
& DRIVER_GEM
)) {
492 DRM_ERROR("does not support GEM.\n");
496 return exynos_drm_gem_dumb_map_offset(file_priv
, dev
, args
->handle
,
500 static int exynos_drm_gem_mmap_buffer(struct file
*filp
,
501 struct vm_area_struct
*vma
)
503 struct drm_gem_object
*obj
= filp
->private_data
;
504 struct exynos_drm_gem_obj
*exynos_gem_obj
= to_exynos_gem_obj(obj
);
505 struct exynos_drm_gem_buf
*buffer
;
506 unsigned long pfn
, vm_size
, usize
, uaddr
= vma
->vm_start
;
509 DRM_DEBUG_KMS("%s\n", __FILE__
);
511 vma
->vm_flags
|= (VM_IO
| VM_RESERVED
);
513 update_vm_cache_attr(exynos_gem_obj
, vma
);
515 vm_size
= usize
= vma
->vm_end
- vma
->vm_start
;
518 * a buffer contains information to physically continuous memory
519 * allocated by user request or at framebuffer creation.
521 buffer
= exynos_gem_obj
->buffer
;
523 /* check if user-requested size is valid. */
524 if (vm_size
> buffer
->size
)
527 if (exynos_gem_obj
->flags
& EXYNOS_BO_NONCONTIG
) {
533 vma
->vm_flags
|= VM_MIXEDMAP
;
536 ret
= vm_insert_page(vma
, uaddr
, buffer
->pages
[i
++]);
538 DRM_ERROR("failed to remap user space.\n");
547 * get page frame number to physical memory to be mapped
550 pfn
= ((unsigned long)exynos_gem_obj
->buffer
->dma_addr
) >>
553 DRM_DEBUG_KMS("pfn = 0x%lx\n", pfn
);
555 if (remap_pfn_range(vma
, vma
->vm_start
, pfn
, vm_size
,
556 vma
->vm_page_prot
)) {
557 DRM_ERROR("failed to remap pfn range.\n");
565 static const struct file_operations exynos_drm_gem_fops
= {
566 .mmap
= exynos_drm_gem_mmap_buffer
,
569 int exynos_drm_gem_mmap_ioctl(struct drm_device
*dev
, void *data
,
570 struct drm_file
*file_priv
)
572 struct drm_exynos_gem_mmap
*args
= data
;
573 struct drm_gem_object
*obj
;
576 DRM_DEBUG_KMS("%s\n", __FILE__
);
578 if (!(dev
->driver
->driver_features
& DRIVER_GEM
)) {
579 DRM_ERROR("does not support GEM.\n");
583 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
585 DRM_ERROR("failed to lookup gem object.\n");
589 obj
->filp
->f_op
= &exynos_drm_gem_fops
;
590 obj
->filp
->private_data
= obj
;
592 addr
= vm_mmap(obj
->filp
, 0, args
->size
,
593 PROT_READ
| PROT_WRITE
, MAP_SHARED
, 0);
595 drm_gem_object_unreference_unlocked(obj
);
597 if (IS_ERR((void *)addr
))
598 return PTR_ERR((void *)addr
);
602 DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args
->mapped
);
607 int exynos_drm_gem_get_ioctl(struct drm_device
*dev
, void *data
,
608 struct drm_file
*file_priv
)
609 { struct exynos_drm_gem_obj
*exynos_gem_obj
;
610 struct drm_exynos_gem_info
*args
= data
;
611 struct drm_gem_object
*obj
;
613 mutex_lock(&dev
->struct_mutex
);
615 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
617 DRM_ERROR("failed to lookup gem object.\n");
618 mutex_unlock(&dev
->struct_mutex
);
622 exynos_gem_obj
= to_exynos_gem_obj(obj
);
624 args
->flags
= exynos_gem_obj
->flags
;
625 args
->size
= exynos_gem_obj
->size
;
627 drm_gem_object_unreference(obj
);
628 mutex_unlock(&dev
->struct_mutex
);
633 int exynos_drm_gem_init_object(struct drm_gem_object
*obj
)
635 DRM_DEBUG_KMS("%s\n", __FILE__
);
640 void exynos_drm_gem_free_object(struct drm_gem_object
*obj
)
642 struct exynos_drm_gem_obj
*exynos_gem_obj
;
643 struct exynos_drm_gem_buf
*buf
;
645 DRM_DEBUG_KMS("%s\n", __FILE__
);
647 exynos_gem_obj
= to_exynos_gem_obj(obj
);
648 buf
= exynos_gem_obj
->buffer
;
650 if (obj
->import_attach
)
651 drm_prime_gem_destroy(obj
, buf
->sgt
);
653 exynos_drm_gem_destroy(to_exynos_gem_obj(obj
));
656 int exynos_drm_gem_dumb_create(struct drm_file
*file_priv
,
657 struct drm_device
*dev
,
658 struct drm_mode_create_dumb
*args
)
660 struct exynos_drm_gem_obj
*exynos_gem_obj
;
663 DRM_DEBUG_KMS("%s\n", __FILE__
);
666 * alocate memory to be used for framebuffer.
667 * - this callback would be called by user application
668 * with DRM_IOCTL_MODE_CREATE_DUMB command.
671 args
->pitch
= args
->width
* args
->bpp
>> 3;
672 args
->size
= PAGE_ALIGN(args
->pitch
* args
->height
);
674 exynos_gem_obj
= exynos_drm_gem_create(dev
, args
->flags
, args
->size
);
675 if (IS_ERR(exynos_gem_obj
))
676 return PTR_ERR(exynos_gem_obj
);
678 ret
= exynos_drm_gem_handle_create(&exynos_gem_obj
->base
, file_priv
,
681 exynos_drm_gem_destroy(exynos_gem_obj
);
688 int exynos_drm_gem_dumb_map_offset(struct drm_file
*file_priv
,
689 struct drm_device
*dev
, uint32_t handle
,
692 struct exynos_drm_gem_obj
*exynos_gem_obj
;
693 struct drm_gem_object
*obj
;
696 DRM_DEBUG_KMS("%s\n", __FILE__
);
698 mutex_lock(&dev
->struct_mutex
);
701 * get offset of memory allocated for drm framebuffer.
702 * - this callback would be called by user application
703 * with DRM_IOCTL_MODE_MAP_DUMB command.
706 obj
= drm_gem_object_lookup(dev
, file_priv
, handle
);
708 DRM_ERROR("failed to lookup gem object.\n");
713 exynos_gem_obj
= to_exynos_gem_obj(obj
);
715 if (!exynos_gem_obj
->base
.map_list
.map
) {
716 ret
= drm_gem_create_mmap_offset(&exynos_gem_obj
->base
);
721 *offset
= (u64
)exynos_gem_obj
->base
.map_list
.hash
.key
<< PAGE_SHIFT
;
722 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset
);
725 drm_gem_object_unreference(obj
);
727 mutex_unlock(&dev
->struct_mutex
);
731 int exynos_drm_gem_dumb_destroy(struct drm_file
*file_priv
,
732 struct drm_device
*dev
,
737 DRM_DEBUG_KMS("%s\n", __FILE__
);
740 * obj->refcount and obj->handle_count are decreased and
741 * if both them are 0 then exynos_drm_gem_free_object()
742 * would be called by callback to release resources.
744 ret
= drm_gem_handle_delete(file_priv
, handle
);
746 DRM_ERROR("failed to delete drm_gem_handle.\n");
753 int exynos_drm_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
755 struct drm_gem_object
*obj
= vma
->vm_private_data
;
756 struct drm_device
*dev
= obj
->dev
;
757 unsigned long f_vaddr
;
761 page_offset
= ((unsigned long)vmf
->virtual_address
-
762 vma
->vm_start
) >> PAGE_SHIFT
;
763 f_vaddr
= (unsigned long)vmf
->virtual_address
;
765 mutex_lock(&dev
->struct_mutex
);
767 ret
= exynos_drm_gem_map_pages(obj
, vma
, f_vaddr
, page_offset
);
769 DRM_ERROR("failed to map pages.\n");
771 mutex_unlock(&dev
->struct_mutex
);
773 return convert_to_vm_err_msg(ret
);
776 int exynos_drm_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
778 struct exynos_drm_gem_obj
*exynos_gem_obj
;
779 struct drm_gem_object
*obj
;
782 DRM_DEBUG_KMS("%s\n", __FILE__
);
784 /* set vm_area_struct. */
785 ret
= drm_gem_mmap(filp
, vma
);
787 DRM_ERROR("failed to mmap.\n");
791 obj
= vma
->vm_private_data
;
792 exynos_gem_obj
= to_exynos_gem_obj(obj
);
794 ret
= check_gem_flags(exynos_gem_obj
->flags
);
796 drm_gem_vm_close(vma
);
797 drm_gem_free_mmap_offset(obj
);
801 vma
->vm_flags
&= ~VM_PFNMAP
;
802 vma
->vm_flags
|= VM_MIXEDMAP
;
804 update_vm_cache_attr(exynos_gem_obj
, vma
);