diff mbox series

[PATCHv3/RFC,1/4] drm/arm: Factor out generic afbc helpers

Message ID 20191121172247.31150-2-andrzej.p@collabora.com (mailing list archive)
State New, archived
Headers show
Series AFBC rework and support for Rockchip | expand

Commit Message

Andrzej Pietrasiewicz Nov. 21, 2019, 5:22 p.m. UTC
These are useful for other users of afbc, e.g. rockchip.

Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
---
 drivers/gpu/drm/Makefile          |  2 +-
 drivers/gpu/drm/drm_afbc.c        | 84 +++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_fourcc.c      | 11 +++-
 drivers/gpu/drm/drm_framebuffer.c | 71 +++++++++++++++++++++++++-
 include/drm/drm_afbc.h            | 35 +++++++++++++
 5 files changed, 199 insertions(+), 4 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_afbc.c
 create mode 100644 include/drm/drm_afbc.h

Comments

Ezequiel Garcia Nov. 23, 2019, 7:13 a.m. UTC | #1
Hi Andrzej,

Thanks for the series. It's certainly going well.

Please see some minor comments below.

On Thu, 2019-11-21 at 18:22 +0100, Andrzej Pietrasiewicz wrote:
> These are useful for other users of afbc, e.g. rockchip.
> 
> Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
> ---
>  drivers/gpu/drm/Makefile          |  2 +-
>  drivers/gpu/drm/drm_afbc.c        | 84 +++++++++++++++++++++++++++++++
>  drivers/gpu/drm/drm_fourcc.c      | 11 +++-
>  drivers/gpu/drm/drm_framebuffer.c | 71 +++++++++++++++++++++++++-
>  include/drm/drm_afbc.h            | 35 +++++++++++++
>  5 files changed, 199 insertions(+), 4 deletions(-)
>  create mode 100644 drivers/gpu/drm/drm_afbc.c
>  create mode 100644 include/drm/drm_afbc.h
> 
> diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
> index d9bcc9f2a0a4..3a58f30b83a6 100644
> --- a/drivers/gpu/drm/Makefile
> +++ b/drivers/gpu/drm/Makefile
> @@ -44,7 +44,7 @@ drm_kms_helper-y := drm_crtc_helper.o drm_dp_helper.o drm_dsc.o drm_probe_helper
>  		drm_simple_kms_helper.o drm_modeset_helper.o \
>  		drm_scdc_helper.o drm_gem_framebuffer_helper.o \
>  		drm_atomic_state_helper.o drm_damage_helper.o \
> -		drm_format_helper.o drm_self_refresh_helper.o
> +		drm_format_helper.o drm_self_refresh_helper.o drm_afbc.o
>  
>  drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
>  drm_kms_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fb_helper.o
> diff --git a/drivers/gpu/drm/drm_afbc.c b/drivers/gpu/drm/drm_afbc.c
> new file mode 100644
> index 000000000000..f308c4719546
> --- /dev/null
> +++ b/drivers/gpu/drm/drm_afbc.c
> @@ -0,0 +1,84 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * (C) 2019 Collabora Ltd.
> + *
> + * author: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
> + *
> + */
> +#include <linux/module.h>
> +
> +#include <drm/drm_afbc.h>
> +#include <drm/drm_device.h>
> +#include <drm/drm_fourcc.h>
> +#include <drm/drm_gem.h>
> +#include <drm/drm_mode.h>
> +#include <drm/drm_print.h>
> +
> +/**
> + * drm_afbc_get_superblk_wh - extract afbc block width/height from modifier
> + * @modifier: the modifier to be looked at
> + * @w: address of a place to store the block width
> + * @h: address of a place to store the block height
> + *
> + * Returns: true if the modifier describes a supported block size
> + */
> +bool drm_afbc_get_superblk_wh(u64 modifier, u32 *w, u32 *h)
> +{

Name nitpicking: drm_afbc_get_superblock_size. The coding-style
guide has a chapter on naming and suggests avoiding stuff like
"superblk".

> +	switch (modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
> +	case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
> +		*w = 16;
> +		*h = 16;
> +		break;
> +	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
> +		*w = 32;
> +		*h = 8;
> +		break;
> +	case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
> +		/* fall through */
> +	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
> +		/* fall through */
> +	default:
> +		DRM_DEBUG_KMS("Invalid AFBC_FORMAT_MOD_BLOCK_SIZE: %lld.\n",
> +			      modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK);
> +		return false;
> +	}
> +	return true;
> +}
> +EXPORT_SYMBOL_GPL(drm_afbc_get_superblk_wh);
> +
> +/**
> + * drm_afbc_get_parameters - extract afbc parameters from mode command
> + * @mode_cmd: mode command to be looked at
> + * @afbc: address of a struct to be filled in
> + */
> +void drm_afbc_get_parameters(const struct drm_mode_fb_cmd2 *mode_cmd,
> +			     struct drm_afbc *afbc)
> +{
> +	drm_afbc_get_superblk_wh(mode_cmd->modifier[0],
> +				 &afbc->tile_w, &afbc->tile_h);
> +	afbc->width = mode_cmd->pitches[0];
> +	afbc->height =
> +		DIV_ROUND_UP(mode_cmd->height, afbc->tile_h) * afbc->tile_h;
> +	afbc->offset = mode_cmd->offsets[0];
> +}
> +EXPORT_SYMBOL(drm_afbc_get_parameters);
> +

Not sure if this was discussed before, but why some
are EXPORT_SYMBOL and others are exported as _GPL?

> +/**
> + * drm_is_afbc - test if the modifier describes an afbc buffer
> + * @modifier - modifier to be tested
> + *
> + * Returns: true if the modifier describes an afbc buffer
> + */
> +bool drm_is_afbc(u64 modifier)
> +{
> +	/* is it ARM AFBC? */
> +	if ((modifier & DRM_FORMAT_MOD_ARM_AFBC(0)) == 0)
> +		return false;
> +
> +	/* Block size must be known */
> +	if ((modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) == 0)
> +		return false;
> +
> +	return true;
> +}
> +EXPORT_SYMBOL_GPL(drm_is_afbc);
> diff --git a/drivers/gpu/drm/drm_fourcc.c b/drivers/gpu/drm/drm_fourcc.c
> index c630064ccf41..8d9f197cc0ab 100644
> --- a/drivers/gpu/drm/drm_fourcc.c
> +++ b/drivers/gpu/drm/drm_fourcc.c
> @@ -27,6 +27,7 @@
>  #include <linux/export.h>
>  #include <linux/kernel.h>
>  
> +#include <drm/drm_afbc.h>
>  #include <drm/drm_device.h>
>  #include <drm/drm_fourcc.h>
>  
> @@ -322,8 +323,14 @@ drm_get_format_info(struct drm_device *dev,
>  {
>  	const struct drm_format_info *info = NULL;
>  
> -	if (dev->mode_config.funcs->get_format_info)
> -		info = dev->mode_config.funcs->get_format_info(mode_cmd);
> +	/* bypass driver callback if afbc */
> +	if (!drm_is_afbc(mode_cmd->modifier[0]))
> +		if (dev->mode_config.funcs->get_format_info) {
> +			const struct drm_mode_config_funcs *funcs;
> +
> +			funcs = dev->mode_config.funcs;
> +			info = funcs->get_format_info(mode_cmd);
> +		}
>  
>  	if (!info)
>  		info = drm_format_info(mode_cmd->pixel_format);
> diff --git a/drivers/gpu/drm/drm_framebuffer.c b/drivers/gpu/drm/drm_framebuffer.c
> index 57564318ceea..303eea624a02 100644
> --- a/drivers/gpu/drm/drm_framebuffer.c
> +++ b/drivers/gpu/drm/drm_framebuffer.c
> @@ -23,6 +23,7 @@
>  #include <linux/export.h>
>  #include <linux/uaccess.h>
>  
> +#include <drm/drm_afbc.h>
>  #include <drm/drm_atomic.h>
>  #include <drm/drm_atomic_uapi.h>
>  #include <drm/drm_auth.h>
> @@ -31,6 +32,7 @@
>  #include <drm/drm_file.h>
>  #include <drm/drm_fourcc.h>
>  #include <drm/drm_framebuffer.h>
> +#include <drm/drm_gem.h>
>  #include <drm/drm_print.h>
>  #include <drm/drm_util.h>
>  
> @@ -168,7 +170,69 @@ static int fb_plane_height(int height,
>  	return DIV_ROUND_UP(height, format->vsub);
>  }
>  
> +static int afbc_check(struct drm_file *file_priv,
> +		      const struct drm_mode_fb_cmd2 *r, int i,
> +		      const struct drm_format_info *info)
> +{
> +	struct drm_gem_object *obj;
> +	int bpp = info->cpp[0] * 8;
> +	int tiles;
> +	u32 w, h, height, tile_sz, afbc_size;
> +	int result = 0;
> +
> +	if (i) {
> +		DRM_DEBUG_KMS("AFBC supported only for plane 0\n");
> +
> +		return 1;

Any reason to return 1, instead of an errno, or a boolean?

> +	}
> +
> +	/* get tile w/h */
> +	if (!drm_afbc_get_superblk_wh(r->modifier[0], &w, &h))
> +		return 1;
> +
> +	/* pitch must be divisible by tile width */
> +	if (r->pitches[0] % w) {
> +		DRM_DEBUG_KMS("Invalid pitch for plane %d\n", i);
> +
> +		return 1;
> +	}
> +
> +	obj = drm_gem_object_lookup(file_priv, r->handles[0]);
> +	if (!obj) {
> +		DRM_DEBUG_KMS("Failed to lookup GEM object\n");
> +
> +		return 1;
> +	}
> +
> +	/* estimate height based on tile size and height from userspace */
> +	height = DIV_ROUND_UP(r->height, h) * h;
> +
> +	tiles = (r->pitches[0] / w) * (height / h);
> +	afbc_size = ALIGN(tiles * AFBC_HEADER_SIZE, AFBC_SUPERBLK_ALIGNMENT);
> +
> +	tile_sz = (bpp * w * h) / BITS_PER_BYTE;
> +	afbc_size += tiles * ALIGN(tile_sz, AFBC_SUPERBLK_ALIGNMENT);
> +
> +	result = obj->size < afbc_size;
> +
> +	drm_gem_object_put_unlocked(obj);
> +
> +	return result;
> +}
> +
> +static int modifier_check(struct drm_file *file_priv,
> +			  const struct drm_mode_fb_cmd2 *r, int i,
> +			  const struct drm_format_info *info)
> +{
> +	/* non-afbc format */
> +	if (!drm_is_afbc(r->modifier[i]))
> +		return 0;
> +
> +	return afbc_check(file_priv, r, i, info);
> +}
> +
>  static int framebuffer_check(struct drm_device *dev,
> +			     struct drm_file *file_priv,
>  			     const struct drm_mode_fb_cmd2 *r)
>  {
>  	const struct drm_format_info *info;
> @@ -204,6 +268,9 @@ static int framebuffer_check(struct drm_device *dev,
>  		unsigned int block_size = info->char_per_block[i];
>  		u64 min_pitch = drm_format_info_min_pitch(info, i, width);
>  
> +		if (drm_is_afbc(r->modifier[i]))
> +			block_size = 0;
> +
>  		if (!block_size && (r->modifier[i] == DRM_FORMAT_MOD_LINEAR)) {
>  			DRM_DEBUG_KMS("Format requires non-linear modifier for plane %d\n", i);
>  			return -EINVAL;
> @@ -253,6 +320,8 @@ static int framebuffer_check(struct drm_device *dev,
>  			break;
>  
>  		default:
> +			if (modifier_check(file_priv, r, i, info))
> +				return -EINVAL;
>  			break;
>  		}
>  	}
> @@ -317,7 +386,7 @@ drm_internal_framebuffer_create(struct drm_device *dev,
>  		return ERR_PTR(-EINVAL);
>  	}
>  
> -	ret = framebuffer_check(dev, r);
> +	ret = framebuffer_check(dev, file_priv, r);
>  	if (ret)
>  		return ERR_PTR(ret);
>  
> diff --git a/include/drm/drm_afbc.h b/include/drm/drm_afbc.h
> new file mode 100644
> index 000000000000..fb4d4549f77e
> --- /dev/null
> +++ b/include/drm/drm_afbc.h
> @@ -0,0 +1,35 @@
> +/* SPDX-License-Identifier: GPL-2.0+ */
> +/*
> + * (C) 2019 Collabora Ltd.
> + *
> + * author: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
> + *
> + */
> +#ifndef __DRM_AFBC_H__
> +#define __DRM_AFBC_H__
> +
> +#include <linux/types.h>
> +
> +struct drm_device;
> +struct drm_mode_fb_cmd2;
> +struct drm_gem_object;
> +
> +struct drm_afbc {
> +	u32 tile_w;
> +	u32 tile_h;

I'd go with tile_width and tile_height.

> +	u32 width;
> +	u32 height;
> +	u32 offset;
> +};
> +
> +#define AFBC_HEADER_SIZE		16
> +#define AFBC_SUPERBLK_ALIGNMENT		128
> +

Ditto, s/SUPERBLK/SUPERBLOCK.

> +bool drm_afbc_get_superblk_wh(u64 modifier, u32 *w, u32 *h);
> +
> +void drm_afbc_get_parameters(const struct drm_mode_fb_cmd2 *mode_cmd,
> +			     struct drm_afbc *afbc);
> +
> +bool drm_is_afbc(u64 modifier);
> +
> +#endif /* __DRM_AFBC_H__ */
> -- 
> 2.17.1
> 
> 

Thanks!
Eze
Daniel Vetter Nov. 25, 2019, 8:55 a.m. UTC | #2
On Thu, Nov 21, 2019 at 06:22:44PM +0100, Andrzej Pietrasiewicz wrote:
> These are useful for other users of afbc, e.g. rockchip.
> 
> Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
> ---
>  drivers/gpu/drm/Makefile          |  2 +-
>  drivers/gpu/drm/drm_afbc.c        | 84 +++++++++++++++++++++++++++++++
>  drivers/gpu/drm/drm_fourcc.c      | 11 +++-
>  drivers/gpu/drm/drm_framebuffer.c | 71 +++++++++++++++++++++++++-
>  include/drm/drm_afbc.h            | 35 +++++++++++++
>  5 files changed, 199 insertions(+), 4 deletions(-)
>  create mode 100644 drivers/gpu/drm/drm_afbc.c
>  create mode 100644 include/drm/drm_afbc.h
> 
> diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
> index d9bcc9f2a0a4..3a58f30b83a6 100644
> --- a/drivers/gpu/drm/Makefile
> +++ b/drivers/gpu/drm/Makefile
> @@ -44,7 +44,7 @@ drm_kms_helper-y := drm_crtc_helper.o drm_dp_helper.o drm_dsc.o drm_probe_helper
>  		drm_simple_kms_helper.o drm_modeset_helper.o \
>  		drm_scdc_helper.o drm_gem_framebuffer_helper.o \
>  		drm_atomic_state_helper.o drm_damage_helper.o \
> -		drm_format_helper.o drm_self_refresh_helper.o
> +		drm_format_helper.o drm_self_refresh_helper.o drm_afbc.o

Just a quick drive-by:
- you can't put this into helpers and call from core code. This should be
  core code. Also, I'd have just stuffed it into drm_format.c.

- If you want to keep your separate file, please include it in the doc
  template, next to the format handling functions.
>  
>  drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
>  drm_kms_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fb_helper.o
> diff --git a/drivers/gpu/drm/drm_afbc.c b/drivers/gpu/drm/drm_afbc.c
> new file mode 100644
> index 000000000000..f308c4719546
> --- /dev/null
> +++ b/drivers/gpu/drm/drm_afbc.c
> @@ -0,0 +1,84 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * (C) 2019 Collabora Ltd.
> + *
> + * author: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
> + *
> + */
> +#include <linux/module.h>
> +
> +#include <drm/drm_afbc.h>
> +#include <drm/drm_device.h>
> +#include <drm/drm_fourcc.h>
> +#include <drm/drm_gem.h>
> +#include <drm/drm_mode.h>
> +#include <drm/drm_print.h>
> +
> +/**
> + * drm_afbc_get_superblk_wh - extract afbc block width/height from modifier
> + * @modifier: the modifier to be looked at
> + * @w: address of a place to store the block width
> + * @h: address of a place to store the block height
> + *
> + * Returns: true if the modifier describes a supported block size
> + */
> +bool drm_afbc_get_superblk_wh(u64 modifier, u32 *w, u32 *h)
> +{
> +	switch (modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
> +	case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
> +		*w = 16;
> +		*h = 16;
> +		break;
> +	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
> +		*w = 32;
> +		*h = 8;
> +		break;
> +	case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
> +		/* fall through */
> +	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
> +		/* fall through */
> +	default:
> +		DRM_DEBUG_KMS("Invalid AFBC_FORMAT_MOD_BLOCK_SIZE: %lld.\n",
> +			      modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK);
> +		return false;
> +	}
> +	return true;
> +}
> +EXPORT_SYMBOL_GPL(drm_afbc_get_superblk_wh);
> +
> +/**
> + * drm_afbc_get_parameters - extract afbc parameters from mode command
> + * @mode_cmd: mode command to be looked at
> + * @afbc: address of a struct to be filled in
> + */
> +void drm_afbc_get_parameters(const struct drm_mode_fb_cmd2 *mode_cmd,
> +			     struct drm_afbc *afbc)
> +{
> +	drm_afbc_get_superblk_wh(mode_cmd->modifier[0],
> +				 &afbc->tile_w, &afbc->tile_h);
> +	afbc->width = mode_cmd->pitches[0];
> +	afbc->height =
> +		DIV_ROUND_UP(mode_cmd->height, afbc->tile_h) * afbc->tile_h;
> +	afbc->offset = mode_cmd->offsets[0];
> +}
> +EXPORT_SYMBOL(drm_afbc_get_parameters);
> +
> +/**
> + * drm_is_afbc - test if the modifier describes an afbc buffer
> + * @modifier - modifier to be tested
> + *
> + * Returns: true if the modifier describes an afbc buffer
> + */
> +bool drm_is_afbc(u64 modifier)
> +{
> +	/* is it ARM AFBC? */
> +	if ((modifier & DRM_FORMAT_MOD_ARM_AFBC(0)) == 0)
> +		return false;
> +
> +	/* Block size must be known */
> +	if ((modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) == 0)
> +		return false;
> +
> +	return true;
> +}
> +EXPORT_SYMBOL_GPL(drm_is_afbc);
> diff --git a/drivers/gpu/drm/drm_fourcc.c b/drivers/gpu/drm/drm_fourcc.c
> index c630064ccf41..8d9f197cc0ab 100644
> --- a/drivers/gpu/drm/drm_fourcc.c
> +++ b/drivers/gpu/drm/drm_fourcc.c
> @@ -27,6 +27,7 @@
>  #include <linux/export.h>
>  #include <linux/kernel.h>
>  
> +#include <drm/drm_afbc.h>
>  #include <drm/drm_device.h>
>  #include <drm/drm_fourcc.h>
>  
> @@ -322,8 +323,14 @@ drm_get_format_info(struct drm_device *dev,
>  {
>  	const struct drm_format_info *info = NULL;
>  
> -	if (dev->mode_config.funcs->get_format_info)
> -		info = dev->mode_config.funcs->get_format_info(mode_cmd);
> +	/* bypass driver callback if afbc */
> +	if (!drm_is_afbc(mode_cmd->modifier[0]))
> +		if (dev->mode_config.funcs->get_format_info) {
> +			const struct drm_mode_config_funcs *funcs;
> +
> +			funcs = dev->mode_config.funcs;
> +			info = funcs->get_format_info(mode_cmd);
> +		}
>  
>  	if (!info)
>  		info = drm_format_info(mode_cmd->pixel_format);
> diff --git a/drivers/gpu/drm/drm_framebuffer.c b/drivers/gpu/drm/drm_framebuffer.c
> index 57564318ceea..303eea624a02 100644
> --- a/drivers/gpu/drm/drm_framebuffer.c
> +++ b/drivers/gpu/drm/drm_framebuffer.c
> @@ -23,6 +23,7 @@
>  #include <linux/export.h>
>  #include <linux/uaccess.h>
>  
> +#include <drm/drm_afbc.h>
>  #include <drm/drm_atomic.h>
>  #include <drm/drm_atomic_uapi.h>
>  #include <drm/drm_auth.h>
> @@ -31,6 +32,7 @@
>  #include <drm/drm_file.h>
>  #include <drm/drm_fourcc.h>
>  #include <drm/drm_framebuffer.h>
> +#include <drm/drm_gem.h>
>  #include <drm/drm_print.h>
>  #include <drm/drm_util.h>
>  
> @@ -168,7 +170,69 @@ static int fb_plane_height(int height,
>  	return DIV_ROUND_UP(height, format->vsub);
>  }
>  
> +static int afbc_check(struct drm_file *file_priv,
> +		      const struct drm_mode_fb_cmd2 *r, int i,
> +		      const struct drm_format_info *info)
> +{
> +	struct drm_gem_object *obj;
> +	int bpp = info->cpp[0] * 8;
> +	int tiles;
> +	u32 w, h, height, tile_sz, afbc_size;
> +	int result = 0;
> +
> +	if (i) {
> +		DRM_DEBUG_KMS("AFBC supported only for plane 0\n");
> +
> +		return 1;
> +	}
> +
> +	/* get tile w/h */
> +	if (!drm_afbc_get_superblk_wh(r->modifier[0], &w, &h))
> +		return 1;
> +
> +	/* pitch must be divisible by tile width */
> +	if (r->pitches[0] % w) {
> +		DRM_DEBUG_KMS("Invalid pitch for plane %d\n", i);
> +
> +		return 1;
> +	}
> +
> +	obj = drm_gem_object_lookup(file_priv, r->handles[0]);

I think this is a bit ugly ... I'd split this into a new
framebuffer_check_post() function which is called after fb_create, at that
point you do have the objects already looked up.

Also I suggested that this could be done as a helper implementation for
fb_create, wrapping it around the default gem implementation. That way you
could keep all the afbc stuff in helpers entirely (but drivers could screw
things up, so there's a tradeoff).

What's definitely not ok is calling drm_gem_object_lookup unconditionally
from core code here. For consistency I think the helper approach would be
good, since currently the size related checks are done in
drm_gem_fb_create() - i.e. in the helpers, not in core. Otoh having checks
split like this is also ugly, so maybe we should have a
framebuffer_check_post for everyone, and move all the size checks into
core (not just for afbc).
-Daniel

> +	if (!obj) {
> +		DRM_DEBUG_KMS("Failed to lookup GEM object\n");
> +
> +		return 1;
> +	}
> +
> +	/* estimate height based on tile size and height from userspace */
> +	height = DIV_ROUND_UP(r->height, h) * h;
> +
> +	tiles = (r->pitches[0] / w) * (height / h);
> +	afbc_size = ALIGN(tiles * AFBC_HEADER_SIZE, AFBC_SUPERBLK_ALIGNMENT);
> +
> +	tile_sz = (bpp * w * h) / BITS_PER_BYTE;
> +	afbc_size += tiles * ALIGN(tile_sz, AFBC_SUPERBLK_ALIGNMENT);
> +
> +	result = obj->size < afbc_size;
> +
> +	drm_gem_object_put_unlocked(obj);
> +
> +	return result;
> +}
> +
> +static int modifier_check(struct drm_file *file_priv,
> +			  const struct drm_mode_fb_cmd2 *r, int i,
> +			  const struct drm_format_info *info)
> +{
> +	/* non-afbc format */
> +	if (!drm_is_afbc(r->modifier[i]))
> +		return 0;
> +
> +	return afbc_check(file_priv, r, i, info);
> +}
> +
>  static int framebuffer_check(struct drm_device *dev,
> +			     struct drm_file *file_priv,
>  			     const struct drm_mode_fb_cmd2 *r)
>  {
>  	const struct drm_format_info *info;
> @@ -204,6 +268,9 @@ static int framebuffer_check(struct drm_device *dev,
>  		unsigned int block_size = info->char_per_block[i];
>  		u64 min_pitch = drm_format_info_min_pitch(info, i, width);
>  
> +		if (drm_is_afbc(r->modifier[i]))
> +			block_size = 0;
> +
>  		if (!block_size && (r->modifier[i] == DRM_FORMAT_MOD_LINEAR)) {
>  			DRM_DEBUG_KMS("Format requires non-linear modifier for plane %d\n", i);
>  			return -EINVAL;
> @@ -253,6 +320,8 @@ static int framebuffer_check(struct drm_device *dev,
>  			break;
>  
>  		default:
> +			if (modifier_check(file_priv, r, i, info))
> +				return -EINVAL;
>  			break;
>  		}
>  	}
> @@ -317,7 +386,7 @@ drm_internal_framebuffer_create(struct drm_device *dev,
>  		return ERR_PTR(-EINVAL);
>  	}
>  
> -	ret = framebuffer_check(dev, r);
> +	ret = framebuffer_check(dev, file_priv, r);
>  	if (ret)
>  		return ERR_PTR(ret);
>  
> diff --git a/include/drm/drm_afbc.h b/include/drm/drm_afbc.h
> new file mode 100644
> index 000000000000..fb4d4549f77e
> --- /dev/null
> +++ b/include/drm/drm_afbc.h
> @@ -0,0 +1,35 @@
> +/* SPDX-License-Identifier: GPL-2.0+ */
> +/*
> + * (C) 2019 Collabora Ltd.
> + *
> + * author: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
> + *
> + */
> +#ifndef __DRM_AFBC_H__
> +#define __DRM_AFBC_H__
> +
> +#include <linux/types.h>
> +
> +struct drm_device;
> +struct drm_mode_fb_cmd2;
> +struct drm_gem_object;
> +
> +struct drm_afbc {
> +	u32 tile_w;
> +	u32 tile_h;
> +	u32 width;
> +	u32 height;
> +	u32 offset;
> +};
> +
> +#define AFBC_HEADER_SIZE		16
> +#define AFBC_SUPERBLK_ALIGNMENT		128
> +
> +bool drm_afbc_get_superblk_wh(u64 modifier, u32 *w, u32 *h);
> +
> +void drm_afbc_get_parameters(const struct drm_mode_fb_cmd2 *mode_cmd,
> +			     struct drm_afbc *afbc);
> +
> +bool drm_is_afbc(u64 modifier);
> +
> +#endif /* __DRM_AFBC_H__ */
> -- 
> 2.17.1
>
Andrzej Pietrasiewicz Nov. 26, 2019, 8:27 p.m. UTC | #3
Hi Daniel,

Thanks for the comments, please see inline

W dniu 25.11.2019 o 09:55, Daniel Vetter pisze:
> On Thu, Nov 21, 2019 at 06:22:44PM +0100, Andrzej Pietrasiewicz wrote:
>> These are useful for other users of afbc, e.g. rockchip.
>>
>> Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
>> ---
>>   drivers/gpu/drm/Makefile          |  2 +-
>>   drivers/gpu/drm/drm_afbc.c        | 84 +++++++++++++++++++++++++++++++
>>   drivers/gpu/drm/drm_fourcc.c      | 11 +++-
>>   drivers/gpu/drm/drm_framebuffer.c | 71 +++++++++++++++++++++++++-
>>   include/drm/drm_afbc.h            | 35 +++++++++++++
>>   5 files changed, 199 insertions(+), 4 deletions(-)
>>   create mode 100644 drivers/gpu/drm/drm_afbc.c
>>   create mode 100644 include/drm/drm_afbc.h
>>
>> diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
>> index d9bcc9f2a0a4..3a58f30b83a6 100644
>> --- a/drivers/gpu/drm/Makefile
>> +++ b/drivers/gpu/drm/Makefile
>> @@ -44,7 +44,7 @@ drm_kms_helper-y := drm_crtc_helper.o drm_dp_helper.o drm_dsc.o drm_probe_helper
>>   		drm_simple_kms_helper.o drm_modeset_helper.o \
>>   		drm_scdc_helper.o drm_gem_framebuffer_helper.o \
>>   		drm_atomic_state_helper.o drm_damage_helper.o \
>> -		drm_format_helper.o drm_self_refresh_helper.o
>> +		drm_format_helper.o drm_self_refresh_helper.o drm_afbc.o
> 
> Just a quick drive-by:
> - you can't put this into helpers and call from core code. This should be
>    core code. Also, I'd have just stuffed it into drm_format.c.
> 

drm_format.c does not exist. Did you mean drm_format_helper.c?

<snip>

>> diff --git a/drivers/gpu/drm/drm_framebuffer.c b/drivers/gpu/drm/drm_framebuffer.c
>> index 57564318ceea..303eea624a02 100644
>> --- a/drivers/gpu/drm/drm_framebuffer.c
>> +++ b/drivers/gpu/drm/drm_framebuffer.c
>> @@ -23,6 +23,7 @@
>>   #include <linux/export.h>
>>   #include <linux/uaccess.h>
>>   
>> +#include <drm/drm_afbc.h>
>>   #include <drm/drm_atomic.h>
>>   #include <drm/drm_atomic_uapi.h>
>>   #include <drm/drm_auth.h>
>> @@ -31,6 +32,7 @@
>>   #include <drm/drm_file.h>
>>   #include <drm/drm_fourcc.h>
>>   #include <drm/drm_framebuffer.h>
>> +#include <drm/drm_gem.h>
>>   #include <drm/drm_print.h>
>>   #include <drm/drm_util.h>
>>   
>> @@ -168,7 +170,69 @@ static int fb_plane_height(int height,
>>   	return DIV_ROUND_UP(height, format->vsub);
>>   }
>>   
>> +static int afbc_check(struct drm_file *file_priv,
>> +		      const struct drm_mode_fb_cmd2 *r, int i,
>> +		      const struct drm_format_info *info)
>> +{
>> +	struct drm_gem_object *obj;
>> +	int bpp = info->cpp[0] * 8;
>> +	int tiles;
>> +	u32 w, h, height, tile_sz, afbc_size;
>> +	int result = 0;
>> +
>> +	if (i) {
>> +		DRM_DEBUG_KMS("AFBC supported only for plane 0\n");
>> +
>> +		return 1;
>> +	}
>> +
>> +	/* get tile w/h */
>> +	if (!drm_afbc_get_superblk_wh(r->modifier[0], &w, &h))
>> +		return 1;
>> +
>> +	/* pitch must be divisible by tile width */
>> +	if (r->pitches[0] % w) {
>> +		DRM_DEBUG_KMS("Invalid pitch for plane %d\n", i);
>> +
>> +		return 1;
>> +	}
>> +
>> +	obj = drm_gem_object_lookup(file_priv, r->handles[0]);
> 
> I think this is a bit ugly ... I'd split this into a new
> framebuffer_check_post() function which is called after fb_create, at that
> point you do have the objects already looked up.
> 
> Also I suggested that this could be done as a helper implementation for
> fb_create, wrapping it around the default gem implementation. That way you
> could keep all the afbc stuff in helpers entirely (but drivers could screw
> things up, so there's a tradeoff).
> 
> What's definitely not ok is calling drm_gem_object_lookup unconditionally
> from core code here. For consistency I think the helper approach would be
> good, since currently the size related checks are done in
> drm_gem_fb_create() - i.e. in the helpers, not in core. Otoh having checks
> split like this is also ugly, so maybe we should have a
> framebuffer_check_post for everyone, and move all the size checks into
> core (not just for afbc).
> -Daniel

As far as I understand you see more than one way forward.
Can you please comment on the below? And in particular, can you say
if I understood you correctly? So, my understanding of what you
said above is to either:

1) move the part of the code which requires objects to be looked up
to a new framebuffer_check_post() called after fb_create, that is
from drm_internal_framebuffer_create() after

fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);

because at that point the objects have already been looked up.
How driver-specific checks can be done in this scheme?

or

2) Move the body of afbc_check() to a helper implementation to be used by
driver-specific fb_create implementations. Sorry for my ignorance, it seems
that "helpers" and "core" have precise meaning here but I don't quite
understand what is what of what, so I also don't quite understand where
to move the code to and why this would mean keeping all the afbc stuff
entirely in helpers :O Can you explain?

Did you mean that all the checking code is called by specific drivers
on an opt-in basis? I thought you were not in favor of drivers opting-in,
as some of them might opt-in while some others opt-out, or use different
subsets of available checks.

or

3) e.g. rockchip does not use drm_gem_fb_create(). It uses
drm_gem_fb_create_handle(), though. Anyway, the third way forward
is to have framebuffer_check_post() as in 1) and move the size checks
to "core" - which I don't quite understand where exactly it is.
What is the difference to 1)?

> 
>> +	if (!obj) {
>> +		DRM_DEBUG_KMS("Failed to lookup GEM object\n");
>> +
>> +		return 1;
>> +	}
>> +
>> +	/* estimate height based on tile size and height from userspace */
>> +	height = DIV_ROUND_UP(r->height, h) * h;
>> +
>> +	tiles = (r->pitches[0] / w) * (height / h);
>> +	afbc_size = ALIGN(tiles * AFBC_HEADER_SIZE, AFBC_SUPERBLK_ALIGNMENT);

This computation is for malidp/rockchip, but for komeda the alignment
is different. So it seems it is driver-specific. If so, we need
some way for specific drivers to provide their specific checks
and/or specific data for generic checking code to use.

Thanks

Andrzej
Daniel Vetter Nov. 27, 2019, 9:51 a.m. UTC | #4
On Tue, Nov 26, 2019 at 09:27:59PM +0100, Andrzej Pietrasiewicz wrote:
> Hi Daniel,
> 
> Thanks for the comments, please see inline
> 
> W dniu 25.11.2019 o 09:55, Daniel Vetter pisze:
> > On Thu, Nov 21, 2019 at 06:22:44PM +0100, Andrzej Pietrasiewicz wrote:
> > > These are useful for other users of afbc, e.g. rockchip.
> > > 
> > > Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
> > > ---
> > >   drivers/gpu/drm/Makefile          |  2 +-
> > >   drivers/gpu/drm/drm_afbc.c        | 84 +++++++++++++++++++++++++++++++
> > >   drivers/gpu/drm/drm_fourcc.c      | 11 +++-
> > >   drivers/gpu/drm/drm_framebuffer.c | 71 +++++++++++++++++++++++++-
> > >   include/drm/drm_afbc.h            | 35 +++++++++++++
> > >   5 files changed, 199 insertions(+), 4 deletions(-)
> > >   create mode 100644 drivers/gpu/drm/drm_afbc.c
> > >   create mode 100644 include/drm/drm_afbc.h
> > > 
> > > diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
> > > index d9bcc9f2a0a4..3a58f30b83a6 100644
> > > --- a/drivers/gpu/drm/Makefile
> > > +++ b/drivers/gpu/drm/Makefile
> > > @@ -44,7 +44,7 @@ drm_kms_helper-y := drm_crtc_helper.o drm_dp_helper.o drm_dsc.o drm_probe_helper
> > >   		drm_simple_kms_helper.o drm_modeset_helper.o \
> > >   		drm_scdc_helper.o drm_gem_framebuffer_helper.o \
> > >   		drm_atomic_state_helper.o drm_damage_helper.o \
> > > -		drm_format_helper.o drm_self_refresh_helper.o
> > > +		drm_format_helper.o drm_self_refresh_helper.o drm_afbc.o
> > 
> > Just a quick drive-by:
> > - you can't put this into helpers and call from core code. This should be
> >    core code. Also, I'd have just stuffed it into drm_format.c.
> > 
> 
> drm_format.c does not exist. Did you mean drm_format_helper.c?

drm_fourcc.c. You can't put this into a helper and call it from core code,
it wont link for modular builds.

> <snip>
> 
> > > diff --git a/drivers/gpu/drm/drm_framebuffer.c b/drivers/gpu/drm/drm_framebuffer.c
> > > index 57564318ceea..303eea624a02 100644
> > > --- a/drivers/gpu/drm/drm_framebuffer.c
> > > +++ b/drivers/gpu/drm/drm_framebuffer.c
> > > @@ -23,6 +23,7 @@
> > >   #include <linux/export.h>
> > >   #include <linux/uaccess.h>
> > > +#include <drm/drm_afbc.h>
> > >   #include <drm/drm_atomic.h>
> > >   #include <drm/drm_atomic_uapi.h>
> > >   #include <drm/drm_auth.h>
> > > @@ -31,6 +32,7 @@
> > >   #include <drm/drm_file.h>
> > >   #include <drm/drm_fourcc.h>
> > >   #include <drm/drm_framebuffer.h>
> > > +#include <drm/drm_gem.h>
> > >   #include <drm/drm_print.h>
> > >   #include <drm/drm_util.h>
> > > @@ -168,7 +170,69 @@ static int fb_plane_height(int height,
> > >   	return DIV_ROUND_UP(height, format->vsub);
> > >   }
> > > +static int afbc_check(struct drm_file *file_priv,
> > > +		      const struct drm_mode_fb_cmd2 *r, int i,
> > > +		      const struct drm_format_info *info)
> > > +{
> > > +	struct drm_gem_object *obj;
> > > +	int bpp = info->cpp[0] * 8;
> > > +	int tiles;
> > > +	u32 w, h, height, tile_sz, afbc_size;
> > > +	int result = 0;
> > > +
> > > +	if (i) {
> > > +		DRM_DEBUG_KMS("AFBC supported only for plane 0\n");
> > > +
> > > +		return 1;
> > > +	}
> > > +
> > > +	/* get tile w/h */
> > > +	if (!drm_afbc_get_superblk_wh(r->modifier[0], &w, &h))
> > > +		return 1;
> > > +
> > > +	/* pitch must be divisible by tile width */
> > > +	if (r->pitches[0] % w) {
> > > +		DRM_DEBUG_KMS("Invalid pitch for plane %d\n", i);
> > > +
> > > +		return 1;
> > > +	}
> > > +
> > > +	obj = drm_gem_object_lookup(file_priv, r->handles[0]);
> > 
> > I think this is a bit ugly ... I'd split this into a new
> > framebuffer_check_post() function which is called after fb_create, at that
> > point you do have the objects already looked up.
> > 
> > Also I suggested that this could be done as a helper implementation for
> > fb_create, wrapping it around the default gem implementation. That way you
> > could keep all the afbc stuff in helpers entirely (but drivers could screw
> > things up, so there's a tradeoff).
> > 
> > What's definitely not ok is calling drm_gem_object_lookup unconditionally
> > from core code here. For consistency I think the helper approach would be
> > good, since currently the size related checks are done in
> > drm_gem_fb_create() - i.e. in the helpers, not in core. Otoh having checks
> > split like this is also ugly, so maybe we should have a
> > framebuffer_check_post for everyone, and move all the size checks into
> > core (not just for afbc).
> > -Daniel
> 
> As far as I understand you see more than one way forward.
> Can you please comment on the below? And in particular, can you say
> if I understood you correctly? So, my understanding of what you
> said above is to either:
> 
> 1) move the part of the code which requires objects to be looked up
> to a new framebuffer_check_post() called after fb_create, that is
> from drm_internal_framebuffer_create() after
> 
> fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
> 
> because at that point the objects have already been looked up.
> How driver-specific checks can be done in this scheme?

Yup that's one way of doing it. Still feels a bit icky.

> or
> 
> 2) Move the body of afbc_check() to a helper implementation to be used by
> driver-specific fb_create implementations. Sorry for my ignorance, it seems
> that "helpers" and "core" have precise meaning here but I don't quite
> understand what is what of what, so I also don't quite understand where
> to move the code to and why this would mean keeping all the afbc stuff
> entirely in helpers :O Can you explain?

That's the 2nd option, where 2a would be putting the checks into
drm_gem_fb_create_with_funcs(). That would at least be consistent with the
existing placement of size related checks we have.

For more context on core vs midlayer:

https://blog.ffwll.ch/2016/12/midlayers-once-more-with-feeling.html

The first few paragraphs have a pile of links with what this means for
kernel hackers.

In this case specifically the choice of using gem as backing storage for
drm_framebuffer is up to drivers, and hence anything gem related should be
in opt-in helpers. Although we only have 1 driver not using gem, so we
standardized this a lot and somewhat muddied the core/helper separation
here. Still, we should at least try to be consistent across the different
formats and checks.

> Did you mean that all the checking code is called by specific drivers
> on an opt-in basis? I thought you were not in favor of drivers opting-in,
> as some of them might opt-in while some others opt-out, or use different
> subsets of available checks.
> 
> or
> 
> 3) e.g. rockchip does not use drm_gem_fb_create(). It uses
> drm_gem_fb_create_handle(), though. Anyway, the third way forward
> is to have framebuffer_check_post() as in 1) and move the size checks
> to "core" - which I don't quite understand where exactly it is.
> What is the difference to 1)?

create_handle is a different ioctl, it's called from the GETFB ioctl. We'd
need to move the fb_create hook over to helpers for rockchip, and you're
lucky, I have a patch for you:

"drm/rockchip: Use drm_gem_fb_create_with_dirty"

Can you pls review/test that one?

Thanks, Daniel

> 
> > 
> > > +	if (!obj) {
> > > +		DRM_DEBUG_KMS("Failed to lookup GEM object\n");
> > > +
> > > +		return 1;
> > > +	}
> > > +
> > > +	/* estimate height based on tile size and height from userspace */
> > > +	height = DIV_ROUND_UP(r->height, h) * h;
> > > +
> > > +	tiles = (r->pitches[0] / w) * (height / h);
> > > +	afbc_size = ALIGN(tiles * AFBC_HEADER_SIZE, AFBC_SUPERBLK_ALIGNMENT);
> 
> This computation is for malidp/rockchip, but for komeda the alignment
> is different. So it seems it is driver-specific. If so, we need
> some way for specific drivers to provide their specific checks
> and/or specific data for generic checking code to use.
> 
> Thanks
> 
> Andrzej
James Qian Wang Nov. 28, 2019, 10:47 a.m. UTC | #5
On Mon, Nov 25, 2019 at 09:55:06AM +0100, Daniel Vetter wrote:
> On Thu, Nov 21, 2019 at 06:22:44PM +0100, Andrzej Pietrasiewicz wrote:
> > These are useful for other users of afbc, e.g. rockchip.
> > 
> > Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
> > ---
> >  drivers/gpu/drm/Makefile          |  2 +-
> >  drivers/gpu/drm/drm_afbc.c        | 84 +++++++++++++++++++++++++++++++
> >  drivers/gpu/drm/drm_fourcc.c      | 11 +++-
> >  drivers/gpu/drm/drm_framebuffer.c | 71 +++++++++++++++++++++++++-
> >  include/drm/drm_afbc.h            | 35 +++++++++++++
> >  5 files changed, 199 insertions(+), 4 deletions(-)
> >  create mode 100644 drivers/gpu/drm/drm_afbc.c
> >  create mode 100644 include/drm/drm_afbc.h
> > 
> > diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
> > index d9bcc9f2a0a4..3a58f30b83a6 100644
> > --- a/drivers/gpu/drm/Makefile
> > +++ b/drivers/gpu/drm/Makefile
> > @@ -44,7 +44,7 @@ drm_kms_helper-y := drm_crtc_helper.o drm_dp_helper.o drm_dsc.o drm_probe_helper
> >  		drm_simple_kms_helper.o drm_modeset_helper.o \
> >  		drm_scdc_helper.o drm_gem_framebuffer_helper.o \
> >  		drm_atomic_state_helper.o drm_damage_helper.o \
> > -		drm_format_helper.o drm_self_refresh_helper.o
> > +		drm_format_helper.o drm_self_refresh_helper.o drm_afbc.o
> 
> Just a quick drive-by:
> - you can't put this into helpers and call from core code. This should be
>   core code. Also, I'd have just stuffed it into drm_format.c.
> 
> - If you want to keep your separate file, please include it in the doc
>   template, next to the format handling functions.
> >  
> >  drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
> >  drm_kms_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fb_helper.o
> > diff --git a/drivers/gpu/drm/drm_afbc.c b/drivers/gpu/drm/drm_afbc.c
> > new file mode 100644
> > index 000000000000..f308c4719546
> > --- /dev/null
> > +++ b/drivers/gpu/drm/drm_afbc.c
> > @@ -0,0 +1,84 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * (C) 2019 Collabora Ltd.
> > + *
> > + * author: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
> > + *
> > + */
> > +#include <linux/module.h>
> > +
> > +#include <drm/drm_afbc.h>
> > +#include <drm/drm_device.h>
> > +#include <drm/drm_fourcc.h>
> > +#include <drm/drm_gem.h>
> > +#include <drm/drm_mode.h>
> > +#include <drm/drm_print.h>
> > +
> > +/**
> > + * drm_afbc_get_superblk_wh - extract afbc block width/height from modifier
> > + * @modifier: the modifier to be looked at
> > + * @w: address of a place to store the block width
> > + * @h: address of a place to store the block height
> > + *
> > + * Returns: true if the modifier describes a supported block size
> > + */
> > +bool drm_afbc_get_superblk_wh(u64 modifier, u32 *w, u32 *h)
> > +{
> > +	switch (modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
> > +	case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
> > +		*w = 16;
> > +		*h = 16;
> > +		break;
> > +	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
> > +		*w = 32;
> > +		*h = 8;
> > +		break;
> > +	case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
> > +		/* fall through */
> > +	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
> > +		/* fall through */
> > +	default:
> > +		DRM_DEBUG_KMS("Invalid AFBC_FORMAT_MOD_BLOCK_SIZE: %lld.\n",
> > +			      modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK);
> > +		return false;
> > +	}
> > +	return true;
> > +}
> > +EXPORT_SYMBOL_GPL(drm_afbc_get_superblk_wh);
> > +
> > +/**
> > + * drm_afbc_get_parameters - extract afbc parameters from mode command
> > + * @mode_cmd: mode command to be looked at
> > + * @afbc: address of a struct to be filled in
> > + */
> > +void drm_afbc_get_parameters(const struct drm_mode_fb_cmd2 *mode_cmd,
> > +			     struct drm_afbc *afbc)
> > +{
> > +	drm_afbc_get_superblk_wh(mode_cmd->modifier[0],
> > +				 &afbc->tile_w, &afbc->tile_h);
> > +	afbc->width = mode_cmd->pitches[0];
> > +	afbc->height =
> > +		DIV_ROUND_UP(mode_cmd->height, afbc->tile_h) * afbc->tile_h;
> > +	afbc->offset = mode_cmd->offsets[0];
> > +}
> > +EXPORT_SYMBOL(drm_afbc_get_parameters);
> > +
> > +/**
> > + * drm_is_afbc - test if the modifier describes an afbc buffer
> > + * @modifier - modifier to be tested
> > + *
> > + * Returns: true if the modifier describes an afbc buffer
> > + */
> > +bool drm_is_afbc(u64 modifier)
> > +{
> > +	/* is it ARM AFBC? */
> > +	if ((modifier & DRM_FORMAT_MOD_ARM_AFBC(0)) == 0)
> > +		return false;
> > +
> > +	/* Block size must be known */
> > +	if ((modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) == 0)
> > +		return false;
> > +
> > +	return true;
> > +}
> > +EXPORT_SYMBOL_GPL(drm_is_afbc);
> > diff --git a/drivers/gpu/drm/drm_fourcc.c b/drivers/gpu/drm/drm_fourcc.c
> > index c630064ccf41..8d9f197cc0ab 100644
> > --- a/drivers/gpu/drm/drm_fourcc.c
> > +++ b/drivers/gpu/drm/drm_fourcc.c
> > @@ -27,6 +27,7 @@
> >  #include <linux/export.h>
> >  #include <linux/kernel.h>
> >  
> > +#include <drm/drm_afbc.h>
> >  #include <drm/drm_device.h>
> >  #include <drm/drm_fourcc.h>
> >  
> > @@ -322,8 +323,14 @@ drm_get_format_info(struct drm_device *dev,
> >  {
> >  	const struct drm_format_info *info = NULL;
> >  
> > -	if (dev->mode_config.funcs->get_format_info)
> > -		info = dev->mode_config.funcs->get_format_info(mode_cmd);
> > +	/* bypass driver callback if afbc */
> > +	if (!drm_is_afbc(mode_cmd->modifier[0]))
> > +		if (dev->mode_config.funcs->get_format_info) {
> > +			const struct drm_mode_config_funcs *funcs;
> > +
> > +			funcs = dev->mode_config.funcs;
> > +			info = funcs->get_format_info(mode_cmd);
> > +		}
> >  
> >  	if (!info)
> >  		info = drm_format_info(mode_cmd->pixel_format);
> > diff --git a/drivers/gpu/drm/drm_framebuffer.c b/drivers/gpu/drm/drm_framebuffer.c
> > index 57564318ceea..303eea624a02 100644
> > --- a/drivers/gpu/drm/drm_framebuffer.c
> > +++ b/drivers/gpu/drm/drm_framebuffer.c
> > @@ -23,6 +23,7 @@
> >  #include <linux/export.h>
> >  #include <linux/uaccess.h>
> >  
> > +#include <drm/drm_afbc.h>
> >  #include <drm/drm_atomic.h>
> >  #include <drm/drm_atomic_uapi.h>
> >  #include <drm/drm_auth.h>
> > @@ -31,6 +32,7 @@
> >  #include <drm/drm_file.h>
> >  #include <drm/drm_fourcc.h>
> >  #include <drm/drm_framebuffer.h>
> > +#include <drm/drm_gem.h>
> >  #include <drm/drm_print.h>
> >  #include <drm/drm_util.h>
> >  
> > @@ -168,7 +170,69 @@ static int fb_plane_height(int height,
> >  	return DIV_ROUND_UP(height, format->vsub);
> >  }
> >  
> > +static int afbc_check(struct drm_file *file_priv,
> > +		      const struct drm_mode_fb_cmd2 *r, int i,
> > +		      const struct drm_format_info *info)
> > +{
> > +	struct drm_gem_object *obj;
> > +	int bpp = info->cpp[0] * 8;
> > +	int tiles;
> > +	u32 w, h, height, tile_sz, afbc_size;
> > +	int result = 0;
> > +
> > +	if (i) {
> > +		DRM_DEBUG_KMS("AFBC supported only for plane 0\n");
> > +
> > +		return 1;
> > +	}
> > +
> > +	/* get tile w/h */
> > +	if (!drm_afbc_get_superblk_wh(r->modifier[0], &w, &h))
> > +		return 1;
> > +
> > +	/* pitch must be divisible by tile width */
> > +	if (r->pitches[0] % w) {
> > +		DRM_DEBUG_KMS("Invalid pitch for plane %d\n", i);
> > +
> > +		return 1;
> > +	}
> > +
> > +	obj = drm_gem_object_lookup(file_priv, r->handles[0]);
> 
> I think this is a bit ugly ... I'd split this into a new
> framebuffer_check_post() function which is called after fb_create, at that
> point you do have the objects already looked up.
> 
> Also I suggested that this could be done as a helper implementation for
> fb_create, wrapping it around the default gem implementation. That way you
> could keep all the afbc stuff in helpers entirely (but drivers could screw
> things up, so there's a tradeoff).
> 
> What's definitely not ok is calling drm_gem_object_lookup unconditionally
> from core code here. For consistency I think the helper approach would be
> good, since currently the size related checks are done in
> drm_gem_fb_create() - i.e. in the helpers, not in core. Otoh having checks
> split like this is also ugly, so maybe we should have a
> framebuffer_check_post for everyone, and move all the size checks into
> core (not just for afbc).
> -Daniel
> 
> > +	if (!obj) {
> > +		DRM_DEBUG_KMS("Failed to lookup GEM object\n");
> > +
> > +		return 1;
> > +	}
> > +
> > +	/* estimate height based on tile size and height from userspace */
> > +	height = DIV_ROUND_UP(r->height, h) * h;
> > +
> > +	tiles = (r->pitches[0] / w) * (height / h);
> > +	afbc_size = ALIGN(tiles * AFBC_HEADER_SIZE, AFBC_SUPERBLK_ALIGNMENT);
> > +
> > +	tile_sz = (bpp * w * h) / BITS_PER_BYTE;
> > +	afbc_size += tiles * ALIGN(tile_sz, AFBC_SUPERBLK_ALIGNMENT);
> > +
> > +	result = obj->size < afbc_size;
> > +
> > +	drm_gem_object_put_unlocked(obj);
> > +
> > +	return result;
> > +}
> > +
> > +static int modifier_check(struct drm_file *file_priv,
> > +			  const struct drm_mode_fb_cmd2 *r, int i,
> > +			  const struct drm_format_info *info)
> > +{
> > +	/* non-afbc format */
> > +	if (!drm_is_afbc(r->modifier[i]))
> > +		return 0;
> > +
> > +	return afbc_check(file_priv, r, i, info);
> > +}
> > +
> >  static int framebuffer_check(struct drm_device *dev,
> > +			     struct drm_file *file_priv,
> >  			     const struct drm_mode_fb_cmd2 *r)
> >  {
> >  	const struct drm_format_info *info;
> > @@ -204,6 +268,9 @@ static int framebuffer_check(struct drm_device *dev,
> >  		unsigned int block_size = info->char_per_block[i];
> >  		u64 min_pitch = drm_format_info_min_pitch(info, i, width);
> >  
> > +		if (drm_is_afbc(r->modifier[i]))
> > +			block_size = 0;
> > +
> >  		if (!block_size && (r->modifier[i] == DRM_FORMAT_MOD_LINEAR)) {
> >  			DRM_DEBUG_KMS("Format requires non-linear modifier for plane %d\n", i);
> >  			return -EINVAL;
> > @@ -253,6 +320,8 @@ static int framebuffer_check(struct drm_device *dev,
> >  			break;
> >  
> >  		default:
> > +			if (modifier_check(file_priv, r, i, info))
> > +				return -EINVAL;
> >  			break;
> >  		}
> >  	}
> > @@ -317,7 +386,7 @@ drm_internal_framebuffer_create(struct drm_device *dev,
> >  		return ERR_PTR(-EINVAL);
> >  	}
> >  
> > -	ret = framebuffer_check(dev, r);
> > +	ret = framebuffer_check(dev, file_priv, r);
> >  	if (ret)
> >  		return ERR_PTR(ret);
> >  
> > diff --git a/include/drm/drm_afbc.h b/include/drm/drm_afbc.h
> > new file mode 100644
> > index 000000000000..fb4d4549f77e
> > --- /dev/null
> > +++ b/include/drm/drm_afbc.h
> > @@ -0,0 +1,35 @@
> > +/* SPDX-License-Identifier: GPL-2.0+ */
> > +/*
> > + * (C) 2019 Collabora Ltd.
> > + *
> > + * author: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
> > + *
> > + */
> > +#ifndef __DRM_AFBC_H__
> > +#define __DRM_AFBC_H__
> > +
> > +#include <linux/types.h>
> > +
> > +struct drm_device;
> > +struct drm_mode_fb_cmd2;
> > +struct drm_gem_object;
> > +
> > +struct drm_afbc {
> > +	u32 tile_w;
> > +	u32 tile_h;
> > +	u32 width;
> > +	u32 height;
> > +	u32 offset;
> > +};
> > +

Can we put a afbc ptr into drm_framebuffer, as the afbc extension if
the fb is afbc.
Since mostly this drm_afbc is enough for afbc support, but no need to
expose check func to driver, since per design the afbc modifier bits represent
all the requrement, the check is like a sanity check between the modifier and
fb info.

And we can also extend such afbc_info to all modifier which need an
extra check or extra info. like.

struct drm_framebuffer {
    ...
    union {    
        struct drm_afbc *afbc_info;
        void *mod_info; //extra modifier extension.  
    }
    ...
}

and such modifier info can be returned modifier_check()

drm_gem_fb_create_with_funcs()
{
    void *mod_info;

    if (!modifier)
        none_modifier_size_check();
    else if (afbc)
        mod_info = drm_afbc_size_check();
    else if (mode_config->fb_modifier_check())
        // for vendor specific modifier check
        mod_info = mode_config->fb_modifier_check();

    fb = drm_gem_fb_alloc();

    fb->mod_info = mod_info;
}

And in specific driver, you can directly convert the fb->mod_info to
your own modifier info. like current modifier is afbc, then

   struct drm_afbc *afbc_info = fb->mod_info;

Thanks
James
> > +#define AFBC_HEADER_SIZE		16
> > +#define AFBC_SUPERBLK_ALIGNMENT		128
> > +
> > +bool drm_afbc_get_superblk_wh(u64 modifier, u32 *w, u32 *h);
> > +
> > +void drm_afbc_get_parameters(const struct drm_mode_fb_cmd2 *mode_cmd,
> > +			     struct drm_afbc *afbc);
> > +
> > +bool drm_is_afbc(u64 modifier);
> > +
> > +#endif /* __DRM_AFBC_H__ */
> > -- 
> > 2.17.1
> > 
> 
> -- 
> Daniel Vetter
> Software Engineer, Intel Corporation
> http://blog.ffwll.ch
diff mbox series

Patch

diff --git a/drivers/gpu/drm/Makefile b/drivers/gpu/drm/Makefile
index d9bcc9f2a0a4..3a58f30b83a6 100644
--- a/drivers/gpu/drm/Makefile
+++ b/drivers/gpu/drm/Makefile
@@ -44,7 +44,7 @@  drm_kms_helper-y := drm_crtc_helper.o drm_dp_helper.o drm_dsc.o drm_probe_helper
 		drm_simple_kms_helper.o drm_modeset_helper.o \
 		drm_scdc_helper.o drm_gem_framebuffer_helper.o \
 		drm_atomic_state_helper.o drm_damage_helper.o \
-		drm_format_helper.o drm_self_refresh_helper.o
+		drm_format_helper.o drm_self_refresh_helper.o drm_afbc.o
 
 drm_kms_helper-$(CONFIG_DRM_PANEL_BRIDGE) += bridge/panel.o
 drm_kms_helper-$(CONFIG_DRM_FBDEV_EMULATION) += drm_fb_helper.o
diff --git a/drivers/gpu/drm/drm_afbc.c b/drivers/gpu/drm/drm_afbc.c
new file mode 100644
index 000000000000..f308c4719546
--- /dev/null
+++ b/drivers/gpu/drm/drm_afbc.c
@@ -0,0 +1,84 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * (C) 2019 Collabora Ltd.
+ *
+ * author: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
+ *
+ */
+#include <linux/module.h>
+
+#include <drm/drm_afbc.h>
+#include <drm/drm_device.h>
+#include <drm/drm_fourcc.h>
+#include <drm/drm_gem.h>
+#include <drm/drm_mode.h>
+#include <drm/drm_print.h>
+
+/**
+ * drm_afbc_get_superblk_wh - extract afbc block width/height from modifier
+ * @modifier: the modifier to be looked at
+ * @w: address of a place to store the block width
+ * @h: address of a place to store the block height
+ *
+ * Returns: true if the modifier describes a supported block size
+ */
+bool drm_afbc_get_superblk_wh(u64 modifier, u32 *w, u32 *h)
+{
+	switch (modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) {
+	case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
+		*w = 16;
+		*h = 16;
+		break;
+	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
+		*w = 32;
+		*h = 8;
+		break;
+	case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
+		/* fall through */
+	case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
+		/* fall through */
+	default:
+		DRM_DEBUG_KMS("Invalid AFBC_FORMAT_MOD_BLOCK_SIZE: %lld.\n",
+			      modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK);
+		return false;
+	}
+	return true;
+}
+EXPORT_SYMBOL_GPL(drm_afbc_get_superblk_wh);
+
+/**
+ * drm_afbc_get_parameters - extract afbc parameters from mode command
+ * @mode_cmd: mode command to be looked at
+ * @afbc: address of a struct to be filled in
+ */
+void drm_afbc_get_parameters(const struct drm_mode_fb_cmd2 *mode_cmd,
+			     struct drm_afbc *afbc)
+{
+	drm_afbc_get_superblk_wh(mode_cmd->modifier[0],
+				 &afbc->tile_w, &afbc->tile_h);
+	afbc->width = mode_cmd->pitches[0];
+	afbc->height =
+		DIV_ROUND_UP(mode_cmd->height, afbc->tile_h) * afbc->tile_h;
+	afbc->offset = mode_cmd->offsets[0];
+}
+EXPORT_SYMBOL(drm_afbc_get_parameters);
+
+/**
+ * drm_is_afbc - test if the modifier describes an afbc buffer
+ * @modifier - modifier to be tested
+ *
+ * Returns: true if the modifier describes an afbc buffer
+ */
+bool drm_is_afbc(u64 modifier)
+{
+	/* is it ARM AFBC? */
+	if ((modifier & DRM_FORMAT_MOD_ARM_AFBC(0)) == 0)
+		return false;
+
+	/* Block size must be known */
+	if ((modifier & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK) == 0)
+		return false;
+
+	return true;
+}
+EXPORT_SYMBOL_GPL(drm_is_afbc);
diff --git a/drivers/gpu/drm/drm_fourcc.c b/drivers/gpu/drm/drm_fourcc.c
index c630064ccf41..8d9f197cc0ab 100644
--- a/drivers/gpu/drm/drm_fourcc.c
+++ b/drivers/gpu/drm/drm_fourcc.c
@@ -27,6 +27,7 @@ 
 #include <linux/export.h>
 #include <linux/kernel.h>
 
+#include <drm/drm_afbc.h>
 #include <drm/drm_device.h>
 #include <drm/drm_fourcc.h>
 
@@ -322,8 +323,14 @@  drm_get_format_info(struct drm_device *dev,
 {
 	const struct drm_format_info *info = NULL;
 
-	if (dev->mode_config.funcs->get_format_info)
-		info = dev->mode_config.funcs->get_format_info(mode_cmd);
+	/* bypass driver callback if afbc */
+	if (!drm_is_afbc(mode_cmd->modifier[0]))
+		if (dev->mode_config.funcs->get_format_info) {
+			const struct drm_mode_config_funcs *funcs;
+
+			funcs = dev->mode_config.funcs;
+			info = funcs->get_format_info(mode_cmd);
+		}
 
 	if (!info)
 		info = drm_format_info(mode_cmd->pixel_format);
diff --git a/drivers/gpu/drm/drm_framebuffer.c b/drivers/gpu/drm/drm_framebuffer.c
index 57564318ceea..303eea624a02 100644
--- a/drivers/gpu/drm/drm_framebuffer.c
+++ b/drivers/gpu/drm/drm_framebuffer.c
@@ -23,6 +23,7 @@ 
 #include <linux/export.h>
 #include <linux/uaccess.h>
 
+#include <drm/drm_afbc.h>
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_uapi.h>
 #include <drm/drm_auth.h>
@@ -31,6 +32,7 @@ 
 #include <drm/drm_file.h>
 #include <drm/drm_fourcc.h>
 #include <drm/drm_framebuffer.h>
+#include <drm/drm_gem.h>
 #include <drm/drm_print.h>
 #include <drm/drm_util.h>
 
@@ -168,7 +170,69 @@  static int fb_plane_height(int height,
 	return DIV_ROUND_UP(height, format->vsub);
 }
 
+static int afbc_check(struct drm_file *file_priv,
+		      const struct drm_mode_fb_cmd2 *r, int i,
+		      const struct drm_format_info *info)
+{
+	struct drm_gem_object *obj;
+	int bpp = info->cpp[0] * 8;
+	int tiles;
+	u32 w, h, height, tile_sz, afbc_size;
+	int result = 0;
+
+	if (i) {
+		DRM_DEBUG_KMS("AFBC supported only for plane 0\n");
+
+		return 1;
+	}
+
+	/* get tile w/h */
+	if (!drm_afbc_get_superblk_wh(r->modifier[0], &w, &h))
+		return 1;
+
+	/* pitch must be divisible by tile width */
+	if (r->pitches[0] % w) {
+		DRM_DEBUG_KMS("Invalid pitch for plane %d\n", i);
+
+		return 1;
+	}
+
+	obj = drm_gem_object_lookup(file_priv, r->handles[0]);
+	if (!obj) {
+		DRM_DEBUG_KMS("Failed to lookup GEM object\n");
+
+		return 1;
+	}
+
+	/* estimate height based on tile size and height from userspace */
+	height = DIV_ROUND_UP(r->height, h) * h;
+
+	tiles = (r->pitches[0] / w) * (height / h);
+	afbc_size = ALIGN(tiles * AFBC_HEADER_SIZE, AFBC_SUPERBLK_ALIGNMENT);
+
+	tile_sz = (bpp * w * h) / BITS_PER_BYTE;
+	afbc_size += tiles * ALIGN(tile_sz, AFBC_SUPERBLK_ALIGNMENT);
+
+	result = obj->size < afbc_size;
+
+	drm_gem_object_put_unlocked(obj);
+
+	return result;
+}
+
+static int modifier_check(struct drm_file *file_priv,
+			  const struct drm_mode_fb_cmd2 *r, int i,
+			  const struct drm_format_info *info)
+{
+	/* non-afbc format */
+	if (!drm_is_afbc(r->modifier[i]))
+		return 0;
+
+	return afbc_check(file_priv, r, i, info);
+}
+
 static int framebuffer_check(struct drm_device *dev,
+			     struct drm_file *file_priv,
 			     const struct drm_mode_fb_cmd2 *r)
 {
 	const struct drm_format_info *info;
@@ -204,6 +268,9 @@  static int framebuffer_check(struct drm_device *dev,
 		unsigned int block_size = info->char_per_block[i];
 		u64 min_pitch = drm_format_info_min_pitch(info, i, width);
 
+		if (drm_is_afbc(r->modifier[i]))
+			block_size = 0;
+
 		if (!block_size && (r->modifier[i] == DRM_FORMAT_MOD_LINEAR)) {
 			DRM_DEBUG_KMS("Format requires non-linear modifier for plane %d\n", i);
 			return -EINVAL;
@@ -253,6 +320,8 @@  static int framebuffer_check(struct drm_device *dev,
 			break;
 
 		default:
+			if (modifier_check(file_priv, r, i, info))
+				return -EINVAL;
 			break;
 		}
 	}
@@ -317,7 +386,7 @@  drm_internal_framebuffer_create(struct drm_device *dev,
 		return ERR_PTR(-EINVAL);
 	}
 
-	ret = framebuffer_check(dev, r);
+	ret = framebuffer_check(dev, file_priv, r);
 	if (ret)
 		return ERR_PTR(ret);
 
diff --git a/include/drm/drm_afbc.h b/include/drm/drm_afbc.h
new file mode 100644
index 000000000000..fb4d4549f77e
--- /dev/null
+++ b/include/drm/drm_afbc.h
@@ -0,0 +1,35 @@ 
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * (C) 2019 Collabora Ltd.
+ *
+ * author: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
+ *
+ */
+#ifndef __DRM_AFBC_H__
+#define __DRM_AFBC_H__
+
+#include <linux/types.h>
+
+struct drm_device;
+struct drm_mode_fb_cmd2;
+struct drm_gem_object;
+
+struct drm_afbc {
+	u32 tile_w;
+	u32 tile_h;
+	u32 width;
+	u32 height;
+	u32 offset;
+};
+
+#define AFBC_HEADER_SIZE		16
+#define AFBC_SUPERBLK_ALIGNMENT		128
+
+bool drm_afbc_get_superblk_wh(u64 modifier, u32 *w, u32 *h);
+
+void drm_afbc_get_parameters(const struct drm_mode_fb_cmd2 *mode_cmd,
+			     struct drm_afbc *afbc);
+
+bool drm_is_afbc(u64 modifier);
+
+#endif /* __DRM_AFBC_H__ */