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 int check_gem_flags(unsigned int flags)
25 if (flags & ~(EXYNOS_BO_MASK)) {
26 DRM_ERROR("invalid flags.\n");
33 static void update_vm_cache_attr(struct exynos_drm_gem_obj *obj,
34 struct vm_area_struct *vma)
36 DRM_DEBUG_KMS("flags = 0x%x\n", obj->flags);
38 /* non-cachable as default. */
39 if (obj->flags & EXYNOS_BO_CACHABLE)
40 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
41 else if (obj->flags & EXYNOS_BO_WC)
43 pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
46 pgprot_noncached(vm_get_page_prot(vma->vm_flags));
49 static unsigned long roundup_gem_size(unsigned long size, unsigned int flags)
53 return roundup(size, PAGE_SIZE);
56 static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
57 struct drm_file *file_priv,
63 * allocate a id of idr table where the obj is registered
64 * and handle has the id what user can see.
66 ret = drm_gem_handle_create(file_priv, obj, handle);
70 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
72 /* drop reference from allocate - handle holds it now. */
73 drm_gem_object_unreference_unlocked(obj);
78 void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj)
80 struct drm_gem_object *obj;
81 struct exynos_drm_gem_buf *buf;
83 obj = &exynos_gem_obj->base;
84 buf = exynos_gem_obj->buffer;
86 DRM_DEBUG_KMS("handle count = %d\n", obj->handle_count);
89 * do not release memory region from exporter.
91 * the region will be released by exporter
92 * once dmabuf's refcount becomes 0.
94 if (obj->import_attach)
97 exynos_drm_free_buf(obj->dev, exynos_gem_obj->flags, buf);
100 exynos_drm_fini_buf(obj->dev, buf);
101 exynos_gem_obj->buffer = NULL;
103 drm_gem_free_mmap_offset(obj);
105 /* release file pointer to gem object. */
106 drm_gem_object_release(obj);
108 kfree(exynos_gem_obj);
109 exynos_gem_obj = NULL;
112 unsigned long exynos_drm_gem_get_size(struct drm_device *dev,
113 unsigned int gem_handle,
114 struct drm_file *file_priv)
116 struct exynos_drm_gem_obj *exynos_gem_obj;
117 struct drm_gem_object *obj;
119 obj = drm_gem_object_lookup(dev, file_priv, gem_handle);
121 DRM_ERROR("failed to lookup gem object.\n");
125 exynos_gem_obj = to_exynos_gem_obj(obj);
127 drm_gem_object_unreference_unlocked(obj);
129 return exynos_gem_obj->buffer->size;
133 struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev,
136 struct exynos_drm_gem_obj *exynos_gem_obj;
137 struct drm_gem_object *obj;
140 exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL);
144 exynos_gem_obj->size = size;
145 obj = &exynos_gem_obj->base;
147 ret = drm_gem_object_init(dev, obj, size);
149 DRM_ERROR("failed to initialize gem object\n");
150 kfree(exynos_gem_obj);
154 DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);
156 return exynos_gem_obj;
159 struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
163 struct exynos_drm_gem_obj *exynos_gem_obj;
164 struct exynos_drm_gem_buf *buf;
168 DRM_ERROR("invalid size.\n");
169 return ERR_PTR(-EINVAL);
172 size = roundup_gem_size(size, flags);
174 ret = check_gem_flags(flags);
178 buf = exynos_drm_init_buf(dev, size);
180 return ERR_PTR(-ENOMEM);
182 exynos_gem_obj = exynos_drm_gem_init(dev, size);
183 if (!exynos_gem_obj) {
188 exynos_gem_obj->buffer = buf;
190 /* set memory type and cache attribute from user side. */
191 exynos_gem_obj->flags = flags;
193 ret = exynos_drm_alloc_buf(dev, buf, flags);
197 return exynos_gem_obj;
200 drm_gem_object_release(&exynos_gem_obj->base);
201 kfree(exynos_gem_obj);
203 exynos_drm_fini_buf(dev, buf);
207 int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
208 struct drm_file *file_priv)
210 struct drm_exynos_gem_create *args = data;
211 struct exynos_drm_gem_obj *exynos_gem_obj;
214 exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size);
215 if (IS_ERR(exynos_gem_obj))
216 return PTR_ERR(exynos_gem_obj);
218 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
221 exynos_drm_gem_destroy(exynos_gem_obj);
228 dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev,
229 unsigned int gem_handle,
230 struct drm_file *filp)
232 struct exynos_drm_gem_obj *exynos_gem_obj;
233 struct drm_gem_object *obj;
235 obj = drm_gem_object_lookup(dev, filp, gem_handle);
237 DRM_ERROR("failed to lookup gem object.\n");
238 return ERR_PTR(-EINVAL);
241 exynos_gem_obj = to_exynos_gem_obj(obj);
243 return &exynos_gem_obj->buffer->dma_addr;
246 void exynos_drm_gem_put_dma_addr(struct drm_device *dev,
247 unsigned int gem_handle,
248 struct drm_file *filp)
250 struct drm_gem_object *obj;
252 obj = drm_gem_object_lookup(dev, filp, gem_handle);
254 DRM_ERROR("failed to lookup gem object.\n");
258 drm_gem_object_unreference_unlocked(obj);
261 * decrease obj->refcount one more time because we has already
262 * increased it at exynos_drm_gem_get_dma_addr().
264 drm_gem_object_unreference_unlocked(obj);
267 int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem_obj *exynos_gem_obj,
268 struct vm_area_struct *vma)
270 struct drm_device *drm_dev = exynos_gem_obj->base.dev;
271 struct exynos_drm_gem_buf *buffer;
272 unsigned long vm_size;
275 vma->vm_flags &= ~VM_PFNMAP;
278 vm_size = vma->vm_end - vma->vm_start;
281 * a buffer contains information to physically continuous memory
282 * allocated by user request or at framebuffer creation.
284 buffer = exynos_gem_obj->buffer;
286 /* check if user-requested size is valid. */
287 if (vm_size > buffer->size)
290 ret = dma_mmap_attrs(drm_dev->dev, vma, buffer->pages,
291 buffer->dma_addr, buffer->size,
294 DRM_ERROR("failed to mmap.\n");
301 int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data,
302 struct drm_file *file_priv)
303 { struct exynos_drm_gem_obj *exynos_gem_obj;
304 struct drm_exynos_gem_info *args = data;
305 struct drm_gem_object *obj;
307 mutex_lock(&dev->struct_mutex);
309 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
311 DRM_ERROR("failed to lookup gem object.\n");
312 mutex_unlock(&dev->struct_mutex);
316 exynos_gem_obj = to_exynos_gem_obj(obj);
318 args->flags = exynos_gem_obj->flags;
319 args->size = exynos_gem_obj->size;
321 drm_gem_object_unreference(obj);
322 mutex_unlock(&dev->struct_mutex);
327 struct vm_area_struct *exynos_gem_get_vma(struct vm_area_struct *vma)
329 struct vm_area_struct *vma_copy;
331 vma_copy = kmalloc(sizeof(*vma_copy), GFP_KERNEL);
335 if (vma->vm_ops && vma->vm_ops->open)
336 vma->vm_ops->open(vma);
339 get_file(vma->vm_file);
341 memcpy(vma_copy, vma, sizeof(*vma));
343 vma_copy->vm_mm = NULL;
344 vma_copy->vm_next = NULL;
345 vma_copy->vm_prev = NULL;
350 void exynos_gem_put_vma(struct vm_area_struct *vma)
355 if (vma->vm_ops && vma->vm_ops->close)
356 vma->vm_ops->close(vma);
364 int exynos_gem_get_pages_from_userptr(unsigned long start,
367 struct vm_area_struct *vma)
371 /* the memory region mmaped with VM_PFNMAP. */
372 if (vma_is_io(vma)) {
375 for (i = 0; i < npages; ++i, start += PAGE_SIZE) {
377 int ret = follow_pfn(vma, start, &pfn);
381 pages[i] = pfn_to_page(pfn);
385 DRM_ERROR("failed to get user_pages.\n");
392 get_npages = get_user_pages(current, current->mm, start,
393 npages, 1, 1, pages, NULL);
394 get_npages = max(get_npages, 0);
395 if (get_npages != npages) {
396 DRM_ERROR("failed to get user_pages.\n");
398 put_page(pages[--get_npages]);
405 void exynos_gem_put_pages_to_userptr(struct page **pages,
407 struct vm_area_struct *vma)
409 if (!vma_is_io(vma)) {
412 for (i = 0; i < npages; i++) {
413 set_page_dirty_lock(pages[i]);
416 * undo the reference we took when populating
424 int exynos_gem_map_sgt_with_dma(struct drm_device *drm_dev,
425 struct sg_table *sgt,
426 enum dma_data_direction dir)
430 mutex_lock(&drm_dev->struct_mutex);
432 nents = dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
434 DRM_ERROR("failed to map sgl with dma.\n");
435 mutex_unlock(&drm_dev->struct_mutex);
439 mutex_unlock(&drm_dev->struct_mutex);
443 void exynos_gem_unmap_sgt_from_dma(struct drm_device *drm_dev,
444 struct sg_table *sgt,
445 enum dma_data_direction dir)
447 dma_unmap_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
450 void exynos_drm_gem_free_object(struct drm_gem_object *obj)
452 struct exynos_drm_gem_obj *exynos_gem_obj;
453 struct exynos_drm_gem_buf *buf;
455 exynos_gem_obj = to_exynos_gem_obj(obj);
456 buf = exynos_gem_obj->buffer;
458 if (obj->import_attach)
459 drm_prime_gem_destroy(obj, buf->sgt);
461 exynos_drm_gem_destroy(to_exynos_gem_obj(obj));
464 int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
465 struct drm_device *dev,
466 struct drm_mode_create_dumb *args)
468 struct exynos_drm_gem_obj *exynos_gem_obj;
472 * allocate memory to be used for framebuffer.
473 * - this callback would be called by user application
474 * with DRM_IOCTL_MODE_CREATE_DUMB command.
477 args->pitch = args->width * ((args->bpp + 7) / 8);
478 args->size = args->pitch * args->height;
480 if (is_drm_iommu_supported(dev)) {
481 exynos_gem_obj = exynos_drm_gem_create(dev,
482 EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC,
485 exynos_gem_obj = exynos_drm_gem_create(dev,
486 EXYNOS_BO_CONTIG | EXYNOS_BO_WC,
490 if (IS_ERR(exynos_gem_obj)) {
491 dev_warn(dev->dev, "FB allocation failed.\n");
492 return PTR_ERR(exynos_gem_obj);
495 ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
498 exynos_drm_gem_destroy(exynos_gem_obj);
505 int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
506 struct drm_device *dev, uint32_t handle,
509 struct drm_gem_object *obj;
512 mutex_lock(&dev->struct_mutex);
515 * get offset of memory allocated for drm framebuffer.
516 * - this callback would be called by user application
517 * with DRM_IOCTL_MODE_MAP_DUMB command.
520 obj = drm_gem_object_lookup(dev, file_priv, handle);
522 DRM_ERROR("failed to lookup gem object.\n");
527 ret = drm_gem_create_mmap_offset(obj);
531 *offset = drm_vma_node_offset_addr(&obj->vma_node);
532 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
535 drm_gem_object_unreference(obj);
537 mutex_unlock(&dev->struct_mutex);
541 int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
543 struct drm_gem_object *obj = vma->vm_private_data;
544 struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
545 struct exynos_drm_gem_buf *buf = exynos_gem_obj->buffer;
550 page_offset = ((unsigned long)vmf->virtual_address -
551 vma->vm_start) >> PAGE_SHIFT;
553 if (page_offset >= (buf->size >> PAGE_SHIFT)) {
554 DRM_ERROR("invalid page offset\n");
559 pfn = page_to_pfn(buf->pages[page_offset]);
560 ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
567 return VM_FAULT_NOPAGE;
571 return VM_FAULT_SIGBUS;
575 int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
577 struct exynos_drm_gem_obj *exynos_gem_obj;
578 struct drm_gem_object *obj;
581 /* set vm_area_struct. */
582 ret = drm_gem_mmap(filp, vma);
584 DRM_ERROR("failed to mmap.\n");
588 obj = vma->vm_private_data;
589 exynos_gem_obj = to_exynos_gem_obj(obj);
591 ret = check_gem_flags(exynos_gem_obj->flags);
595 update_vm_cache_attr(exynos_gem_obj, vma);
597 ret = exynos_drm_gem_mmap_buffer(exynos_gem_obj, vma);
604 drm_gem_vm_close(vma);
605 drm_gem_free_mmap_offset(obj);