[RFC PATCH 05/17] drm/vkms: Use managed memory to create encoders

Louis Chauvet louis.chauvet at bootlin.com
Tue Aug 13 17:58:57 UTC 2024


Le 13/08/24 - 12:44, José Expósito a écrit :
> A future patch will allow to create multiple encoders. Use managed
> memory to simplify the code.
> 
> Refactor, no functional changes.
> 
> Signed-off-by: José Expósito <jose.exposito89 at gmail.com>
> ---
>  drivers/gpu/drm/vkms/vkms_drv.h    |  1 -
>  drivers/gpu/drm/vkms/vkms_output.c | 45 ++++++++++++++++++++----------
>  2 files changed, 30 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/gpu/drm/vkms/vkms_drv.h b/drivers/gpu/drm/vkms/vkms_drv.h
> index 3156ff896c33..2466e8b0231f 100644
> --- a/drivers/gpu/drm/vkms/vkms_drv.h
> +++ b/drivers/gpu/drm/vkms/vkms_drv.h
> @@ -122,7 +122,6 @@ struct vkms_crtc {
>  struct vkms_config;
>  
>  struct vkms_output {
> -	struct drm_encoder encoder;
>  	struct drm_connector connector;
>  };
>  
> diff --git a/drivers/gpu/drm/vkms/vkms_output.c b/drivers/gpu/drm/vkms/vkms_output.c
> index dcd32bc30e17..15f0b72af325 100644
> --- a/drivers/gpu/drm/vkms/vkms_output.c
> +++ b/drivers/gpu/drm/vkms/vkms_output.c
> @@ -4,6 +4,7 @@
>  #include "vkms_drv.h"
>  #include <drm/drm_atomic_helper.h>
>  #include <drm/drm_edid.h>
> +#include <drm/drm_managed.h>
>  #include <drm/drm_probe_helper.h>
>  
>  static const struct drm_connector_funcs vkms_connector_funcs = {
> @@ -14,10 +15,6 @@ static const struct drm_connector_funcs vkms_connector_funcs = {
>  	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
>  };
>  
> -static const struct drm_encoder_funcs vkms_encoder_funcs = {
> -	.destroy = drm_encoder_cleanup,
> -};
> -
>  static int vkms_conn_get_modes(struct drm_connector *connector)
>  {
>  	int count;
> @@ -32,6 +29,31 @@ static const struct drm_connector_helper_funcs vkms_conn_helper_funcs = {
>  	.get_modes    = vkms_conn_get_modes,
>  };
>  
> +static struct drm_encoder *vkms_encoder_init(struct vkms_device *vkms_device,
> +					     uint32_t possible_crtcs)
> +{
> +	struct drm_encoder *encoder;
> +	int ret;
> +
> +	encoder = drmm_kzalloc(&vkms_device->drm, sizeof(*encoder), GFP_KERNEL);
> +	if (!encoder) {
> +		DRM_ERROR("Failed to allocate encoder\n");
> +		return ERR_PTR(-ENOMEM);
> +	}

Thanks, I forgot this error handling in my implementation!

And while checking, I also found the drmm_encoder_alloc macro, which do 
the kzalloc AND the _init. Maybe we should use this?

> +
> +	ret = drmm_encoder_init(&vkms_device->drm, encoder, NULL,
> +				DRM_MODE_ENCODER_VIRTUAL, NULL);
> +	if (ret) {
> +		DRM_ERROR("Failed to init encoder\n");
> +		kfree(encoder);

Are you sure about this kfree? As the encoder was initialized by 
drmm_kzalloc, it should be freed by drm?

Or at least, drmm_kfree?

> +		return ERR_PTR(ret);
> +	}
> +
> +	encoder->possible_crtcs = possible_crtcs;
> +
> +	return encoder;
> +}
> +
>  static int vkms_add_overlay_plane(struct vkms_device *vkmsdev, int index)
>  {
>  	struct vkms_plane *overlay;
> @@ -51,7 +73,7 @@ int vkms_output_init(struct vkms_device *vkmsdev, int index)
>  	struct vkms_output *output = &vkmsdev->output;
>  	struct drm_device *dev = &vkmsdev->drm;
>  	struct drm_connector *connector = &output->connector;
> -	struct drm_encoder *encoder = &output->encoder;
> +	struct drm_encoder *encoder;
>  	struct vkms_crtc *vkms_crtc;
>  	struct vkms_config_crtc *crtc_cfg;
>  	struct vkms_plane *primary, *cursor = NULL;
> @@ -101,13 +123,9 @@ int vkms_output_init(struct vkms_device *vkmsdev, int index)
>  
>  	drm_connector_helper_add(connector, &vkms_conn_helper_funcs);
>  
> -	ret = drm_encoder_init(dev, encoder, &vkms_encoder_funcs,
> -			       DRM_MODE_ENCODER_VIRTUAL, NULL);
> -	if (ret) {
> -		DRM_ERROR("Failed to init encoder\n");
> -		goto err_encoder;
> -	}
> -	encoder->possible_crtcs = 1;
> +	encoder = vkms_encoder_init(vkmsdev, BIT(0));
> +	if (IS_ERR(encoder))
> +		return PTR_ERR(encoder);
>  
>  	ret = drm_connector_attach_encoder(connector, encoder);
>  	if (ret) {
> @@ -120,9 +138,6 @@ int vkms_output_init(struct vkms_device *vkmsdev, int index)
>  	return 0;
>  
>  err_attach:
> -	drm_encoder_cleanup(encoder);
> -
> -err_encoder:
>  	drm_connector_cleanup(connector);
>  
>  	return ret;
> -- 
> 2.46.0
> 

-- 
Louis Chauvet, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com


More information about the dri-devel mailing list