2011-10-04 18:19:01 +08:00
|
|
|
/* exynos_drm_gem.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 Samsung Electronics Co., Ltd.
|
|
|
|
* Author: Inki Dae <inki.dae@samsung.com>
|
|
|
|
*
|
2012-12-18 01:30:17 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
2011-10-04 18:19:01 +08:00
|
|
|
*/
|
|
|
|
|
2012-10-03 01:01:07 +08:00
|
|
|
#include <drm/drmP.h>
|
2013-07-25 03:07:52 +08:00
|
|
|
#include <drm/drm_vma_manager.h>
|
2011-10-04 18:19:01 +08:00
|
|
|
|
2012-03-16 17:47:05 +08:00
|
|
|
#include <linux/shmem_fs.h>
|
2015-08-16 13:33:08 +08:00
|
|
|
#include <linux/dma-buf.h>
|
2011-10-04 18:19:01 +08:00
|
|
|
#include <drm/exynos_drm.h>
|
|
|
|
|
|
|
|
#include "exynos_drm_drv.h"
|
|
|
|
#include "exynos_drm_gem.h"
|
2013-08-23 14:35:06 +08:00
|
|
|
#include "exynos_drm_iommu.h"
|
2011-10-04 18:19:01 +08:00
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
static int exynos_drm_alloc_buf(struct exynos_drm_gem_obj *obj)
|
|
|
|
{
|
|
|
|
struct drm_device *dev = obj->base.dev;
|
|
|
|
enum dma_attr attr;
|
|
|
|
unsigned int nr_pages;
|
|
|
|
|
|
|
|
if (obj->dma_addr) {
|
|
|
|
DRM_DEBUG_KMS("already allocated.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
init_dma_attrs(&obj->dma_attrs);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if EXYNOS_BO_CONTIG, fully physically contiguous memory
|
|
|
|
* region will be allocated else physically contiguous
|
|
|
|
* as possible.
|
|
|
|
*/
|
|
|
|
if (!(obj->flags & EXYNOS_BO_NONCONTIG))
|
|
|
|
dma_set_attr(DMA_ATTR_FORCE_CONTIGUOUS, &obj->dma_attrs);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping
|
|
|
|
* else cachable mapping.
|
|
|
|
*/
|
|
|
|
if (obj->flags & EXYNOS_BO_WC || !(obj->flags & EXYNOS_BO_CACHABLE))
|
|
|
|
attr = DMA_ATTR_WRITE_COMBINE;
|
|
|
|
else
|
|
|
|
attr = DMA_ATTR_NON_CONSISTENT;
|
|
|
|
|
|
|
|
dma_set_attr(attr, &obj->dma_attrs);
|
|
|
|
dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &obj->dma_attrs);
|
|
|
|
|
|
|
|
nr_pages = obj->size >> PAGE_SHIFT;
|
|
|
|
|
|
|
|
if (!is_drm_iommu_supported(dev)) {
|
|
|
|
dma_addr_t start_addr;
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
|
|
|
obj->pages = drm_calloc_large(nr_pages, sizeof(struct page *));
|
|
|
|
if (!obj->pages) {
|
|
|
|
DRM_ERROR("failed to allocate pages.\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj->cookie = dma_alloc_attrs(dev->dev,
|
|
|
|
obj->size,
|
|
|
|
&obj->dma_addr, GFP_KERNEL,
|
|
|
|
&obj->dma_attrs);
|
|
|
|
if (!obj->cookie) {
|
|
|
|
DRM_ERROR("failed to allocate buffer.\n");
|
|
|
|
drm_free_large(obj->pages);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
start_addr = obj->dma_addr;
|
|
|
|
while (i < nr_pages) {
|
|
|
|
obj->pages[i] = phys_to_page(start_addr);
|
|
|
|
start_addr += PAGE_SIZE;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
obj->pages = dma_alloc_attrs(dev->dev, obj->size,
|
|
|
|
&obj->dma_addr, GFP_KERNEL,
|
|
|
|
&obj->dma_attrs);
|
|
|
|
if (!obj->pages) {
|
|
|
|
DRM_ERROR("failed to allocate buffer.\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
|
|
|
|
(unsigned long)obj->dma_addr,
|
|
|
|
obj->size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void exynos_drm_free_buf(struct exynos_drm_gem_obj *obj)
|
|
|
|
{
|
|
|
|
struct drm_device *dev = obj->base.dev;
|
|
|
|
|
|
|
|
if (!obj->dma_addr) {
|
|
|
|
DRM_DEBUG_KMS("dma_addr is invalid.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
|
|
|
|
(unsigned long)obj->dma_addr, obj->size);
|
|
|
|
|
|
|
|
if (!is_drm_iommu_supported(dev)) {
|
|
|
|
dma_free_attrs(dev->dev, obj->size, obj->cookie,
|
|
|
|
(dma_addr_t)obj->dma_addr, &obj->dma_attrs);
|
|
|
|
drm_free_large(obj->pages);
|
|
|
|
} else
|
|
|
|
dma_free_attrs(dev->dev, obj->size, obj->pages,
|
|
|
|
(dma_addr_t)obj->dma_addr, &obj->dma_attrs);
|
|
|
|
|
|
|
|
obj->dma_addr = (dma_addr_t)NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-13 13:39:13 +08:00
|
|
|
static int exynos_drm_gem_handle_create(struct drm_gem_object *obj,
|
|
|
|
struct drm_file *file_priv,
|
|
|
|
unsigned int *handle)
|
2011-10-04 18:19:01 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate a id of idr table where the obj is registered
|
|
|
|
* and handle has the id what user can see.
|
|
|
|
*/
|
|
|
|
ret = drm_gem_handle_create(file_priv, obj, handle);
|
|
|
|
if (ret)
|
2011-12-13 13:39:13 +08:00
|
|
|
return ret;
|
2011-10-04 18:19:01 +08:00
|
|
|
|
|
|
|
DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle);
|
|
|
|
|
|
|
|
/* drop reference from allocate - handle holds it now. */
|
|
|
|
drm_gem_object_unreference_unlocked(obj);
|
|
|
|
|
2011-12-13 13:39:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj)
|
|
|
|
{
|
2015-08-16 13:38:49 +08:00
|
|
|
struct drm_gem_object *obj = &exynos_gem_obj->base;
|
2011-12-13 13:39:13 +08:00
|
|
|
|
2013-08-15 06:02:37 +08:00
|
|
|
DRM_DEBUG_KMS("handle count = %d\n", obj->handle_count);
|
2011-10-04 18:19:01 +08:00
|
|
|
|
2012-06-12 15:52:54 +08:00
|
|
|
/*
|
|
|
|
* do not release memory region from exporter.
|
|
|
|
*
|
|
|
|
* the region will be released by exporter
|
|
|
|
* once dmabuf's refcount becomes 0.
|
|
|
|
*/
|
|
|
|
if (obj->import_attach)
|
|
|
|
goto out;
|
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
exynos_drm_free_buf(exynos_gem_obj);
|
2012-03-16 17:47:05 +08:00
|
|
|
|
2012-06-12 15:52:54 +08:00
|
|
|
out:
|
2013-07-25 03:07:52 +08:00
|
|
|
drm_gem_free_mmap_offset(obj);
|
2011-12-13 13:39:13 +08:00
|
|
|
|
|
|
|
/* release file pointer to gem object. */
|
2011-10-04 18:19:01 +08:00
|
|
|
drm_gem_object_release(obj);
|
|
|
|
|
|
|
|
kfree(exynos_gem_obj);
|
2012-03-16 17:47:05 +08:00
|
|
|
exynos_gem_obj = NULL;
|
2011-12-13 13:39:13 +08:00
|
|
|
}
|
|
|
|
|
2013-03-11 20:15:59 +08:00
|
|
|
unsigned long exynos_drm_gem_get_size(struct drm_device *dev,
|
|
|
|
unsigned int gem_handle,
|
|
|
|
struct drm_file *file_priv)
|
|
|
|
{
|
|
|
|
struct exynos_drm_gem_obj *exynos_gem_obj;
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
|
|
|
|
obj = drm_gem_object_lookup(dev, file_priv, gem_handle);
|
|
|
|
if (!obj) {
|
|
|
|
DRM_ERROR("failed to lookup gem object.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
exynos_gem_obj = to_exynos_gem_obj(obj);
|
|
|
|
|
|
|
|
drm_gem_object_unreference_unlocked(obj);
|
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
return exynos_gem_obj->size;
|
2013-03-11 20:15:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-23 20:01:28 +08:00
|
|
|
struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev,
|
2011-12-13 13:39:13 +08:00
|
|
|
unsigned long size)
|
|
|
|
{
|
|
|
|
struct exynos_drm_gem_obj *exynos_gem_obj;
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
exynos_gem_obj = kzalloc(sizeof(*exynos_gem_obj), GFP_KERNEL);
|
2013-08-19 18:04:55 +08:00
|
|
|
if (!exynos_gem_obj)
|
2015-07-28 16:53:22 +08:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2011-12-13 13:39:13 +08:00
|
|
|
|
2012-03-16 17:47:05 +08:00
|
|
|
exynos_gem_obj->size = size;
|
2011-12-13 13:39:13 +08:00
|
|
|
obj = &exynos_gem_obj->base;
|
|
|
|
|
|
|
|
ret = drm_gem_object_init(dev, obj, size);
|
|
|
|
if (ret < 0) {
|
|
|
|
DRM_ERROR("failed to initialize gem object\n");
|
|
|
|
kfree(exynos_gem_obj);
|
2015-07-28 16:53:22 +08:00
|
|
|
return ERR_PTR(ret);
|
2011-12-13 13:39:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DRM_DEBUG_KMS("created file object = 0x%x\n", (unsigned int)obj->filp);
|
|
|
|
|
|
|
|
return exynos_gem_obj;
|
2011-10-04 18:19:01 +08:00
|
|
|
}
|
|
|
|
|
2011-11-12 13:51:23 +08:00
|
|
|
struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
|
2012-03-16 17:47:05 +08:00
|
|
|
unsigned int flags,
|
|
|
|
unsigned long size)
|
2011-11-12 13:51:23 +08:00
|
|
|
{
|
2011-12-13 13:39:13 +08:00
|
|
|
struct exynos_drm_gem_obj *exynos_gem_obj;
|
2012-03-16 17:47:05 +08:00
|
|
|
int ret;
|
2011-11-12 13:51:23 +08:00
|
|
|
|
2015-08-16 13:15:06 +08:00
|
|
|
if (flags & ~(EXYNOS_BO_MASK)) {
|
|
|
|
DRM_ERROR("invalid flags.\n");
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
2012-04-03 20:27:58 +08:00
|
|
|
if (!size) {
|
|
|
|
DRM_ERROR("invalid size.\n");
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
2015-07-28 16:53:27 +08:00
|
|
|
size = roundup(size, PAGE_SIZE);
|
2011-11-12 13:51:23 +08:00
|
|
|
|
2011-12-13 13:39:13 +08:00
|
|
|
exynos_gem_obj = exynos_drm_gem_init(dev, size);
|
2015-08-16 13:38:49 +08:00
|
|
|
if (IS_ERR(exynos_gem_obj))
|
|
|
|
return exynos_gem_obj;
|
2012-03-16 17:47:05 +08:00
|
|
|
|
|
|
|
/* set memory type and cache attribute from user side. */
|
|
|
|
exynos_gem_obj->flags = flags;
|
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
ret = exynos_drm_alloc_buf(exynos_gem_obj);
|
|
|
|
if (ret < 0) {
|
|
|
|
drm_gem_object_release(&exynos_gem_obj->base);
|
|
|
|
kfree(exynos_gem_obj);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
2011-11-12 13:51:23 +08:00
|
|
|
|
|
|
|
return exynos_gem_obj;
|
|
|
|
}
|
|
|
|
|
2011-10-04 18:19:01 +08:00
|
|
|
int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
|
2011-12-13 13:20:23 +08:00
|
|
|
struct drm_file *file_priv)
|
2011-10-04 18:19:01 +08:00
|
|
|
{
|
|
|
|
struct drm_exynos_gem_create *args = data;
|
2011-12-13 13:20:23 +08:00
|
|
|
struct exynos_drm_gem_obj *exynos_gem_obj;
|
2011-12-13 13:39:13 +08:00
|
|
|
int ret;
|
2011-10-04 18:19:01 +08:00
|
|
|
|
2012-03-16 17:47:05 +08:00
|
|
|
exynos_gem_obj = exynos_drm_gem_create(dev, args->flags, args->size);
|
2011-10-04 18:19:01 +08:00
|
|
|
if (IS_ERR(exynos_gem_obj))
|
|
|
|
return PTR_ERR(exynos_gem_obj);
|
|
|
|
|
2011-12-13 13:39:13 +08:00
|
|
|
ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
|
|
|
|
&args->handle);
|
|
|
|
if (ret) {
|
|
|
|
exynos_drm_gem_destroy(exynos_gem_obj);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-04 18:19:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-04 12:53:24 +08:00
|
|
|
dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev,
|
2012-03-16 17:47:06 +08:00
|
|
|
unsigned int gem_handle,
|
2012-11-04 12:53:24 +08:00
|
|
|
struct drm_file *filp)
|
2012-03-16 17:47:06 +08:00
|
|
|
{
|
|
|
|
struct exynos_drm_gem_obj *exynos_gem_obj;
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
|
2012-11-04 12:53:24 +08:00
|
|
|
obj = drm_gem_object_lookup(dev, filp, gem_handle);
|
2012-03-16 17:47:06 +08:00
|
|
|
if (!obj) {
|
|
|
|
DRM_ERROR("failed to lookup gem object.\n");
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
exynos_gem_obj = to_exynos_gem_obj(obj);
|
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
return &exynos_gem_obj->dma_addr;
|
2012-03-16 17:47:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void exynos_drm_gem_put_dma_addr(struct drm_device *dev,
|
|
|
|
unsigned int gem_handle,
|
2012-11-04 12:53:24 +08:00
|
|
|
struct drm_file *filp)
|
2012-03-16 17:47:06 +08:00
|
|
|
{
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
|
2012-11-04 12:53:24 +08:00
|
|
|
obj = drm_gem_object_lookup(dev, filp, gem_handle);
|
2012-03-16 17:47:06 +08:00
|
|
|
if (!obj) {
|
|
|
|
DRM_ERROR("failed to lookup gem object.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
drm_gem_object_unreference_unlocked(obj);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* decrease obj->refcount one more time because we has already
|
|
|
|
* increased it at exynos_drm_gem_get_dma_addr().
|
|
|
|
*/
|
|
|
|
drm_gem_object_unreference_unlocked(obj);
|
|
|
|
}
|
|
|
|
|
2014-09-18 13:19:01 +08:00
|
|
|
int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem_obj *exynos_gem_obj,
|
2011-12-13 13:20:23 +08:00
|
|
|
struct vm_area_struct *vma)
|
2011-10-04 18:19:01 +08:00
|
|
|
{
|
2014-09-18 13:19:01 +08:00
|
|
|
struct drm_device *drm_dev = exynos_gem_obj->base.dev;
|
2012-10-20 22:53:42 +08:00
|
|
|
unsigned long vm_size;
|
2012-11-08 20:52:54 +08:00
|
|
|
int ret;
|
2011-10-04 18:19:01 +08:00
|
|
|
|
2014-09-18 13:19:01 +08:00
|
|
|
vma->vm_flags &= ~VM_PFNMAP;
|
|
|
|
vma->vm_pgoff = 0;
|
2011-10-04 18:19:01 +08:00
|
|
|
|
2012-10-20 22:53:42 +08:00
|
|
|
vm_size = vma->vm_end - vma->vm_start;
|
2012-03-16 17:47:05 +08:00
|
|
|
|
2011-10-04 18:19:01 +08:00
|
|
|
/* check if user-requested size is valid. */
|
2015-08-16 13:38:49 +08:00
|
|
|
if (vm_size > exynos_gem_obj->size)
|
2011-10-04 18:19:01 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
ret = dma_mmap_attrs(drm_dev->dev, vma, exynos_gem_obj->pages,
|
|
|
|
exynos_gem_obj->dma_addr, exynos_gem_obj->size,
|
|
|
|
&exynos_gem_obj->dma_attrs);
|
2012-11-08 20:52:54 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
DRM_ERROR("failed to mmap.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-04 18:19:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-04 14:51:17 +08:00
|
|
|
int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data,
|
|
|
|
struct drm_file *file_priv)
|
|
|
|
{ struct exynos_drm_gem_obj *exynos_gem_obj;
|
|
|
|
struct drm_exynos_gem_info *args = data;
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
|
|
|
|
mutex_lock(&dev->struct_mutex);
|
|
|
|
|
|
|
|
obj = drm_gem_object_lookup(dev, file_priv, args->handle);
|
|
|
|
if (!obj) {
|
|
|
|
DRM_ERROR("failed to lookup gem object.\n");
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
exynos_gem_obj = to_exynos_gem_obj(obj);
|
|
|
|
|
|
|
|
args->flags = exynos_gem_obj->flags;
|
|
|
|
args->size = exynos_gem_obj->size;
|
|
|
|
|
|
|
|
drm_gem_object_unreference(obj);
|
|
|
|
mutex_unlock(&dev->struct_mutex);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
drm/exynos: add userptr feature for g2d module
This patch adds userptr feautre for G2D module.
The userptr means user space address allocated by malloc().
And the purpose of this feature is to make G2D's dma able
to access the user space region.
To user this feature, user should flag G2D_BUF_USRPTR to
offset variable of struct drm_exynos_g2d_cmd and fill
struct drm_exynos_g2d_userptr with user space address
and size for it and then should set a pointer to
drm_exynos_g2d_userptr object to data variable of struct
drm_exynos_g2d_cmd. The last bit of offset variable is used
to check if the cmdlist's buffer type is userptr or not.
If userptr, the g2d driver gets user space address and size
and then gets pages through get_user_pages().
(another case is counted as gem handle)
Below is sample codes:
static void set_cmd(struct drm_exynos_g2d_cmd *cmd,
unsigned long offset, unsigned long data)
{
cmd->offset = offset;
cmd->data = data;
}
static int solid_fill_test(int x, int y, unsigned long userptr)
{
struct drm_exynos_g2d_cmd cmd_gem[5];
struct drm_exynos_g2d_userptr g2d_userptr;
unsigned int gem_nr = 0;
...
g2d_userptr.userptr = userptr;
g2d_userptr.size = x * y * 4;
set_cmd(&cmd_gem[gem_nr++], DST_BASE_ADDR_REG |
G2D_BUF_USERPTR,
(unsigned long)&g2d_userptr);
...
}
int main(int argc, char **argv)
{
unsigned long addr;
...
addr = malloc(x * y * 4);
...
solid_fill_test(x, y, addr);
...
}
And next, the pages are mapped with iommu table and the device
address is set to cmdlist so that G2D's dma can access it.
As you may know, the pages from get_user_pages() are pinned.
In other words, they CAN NOT be migrated and also swapped out.
So the dma access would be safe.
But the use of userptr feature has performance overhead so
this patch also has memory pool to the userptr feature.
Please, assume that user sends cmdlist filled with userptr
and size every time to g2d driver, and the get_user_pages
funcion will be called every time.
The memory pool has maximum 64MB size and the userptr that
user had ever sent, is holded in the memory pool.
This meaning is that if the userptr from user is same as one
in the memory pool, device address to the userptr in the memory
pool is set to cmdlist.
And last, the pages from get_user_pages() will be freed once
user calls free() and the dma access is completed. Actually,
get_user_pages() takes 2 reference counts if the user process
has never accessed user region allocated by malloc(). Then, if
the user calls free(), the page reference count becomes 1 and
becomes 0 with put_page() call. And the reverse holds as well.
This means how the pages backed are used by dma and freed.
This patch is based on "drm/exynos: add iommu support for g2d",
https://patchwork.kernel.org/patch/1629481/
Signed-off-by: Inki Dae <inki.dae@samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
2012-11-04 21:48:52 +08:00
|
|
|
struct vm_area_struct *exynos_gem_get_vma(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct vm_area_struct *vma_copy;
|
|
|
|
|
|
|
|
vma_copy = kmalloc(sizeof(*vma_copy), GFP_KERNEL);
|
|
|
|
if (!vma_copy)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (vma->vm_ops && vma->vm_ops->open)
|
|
|
|
vma->vm_ops->open(vma);
|
|
|
|
|
|
|
|
if (vma->vm_file)
|
|
|
|
get_file(vma->vm_file);
|
|
|
|
|
|
|
|
memcpy(vma_copy, vma, sizeof(*vma));
|
|
|
|
|
|
|
|
vma_copy->vm_mm = NULL;
|
|
|
|
vma_copy->vm_next = NULL;
|
|
|
|
vma_copy->vm_prev = NULL;
|
|
|
|
|
|
|
|
return vma_copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void exynos_gem_put_vma(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
if (!vma)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (vma->vm_ops && vma->vm_ops->close)
|
|
|
|
vma->vm_ops->close(vma);
|
|
|
|
|
|
|
|
if (vma->vm_file)
|
|
|
|
fput(vma->vm_file);
|
|
|
|
|
|
|
|
kfree(vma);
|
|
|
|
}
|
|
|
|
|
|
|
|
int exynos_gem_get_pages_from_userptr(unsigned long start,
|
|
|
|
unsigned int npages,
|
|
|
|
struct page **pages,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
int get_npages;
|
|
|
|
|
|
|
|
/* the memory region mmaped with VM_PFNMAP. */
|
|
|
|
if (vma_is_io(vma)) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < npages; ++i, start += PAGE_SIZE) {
|
|
|
|
unsigned long pfn;
|
|
|
|
int ret = follow_pfn(vma, start, &pfn);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
pages[i] = pfn_to_page(pfn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != npages) {
|
|
|
|
DRM_ERROR("failed to get user_pages.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
get_npages = get_user_pages(current, current->mm, start,
|
|
|
|
npages, 1, 1, pages, NULL);
|
|
|
|
get_npages = max(get_npages, 0);
|
|
|
|
if (get_npages != npages) {
|
|
|
|
DRM_ERROR("failed to get user_pages.\n");
|
|
|
|
while (get_npages)
|
|
|
|
put_page(pages[--get_npages]);
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void exynos_gem_put_pages_to_userptr(struct page **pages,
|
|
|
|
unsigned int npages,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
if (!vma_is_io(vma)) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < npages; i++) {
|
|
|
|
set_page_dirty_lock(pages[i]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* undo the reference we took when populating
|
|
|
|
* the table.
|
|
|
|
*/
|
|
|
|
put_page(pages[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int exynos_gem_map_sgt_with_dma(struct drm_device *drm_dev,
|
|
|
|
struct sg_table *sgt,
|
|
|
|
enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
int nents;
|
|
|
|
|
|
|
|
mutex_lock(&drm_dev->struct_mutex);
|
|
|
|
|
|
|
|
nents = dma_map_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
|
|
|
|
if (!nents) {
|
|
|
|
DRM_ERROR("failed to map sgl with dma.\n");
|
|
|
|
mutex_unlock(&drm_dev->struct_mutex);
|
|
|
|
return nents;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&drm_dev->struct_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void exynos_gem_unmap_sgt_from_dma(struct drm_device *drm_dev,
|
|
|
|
struct sg_table *sgt,
|
|
|
|
enum dma_data_direction dir)
|
|
|
|
{
|
|
|
|
dma_unmap_sg(drm_dev->dev, sgt->sgl, sgt->nents, dir);
|
|
|
|
}
|
|
|
|
|
2011-12-13 13:20:23 +08:00
|
|
|
void exynos_drm_gem_free_object(struct drm_gem_object *obj)
|
2011-10-04 18:19:01 +08:00
|
|
|
{
|
2011-12-13 13:39:13 +08:00
|
|
|
exynos_drm_gem_destroy(to_exynos_gem_obj(obj));
|
2011-10-04 18:19:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
|
2011-12-13 13:20:23 +08:00
|
|
|
struct drm_device *dev,
|
|
|
|
struct drm_mode_create_dumb *args)
|
2011-10-04 18:19:01 +08:00
|
|
|
{
|
|
|
|
struct exynos_drm_gem_obj *exynos_gem_obj;
|
2011-12-13 13:39:13 +08:00
|
|
|
int ret;
|
2011-10-04 18:19:01 +08:00
|
|
|
|
|
|
|
/*
|
2013-10-24 15:02:57 +08:00
|
|
|
* allocate memory to be used for framebuffer.
|
2011-10-04 18:19:01 +08:00
|
|
|
* - this callback would be called by user application
|
|
|
|
* with DRM_IOCTL_MODE_CREATE_DUMB command.
|
|
|
|
*/
|
|
|
|
|
2012-06-29 10:49:45 +08:00
|
|
|
args->pitch = args->width * ((args->bpp + 7) / 8);
|
2012-08-17 14:24:03 +08:00
|
|
|
args->size = args->pitch * args->height;
|
2011-10-04 18:19:01 +08:00
|
|
|
|
2014-05-07 19:51:29 +08:00
|
|
|
if (is_drm_iommu_supported(dev)) {
|
|
|
|
exynos_gem_obj = exynos_drm_gem_create(dev,
|
|
|
|
EXYNOS_BO_NONCONTIG | EXYNOS_BO_WC,
|
|
|
|
args->size);
|
|
|
|
} else {
|
2013-08-23 14:35:06 +08:00
|
|
|
exynos_gem_obj = exynos_drm_gem_create(dev,
|
2014-05-07 19:51:29 +08:00
|
|
|
EXYNOS_BO_CONTIG | EXYNOS_BO_WC,
|
|
|
|
args->size);
|
2013-08-23 14:35:06 +08:00
|
|
|
}
|
|
|
|
|
2014-05-07 19:51:29 +08:00
|
|
|
if (IS_ERR(exynos_gem_obj)) {
|
|
|
|
dev_warn(dev->dev, "FB allocation failed.\n");
|
2011-10-04 18:19:01 +08:00
|
|
|
return PTR_ERR(exynos_gem_obj);
|
2014-05-07 19:51:29 +08:00
|
|
|
}
|
2011-10-04 18:19:01 +08:00
|
|
|
|
2011-12-13 13:39:13 +08:00
|
|
|
ret = exynos_drm_gem_handle_create(&exynos_gem_obj->base, file_priv,
|
|
|
|
&args->handle);
|
|
|
|
if (ret) {
|
|
|
|
exynos_drm_gem_destroy(exynos_gem_obj);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-10-04 18:19:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
|
2011-12-13 13:20:23 +08:00
|
|
|
struct drm_device *dev, uint32_t handle,
|
|
|
|
uint64_t *offset)
|
2011-10-04 18:19:01 +08:00
|
|
|
{
|
|
|
|
struct drm_gem_object *obj;
|
2011-12-13 13:32:24 +08:00
|
|
|
int ret = 0;
|
2011-10-04 18:19:01 +08:00
|
|
|
|
|
|
|
mutex_lock(&dev->struct_mutex);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get offset of memory allocated for drm framebuffer.
|
|
|
|
* - this callback would be called by user application
|
|
|
|
* with DRM_IOCTL_MODE_MAP_DUMB command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
obj = drm_gem_object_lookup(dev, file_priv, handle);
|
|
|
|
if (!obj) {
|
|
|
|
DRM_ERROR("failed to lookup gem object.\n");
|
2011-12-13 13:32:24 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto unlock;
|
2011-10-04 18:19:01 +08:00
|
|
|
}
|
|
|
|
|
2013-07-25 03:07:52 +08:00
|
|
|
ret = drm_gem_create_mmap_offset(obj);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
2011-10-04 18:19:01 +08:00
|
|
|
|
2013-07-25 03:07:52 +08:00
|
|
|
*offset = drm_vma_node_offset_addr(&obj->vma_node);
|
2011-10-04 18:19:01 +08:00
|
|
|
DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
|
|
|
|
|
2011-12-13 13:32:24 +08:00
|
|
|
out:
|
|
|
|
drm_gem_object_unreference(obj);
|
|
|
|
unlock:
|
2011-10-04 18:19:01 +08:00
|
|
|
mutex_unlock(&dev->struct_mutex);
|
2011-12-13 13:32:24 +08:00
|
|
|
return ret;
|
2011-10-04 18:19:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
|
|
|
|
{
|
|
|
|
struct drm_gem_object *obj = vma->vm_private_data;
|
2015-07-28 16:53:19 +08:00
|
|
|
struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
|
|
|
|
unsigned long pfn;
|
2011-10-04 18:19:01 +08:00
|
|
|
pgoff_t page_offset;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
page_offset = ((unsigned long)vmf->virtual_address -
|
|
|
|
vma->vm_start) >> PAGE_SHIFT;
|
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
if (page_offset >= (exynos_gem_obj->size >> PAGE_SHIFT)) {
|
2015-07-28 16:53:19 +08:00
|
|
|
DRM_ERROR("invalid page offset\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2011-10-04 18:19:01 +08:00
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
pfn = page_to_pfn(exynos_gem_obj->pages[page_offset]);
|
2015-07-28 16:53:19 +08:00
|
|
|
ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn);
|
|
|
|
|
|
|
|
out:
|
2015-07-28 16:53:17 +08:00
|
|
|
switch (ret) {
|
|
|
|
case 0:
|
|
|
|
case -ERESTARTSYS:
|
|
|
|
case -EINTR:
|
|
|
|
return VM_FAULT_NOPAGE;
|
|
|
|
case -ENOMEM:
|
|
|
|
return VM_FAULT_OOM;
|
|
|
|
default:
|
|
|
|
return VM_FAULT_SIGBUS;
|
|
|
|
}
|
2011-10-04 18:19:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
|
|
|
|
{
|
2012-04-23 18:26:34 +08:00
|
|
|
struct exynos_drm_gem_obj *exynos_gem_obj;
|
|
|
|
struct drm_gem_object *obj;
|
2011-10-04 18:19:01 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* set vm_area_struct. */
|
|
|
|
ret = drm_gem_mmap(filp, vma);
|
|
|
|
if (ret < 0) {
|
|
|
|
DRM_ERROR("failed to mmap.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-23 18:26:34 +08:00
|
|
|
obj = vma->vm_private_data;
|
|
|
|
exynos_gem_obj = to_exynos_gem_obj(obj);
|
|
|
|
|
2015-08-16 13:16:49 +08:00
|
|
|
DRM_DEBUG_KMS("flags = 0x%x\n", exynos_gem_obj->flags);
|
|
|
|
|
|
|
|
/* non-cachable as default. */
|
|
|
|
if (exynos_gem_obj->flags & EXYNOS_BO_CACHABLE)
|
|
|
|
vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
|
|
|
|
else if (exynos_gem_obj->flags & EXYNOS_BO_WC)
|
|
|
|
vma->vm_page_prot =
|
|
|
|
pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
|
|
|
|
else
|
|
|
|
vma->vm_page_prot =
|
|
|
|
pgprot_noncached(vm_get_page_prot(vma->vm_flags));
|
2012-04-23 18:26:34 +08:00
|
|
|
|
2014-09-18 13:19:01 +08:00
|
|
|
ret = exynos_drm_gem_mmap_buffer(exynos_gem_obj, vma);
|
|
|
|
if (ret)
|
|
|
|
goto err_close_vm;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
err_close_vm:
|
|
|
|
drm_gem_vm_close(vma);
|
|
|
|
drm_gem_free_mmap_offset(obj);
|
|
|
|
|
2011-10-04 18:19:01 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2015-08-16 13:33:08 +08:00
|
|
|
|
|
|
|
/* low-level interface prime helpers */
|
|
|
|
struct sg_table *exynos_drm_gem_prime_get_sg_table(struct drm_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
|
|
|
|
int npages;
|
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
npages = exynos_gem_obj->size >> PAGE_SHIFT;
|
2015-08-16 13:33:08 +08:00
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
return drm_prime_pages_to_sg(exynos_gem_obj->pages, npages);
|
2015-08-16 13:33:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct drm_gem_object *
|
|
|
|
exynos_drm_gem_prime_import_sg_table(struct drm_device *dev,
|
|
|
|
struct dma_buf_attachment *attach,
|
|
|
|
struct sg_table *sgt)
|
|
|
|
{
|
|
|
|
struct exynos_drm_gem_obj *exynos_gem_obj;
|
|
|
|
int npages;
|
|
|
|
int ret;
|
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
exynos_gem_obj = exynos_drm_gem_init(dev, attach->dmabuf->size);
|
|
|
|
if (IS_ERR(exynos_gem_obj)) {
|
|
|
|
ret = PTR_ERR(exynos_gem_obj);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-08-16 13:33:08 +08:00
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
exynos_gem_obj->dma_addr = sg_dma_address(sgt->sgl);
|
2015-08-16 13:33:08 +08:00
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
npages = exynos_gem_obj->size >> PAGE_SHIFT;
|
|
|
|
exynos_gem_obj->pages = drm_malloc_ab(npages, sizeof(struct page *));
|
|
|
|
if (!exynos_gem_obj->pages) {
|
2015-08-16 13:33:08 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-08-16 13:38:49 +08:00
|
|
|
ret = drm_prime_sg_to_page_addr_arrays(sgt, exynos_gem_obj->pages, NULL,
|
|
|
|
npages);
|
2015-08-16 13:33:08 +08:00
|
|
|
if (ret < 0)
|
|
|
|
goto err_free_large;
|
|
|
|
|
|
|
|
if (sgt->nents == 1) {
|
|
|
|
/* always physically continuous memory if sgt->nents is 1. */
|
|
|
|
exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* this case could be CONTIG or NONCONTIG type but for now
|
|
|
|
* sets NONCONTIG.
|
|
|
|
* TODO. we have to find a way that exporter can notify
|
|
|
|
* the type of its own buffer to importer.
|
|
|
|
*/
|
|
|
|
exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &exynos_gem_obj->base;
|
|
|
|
|
|
|
|
err_free_large:
|
2015-08-16 13:38:49 +08:00
|
|
|
drm_free_large(exynos_gem_obj->pages);
|
2015-08-16 13:33:08 +08:00
|
|
|
err:
|
2015-08-16 13:38:49 +08:00
|
|
|
drm_gem_object_release(&exynos_gem_obj->base);
|
|
|
|
kfree(exynos_gem_obj);
|
2015-08-16 13:33:08 +08:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *exynos_drm_gem_prime_vmap(struct drm_gem_object *obj)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void exynos_drm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
|
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
}
|