diff mbox

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

Message ID 1452611750-16283-15-git-send-email-p.zabel@pengutronix.de (mailing list archive)
State New, archived
Headers show

Commit Message

Philipp Zabel Jan. 12, 2016, 3:15 p.m. UTC
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

Comments

Frank Binns Jan. 12, 2016, 3:47 p.m. UTC | #1
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 */
Rob Herring Jan. 12, 2016, 10:02 p.m. UTC | #2
On Tue, Jan 12, 2016 at 9:15 AM, Philipp Zabel <p.zabel@pengutronix.de> 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.

But it should not a be render node unless it has a GPU AFAIK. Then
again, I still don't understand the madness of every driver defining
their own buffer ioctls either. The only line remotely h/w specific
here is mtk_drm_gem_create call.

Rob
Daniel Vetter Jan. 12, 2016, 10:40 p.m. UTC | #3
On Tue, Jan 12, 2016 at 11:02 PM, Rob Herring <robh+dt@kernel.org> wrote:
> On Tue, Jan 12, 2016 at 9:15 AM, Philipp Zabel <p.zabel@pengutronix.de> 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.
>
> But it should not a be render node unless it has a GPU AFAIK. Then
> again, I still don't understand the madness of every driver defining
> their own buffer ioctls either. The only line remotely h/w specific
> here is mtk_drm_gem_create call.

Support gem_create/mmap_offet alone is indeed pointless without some
real support for gpu workloads. For plain display drivers the dumb
buffer api, plus prime/dma-buf import should be plenty enough.

The usual reason for doing this is some blob driver for opengl that
can't be open-source, which is a big no-go for upstream.

Imo best to just rip this out, consider it nacked without full-blown
userspace and whatever else is needing these buffers.
-Daniel
Philipp Zabel Jan. 13, 2016, 11:42 a.m. UTC | #4
Am Dienstag, den 12.01.2016, 23:40 +0100 schrieb Daniel Vetter:
> On Tue, Jan 12, 2016 at 11:02 PM, Rob Herring <robh+dt@kernel.org> wrote:
> > On Tue, Jan 12, 2016 at 9:15 AM, Philipp Zabel <p.zabel@pengutronix.de> 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.
> >
> > But it should not a be render node unless it has a GPU AFAIK. Then
> > again, I still don't understand the madness of every driver defining
> > their own buffer ioctls either. The only line remotely h/w specific
> > here is mtk_drm_gem_create call.
> 
> Support gem_create/mmap_offet alone is indeed pointless without some
> real support for gpu workloads. For plain display drivers the dumb
> buffer api, plus prime/dma-buf import should be plenty enough.
> 
> The usual reason for doing this is some blob driver for opengl that
> can't be open-source, which is a big no-go for upstream.
> 
> Imo best to just rip this out, consider it nacked without full-blown
> userspace and whatever else is needing these buffers.
> -Daniel

Yes, I'll drop this one.

thanks
Philipp
diff mbox

Patch

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;
+
+	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;
+
+	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.
+	 */
+	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;
+};
+
+/**
+ * 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.
+ */
+struct drm_mtk_gem_map_off {
+	uint32_t handle;
+	uint32_t pad;
+	uint64_t offset;
+};
+
+#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 */