mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Roy <rongh7426@gmail.com>
To: Maxime Ripard <mripard@kernel.org>,
	Dave Stevenson <dave.stevenson@raspberrypi.com>
Cc: "Maíra Canal" <mcanal@igalia.com>,
	"Raspberry Pi Kernel --confirm=neverMaintenance"
	<kernel-list@raspberrypi.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	Roy <rongh7426@gmail.com>
Subject: [PATCH] drm/vc4: hdmi: Reject modes that don't fit the BCM2711 timing registers
Date: Thu, 26 Nov 2026 10:53:11 +0800	[thread overview]
Message-ID: <20261126025311.640543-1-rongh7426@gmail.com> (raw)

vc5_hdmi_set_timings() packs the mode timings into register fields that
are much narrower than the values a mode can carry (for example 11 bits
for the horizontal back porch and 9 bits for the vertical back porch),
but nothing checks that a mode fits before it gets there. Such a mode
passes the atomic check, VC4_SET_FIELD() warns once for every field
that overflows, and the truncated timings are written to the hardware.

The igt test kms_invalid_mode, subtest overflow-vrefresh, hits this on a
Raspberry Pi 5: it sets a mode with htotal = vtotal = 32768, the commit
succeeds and four warnings are printed:

  WARNING: CPU: 0 PID: 64500 at vc4_hdmi.c:1282 vc5_hdmi_set_timings+0x970/0xed8 [vc4]
  WARNING: CPU: 0 PID: 64500 at vc4_hdmi.c:1284 vc5_hdmi_set_timings+0x980/0xed8 [vc4]
  WARNING: CPU: 0 PID: 64500 at vc4_hdmi.c:1288 vc5_hdmi_set_timings+0x994/0xed8 [vc4]
  WARNING: CPU: 0 PID: 64500 at vc4_hdmi.c:1311 vc5_hdmi_set_timings+0xbb4/0xed8 [vc4]

Add a timings_valid() variant hook, implemented for the BCM2711 and
BCM2712 controllers, that computes the same values as
vc5_hdmi_set_timings() and checks each one against its field, and call
it from the encoder's mode_valid(). Modes that don't fit are now
rejected at atomic check time, and dropped from the probed mode list.

The BCM2835 controller (vc4_hdmi_set_timings()) has the same kind of
narrow fields but is left alone here, as I have no hardware to test it.

Tested on a Raspberry Pi 5 (Raspberry Pi OS kernel 6.18.39, with this
change applied to its vc4 driver):
- kms_invalid_mode: 12 pass / 1 fail, 4 warnings -> 13 pass / 0 fail,
  no warnings
- all 12 modes from the monitor's EDID (including 1920x1080i and
  720x480) are still listed, and none is reported as rejected

The failure was found with igt-gpu-tools. The fix and this changelog
were written with the help of an AI coding assistant (Claude); the
testing above was done on real hardware.

Fixes: 8323989140f3 ("drm/vc4: hdmi: Support the BCM2711 HDMI controllers")
Signed-off-by: Roy <rongh7426@gmail.com>
---
 drivers/gpu/drm/vc4/vc4_hdmi.c | 50 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/vc4/vc4_hdmi.h |  6 ++++
 2 files changed, 56 insertions(+)

diff --git a/drivers/gpu/drm/vc4/vc4_hdmi.c b/drivers/gpu/drm/vc4/vc4_hdmi.c
index f3ec65d..4e10cde 100644
--- a/drivers/gpu/drm/vc4/vc4_hdmi.c
+++ b/drivers/gpu/drm/vc4/vc4_hdmi.c
@@ -1254,6 +1254,44 @@ static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi,
 	drm_dev_exit(idx);
 }
 
+/*
+ * Check that every value vc5_hdmi_set_timings() writes fits in its
+ * register field. Such a mode would otherwise be programmed with
+ * truncated timings, and VC4_SET_FIELD() would warn about it on every
+ * modeset.
+ */
+static enum drm_mode_status
+vc5_hdmi_timings_valid(const struct drm_display_mode *mode)
+{
+	bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
+	u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
+	struct drm_display_mode m;
+
+	drm_mode_init(&m, mode);
+	drm_mode_set_crtcinfo(&m, CRTC_INTERLACE_HALVE_V);
+
+	if (!FIELD_FIT(VC5_HDMI_HORZA_HAP_MASK, m.hdisplay * pixel_rep) ||
+	    !FIELD_FIT(VC5_HDMI_HORZA_HFP_MASK,
+		       (m.hsync_start - m.hdisplay) * pixel_rep) ||
+	    !FIELD_FIT(VC5_HDMI_HORZB_HBP_MASK,
+		       (m.htotal - m.hsync_end) * pixel_rep) ||
+	    !FIELD_FIT(VC5_HDMI_HORZB_HSP_MASK,
+		       (m.hsync_end - m.hsync_start) * pixel_rep) ||
+	    !FIELD_FIT(VC5_HDMI_VERTB_VSPO_MASK, m.htotal >> (2 - pixel_rep)))
+		return MODE_H_ILLEGAL;
+
+	if (!FIELD_FIT(VC5_HDMI_VERTA_VSP_MASK,
+		       m.crtc_vsync_end - m.crtc_vsync_start) ||
+	    !FIELD_FIT(VC5_HDMI_VERTA_VFP_MASK,
+		       m.crtc_vsync_start - m.crtc_vdisplay) ||
+	    !FIELD_FIT(VC5_HDMI_VERTA_VAL_MASK, m.crtc_vdisplay) ||
+	    !FIELD_FIT(VC4_HDMI_VERTB_VBP_MASK,
+		       m.crtc_vtotal - m.crtc_vsync_end + interlaced))
+		return MODE_V_ILLEGAL;
+
+	return MODE_OK;
+}
+
 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi *vc4_hdmi)
 {
 	struct drm_device *drm = vc4_hdmi->connector.dev;
@@ -1656,6 +1694,14 @@ vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder,
 {
 	struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
 
+	if (vc4_hdmi->variant->timings_valid) {
+		enum drm_mode_status status;
+
+		status = vc4_hdmi->variant->timings_valid(mode);
+		if (status != MODE_OK)
+			return status;
+	}
+
 	if (vc4_hdmi->variant->unsupported_odd_h_timings &&
 	    !(mode->flags & DRM_MODE_FLAG_DBLCLK) &&
 	    ((mode->hdisplay % 2) || (mode->hsync_start % 2) ||
@@ -3233,6 +3279,7 @@ static const struct vc4_hdmi_variant bcm2711_hdmi0_variant = {
 	.csc_setup		= vc5_hdmi_csc_setup,
 	.reset			= vc5_hdmi_reset,
 	.set_timings		= vc5_hdmi_set_timings,
+	.timings_valid		= vc5_hdmi_timings_valid,
 	.phy_init		= vc5_hdmi_phy_init,
 	.phy_disable		= vc5_hdmi_phy_disable,
 	.phy_rng_enable		= vc5_hdmi_phy_rng_enable,
@@ -3261,6 +3308,7 @@ static const struct vc4_hdmi_variant bcm2711_hdmi1_variant = {
 	.csc_setup		= vc5_hdmi_csc_setup,
 	.reset			= vc5_hdmi_reset,
 	.set_timings		= vc5_hdmi_set_timings,
+	.timings_valid		= vc5_hdmi_timings_valid,
 	.phy_init		= vc5_hdmi_phy_init,
 	.phy_disable		= vc5_hdmi_phy_disable,
 	.phy_rng_enable		= vc5_hdmi_phy_rng_enable,
@@ -3289,6 +3337,7 @@ static const struct vc4_hdmi_variant bcm2712_hdmi0_variant = {
 	.csc_setup		= vc5_hdmi_csc_setup,
 	.reset			= vc5_hdmi_reset,
 	.set_timings		= vc5_hdmi_set_timings,
+	.timings_valid		= vc5_hdmi_timings_valid,
 	.phy_init		= vc6_hdmi_phy_init,
 	.phy_disable		= vc6_hdmi_phy_disable,
 	.channel_map		= vc5_hdmi_channel_map,
@@ -3315,6 +3364,7 @@ static const struct vc4_hdmi_variant bcm2712_hdmi1_variant = {
 	.csc_setup		= vc5_hdmi_csc_setup,
 	.reset			= vc5_hdmi_reset,
 	.set_timings		= vc5_hdmi_set_timings,
+	.timings_valid		= vc5_hdmi_timings_valid,
 	.phy_init		= vc6_hdmi_phy_init,
 	.phy_disable		= vc6_hdmi_phy_disable,
 	.channel_map		= vc5_hdmi_channel_map,
diff --git a/drivers/gpu/drm/vc4/vc4_hdmi.h b/drivers/gpu/drm/vc4/vc4_hdmi.h
index b1f41d5..397ebaf 100644
--- a/drivers/gpu/drm/vc4/vc4_hdmi.h
+++ b/drivers/gpu/drm/vc4/vc4_hdmi.h
@@ -71,6 +71,12 @@ struct vc4_hdmi_variant {
 			    struct drm_connector_state *state,
 			    const struct drm_display_mode *mode);
 
+	/*
+	 * Callback to check that the video timings of a mode fit in the
+	 * timing registers of the HDMI block. Optional.
+	 */
+	enum drm_mode_status (*timings_valid)(const struct drm_display_mode *mode);
+
 	/* Callback to initialize the PHY according to the connector state */
 	void (*phy_init)(struct vc4_hdmi *vc4_hdmi,
 			 struct drm_connector_state *conn_state);
-- 
2.51.1.windows.1


                 reply	other threads:[~2026-11-26  2:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261126025311.640543-1-rongh7426@gmail.com \
    --to=rongh7426@gmail.com \
    --cc=airlied@gmail.com \
    --cc=dave.stevenson@raspberrypi.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=kernel-list@raspberrypi.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mcanal@igalia.com \
    --cc=mripard@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tzimmermann@suse.de \
    /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

all inboxes | Powered by JetHome®