Re: [PATCH v9 14/14] drm/mediatek: Add interface to allocate Mediatek GEM buffer.

Frank Binns
2016-01-12T15:47:58Z

Hi Philipp,

Comments below.

On 12/01/16 15:15, Philipp Zabel wrote:
> From: CK Hu <ck.hu@mediatek.com>
>
> Add an interface to allocate Mediatek GEM buffers, allow the IOCTLs
> to be used by render nodes.
> This patch also sets the RENDER driver feature.
>
> Signed-off-by: CK Hu <ck.hu@mediatek.com>
> Signed-off-by: Nicolas Boichat <drinkcat@chromium.org>
> Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
> ---
> drivers/gpu/drm/mediatek/mtk_drm_drv.c | 13 +++++++-
> drivers/gpu/drm/mediatek/mtk_drm_gem.c | 39 ++++++++++++++++++++++
> drivers/gpu/drm/mediatek/mtk_drm_gem.h | 12 +++++++
> include/uapi/drm/mediatek_drm.h | 59 ++++++++++++++++++++++++++++++++++
> 4 files changed, 122 insertions(+), 1 deletion(-)
> create mode 100644 include/uapi/drm/mediatek_drm.h
>
> diff --git a/drivers/gpu/drm/mediatek/mtk_drm_drv.c b/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> index fdb27e9..1f776a9 100644
> --- a/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> +++ b/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> @@ -22,6 +22,7 @@
> #include <linux/of_address.h>
> #include <linux/of_platform.h>
> #include <linux/pm_runtime.h>
> +#include <drm/mediatek_drm.h>
>
> #include "mtk_cec.h"
> #include "mtk_drm_crtc.h"
> @@ -222,6 +223,14 @@ static const struct vm_operations_struct mtk_drm_gem_vm_ops = {
> .close = drm_gem_vm_close,
> };
>
> +static const struct drm_ioctl_desc mtk_ioctls[] = {
> + DRM_IOCTL_DEF_DRV(MTK_GEM_CREATE, mtk_gem_create_ioctl,
> + DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
> + DRM_IOCTL_DEF_DRV(MTK_GEM_MAP_OFFSET,
> + mtk_gem_map_offset_ioctl,
> + DRM_UNLOCKED | DRM_AUTH | DRM_RENDER_ALLOW),
> +};
> +
> static const struct file_operations mtk_drm_fops = {
> .owner = THIS_MODULE,
> .open = drm_open,
> @@ -237,7 +246,7 @@ static const struct file_operations mtk_drm_fops = {
>
> static struct drm_driver mtk_drm_driver = {
> .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
> - DRIVER_ATOMIC,
> + DRIVER_ATOMIC | DRIVER_RENDER,
> .unload = mtk_drm_unload,
> .set_busid = drm_platform_set_busid,
>
> @@ -257,6 +266,8 @@ static struct drm_driver mtk_drm_driver = {
> .gem_prime_import = drm_gem_prime_import,
> .gem_prime_get_sg_table = mtk_gem_prime_get_sg_table,
> .gem_prime_mmap = mtk_drm_gem_mmap_buf,
> + .ioctls = mtk_ioctls,
> + .num_ioctls = ARRAY_SIZE(mtk_ioctls),
> .fops = &mtk_drm_fops,
>
> .name = DRIVER_NAME,
> diff --git a/drivers/gpu/drm/mediatek/mtk_drm_gem.c b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> index 96cc980..f726d55 100644
> --- a/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> +++ b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> @@ -13,6 +13,7 @@
>
> #include <drm/drmP.h>
> #include <drm/drm_gem.h>
> +#include <drm/mediatek_drm.h>
>
> #include "mtk_drm_gem.h"
>
> @@ -225,3 +226,41 @@ struct sg_table *mtk_gem_prime_get_sg_table(struct drm_gem_object *obj)
>
> return sgt;
> }
> +
> +int mtk_gem_map_offset_ioctl(struct drm_device *drm, void *data,
> + struct drm_file *file_priv)
> +{
> + struct drm_mtk_gem_map_off *args = data;
> +
You should validate args->pad here.

> + return mtk_drm_gem_dumb_map_offset(file_priv, drm, args->handle,
> + &args->offset);
> +}
> +
> +int mtk_gem_create_ioctl(struct drm_device *dev, void *data,
> + struct drm_file *file_priv)
> +{
> + struct mtk_drm_gem_obj *mtk_gem;
> + struct drm_mtk_gem_create *args = data;
> + int ret;
> +
You should validate args->flags here.

> + mtk_gem = mtk_drm_gem_create(dev, args->size, false);
> + if (IS_ERR(mtk_gem))
> + return PTR_ERR(mtk_gem);
> +
> + /*
> + * allocate a id of idr table where the obj is registered
> + * and handle has the id what user can see.
> + */
This comment doesn't seem that useful.

> + ret = drm_gem_handle_create(file_priv, &mtk_gem->base, &args->handle);
> + if (ret)
> + goto err_handle_create;
> +
> + /* drop reference from allocate - handle holds it now. */
> + drm_gem_object_unreference_unlocked(&mtk_gem->base);
> +
> + return 0;
> +
> +err_handle_create:
> + mtk_drm_gem_free_object(&mtk_gem->base);
> + return ret;
> +}
> diff --git a/drivers/gpu/drm/mediatek/mtk_drm_gem.h b/drivers/gpu/drm/mediatek/mtk_drm_gem.h
> index 9bdeeb3..28b8fa7 100644
> --- a/drivers/gpu/drm/mediatek/mtk_drm_gem.h
> +++ b/drivers/gpu/drm/mediatek/mtk_drm_gem.h
> @@ -52,4 +52,16 @@ int mtk_drm_gem_mmap_buf(struct drm_gem_object *obj,
> struct vm_area_struct *vma);
> struct sg_table *mtk_gem_prime_get_sg_table(struct drm_gem_object *obj);
>
> +/*
> + * request gem object creation and buffer allocation as the size
> + * that it is calculated with framebuffer information such as width,
> + * height and bpp.
> + */
> +int mtk_gem_create_ioctl(struct drm_device *dev, void *data,
> + struct drm_file *file_priv);
> +
> +/* get buffer offset to map to user space. */
> +int mtk_gem_map_offset_ioctl(struct drm_device *dev, void *data,
> + struct drm_file *file_priv);
> +
> #endif
> diff --git a/include/uapi/drm/mediatek_drm.h b/include/uapi/drm/mediatek_drm.h
> new file mode 100644
> index 0000000..19ea357
> --- /dev/null
> +++ b/include/uapi/drm/mediatek_drm.h
> @@ -0,0 +1,59 @@
> +/*
> + * Copyright (c) 2015 MediaTek Inc.
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + * 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.
> + */
> +
> +
> +#ifndef _UAPI_MEDIATEK_DRM_H
> +#define _UAPI_MEDIATEK_DRM_H
> +
> +#include <drm/drm.h>
> +
> +/**
> + * User-desired buffer creation information structure.
> + *
> + * @size: user-desired memory allocation size.
> + * - this size value would be page-aligned internally.
> + * @flags: user request for setting memory type or cache attributes.
> + * @handle: returned a handle to created gem object.
> + * - this handle will be set by gem module of kernel side.
> + */
> +struct drm_mtk_gem_create {
> + uint64_t size;
> + uint32_t flags;
> + uint32_t handle;
> +};
These should be __u32 and friends according to the kernel CodingStyle doc.

> +
> +/**
> + * A structure for getting buffer offset.
> + *
> + * @handle: a pointer to gem object created.
> + * @pad: just padding to be 64-bit aligned.
> + * @offset: relatived offset value of the memory region allocated.
> + * - this value should be set by user.
relatived -> relative?

Also, the 'offset' documentation doesn't seem right as this will be set
by the kernel?

> + */
> +struct drm_mtk_gem_map_off {
> + uint32_t handle;
> + uint32_t pad;
> + uint64_t offset;
> +};
These should also be __u32 and friends.

> +
> +#define DRM_MTK_GEM_CREATE 0x00
> +#define DRM_MTK_GEM_MAP_OFFSET 0x01
> +
> +#define DRM_IOCTL_MTK_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + \
> + DRM_MTK_GEM_CREATE, struct drm_mtk_gem_create)
> +
> +#define DRM_IOCTL_MTK_GEM_MAP_OFFSET DRM_IOWR(DRM_COMMAND_BASE + \
> + DRM_MTK_GEM_MAP_OFFSET, struct drm_mtk_gem_map_off)
> +
> +
> +#endif /* _UAPI_MEDIATEK_DRM_H */