29 #include <drm/exynos_drm.h>
35 static unsigned int convert_to_vm_err_msg(
int msg)
43 out_msg = VM_FAULT_NOPAGE;
47 out_msg = VM_FAULT_OOM;
51 out_msg = VM_FAULT_SIGBUS;
58 static int check_gem_flags(
unsigned int flags)
61 DRM_ERROR(
"invalid flags.\n");
71 DRM_DEBUG_KMS(
"flags = 0x%x\n", obj->
flags);
84 static unsigned long roundup_gem_size(
unsigned long size,
unsigned int flags)
106 pages = drm_malloc_ab(npages,
sizeof(
struct page *));
110 for (i = 0; i < npages; i++) {
123 drm_free_large(pages);
127 static void exynos_gem_put_pages(
struct drm_gem_object *obj,
134 while (--npages >= 0)
137 drm_free_large(pages);
140 static int exynos_drm_gem_map_pages(
struct drm_gem_object *obj,
142 unsigned long f_vaddr,
160 static int exynos_drm_gem_get_pages(
struct drm_gem_object *obj)
166 unsigned int npages,
i = 0;
170 DRM_DEBUG_KMS(
"already allocated.\n");
176 DRM_ERROR(
"failed to get pages.\n");
177 return PTR_ERR(pages);
185 DRM_ERROR(
"failed to allocate sg table.\n");
192 DRM_ERROR(
"failed to initialize sg table.\n");
201 sg_set_page(sgl, pages[i],
PAGE_SIZE, 0);
215 exynos_gem_put_pages(obj, pages);
220 static void exynos_drm_gem_put_pages(
struct drm_gem_object *obj)
233 exynos_gem_put_pages(obj, buf->
pages);
239 static int exynos_drm_gem_handle_create(
struct drm_gem_object *obj,
240 struct drm_file *file_priv,
253 DRM_DEBUG_KMS(
"gem handle = 0x%x\n", *handle);
256 drm_gem_object_unreference_unlocked(obj);
263 struct drm_gem_object *obj;
266 DRM_DEBUG_KMS(
"%s\n", __FILE__);
268 obj = &exynos_gem_obj->
base;
269 buf = exynos_gem_obj->
buffer;
271 DRM_DEBUG_KMS(
"handle count = %d\n",
atomic_read(&obj->handle_count));
282 if (obj->import_attach)
286 exynos_drm_gem_put_pages(obj);
294 if (obj->map_list.map)
300 kfree(exynos_gem_obj);
301 exynos_gem_obj =
NULL;
308 struct drm_gem_object *obj;
311 exynos_gem_obj = kzalloc(
sizeof(*exynos_gem_obj),
GFP_KERNEL);
312 if (!exynos_gem_obj) {
313 DRM_ERROR(
"failed to allocate exynos gem object\n");
318 obj = &exynos_gem_obj->
base;
322 DRM_ERROR(
"failed to initialize gem object\n");
323 kfree(exynos_gem_obj);
327 DRM_DEBUG_KMS(
"created file object = 0x%x\n", (
unsigned int)obj->filp);
329 return exynos_gem_obj;
341 DRM_ERROR(
"invalid size.\n");
345 size = roundup_gem_size(size, flags);
346 DRM_DEBUG_KMS(
"%s\n", __FILE__);
348 ret = check_gem_flags(flags);
357 if (!exynos_gem_obj) {
372 ret = exynos_drm_gem_get_pages(&exynos_gem_obj->
base);
385 return exynos_gem_obj;
393 struct drm_file *file_priv)
399 DRM_DEBUG_KMS(
"%s\n", __FILE__);
402 if (IS_ERR(exynos_gem_obj))
403 return PTR_ERR(exynos_gem_obj);
405 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->
base, file_priv,
416 unsigned int gem_handle,
417 struct drm_file *file_priv)
420 struct drm_gem_object *obj;
424 DRM_ERROR(
"failed to lookup gem object.\n");
431 DRM_DEBUG_KMS(
"not support NONCONTIG type.\n");
432 drm_gem_object_unreference_unlocked(obj);
438 return &exynos_gem_obj->
buffer->dma_addr;
442 unsigned int gem_handle,
443 struct drm_file *file_priv)
446 struct drm_gem_object *obj;
450 DRM_ERROR(
"failed to lookup gem object.\n");
457 DRM_DEBUG_KMS(
"not support NONCONTIG type.\n");
458 drm_gem_object_unreference_unlocked(obj);
464 drm_gem_object_unreference_unlocked(obj);
470 drm_gem_object_unreference_unlocked(obj);
474 struct drm_file *file_priv)
478 DRM_DEBUG_KMS(
"%s\n", __FILE__);
480 DRM_DEBUG_KMS(
"handle = 0x%x, offset = 0x%lx\n",
483 if (!(dev->driver->driver_features & DRIVER_GEM)) {
484 DRM_ERROR(
"does not support GEM.\n");
492 static int exynos_drm_gem_mmap_buffer(
struct file *filp,
501 DRM_DEBUG_KMS(
"%s\n", __FILE__);
503 vma->
vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
505 update_vm_cache_attr(exynos_gem_obj, vma);
513 buffer = exynos_gem_obj->
buffer;
516 if (vm_size > buffer->
size)
530 DRM_ERROR(
"failed to remap user space.\n");
542 pfn = ((
unsigned long)exynos_gem_obj->
buffer->dma_addr) >>
545 DRM_DEBUG_KMS(
"pfn = 0x%lx\n", pfn);
549 DRM_ERROR(
"failed to remap pfn range.\n");
558 .mmap = exynos_drm_gem_mmap_buffer,
562 struct drm_file *file_priv)
565 struct drm_gem_object *obj;
568 DRM_DEBUG_KMS(
"%s\n", __FILE__);
570 if (!(dev->driver->driver_features & DRIVER_GEM)) {
571 DRM_ERROR(
"does not support GEM.\n");
577 DRM_ERROR(
"failed to lookup gem object.\n");
581 obj->filp->f_op = &exynos_drm_gem_fops;
582 obj->filp->private_data = obj;
587 drm_gem_object_unreference_unlocked(obj);
589 if (IS_ERR((
void *)addr))
590 return PTR_ERR((
void *)addr);
594 DRM_DEBUG_KMS(
"mapped = 0x%lx\n", (
unsigned long)args->
mapped);
600 struct drm_file *file_priv)
603 struct drm_gem_object *obj;
609 DRM_ERROR(
"failed to lookup gem object.\n");
619 drm_gem_object_unreference(obj);
627 DRM_DEBUG_KMS(
"%s\n", __FILE__);
637 DRM_DEBUG_KMS(
"%s\n", __FILE__);
640 buf = exynos_gem_obj->
buffer;
642 if (obj->import_attach)
655 DRM_DEBUG_KMS(
"%s\n", __FILE__);
667 if (IS_ERR(exynos_gem_obj))
668 return PTR_ERR(exynos_gem_obj);
670 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->
base, file_priv,
684 struct drm_gem_object *obj;
687 DRM_DEBUG_KMS(
"%s\n", __FILE__);
699 DRM_ERROR(
"failed to lookup gem object.\n");
704 if (!obj->map_list.map) {
711 DRM_DEBUG_KMS(
"offset = 0x%lx\n", (
unsigned long)*
offset);
714 drm_gem_object_unreference(obj);
726 DRM_DEBUG_KMS(
"%s\n", __FILE__);
735 DRM_ERROR(
"failed to delete drm_gem_handle.\n");
746 unsigned long f_vaddr;
750 page_offset = ((
unsigned long)vmf->virtual_address -
752 f_vaddr = (
unsigned long)vmf->virtual_address;
756 ret = exynos_drm_gem_map_pages(obj, vma, f_vaddr, page_offset);
758 DRM_ERROR(
"failed to map pages.\n");
762 return convert_to_vm_err_msg(ret);
768 struct drm_gem_object *obj;
771 DRM_DEBUG_KMS(
"%s\n", __FILE__);
776 DRM_ERROR(
"failed to mmap.\n");
783 ret = check_gem_flags(exynos_gem_obj->
flags);
793 update_vm_cache_attr(exynos_gem_obj, vma);