From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796019430; b=OeJmJneN0+CMqHcsmt1bGdcIqklgUH5boZzNdSKLaBzcf/scK+HSgaA1/3lSDh6x5E75 oWqIbfpiJM2ztg4cr41Sib4pKlAIGrVomoNczgc7BTw5TRxHgoCS+aDYcFs8Cv0TDf6pr pDcBdqH6JrA3BHWetdWulXkvaDLCxsz/f1noTaJkle5Oy16eKxc4xz5zi8UrCSYHj/YfF x9NiJMm5yh/+eRRHF9MlncO7eKuWgLqYtVgJkBBzzwxMsqkOQ5xIjrz0QkqgRxPUv2im+ URcZhsmcK0a/98eQ9+ndLvMyBiiwuTUVp2QekNBld6IgnnQkCcSL4hCM1EUryDlKxpA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796019430; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=/hNjvyFvCwjzCwfBQeTxNHKmCB3EyMDsgrof8SwYGnc=; b=lHlRwgtDtvoBklpO/vUS+0LR+s6ST/+H0xGF6Clqj4dZUjep5vAvPjN1NXmSESdPGpJK z+rn3USu1qcsENjtLXxVv+O1yEVScdjgjCm5+4yf5oeC1SitwFYdGIGsCazUliz09chAb cvmhzfrbnHzfjDl/ZCC2AJE0MfRjWAvsfzvUDG2zTTMHDmY3fED4AQYnDiz7MI5ZPoqTg Qd0tr4Qojd+HoQhWuixX5Ml7ubK5jOnyOk9hZz16qLXmmo95Psg7Io4Ufp5UafkSF290m vRprDYF83NJ6xbKdgn81KD62VLxtSQYevqQjfMlDrP3UmEACwos9xg8BTsTjl4504Tw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=LQPZAPBi; arc=none smtp.remote-ip=209.85.210.179 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28BAE606A31C for ; Mon, 30 Nov 2026 06:17:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 28BAE606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=LQPZAPBi Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-8c5ebbc5150so6810384b3a.3 for ; Sun, 29 Nov 2026 22:17:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796019429; x=1796624229; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=/hNjvyFvCwjzCwfBQeTxNHKmCB3EyMDsgrof8SwYGnc=; b=LQPZAPBi20eJLJyWULTEZoccmQI3h+970v8zbAWAghJ8CN2eAlO/wxxr+jQiuo7KUq ZFiMsV/RNvVb7wgYmK2zNUNd9T/tZQ15IOle2ULCyfVNIv/F3QscOzPfhJpV97QMgQsX eOfTJBjbaWh/PMplsw0JCxE29/WSnhaA2pVyJdUSm8XF7NEa6hhZU/ojMtX9CfjqgzNW W3qO8cEbtd8LBPyC7ulfe+n2WLXiwURh6OXfkRYkh09YOoEWEXPHSLp+ktCe08Hc/snd Lvbn/vS5BJlzQgpmzkmYtGEo0IPq8qkSsLp6q0Fy0Iiitd9QsHRtqqVbS06jT/lJvCI7 1+OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796019429; x=1796624229; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/hNjvyFvCwjzCwfBQeTxNHKmCB3EyMDsgrof8SwYGnc=; b=tfBOD9pwMMTV4Tdd44YUEQOknIcLFTZ+08teWbmQDP7boHqZMDIhFoxC5aAngsXlF9 puzb/AJoXy9rvmqgTiauGCTtT9Q3EBvVC98Gk07YKA0I1eSxT6wXq7AD6z66zzOuPwfH w15vZ4Bln42WYfammwAQg+KnQjgYmVDNVJpU6cWyJ5kC1Xl5zsqZEyLroNXeve0kHloE UqLi0ZDROoko889+LDgb4v4cs76b5FQqaUbmZsJuu9vot5jx9YU4n4iwBqpHbhnYndkM m8+Pgr4/6n+RluUAJtsAGyhbediBc/phR3r5LvECXWZRApfcrwFMStLdNkXf828VqLuQ lyqA== X-Forwarded-Encrypted: i=1; AL+hErEo/d77PzictZbQRklfy74Zj1VjDbNPhoiQL16Ncf1ReggmUN8Fq5Y6soCQGnrIWN21evgpw89QnIzBTI4=@vger.kernel.org X-Gm-Message-State: APuU4Z8wzNLEO3Gc9qK37KSDhPrlLPv/1H9TKMR61F7w8+tbWUSxwMlo c0BMvqeKXc8ygWzSKaxB9CS3BWMel1TdlGK22ggjv4YdR8HCPCCMRqAy X-Gm-Gg: AZFdTZfkbH/rEAhx9jlw3XsMianunCTC4mdoG+OnI/OKaUJOz9uQrMQpolZ7UL7il6w A0nLXdDNBjAwAIt+n2wcsCmYReIVv6N/cWdWNkS9YgW39l0TD+YwRbdC4NfQ8yKV7jeOe6cl5uj Zox6WSkdznd9yFzMV6SfLHZpXx0Ag5B3uPaM9bWMIheGM4d22BQeYJVqNigfao1Zk19fxEv0klI 4+xmm2FKMPW2Fwh5Wy6CMHgoXtLJ28LlWbE1nADCT6wX30fr8eiAbFFSbcmm0uU9gbA63Q3zhjk 11qtnGcQdf1157SXF/5Vo5qNqcQd5OaPfIynKr2SogIVW5z8R2VCefJxZL+ig0nsiZ7YRowhPdm ve/g/w66xfEEb6aiDkSNkkDYzr/kzjCcpTEQEo5qMrykYgnxxyipS4/eMugGIKHPa+7hqBujDE9 2RZ++W9V3ZTYymQmRm9eHv7c+ExyI7mhD5I/Q2JIysMLY5ak94ODDr8K+pATYEhGEroiigheB+9 JHwpvd5Xf5s9Y715F/R1L8pFlDo00InTWDeJWZ3TtJQdlEFrUkFqvX4CzD9MtPzuPcUlyerTBO5 xX8uksOmQgMixAVj/G4PYEseopFzJANezRSL7OGn6LQe+1dmUL672MlJUXpdEq1P6PJo1UKAbgk YTdWfljA= X-Received: by 2002:a05:6a00:3d12:b0:89f:d9b2:4a1b with SMTP id d2e1a72fcca58-8c87b4f39e7mr30601355b3a.9.1796019429104; Sun, 29 Nov 2026 22:17:09 -0800 (PST) Received: from DESKTOP-TPPBPA1 (36-226-254-86.dynamic-ip.hinet.net. [36.226.254.86]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8c87285c89dsm16828987b3a.13.2026.11.29.22.17.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 22:17:08 -0800 (PST) From: Lin Tzu Yang To: Maxime Ripard , Dave Stevenson Cc: =?UTF-8?q?Ma=C3=ADra=20Canal?= , Raspberry Pi Kernel Maintenance , Maarten Lankhorst , Thomas Zimmermann , David Airlie , Simona Vetter , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] drm/vc4: hdmi: Reject modes that don't fit the BCM2711 timing registers Date: Mon, 30 Nov 2026 14:17:04 +0800 Message-ID: <20261130061704.830142-1-rongh7426@gmail.com> X-Mailer: git-send-email 2.51.1.windows.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 atomic check calls it again after its DBLCLK fixup, since that can lengthen the horizontal back porch. The check compares against FIELD_MAX() rather than using FIELD_FIT(): VC4_MASK() makes the masks u32, and FIELD_FIT() then shifts the value as a u32 and drops its upper bits, so for example a 256-line vertical sync would pass for the 5-bit VSP field. 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: Lin Tzu Yang --- Changes in v2: - Compare against FIELD_MAX() instead of FIELD_FIT(), which misses overflows with the u32 VC4_MASK() masks. - Also check the even field's vertical back porch. - Check the adjusted mode again in atomic_check() after the DBLCLK fixup. - Use my full name. All three were pointed out by the Sashiko review bot. v2 is build tested (W=1). I checked the new logic against v1 in a userspace copy of the function: the same 8 common modes (1080p/i, 480i/576i DBLCLK, 720p, 4K30, ...) are accepted, and the three cases Sashiko described are now rejected. The hardware test above was done on v1. v1: https://lore.kernel.org/all/20261126025311.640543-1-rongh7426@gmail.com/ drivers/gpu/drm/vc4/vc4_hdmi.c | 76 ++++++++++++++++++++++++++++++++++ drivers/gpu/drm/vc4/vc4_hdmi.h | 6 +++ 2 files changed, 82 insertions(+) diff --git a/drivers/gpu/drm/vc4/vc4_hdmi.c b/drivers/gpu/drm/vc4/vc4_hdmi.c index f3ec65d..9e4d6c5 100644 --- a/drivers/gpu/drm/vc4/vc4_hdmi.c +++ b/drivers/gpu/drm/vc4/vc4_hdmi.c @@ -1254,6 +1254,61 @@ static void vc5_hdmi_set_timings(struct vc4_hdmi *vc4_hdmi, drm_dev_exit(idx); } +/* + * FIELD_FIT() can't be used here: VC4_MASK() makes the masks u32, so + * FIELD_FIT() shifts the value as a u32 and loses its upper bits (a + * vertical sync of 256 lines would pass for the 5-bit VSP field). + * Compare against the field's maximum instead. Taking the value as s64 + * also rejects differences that went negative. + */ +static bool vc5_hdmi_fits(u32 max, s64 val) +{ + return val >= 0 && val <= max; +} + +#define VC5_HDMI_FITS(field, val) vc5_hdmi_fits(FIELD_MAX(field##_MASK), val) + +/* + * 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 (!VC5_HDMI_FITS(VC5_HDMI_HORZA_HAP, m.hdisplay * pixel_rep) || + !VC5_HDMI_FITS(VC5_HDMI_HORZA_HFP, + (m.hsync_start - m.hdisplay) * pixel_rep) || + !VC5_HDMI_FITS(VC5_HDMI_HORZB_HBP, + (m.htotal - m.hsync_end) * pixel_rep) || + !VC5_HDMI_FITS(VC5_HDMI_HORZB_HSP, + (m.hsync_end - m.hsync_start) * pixel_rep) || + !VC5_HDMI_FITS(VC5_HDMI_VERTB_VSPO, m.htotal >> (2 - pixel_rep))) + return MODE_H_ILLEGAL; + + /* The even field's back porch is one line shorter than the odd one's. */ + if (!VC5_HDMI_FITS(VC5_HDMI_VERTA_VSP, + m.crtc_vsync_end - m.crtc_vsync_start) || + !VC5_HDMI_FITS(VC5_HDMI_VERTA_VFP, + m.crtc_vsync_start - m.crtc_vdisplay) || + !VC5_HDMI_FITS(VC5_HDMI_VERTA_VAL, m.crtc_vdisplay) || + !VC5_HDMI_FITS(VC4_HDMI_VERTB_VBP, + m.crtc_vtotal - m.crtc_vsync_end) || + !VC5_HDMI_FITS(VC4_HDMI_VERTB_VBP, + 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; @@ -1633,6 +1688,15 @@ static int vc4_hdmi_encoder_atomic_check(struct drm_encoder *encoder, return -EINVAL; } + /* + * mode_valid() checked the mode before the fixup above, which can + * lengthen the horizontal back porch, so check what will actually + * be programmed. + */ + if (vc4_hdmi->variant->timings_valid && + vc4_hdmi->variant->timings_valid(mode) != MODE_OK) + return -EINVAL; + /* * The 1440p@60 pixel rate is in the same range than the first * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz @@ -1656,6 +1720,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 +3305,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 +3334,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 +3363,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 +3390,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