3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
6 * 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.
13 #include <drm/drm_vma_manager.h>
15 #include <linux/shmem_fs.h>
16 #include <drm/exynos_drm.h>
18 #include "exynos_drm_drv.h"
19 #include "exynos_drm_gem.h"
20 #include "exynos_drm_buf.h"
21 #include "exynos_drm_iommu.h"
23 static unsigned int convert_to_vm_err_msg(int msg
)
31 out_msg
= VM_FAULT_NOPAGE
;
35 out_msg
= VM_FAULT_OOM
;
39 out_msg
= VM_FAULT_SIGBUS
;
46 static int check_gem_flags(unsigned int flags
)
48 if (flags
& ~(EXYNOS_BO_MASK
)) {
49 DRM_ERROR("invalid flags.\n");
56 static void update_vm_cache_attr(struct exynos_drm_gem_obj
*obj
,
57 struct vm_area_struct
*vma
)
59 DRM_DEBUG_KMS("flags = 0x%x\n", obj
->flags
);
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
)
66 pgprot_writecombine(vm_get_page_prot(vma
->vm_flags
));
69 pgprot_noncached(vm_get_page_prot(vma
->vm_flags
));
72 static unsigned long roundup_gem_size(unsigned long size
, unsigned int flags
)
76 return roundup(size
, PAGE_SIZE
);
79 static int exynos_drm_gem_map_buf(struct drm_gem_object
*obj
,
80 struct vm_area_struct
*vma
,
81 unsigned long f_vaddr
,
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
;
86 struct scatterlist
*sgl
;
93 if (page_offset
>= (buf
->size
>> PAGE_SHIFT
)) {
94 DRM_ERROR("invalid page offset\n");
99 for_each_sg(buf
->sgt
->sgl
, sgl
, buf
->sgt
->nents
, i
) {
100 if (page_offset
< (sgl
->length
>> PAGE_SHIFT
))
102 page_offset
-= (sgl
->length
>> PAGE_SHIFT
);
105 pfn
= __phys_to_pfn(sg_phys(sgl
)) + page_offset
;
107 return vm_insert_mixed(vma
, f_vaddr
, pfn
);
110 static int exynos_drm_gem_handle_create(struct drm_gem_object
*obj
,
111 struct drm_file
*file_priv
,
112 unsigned int *handle
)
117 * allocate a id of idr table where the obj is registered
118 * and handle has the id what user can see.
120 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
124 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle
);
126 /* drop reference from allocate - handle holds it now. */
127 drm_gem_object_unreference_unlocked(obj
);
132 void exynos_drm_gem_destroy(struct exynos_drm_gem_obj
*exynos_gem_obj
)
134 struct drm_gem_object
*obj
;
135 struct exynos_drm_gem_buf
*buf
;
137 obj
= &exynos_gem_obj
->base
;
138 buf
= exynos_gem_obj
->buffer
;
140 DRM_DEBUG_KMS("handle count = %d\n", obj
->handle_count
);
143 * do not release memory region from exporter.
145 * the region will be released by exporter
146 * once dmabuf's refcount becomes 0.
148 if (obj
->import_attach
)
151 exynos_drm_free_buf(obj
->dev
, exynos_gem_obj
->flags
, buf
);
154 exynos_drm_fini_buf(obj
->dev
, buf
);
155 exynos_gem_obj
->buffer
= NULL
;
157 drm_gem_free_mmap_offset(obj
);
159 /* release file pointer to gem object. */
160 drm_gem_object_release(obj
);
162 kfree(exynos_gem_obj
);
163 exynos_gem_obj
= NULL
;
166 unsigned long exynos_drm_gem_get_size(struct drm_device
*dev
,
167 unsigned int gem_handle
,
168 struct drm_file
*file_priv
)
170 struct exynos_drm_gem_obj
*exynos_gem_obj
;
171 struct drm_gem_object
*obj
;
173 obj
= drm_gem_object_lookup(dev
, file_priv
, gem_handle
);
175 DRM_ERROR("failed to lookup gem object.\n");
179 exynos_gem_obj
= to_exynos_gem_obj(obj
);
181 drm_gem_object_unreference_unlocked(obj
);
183 return exynos_gem_obj
->buffer
->size
;
187 struct exynos_drm_gem_obj
*exynos_drm_gem_init(struct drm_device
*dev
,
190 struct exynos_drm_gem_obj
*exynos_gem_obj
;
191 struct drm_gem_object
*obj
;
194 exynos_gem_obj
= kzalloc(sizeof(*exynos_gem_obj
), GFP_KERNEL
);
198 exynos_gem_obj
->size
= size
;
199 obj
= &exynos_gem_obj
->base
;
201 ret
= drm_gem_object_init(dev
, obj
, size
);
203 DRM_ERROR("failed to initialize gem object\n");
204 kfree(exynos_gem_obj
);
208 DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj
->filp
);
210 return exynos_gem_obj
;
213 struct exynos_drm_gem_obj
*exynos_drm_gem_create(struct drm_device
*dev
,
217 struct exynos_drm_gem_obj
*exynos_gem_obj
;
218 struct exynos_drm_gem_buf
*buf
;
222 DRM_ERROR("invalid size.\n");
223 return ERR_PTR(-EINVAL
);
226 size
= roundup_gem_size(size
, flags
);
228 ret
= check_gem_flags(flags
);
232 buf
= exynos_drm_init_buf(dev
, size
);
234 return ERR_PTR(-ENOMEM
);
236 exynos_gem_obj
= exynos_drm_gem_init(dev
, size
);
237 if (!exynos_gem_obj
) {
242 exynos_gem_obj
->buffer
= buf
;
244 /* set memory type and cache attribute from user side. */
245 exynos_gem_obj
->flags
= flags
;
247 ret
= exynos_drm_alloc_buf(dev
, buf
, flags
);
251 return exynos_gem_obj
;
254 drm_gem_object_release(&exynos_gem_obj
->base
);
255 kfree(exynos_gem_obj
);
257 exynos_drm_fini_buf(dev
, buf
);
261 int exynos_drm_gem_create_ioctl(struct drm_device
*dev
, void *data
,
262 struct drm_file
*file_priv
)
264 struct drm_exynos_gem_create
*args
= data
;
265 struct exynos_drm_gem_obj
*exynos_gem_obj
;
268 exynos_gem_obj
= exynos_drm_gem_create(dev
, args
->flags
, args
->size
);
269 if (IS_ERR(exynos_gem_obj
))
270 return PTR_ERR(exynos_gem_obj
);
272 ret
= exynos_drm_gem_handle_create(&exynos_gem_obj
->base
, file_priv
,
275 exynos_drm_gem_destroy(exynos_gem_obj
);
282 dma_addr_t
*exynos_drm_gem_get_dma_addr(struct drm_device
*dev
,
283 unsigned int gem_handle
,
284 struct drm_file
*filp
)
286 struct exynos_drm_gem_obj
*exynos_gem_obj
;
287 struct drm_gem_object
*obj
;
289 obj
= drm_gem_object_lookup(dev
, filp
, gem_handle
);
291 DRM_ERROR("failed to lookup gem object.\n");
292 return ERR_PTR(-EINVAL
);
295 exynos_gem_obj
= to_exynos_gem_obj(obj
);
297 return &exynos_gem_obj
->buffer
->dma_addr
;
300 void exynos_drm_gem_put_dma_addr(struct drm_device
*dev
,
301 unsigned int gem_handle
,
302 struct drm_file
*filp
)
304 struct exynos_drm_gem_obj
*exynos_gem_obj
;
305 struct drm_gem_object
*obj
;
307 obj
= drm_gem_object_lookup(dev
, filp
, gem_handle
);
309 DRM_ERROR("failed to lookup gem object.\n");
313 exynos_gem_obj
= to_exynos_gem_obj(obj
);
315 drm_gem_object_unreference_unlocked(obj
);
318 * decrease obj->refcount one more time because we has already
319 * increased it at exynos_drm_gem_get_dma_addr().
321 drm_gem_object_unreference_unlocked(obj
);
324 int exynos_drm_gem_map_offset_ioctl(struct drm_device
*dev
, void *data
,
325 struct drm_file
*file_priv
)
327 struct drm_exynos_gem_map_off
*args
= data
;
329 DRM_DEBUG_KMS("handle = 0x%x, offset = 0x%lx\n",
330 args
->handle
, (unsigned long)args
->offset
);
332 if (!(dev
->driver
->driver_features
& DRIVER_GEM
)) {
333 DRM_ERROR("does not support GEM.\n");
337 return exynos_drm_gem_dumb_map_offset(file_priv
, dev
, args
->handle
,
341 int exynos_drm_gem_mmap_buffer(struct file
*filp
,
342 struct vm_area_struct
*vma
)
344 struct drm_gem_object
*obj
= filp
->private_data
;
345 struct exynos_drm_gem_obj
*exynos_gem_obj
= to_exynos_gem_obj(obj
);
346 struct drm_device
*drm_dev
= obj
->dev
;
347 struct exynos_drm_gem_buf
*buffer
;
348 unsigned long vm_size
;
351 WARN_ON(!mutex_is_locked(&obj
->dev
->struct_mutex
));
353 vma
->vm_flags
|= VM_IO
| VM_DONTEXPAND
| VM_DONTDUMP
;
354 vma
->vm_private_data
= obj
;
355 vma
->vm_ops
= drm_dev
->driver
->gem_vm_ops
;
357 update_vm_cache_attr(exynos_gem_obj
, vma
);
359 vm_size
= vma
->vm_end
- vma
->vm_start
;
362 * a buffer contains information to physically continuous memory
363 * allocated by user request or at framebuffer creation.
365 buffer
= exynos_gem_obj
->buffer
;
367 /* check if user-requested size is valid. */
368 if (vm_size
> buffer
->size
)
371 ret
= dma_mmap_attrs(drm_dev
->dev
, vma
, buffer
->pages
,
372 buffer
->dma_addr
, buffer
->size
,
375 DRM_ERROR("failed to mmap.\n");
380 * take a reference to this mapping of the object. And this reference
381 * is unreferenced by the corresponding vm_close call.
383 drm_gem_object_reference(obj
);
385 drm_vm_open_locked(drm_dev
, vma
);
390 int exynos_drm_gem_mmap_ioctl(struct drm_device
*dev
, void *data
,
391 struct drm_file
*file_priv
)
393 struct drm_exynos_file_private
*exynos_file_priv
;
394 struct drm_exynos_gem_mmap
*args
= data
;
395 struct drm_gem_object
*obj
;
396 struct file
*anon_filp
;
399 if (!(dev
->driver
->driver_features
& DRIVER_GEM
)) {
400 DRM_ERROR("does not support GEM.\n");
404 mutex_lock(&dev
->struct_mutex
);
406 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
408 DRM_ERROR("failed to lookup gem object.\n");
409 mutex_unlock(&dev
->struct_mutex
);
413 exynos_file_priv
= file_priv
->driver_priv
;
414 anon_filp
= exynos_file_priv
->anon_filp
;
415 anon_filp
->private_data
= obj
;
417 addr
= vm_mmap(anon_filp
, 0, args
->size
, PROT_READ
| PROT_WRITE
,
420 drm_gem_object_unreference(obj
);
422 if (IS_ERR_VALUE(addr
)) {
423 mutex_unlock(&dev
->struct_mutex
);
427 mutex_unlock(&dev
->struct_mutex
);
431 DRM_DEBUG_KMS("mapped = 0x%lx\n", (unsigned long)args
->mapped
);
436 int 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
;
442 mutex_lock(&dev
->struct_mutex
);
444 obj
= drm_gem_object_lookup(dev
, file_priv
, args
->handle
);
446 DRM_ERROR("failed to lookup gem object.\n");
447 mutex_unlock(&dev
->struct_mutex
);
451 exynos_gem_obj
= to_exynos_gem_obj(obj
);
453 args
->flags
= exynos_gem_obj
->flags
;
454 args
->size
= exynos_gem_obj
->size
;
456 drm_gem_object_unreference(obj
);
457 mutex_unlock(&dev
->struct_mutex
);
462 struct vm_area_struct
*exynos_gem_get_vma(struct vm_area_struct
*vma
)
464 struct vm_area_struct
*vma_copy
;
466 vma_copy
= kmalloc(sizeof(*vma_copy
), GFP_KERNEL
);
470 if (vma
->vm_ops
&& vma
->vm_ops
->open
)
471 vma
->vm_ops
->open(vma
);
474 get_file(vma
->vm_file
);
476 memcpy(vma_copy
, vma
, sizeof(*vma
));
478 vma_copy
->vm_mm
= NULL
;
479 vma_copy
->vm_next
= NULL
;
480 vma_copy
->vm_prev
= NULL
;
485 void exynos_gem_put_vma(struct vm_area_struct
*vma
)
490 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
491 vma
->vm_ops
->close(vma
);
499 int exynos_gem_get_pages_from_userptr(unsigned long start
,
502 struct vm_area_struct
*vma
)
506 /* the memory region mmaped with VM_PFNMAP. */
507 if (vma_is_io(vma
)) {
510 for (i
= 0; i
< npages
; ++i
, start
+= PAGE_SIZE
) {
512 int ret
= follow_pfn(vma
, start
, &pfn
);
516 pages
[i
] = pfn_to_page(pfn
);
520 DRM_ERROR("failed to get user_pages.\n");
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");
533 put_page(pages
[--get_npages
]);
540 void exynos_gem_put_pages_to_userptr(struct page
**pages
,
542 struct vm_area_struct
*vma
)
544 if (!vma_is_io(vma
)) {
547 for (i
= 0; i
< npages
; i
++) {
548 set_page_dirty_lock(pages
[i
]);
551 * undo the reference we took when populating
559 int exynos_gem_map_sgt_with_dma(struct drm_device
*drm_dev
,
560 struct sg_table
*sgt
,
561 enum dma_data_direction dir
)
565 mutex_lock(&drm_dev
->struct_mutex
);
567 nents
= dma_map_sg(drm_dev
->dev
, sgt
->sgl
, sgt
->nents
, dir
);
569 DRM_ERROR("failed to map sgl with dma.\n");
570 mutex_unlock(&drm_dev
->struct_mutex
);
574 mutex_unlock(&drm_dev
->struct_mutex
);
578 void exynos_gem_unmap_sgt_from_dma(struct drm_device
*drm_dev
,
579 struct sg_table
*sgt
,
580 enum dma_data_direction dir
)
582 dma_unmap_sg(drm_dev
->dev
, sgt
->sgl
, sgt
->nents
, dir
);
585 void exynos_drm_gem_free_object(struct drm_gem_object
*obj
)
587 struct exynos_drm_gem_obj
*exynos_gem_obj
;
588 struct exynos_drm_gem_buf
*buf
;
590 exynos_gem_obj
= to_exynos_gem_obj(obj
);
591 buf
= exynos_gem_obj
->buffer
;
593 if (obj
->import_attach
)
594 drm_prime_gem_destroy(obj
, buf
->sgt
);
596 exynos_drm_gem_destroy(to_exynos_gem_obj(obj
));
599 int exynos_drm_gem_dumb_create(struct drm_file
*file_priv
,
600 struct drm_device
*dev
,
601 struct drm_mode_create_dumb
*args
)
603 struct exynos_drm_gem_obj
*exynos_gem_obj
;
607 * allocate memory to be used for framebuffer.
608 * - this callback would be called by user application
609 * with DRM_IOCTL_MODE_CREATE_DUMB command.
612 args
->pitch
= args
->width
* ((args
->bpp
+ 7) / 8);
613 args
->size
= args
->pitch
* args
->height
;
615 exynos_gem_obj
= exynos_drm_gem_create(dev
, EXYNOS_BO_CONTIG
|
616 EXYNOS_BO_WC
, args
->size
);
618 * If physically contiguous memory allocation fails and if IOMMU is
619 * supported then try to get buffer from non physically contiguous
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
,
629 if (IS_ERR(exynos_gem_obj
))
630 return PTR_ERR(exynos_gem_obj
);
632 ret
= exynos_drm_gem_handle_create(&exynos_gem_obj
->base
, file_priv
,
635 exynos_drm_gem_destroy(exynos_gem_obj
);
642 int exynos_drm_gem_dumb_map_offset(struct drm_file
*file_priv
,
643 struct drm_device
*dev
, uint32_t handle
,
646 struct drm_gem_object
*obj
;
649 mutex_lock(&dev
->struct_mutex
);
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.
657 obj
= drm_gem_object_lookup(dev
, file_priv
, handle
);
659 DRM_ERROR("failed to lookup gem object.\n");
664 ret
= drm_gem_create_mmap_offset(obj
);
668 *offset
= drm_vma_node_offset_addr(&obj
->vma_node
);
669 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset
);
672 drm_gem_object_unreference(obj
);
674 mutex_unlock(&dev
->struct_mutex
);
678 int exynos_drm_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
680 struct drm_gem_object
*obj
= vma
->vm_private_data
;
681 struct drm_device
*dev
= obj
->dev
;
682 unsigned long f_vaddr
;
686 page_offset
= ((unsigned long)vmf
->virtual_address
-
687 vma
->vm_start
) >> PAGE_SHIFT
;
688 f_vaddr
= (unsigned long)vmf
->virtual_address
;
690 mutex_lock(&dev
->struct_mutex
);
692 ret
= exynos_drm_gem_map_buf(obj
, vma
, f_vaddr
, page_offset
);
694 DRM_ERROR("failed to map a buffer with user.\n");
696 mutex_unlock(&dev
->struct_mutex
);
698 return convert_to_vm_err_msg(ret
);
701 int exynos_drm_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
703 struct exynos_drm_gem_obj
*exynos_gem_obj
;
704 struct drm_gem_object
*obj
;
707 /* set vm_area_struct. */
708 ret
= drm_gem_mmap(filp
, vma
);
710 DRM_ERROR("failed to mmap.\n");
714 obj
= vma
->vm_private_data
;
715 exynos_gem_obj
= to_exynos_gem_obj(obj
);
717 ret
= check_gem_flags(exynos_gem_obj
->flags
);
719 drm_gem_vm_close(vma
);
720 drm_gem_free_mmap_offset(obj
);
724 vma
->vm_flags
&= ~VM_PFNMAP
;
725 vma
->vm_flags
|= VM_MIXEDMAP
;
727 update_vm_cache_attr(exynos_gem_obj
, vma
);