dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
From: "Noralf Trønnes" <noralf@tronnes.org>
To: maxime@cerno.tech, Karol Herbst <kherbst@redhat.com>,
	Emma Anholt <emma@anholt.net>, Ben Skeggs <bskeggs@redhat.com>,
	Chen-Yu Tsai <wens@csie.org>,
	Rodrigo Vivi <rodrigo.vivi@intel.com>,
	Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Jani Nikula <jani.nikula@linux.intel.com>,
	Daniel Vetter <daniel@ffwll.ch>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>,
	Samuel Holland <samuel@sholland.org>,
	Jernej Skrabec <jernej.skrabec@gmail.com>,
	David Airlie <airlied@linux.ie>,
	Maxime Ripard <mripard@kernel.org>,
	Joonas Lahtinen <joonas.lahtinen@linux.intel.com>,
	Lyude Paul <lyude@redhat.com>
Cc: "Dom Cobley" <dom@raspberrypi.com>,
	"Dave Stevenson" <dave.stevenson@raspberrypi.com>,
	nouveau@lists.freedesktop.org, intel-gfx@lists.freedesktop.org,
	linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
	"Phil Elwell" <phil@raspberrypi.com>,
	"Hans de Goede" <hdegoede@redhat.com>,
	"Noralf Trønnes" <noralf@tronnes.org>,
	"Geert Uytterhoeven" <geert@linux-m68k.org>,
	"Mateusz Kwiatkowski" <kfyatek+publicgit@gmail.com>,
	linux-sunxi@lists.linux.dev,
	linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH v6 16/23] drm/probe-helper: Provide a TV get_modes helper
Date: Sun, 6 Nov 2022 17:33:48 +0100	[thread overview]
Message-ID: <842076aa-8d7c-96d6-ba46-d0e66dacd2df@tronnes.org> (raw)
In-Reply-To: <20220728-rpi-analog-tv-properties-v6-16-e7792734108f@cerno.tech>



Den 26.10.2022 17.33, skrev maxime@cerno.tech:
> Most of the TV connectors will need a similar get_modes implementation
> that will, depending on the drivers' capabilities, register the 480i and
> 576i modes.
> 
> That implementation will also need to set the preferred flag and order
> the modes based on the driver and users preferrence.
> 
> This is especially important to guarantee that a userspace stack such as
> Xorg can start and pick up the preferred mode while maintaining a
> working output.
> 
> Signed-off-by: Maxime Ripard <maxime@cerno.tech>
> 
> ---
> Changes in v6:
> - New patch
> ---
>  drivers/gpu/drm/drm_probe_helper.c | 97 ++++++++++++++++++++++++++++++++++++++
>  include/drm/drm_probe_helper.h     |  1 +
>  2 files changed, 98 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_probe_helper.c b/drivers/gpu/drm/drm_probe_helper.c
> index 69b0b2b9cc1c..4a60575f5c66 100644
> --- a/drivers/gpu/drm/drm_probe_helper.c
> +++ b/drivers/gpu/drm/drm_probe_helper.c
> @@ -1147,3 +1147,100 @@ int drm_connector_helper_get_modes(struct drm_connector *connector)
>  	return count;
>  }
>  EXPORT_SYMBOL(drm_connector_helper_get_modes);
> +
> +static bool tv_mode_supported(struct drm_connector *connector,
> +			      enum drm_connector_tv_mode mode)
> +{
> +	struct drm_device *dev = connector->dev;
> +	struct drm_property *property = dev->mode_config.tv_mode_property;
> +

Superfluous linebreak

> +	unsigned int i;
> +
> +	for (i = 0; i < property->num_values; i++)
> +		if (property->values[i] == mode)
> +			return true;
> +
> +	return false;
> +}
> +
> +/**
> + * drm_connector_helper_tv_get_modes - Fills the modes availables to a TV connector

availables -> available

> + * @connector: The connector
> + *
> + * Fills the available modes for a TV connector based on the supported
> + * TV modes, and the default mode expressed by the kernel command line.
> + *
> + * This can be used as the default TV connector helper .get_modes() hook
> + * if the driver does not need any special processing.
> + *
> + * Returns:
> + * The number of modes added to the connector.
> + */
> +int drm_connector_helper_tv_get_modes(struct drm_connector *connector)
> +{
> +	struct drm_device *dev = connector->dev;
> +	struct drm_cmdline_mode *cmdline = &connector->cmdline_mode;
> +	struct drm_display_mode *tv_modes[2] = {};
> +	struct drm_display_mode *mode;
> +	unsigned int first_mode_idx;
> +	unsigned int count = 0;
> +	uint64_t default_mode;
> +	int ret;
> +
> +	if (!dev->mode_config.tv_mode_property)
> +		return 0;
> +
> +	if (tv_mode_supported(connector, DRM_MODE_TV_MODE_NTSC) ||
> +	    tv_mode_supported(connector, DRM_MODE_TV_MODE_NTSC_443) ||
> +	    tv_mode_supported(connector, DRM_MODE_TV_MODE_NTSC_J) ||
> +	    tv_mode_supported(connector, DRM_MODE_TV_MODE_PAL_M)) {
> +		mode = drm_mode_analog_ntsc_480i(connector->dev);

Nit: You can use the dev variable here and below.

> +		if (!mode)
> +			return 0;
> +
> +		tv_modes[count++] = mode;
> +	}
> +
> +	if (tv_mode_supported(connector, DRM_MODE_TV_MODE_PAL) ||
> +	    tv_mode_supported(connector, DRM_MODE_TV_MODE_PAL_N) ||
> +	    tv_mode_supported(connector, DRM_MODE_TV_MODE_SECAM)) {
> +		mode = drm_mode_analog_pal_576i(connector->dev);
> +		if (!mode)
> +			return 0;

You leak the ntsc mode when returning (possibly).

> +
> +		tv_modes[count++] = mode;
> +	}
> +

Maybe check for count being zero here?

> +	if (count == 1) {
> +		mode->type |= DRM_MODE_TYPE_PREFERRED;
> +		drm_mode_probed_add(connector, mode);
> +		return count;
> +	}
> +
> +	ret = drm_object_property_get_default_value(&connector->base,
> +						    dev->mode_config.tv_mode_property,
> +						    &default_mode);
> +	if (ret)
> +		return 0;

You leak both modes when returning here. Maybe move this up before
allocation to simplify error handling.

> +
> +	if (cmdline->tv_mode_specified)
> +		default_mode = cmdline->tv_mode;

I realised that we don't verify tv_mode coming from the command line,
not here and not in the reset helper. Should we do that? A driver should
be programmed defensively to handle an illegal/unsupported value, but it
doesn't feel right to allow an illegal enum value coming through the
core/helpers.

> +
> +	if ((default_mode == DRM_MODE_TV_MODE_NTSC) ||
> +	    (default_mode == DRM_MODE_TV_MODE_NTSC_443) ||
> +	    (default_mode == DRM_MODE_TV_MODE_NTSC_J) ||
> +	    (default_mode == DRM_MODE_TV_MODE_PAL_M))
> +		first_mode_idx = 0;
> +	else
> +		first_mode_idx = 1;
> +
> +	mode = tv_modes[first_mode_idx];
> +	mode->type |= DRM_MODE_TYPE_PREFERRED;
> +	drm_mode_probed_add(connector, mode);
> +
> +	mode = first_mode_idx ? tv_modes[0] : tv_modes[1];
> +	drm_mode_probed_add(connector, mode);
> +
> +	return count;
> +}
> +EXPORT_SYMBOL(drm_connector_helper_tv_get_modes);

I know this is not expensive, but you're looping over the property
values 7 times. An alternative solution is to rebuild the supported bitmask:

int drm_connector_helper_tv_get_modes(struct drm_connector *connector)
{
...
	unsigned int ntsc_modes = BIT(DRM_MODE_TV_MODE_NTSC) |
				  BIT(DRM_MODE_TV_MODE_NTSC_443) |
				  BIT(DRM_MODE_TV_MODE_NTSC_J) |
				  BIT(DRM_MODE_TV_MODE_PAL_M);
	unsigned int pal_modes = BIT(DRM_MODE_TV_MODE_PAL) |
				 BIT(DRM_MODE_TV_MODE_PAL_N) |
				 BIT(DRM_MODE_TV_MODE_SECAM);
	unsigned int supported_tv_modes = 0;

...
	for (i = 0; i < property->num_values; i++)
		supported_tv_modes |= BIT(property->values[i]);

	if (supported_tv_modes & ntsc_modes)
...
	if (supported_tv_modes & pal_modes)
...

	if (BIT(default_mode) & ntsc_modes)
		first_mode_idx = 0;
	else
		first_mode_idx = 1;


Up to you if you want to do this.

Noralf.

  parent reply	other threads:[~2022-11-06 16:34 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-26 15:33 [PATCH v6 00/23] drm: Analog TV Improvements maxime
2022-10-26 15:33 ` [PATCH v6 01/23] drm/tests: Add Kunit Helpers maxime
2022-10-26 15:33 ` [PATCH v6 02/23] drm/connector: Rename legacy TV property maxime
2022-10-26 15:33 ` [PATCH v6 03/23] drm/connector: Only register TV mode property if present maxime
2022-10-26 15:33 ` [PATCH v6 04/23] drm/connector: Rename drm_mode_create_tv_properties maxime
2022-10-26 15:33 ` [PATCH v6 05/23] drm/connector: Add TV standard property maxime
2022-10-26 15:33 ` [PATCH v6 06/23] drm/modes: Add a function to generate analog display modes maxime
2022-10-26 15:33 ` [PATCH v6 07/23] drm/client: Add some tests for drm_connector_pick_cmdline_mode() maxime
2022-10-26 15:33 ` [PATCH v6 08/23] drm/modes: Move named modes parsing to a separate function maxime
2022-11-05 14:02   ` Noralf Trønnes
2022-10-26 15:33 ` [PATCH v6 09/23] drm/modes: Switch to named mode descriptors maxime
2022-11-05 14:06   ` Noralf Trønnes
2022-10-26 15:33 ` [PATCH v6 10/23] drm/modes: Fill drm_cmdline mode from named modes maxime
2022-11-06 13:04   ` Noralf Trønnes
2022-11-07 10:02     ` Maxime Ripard
2022-10-26 15:33 ` [PATCH v6 11/23] drm/connector: Add pixel clock to cmdline mode maxime
2022-11-06 13:06   ` Noralf Trønnes
2022-10-26 15:33 ` [PATCH v6 12/23] drm/connector: Add a function to lookup a TV mode by its name maxime
2022-10-26 15:33 ` [PATCH v6 13/23] drm/modes: Introduce the tv_mode property as a command-line option maxime
2022-11-06 13:10   ` Noralf Trønnes
2022-10-26 15:33 ` [PATCH v6 14/23] drm/modes: Properly generate a drm_display_mode from a named mode maxime
2022-10-26 21:25   ` Mateusz Kwiatkowski
2022-11-05 17:50   ` Noralf Trønnes
2022-11-07 13:34     ` Maxime Ripard
2022-10-26 15:33 ` [PATCH v6 15/23] drm/modes: Introduce more named modes maxime
2022-10-26 15:33 ` [PATCH v6 16/23] drm/probe-helper: Provide a TV get_modes helper maxime
2022-10-26 22:02   ` Mateusz Kwiatkowski
2022-10-27  9:37     ` Maxime Ripard
2022-11-06 16:59     ` Noralf Trønnes
2022-11-07 10:07       ` Maxime Ripard
2022-11-07 11:17         ` Noralf Trønnes
2022-11-06 16:33   ` Noralf Trønnes [this message]
2022-11-07 10:21     ` Maxime Ripard
2022-11-07 11:29       ` Noralf Trønnes
2022-11-07 12:45         ` Maxime Ripard
2022-10-26 15:33 ` [PATCH v6 17/23] drm/atomic-helper: Add a TV properties reset helper maxime
2022-10-26 15:33 ` [PATCH v6 18/23] drm/atomic-helper: Add an analog TV atomic_check implementation maxime
2022-10-26 15:33 ` [PATCH v6 19/23] drm/vc4: vec: Use TV Reset implementation maxime
2022-10-26 15:33 ` [PATCH v6 20/23] drm/vc4: vec: Check for VEC output constraints maxime
2022-10-26 15:33 ` [PATCH v6 21/23] drm/vc4: vec: Convert to the new TV mode property maxime
2022-10-26 15:33 ` [PATCH v6 22/23] drm/vc4: vec: Add support for more analog TV standards maxime
2022-10-26 22:32   ` Mateusz Kwiatkowski
2022-10-27 11:58     ` Maxime Ripard
2022-10-26 15:33 ` [PATCH v6 23/23] drm/sun4i: tv: Convert to the new TV mode property maxime

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=842076aa-8d7c-96d6-ba46-d0e66dacd2df@tronnes.org \
    --to=noralf@tronnes.org \
    --cc=airlied@linux.ie \
    --cc=bskeggs@redhat.com \
    --cc=daniel@ffwll.ch \
    --cc=dave.stevenson@raspberrypi.com \
    --cc=dom@raspberrypi.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=emma@anholt.net \
    --cc=geert@linux-m68k.org \
    --cc=hdegoede@redhat.com \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=jani.nikula@linux.intel.com \
    --cc=jernej.skrabec@gmail.com \
    --cc=joonas.lahtinen@linux.intel.com \
    --cc=kfyatek+publicgit@gmail.com \
    --cc=kherbst@redhat.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sunxi@lists.linux.dev \
    --cc=lyude@redhat.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=maxime@cerno.tech \
    --cc=mripard@kernel.org \
    --cc=nouveau@lists.freedesktop.org \
    --cc=phil@raspberrypi.com \
    --cc=rodrigo.vivi@intel.com \
    --cc=samuel@sholland.org \
    --cc=tvrtko.ursulin@linux.intel.com \
    --cc=tzimmermann@suse.de \
    --cc=wens@csie.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).