Skip to contentSkip to conversation

drm/vc4: hdmi: Reject modes that don't fit the BCM2711 timing registers

PATCH New Roy 2 participants 2 messages Nov 26

Subsystems (3)
lore
Share & tools
lore

Roy

author
#0 lore raw

PATCH 1/1 drm/vc4: hdmi: Reject modes that don't fit the BCM2711 timing registers

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")
---
 drivers/gpu/drm/vc4/vc4_hdmi.c | 50 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/vc4/vc4_hdmi.h |  6 ++++
 2 files changed, 56 insertions(+)
drivers/gpu/drm/vc4/vc4_hdmi.c +50 -0
index f3ec65d..4e10cde 100644
@@ -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,
drivers/gpu/drm/vc4/vc4_hdmi.h +6 -0
index b1f41d5..397ebaf 100644
@@ -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);
Signature · 1 line
2.51.1.windows.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] drm/vc4: hdmi: unvalidated DBLCLK fixup in vc4_hdmi_encoder_atomic_check() - [Medium] drm/vc4…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] drm/vc4: hdmi: unvalidated DBLCLK fixup in vc4_hdmi_encoder_atomic_check()
- [Medium] drm/vc4: hdmi: u32 FIELD_FIT() truncation in vc5_hdmi_timings_valid()
- [Medium] drm/vc4: hdmi: unchecked even-field VBP in vc5_hdmi_timings_valid()
--

commit cdf386363de742cee9aaccb1142e521a753912f5
Author: Roy <[email protected]>

drm/vc4: hdmi: Reject modes that don't fit the BCM2711 timing registers

This commit adds a timings_valid() variant hook for the BCM2711 and BCM2712 HDMI controllers to check that mode timings fit in the hardware timing register fields before modeset.

Quoted 28 lines
drivers/gpu/drm/vc4/vc4_hdmi.c +20 -0
index f3ec65d86078f..4e10cdedb8a8f 100644
@@ -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,

drivers/gpu/drm/vc4/vc4_hdmi.c static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi, · line 1286

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_hdmi.c +10 -0
+		       (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) ||

[Severity: Medium]
Can FIELD_FIT() miss overflowing values on these shifted masks because VC4_MASK() casts GENMASK() to u32?

include/linux/bitfield.h:FIELD_FIT() shifts ((typeof(_mask))_val) left by __bf_shf(_mask) before testing against ~(_mask). With a 32-bit mask, that shift discards the upper __bf_shf(_mask) bits of _val. For VC5_HDMI_VERTA_VSP_MASK (VC4_MASK(28, 24)), shifting left by 24 drops bits 8..15, so a mode with m.crtc_vsync_end - m.crtc_vsync_start == 256 passes FIELD_FIT() and programs a truncated 0 VSP into HDMI_VERTA0/1. The masks at shift 16 (VC5_HDMI_HORZA_HFP_MASK, VC5_HDMI_HORZB_HBP_MASK and VC5_HDMI_VERTA_VFP_MASK) similarly drop bits 16..31.

drivers/gpu/drm/vc4/vc4_hdmi.c static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi, · line 1290

Quoted 4 lines
drivers/gpu/drm/vc4/vc4_hdmi.c +4 -0
+	    !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;

[Severity: Medium]
Should this also check m.crtc_vtotal - m.crtc_vsync_end without
+ interlaced for vertb_even?

vc5_hdmi_set_timings() packs mode->crtc_vtotal - mode->crtc_vsync_end into VC4_HDMI_VERTB_VBP for vertb_even (HDMI_VERTB0). When an interlaced mode has vscan > 1 (e.g. vdisplay = vsync_start = vsync_end = 512, vtotal = 1022, vscan = 257), the u16 multiplication in drm_mode_set_crtcinfo() wraps crtc_vtotal to 255 and crtc_vsync_end to
256. m.crtc_vtotal - m.crtc_vsync_end + interlaced is 0 and passes this check, after which vc5_hdmi_set_timings() passes -1 to VC4_SET_FIELD() for vertb_even and triggers a WARN_ON().

Quoted 9 lines
drivers/gpu/drm/vc4/vc4_hdmi.c +4 -0
+
+	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,
 {

drivers/gpu/drm/vc4/vc4_hdmi.c vc4_hdmi_encoder_mode_valid(struct drm_encoder *encoder, · line 1704

Quoted 10 lines
drivers/gpu/drm/vc4/vc4_hdmi.c +8 -0
 	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;
+	}
+

[Severity: Medium]
Can the DBLCLK odd-timing fixup in vc4_hdmi_encoder_atomic_check() push adjusted_mode past the 11-bit VC5_HDMI_HORZB_HBP or VC5_HDMI_HORZB_HSP limit after vc4_hdmi_encoder_mode_valid() has already approved the mode?

drivers/gpu/drm/vc4/vc4_hdmi.c:vc4_hdmi_encoder_atomic_check() {
    ...
		if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
			if ((mode->hsync_start - mode->hdisplay) & 1)
				mode->hsync_start--;
			if ((mode->hsync_end - mode->hsync_start) & 1)
				mode->hsync_end--;
		}
    ...
}

On BCM2711, a DRM_MODE_FLAG_DBLCLK mode with even hdisplay, even hsync_start, odd hsync_end and htotal - hsync_end == 1023 passes vc5_hdmi_timings_valid() with (htotal - hsync_end) * 2 == 2046 <= 2047. vc4_hdmi_encoder_atomic_check() then decrements adjusted_mode.hsync_end without re-running timings_valid(), so vc5_hdmi_set_timings() sees (htotal - hsync_end) * 2 == 2048, triggers WARN_ON() in VC4_SET_FIELD(), and writes a truncated 0 HBP to HDMI_HORZB.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1