2014-08-22 14:36:26 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
|
|
|
|
* Author:Mark Yao <mark.yao@rock-chips.com>
|
|
|
|
*
|
|
|
|
* This software is licensed under the terms of the GNU General Public
|
|
|
|
* License version 2, as published by the Free Software Foundation, and
|
|
|
|
* may be copied, distributed, and modified under those terms.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <drm/drm.h>
|
|
|
|
#include <drm/drmP.h>
|
|
|
|
#include <drm/drm_gem.h>
|
|
|
|
#include <drm/drm_vma_manager.h>
|
|
|
|
|
|
|
|
#include <linux/dma-attrs.h>
|
|
|
|
|
|
|
|
#include "rockchip_drm_drv.h"
|
|
|
|
#include "rockchip_drm_gem.h"
|
|
|
|
|
2015-01-12 09:58:23 +03:00
|
|
|
static int rockchip_gem_alloc_buf(struct rockchip_gem_object *rk_obj,
|
|
|
|
bool alloc_kmap)
|
2014-08-22 14:36:26 +04:00
|
|
|
{
|
|
|
|
struct drm_gem_object *obj = &rk_obj->base;
|
|
|
|
struct drm_device *drm = obj->dev;
|
|
|
|
|
|
|
|
init_dma_attrs(&rk_obj->dma_attrs);
|
|
|
|
dma_set_attr(DMA_ATTR_WRITE_COMBINE, &rk_obj->dma_attrs);
|
|
|
|
|
2015-01-12 09:58:23 +03:00
|
|
|
if (!alloc_kmap)
|
|
|
|
dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs);
|
|
|
|
|
2014-08-22 14:36:26 +04:00
|
|
|
rk_obj->kvaddr = dma_alloc_attrs(drm->dev, obj->size,
|
|
|
|
&rk_obj->dma_addr, GFP_KERNEL,
|
|
|
|
&rk_obj->dma_attrs);
|
2015-01-07 12:27:06 +03:00
|
|
|
if (!rk_obj->kvaddr) {
|
2016-06-09 20:46:32 +03:00
|
|
|
DRM_ERROR("failed to allocate %zu byte dma buffer", obj->size);
|
2015-01-07 12:27:06 +03:00
|
|
|
return -ENOMEM;
|
2014-08-22 14:36:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rockchip_gem_free_buf(struct rockchip_gem_object *rk_obj)
|
|
|
|
{
|
|
|
|
struct drm_gem_object *obj = &rk_obj->base;
|
|
|
|
struct drm_device *drm = obj->dev;
|
|
|
|
|
|
|
|
dma_free_attrs(drm->dev, obj->size, rk_obj->kvaddr, rk_obj->dma_addr,
|
|
|
|
&rk_obj->dma_attrs);
|
|
|
|
}
|
|
|
|
|
2015-07-07 12:03:36 +03:00
|
|
|
static int rockchip_drm_gem_object_mmap(struct drm_gem_object *obj,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
|
2014-08-22 14:36:26 +04:00
|
|
|
{
|
2015-07-07 12:03:36 +03:00
|
|
|
int ret;
|
2014-08-22 14:36:26 +04:00
|
|
|
struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
|
|
|
|
struct drm_device *drm = obj->dev;
|
|
|
|
|
2015-07-07 12:03:36 +03:00
|
|
|
/*
|
|
|
|
* dma_alloc_attrs() allocated a struct page table for rk_obj, so clear
|
|
|
|
* VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
|
|
|
|
*/
|
|
|
|
vma->vm_flags &= ~VM_PFNMAP;
|
2015-11-09 22:52:43 +03:00
|
|
|
vma->vm_pgoff = 0;
|
2014-08-22 14:36:26 +04:00
|
|
|
|
2015-07-07 12:03:36 +03:00
|
|
|
ret = dma_mmap_attrs(drm->dev, vma, rk_obj->kvaddr, rk_obj->dma_addr,
|
2014-08-22 14:36:26 +04:00
|
|
|
obj->size, &rk_obj->dma_attrs);
|
2015-07-07 12:03:36 +03:00
|
|
|
if (ret)
|
|
|
|
drm_gem_vm_close(vma);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rockchip_gem_mmap_buf(struct drm_gem_object *obj,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = drm_gem_mmap_obj(obj, obj->size, vma);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return rockchip_drm_gem_object_mmap(obj, vma);
|
2014-08-22 14:36:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* drm driver mmap file operations */
|
|
|
|
int rockchip_gem_mmap(struct file *filp, struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
int ret;
|
|
|
|
|
2015-07-07 12:03:36 +03:00
|
|
|
ret = drm_gem_mmap(filp, vma);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-08-22 14:36:26 +04:00
|
|
|
|
2015-07-07 12:03:36 +03:00
|
|
|
obj = vma->vm_private_data;
|
2014-08-22 14:36:26 +04:00
|
|
|
|
2015-07-07 12:03:36 +03:00
|
|
|
return rockchip_drm_gem_object_mmap(obj, vma);
|
2014-08-22 14:36:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct rockchip_gem_object *
|
2015-01-12 09:58:23 +03:00
|
|
|
rockchip_gem_create_object(struct drm_device *drm, unsigned int size,
|
|
|
|
bool alloc_kmap)
|
2014-08-22 14:36:26 +04:00
|
|
|
{
|
|
|
|
struct rockchip_gem_object *rk_obj;
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
size = round_up(size, PAGE_SIZE);
|
|
|
|
|
|
|
|
rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
|
|
|
|
if (!rk_obj)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
obj = &rk_obj->base;
|
|
|
|
|
|
|
|
drm_gem_private_object_init(drm, obj, size);
|
|
|
|
|
2015-01-12 09:58:23 +03:00
|
|
|
ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap);
|
2014-08-22 14:36:26 +04:00
|
|
|
if (ret)
|
|
|
|
goto err_free_rk_obj;
|
|
|
|
|
|
|
|
return rk_obj;
|
|
|
|
|
|
|
|
err_free_rk_obj:
|
|
|
|
kfree(rk_obj);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rockchip_gem_free_object - (struct drm_driver)->gem_free_object callback
|
|
|
|
* function
|
|
|
|
*/
|
|
|
|
void rockchip_gem_free_object(struct drm_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct rockchip_gem_object *rk_obj;
|
|
|
|
|
|
|
|
drm_gem_free_mmap_offset(obj);
|
|
|
|
|
|
|
|
rk_obj = to_rockchip_obj(obj);
|
|
|
|
|
|
|
|
rockchip_gem_free_buf(rk_obj);
|
|
|
|
|
|
|
|
kfree(rk_obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rockchip_gem_create_with_handle - allocate an object with the given
|
|
|
|
* size and create a gem handle on it
|
|
|
|
*
|
|
|
|
* returns a struct rockchip_gem_object* on success or ERR_PTR values
|
|
|
|
* on failure.
|
|
|
|
*/
|
|
|
|
static struct rockchip_gem_object *
|
|
|
|
rockchip_gem_create_with_handle(struct drm_file *file_priv,
|
|
|
|
struct drm_device *drm, unsigned int size,
|
|
|
|
unsigned int *handle)
|
|
|
|
{
|
|
|
|
struct rockchip_gem_object *rk_obj;
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
int ret;
|
|
|
|
|
2015-01-12 09:58:23 +03:00
|
|
|
rk_obj = rockchip_gem_create_object(drm, size, false);
|
2014-08-22 14:36:26 +04:00
|
|
|
if (IS_ERR(rk_obj))
|
|
|
|
return ERR_CAST(rk_obj);
|
|
|
|
|
|
|
|
obj = &rk_obj->base;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
goto err_handle_create;
|
|
|
|
|
|
|
|
/* drop reference from allocate - handle holds it now. */
|
|
|
|
drm_gem_object_unreference_unlocked(obj);
|
|
|
|
|
|
|
|
return rk_obj;
|
|
|
|
|
|
|
|
err_handle_create:
|
|
|
|
rockchip_gem_free_object(obj);
|
|
|
|
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rockchip_gem_dumb_map_offset(struct drm_file *file_priv,
|
|
|
|
struct drm_device *dev, uint32_t handle,
|
|
|
|
uint64_t *offset)
|
|
|
|
{
|
|
|
|
struct drm_gem_object *obj;
|
|
|
|
int ret;
|
|
|
|
|
2016-05-09 13:04:54 +03:00
|
|
|
obj = drm_gem_object_lookup(file_priv, handle);
|
2014-08-22 14:36:26 +04:00
|
|
|
if (!obj) {
|
|
|
|
DRM_ERROR("failed to lookup gem object.\n");
|
2015-07-10 00:32:42 +03:00
|
|
|
return -EINVAL;
|
2014-08-22 14:36:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = drm_gem_create_mmap_offset(obj);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
*offset = drm_vma_node_offset_addr(&obj->vma_node);
|
|
|
|
DRM_DEBUG_KMS("offset = 0x%llx\n", *offset);
|
|
|
|
|
|
|
|
out:
|
2015-07-10 00:32:42 +03:00
|
|
|
drm_gem_object_unreference_unlocked(obj);
|
|
|
|
|
|
|
|
return 0;
|
2014-08-22 14:36:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
|
|
|
|
* function
|
|
|
|
*
|
|
|
|
* This aligns the pitch and size arguments to the minimum required. wrap
|
|
|
|
* this into your own function if you need bigger alignment.
|
|
|
|
*/
|
|
|
|
int rockchip_gem_dumb_create(struct drm_file *file_priv,
|
|
|
|
struct drm_device *dev,
|
|
|
|
struct drm_mode_create_dumb *args)
|
|
|
|
{
|
|
|
|
struct rockchip_gem_object *rk_obj;
|
|
|
|
int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* align to 64 bytes since Mali requires it.
|
|
|
|
*/
|
drm/rockchip: fix wrong pitch/size using on gem
args->pitch and args->size may not be set by userspace, sometimes
userspace only malloc args and not memset args to zero, then
args->pitch and args->size is random, it is very danger to use
pitch/size on gem.
pitch's type is u32, and min_pitch's type is int, example,
pitch is 0xffffffff, then pitch < min_pitch return true, then gem will
alloc very very big bufffer, it would eat all the memory and cause kernel
crash.
Stop using pitch/size from args, calc them from other args.
Signed-off-by: Mark Yao <mark.yao@rock-chips.com>
2015-09-23 07:34:34 +03:00
|
|
|
args->pitch = ALIGN(min_pitch, 64);
|
|
|
|
args->size = args->pitch * args->height;
|
2014-08-22 14:36:26 +04:00
|
|
|
|
|
|
|
rk_obj = rockchip_gem_create_with_handle(file_priv, dev, args->size,
|
|
|
|
&args->handle);
|
|
|
|
|
|
|
|
return PTR_ERR_OR_ZERO(rk_obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a sg_table for this GEM object.
|
|
|
|
* Note: Both the table's contents, and the sg_table itself must be freed by
|
|
|
|
* the caller.
|
|
|
|
* Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
|
|
|
|
*/
|
|
|
|
struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
|
|
|
|
struct drm_device *drm = obj->dev;
|
|
|
|
struct sg_table *sgt;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
|
|
|
|
if (!sgt)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr,
|
|
|
|
rk_obj->dma_addr, obj->size,
|
|
|
|
&rk_obj->dma_attrs);
|
|
|
|
if (ret) {
|
|
|
|
DRM_ERROR("failed to allocate sgt, %d\n", ret);
|
|
|
|
kfree(sgt);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sgt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *rockchip_gem_prime_vmap(struct drm_gem_object *obj)
|
|
|
|
{
|
|
|
|
struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
|
|
|
|
|
2015-01-12 09:58:23 +03:00
|
|
|
if (dma_get_attr(DMA_ATTR_NO_KERNEL_MAPPING, &rk_obj->dma_attrs))
|
|
|
|
return NULL;
|
|
|
|
|
2014-08-22 14:36:26 +04:00
|
|
|
return rk_obj->kvaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rockchip_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
|
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
}
|