From nobody Sun Nov 24 00:50:34 2024 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C735B152166 for ; Sat, 9 Nov 2024 12:35:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731155720; cv=none; b=ka/xWRPxYi5mbzKGWV7/J/KjDXcm+HkNhiP3AT4vQ0+UCYihmBdvdN8VIfbF3kCotLlqPkzrdJCo9XyZxE40DobZS2za8KtAnivdkuMRgw2KuiToyU16WrQD2OMolHNZ9B3XHpFwjJR4rBwDdaxCklOOE6Qo7J9pFm0x+mgS83E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731155720; c=relaxed/simple; bh=O0+dNPOmXaHGOBHqPcLUaOUNdAb4skVm7YApfGYSVUE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=VHKC+ShTzPBpXfxewJ2HCZWyvRqi+m8IYDx1vPSL/VbhLu0PhX6ESvEiG5W5cqMJ05tHDiUG3rvHsZB8HyxoUSkbJ6nDc0q3k2KFPBYT3SGNNA3fznTV6unpdjZuVHW8SGRTsjvDMMvKuTEwrB+NRU1sK2Ht6xs4XT7S/AZnUdk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=FJjW7m9J; arc=none smtp.client-ip=209.85.167.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="FJjW7m9J" Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-539f8490856so3945484e87.2 for ; Sat, 09 Nov 2024 04:35:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1731155717; x=1731760517; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1+nD5HJByLExsTSOICbG27tCgbmZZrKa+DbcceQO2vk=; b=FJjW7m9Jr95NUZh2rupKrLwkxFuUMYjJWu+48fKo9qt5RehfBXyxqGE+qbFndQMfji XiDKtUVvDuYt1WPX09uFO775UMFZ3LfneKUGS2Q+1VBsdm0xZatA0Kn+ugmRSoXQyQda 1ZmSaDUODaw76EN74wSaBSLh4XQM12gImwEEvnacgmkUGvJkvOGkdJG+z+KrHPN6ua2d /EB3AuVLvTHaBE1bpDDjwSV6uF0P7qPMwiRICxJryGq0dEN61tr4dxvPFbOxPaFuSFiQ l+ESLWPnnvCsLFL/Lu7DuE4tuKhqzeM5v71RYAwA/7P9H0E+hcuBrXHNm2Djw9fZfBqk ULGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731155717; x=1731760517; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1+nD5HJByLExsTSOICbG27tCgbmZZrKa+DbcceQO2vk=; b=FVOaJeBiO1jO+/WAJ9oDgj9JOBPeojxPq6mmoTFkBbRg9BocX1IrBXXlP0eKjMJfvq 24W/jY4Ai7Ecd0iGFFPM6woqpmeRWFwlD1IEE5JIBxcbAmHJ52G/U607jeWa2GTw+JQJ v9SYBqCKETKNkURN0SRyJB6R4EZ7DrcZ1mzyZm2UcatThy0rWxGuB7aXwc02UNKhBWvj jCFu/7iLEMCsNnVqbWMHovBWRVXAxs1W2/vgQqi4zLJSSqdZPIajxiJ2POrzAVix+oHH lwJAJUj6kq1HSgdKrlVVAtHspOIstBlAXbuALnRCnzrdAIzHFy0f6t3V8KfN5Vhkh10D ukHg== X-Forwarded-Encrypted: i=1; AJvYcCU8i3IIJ2tgC14tT5kHejOfCwnofd3mZhlpmoBDp66Ssj4JZnCUdrDEXt5eJWNuW6zRyFQNomK5d97txM8=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6AHK6dhX/KG9eZHRfelY32TPNr400EST4DGx4pqBc/tsrMvhE 8gfIV7JiV6kfFvSUuvOebqDGiakzeK8QzvT2Q9+agGaS+j8jrmOpQZq7rWE/1I0= X-Google-Smtp-Source: AGHT+IGJo4jZoEglm9go0nIAPmKBkvOJcvZk4GwfWpbMZ+lIEILOqU9QXlUgXQNesCnZ0pMqP2i/gQ== X-Received: by 2002:a05:6512:3c8e:b0:536:553f:3ef9 with SMTP id 2adb3069b0e04-53d86296487mr3367516e87.27.1731155716900; Sat, 09 Nov 2024 04:35:16 -0800 (PST) Received: from umbar.lan ([192.130.178.90]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-53d826784dbsm922899e87.15.2024.11.09.04.35.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Nov 2024 04:35:15 -0800 (PST) From: Dmitry Baryshkov Date: Sat, 09 Nov 2024 14:35:05 +0200 Subject: [PATCH v3 1/7] drm/display: hdmi: add generic mode_valid helper Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241109-hdmi-mode-valid-v3-1-5348c2368076@linaro.org> References: <20241109-hdmi-mode-valid-v3-0-5348c2368076@linaro.org> In-Reply-To: <20241109-hdmi-mode-valid-v3-0-5348c2368076@linaro.org> To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Dave Stevenson , =?utf-8?q?Ma=C3=ADra_Canal?= , Raspberry Pi Kernel Maintenance , Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=10210; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=O0+dNPOmXaHGOBHqPcLUaOUNdAb4skVm7YApfGYSVUE=; b=owGbwMvMwMXYbdNlx6SpcZXxtFoSQ7p+2L9z8q471i1Ic2Gzf/eb68z07L/TghJ9VywN2NKgG nXnxZV3nYzGLAyMXAyyYoosPgUtU2M2JYd92DG1HmYQKxPIFAYuTgGYyAUV9v+pyQtrY8KOBvWl cATFFCX/u3qFUz7+u2HChUUB7+fFHGjM6c+Pq7j71DOPxXfecUWOR6kFr9gKG70W6iT6atRUc92 d1zqvSNT2h/0G6W8JcoohfOu4771VcT2S4Mj9IG9iS+jLRo+e6bwJNX5PXAzi+MUnTvUVqBJs// aNpSNud83SKdNPzMgpMgs9LnO6deG/nMcsVr+UT/F0s7YuXOxayJcpq7iLMyI1VPHIuXM85Quv6 c2YfNXcNJ3p8hY7BbbLkXLCPHtEnV+IJ0pr9x76qP1WIsVl2dmQOWdiixt5fyi5XttozqA584Bz 0/GsaDVbp90+Fy6enhbGMMOmSEl7yu+pXLdUgqbrnbkDAA== X-Developer-Key: i=dmitry.baryshkov@linaro.org; a=openpgp; fpr=8F88381DD5C873E4AE487DA5199BF1243632046A Add drm_hdmi_connector_mode_valid(), generic helper for HDMI connectors. It can be either used directly or as a part of the .mode_valid callback. Signed-off-by: Dmitry Baryshkov --- drivers/gpu/drm/display/drm_hdmi_state_helper.c | 21 +++ drivers/gpu/drm/tests/drm_hdmi_state_helper_test.c | 181 +++++++++++++++++= +++- include/drm/display/drm_hdmi_state_helper.h | 4 + 3 files changed, 204 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/display/drm_hdmi_state_helper.c b/drivers/gpu/= drm/display/drm_hdmi_state_helper.c index feb7a3a759811aed70c679be8704072093e2a79b..80bf2829ba89b5f84fed4fa9eb1= d6302e10a4f9e 100644 --- a/drivers/gpu/drm/display/drm_hdmi_state_helper.c +++ b/drivers/gpu/drm/display/drm_hdmi_state_helper.c @@ -521,6 +521,27 @@ int drm_atomic_helper_connector_hdmi_check(struct drm_= connector *connector, } EXPORT_SYMBOL(drm_atomic_helper_connector_hdmi_check); =20 +/** + * drm_hdmi_connector_mode_valid() - Check if mode is valid for HDMI conne= ctor + * @connector: DRM connector to validate the mode + * @mode: Display mode to validate + * + * Generic .mode_valid implementation for HDMI connectors. + */ +enum drm_mode_status +drm_hdmi_connector_mode_valid(struct drm_connector *connector, + struct drm_display_mode *mode) +{ + unsigned long long clock; + + clock =3D drm_hdmi_compute_mode_clock(mode, 8, HDMI_COLORSPACE_RGB); + if (!clock) + return MODE_ERROR; + + return hdmi_clock_valid(connector, mode, clock); +} +EXPORT_SYMBOL(drm_hdmi_connector_mode_valid); + static int clear_device_infoframe(struct drm_connector *connector, enum hdmi_infoframe_type type) { diff --git a/drivers/gpu/drm/tests/drm_hdmi_state_helper_test.c b/drivers/g= pu/drm/tests/drm_hdmi_state_helper_test.c index 294773342e710dc56772f839c2db9c2e487bbc1e..67c3e882592b00d803d8cba5d18= 3aa90339a16b4 100644 --- a/drivers/gpu/drm/tests/drm_hdmi_state_helper_test.c +++ b/drivers/gpu/drm/tests/drm_hdmi_state_helper_test.c @@ -43,10 +43,14 @@ struct drm_atomic_helper_connector_hdmi_priv { static struct drm_display_mode *find_preferred_mode(struct drm_connector *= connector) { struct drm_device *drm =3D connector->dev; - struct drm_display_mode *mode, *preferred; + struct drm_display_mode *mode, *preferred =3D NULL; =20 mutex_lock(&drm->mode_config.mutex); - preferred =3D list_first_entry(&connector->modes, struct drm_display_mode= , head); + + /* Handle the case when all modes were rejected by mode_valid() */ + if (!list_empty(&connector->modes)) + preferred =3D list_first_entry(&connector->modes, struct drm_display_mod= e, head); + list_for_each_entry(mode, &connector->modes, head) if (mode->type & DRM_MODE_TYPE_PREFERRED) preferred =3D mode; @@ -125,6 +129,18 @@ static const struct drm_connector_hdmi_funcs reject_co= nnector_hdmi_funcs =3D { .tmds_char_rate_valid =3D reject_connector_tmds_char_rate_valid, }; =20 +static enum drm_mode_status +reject_100MHz_connector_tmds_char_rate_valid(const struct drm_connector *c= onnector, + const struct drm_display_mode *mode, + unsigned long long tmds_rate) +{ + return (tmds_rate > 100ULL * 1000 * 1000) ? MODE_BAD : MODE_OK; +} + +static const struct drm_connector_hdmi_funcs reject_100_MHz_connector_hdmi= _funcs =3D { + .tmds_char_rate_valid =3D reject_100MHz_connector_tmds_char_rate_valid, +}; + static int dummy_connector_get_modes(struct drm_connector *connector) { struct drm_atomic_helper_connector_hdmi_priv *priv =3D @@ -147,6 +163,33 @@ static int dummy_connector_get_modes(struct drm_connec= tor *connector) static const struct drm_connector_helper_funcs dummy_connector_helper_func= s =3D { .atomic_check =3D drm_atomic_helper_connector_hdmi_check, .get_modes =3D dummy_connector_get_modes, + .mode_valid =3D drm_hdmi_connector_mode_valid, +}; + +static int dummy_connector_get_modes_100MHz_max_clock(struct drm_connector= *connector) +{ + struct drm_atomic_helper_connector_hdmi_priv *priv =3D + connector_to_priv(connector); + const struct drm_edid *edid; + unsigned int num_modes; + + edid =3D drm_edid_alloc(priv->current_edid, priv->current_edid_len); + if (!edid) + return -EINVAL; + + drm_edid_connector_update(connector, edid); + connector->display_info.max_tmds_clock =3D 100 * 1000; + num_modes =3D drm_edid_connector_add_modes(connector); + + drm_edid_free(edid); + + return num_modes; +} + +static const struct drm_connector_helper_funcs dummy_connector_helper_func= s_max_tmds_clock =3D { + .atomic_check =3D drm_atomic_helper_connector_hdmi_check, + .get_modes =3D dummy_connector_get_modes_100MHz_max_clock, + .mode_valid =3D drm_hdmi_connector_mode_valid, }; =20 static void dummy_hdmi_connector_reset(struct drm_connector *connector) @@ -1734,9 +1777,143 @@ static struct kunit_suite drm_atomic_helper_connect= or_hdmi_reset_test_suite =3D { .test_cases =3D drm_atomic_helper_connector_hdmi_reset_tests, }; =20 +static void drm_test_check_mode_valid(struct kunit *test) +{ + struct drm_atomic_helper_connector_hdmi_priv *priv; + struct drm_connector *conn; + struct drm_display_mode *preferred; + + priv =3D drm_atomic_helper_connector_hdmi_init(test, + BIT(HDMI_COLORSPACE_RGB), + 8); + KUNIT_ASSERT_NOT_NULL(test, priv); + + conn =3D &priv->connector; + preferred =3D find_preferred_mode(conn); + KUNIT_ASSERT_NOT_NULL(test, preferred); + + KUNIT_EXPECT_EQ(test, preferred->hdisplay, 1920); + KUNIT_EXPECT_EQ(test, preferred->vdisplay, 1080); + KUNIT_EXPECT_EQ(test, preferred->clock, 148500); +} + +/* + * Verify that tmds_char_rate_valid() can reject all modes. + */ +static void drm_test_check_mode_valid_reject(struct kunit *test) +{ + struct drm_atomic_helper_connector_hdmi_priv *priv; + struct drm_connector *conn; + struct drm_display_mode *preferred; + struct drm_device *drm; + int ret; + + priv =3D drm_atomic_helper_connector_hdmi_init(test, + BIT(HDMI_COLORSPACE_RGB), + 8); + KUNIT_ASSERT_NOT_NULL(test, priv); + + conn =3D &priv->connector; + + /* You shouldn't be doing that at home. */ + conn->hdmi.funcs =3D &reject_connector_hdmi_funcs; + + priv->current_edid =3D test_edid_hdmi_1080p_rgb_max_200mhz; + priv->current_edid_len =3D ARRAY_SIZE(test_edid_hdmi_1080p_rgb_max_200mhz= ); + + drm =3D &priv->drm; + + mutex_lock(&drm->mode_config.mutex); + ret =3D conn->funcs->fill_modes(conn, 4096, 4096); + mutex_unlock(&drm->mode_config.mutex); + KUNIT_ASSERT_EQ(test, ret, 0); + + preferred =3D find_preferred_mode(conn); + KUNIT_ASSERT_NULL(test, preferred); +} + +/* + * Verify that tmds_char_rate_valid() can correctly filter out high-rate m= odes, + * but allows small modes. + */ +static void drm_test_check_mode_valid_reject_rate(struct kunit *test) +{ + struct drm_atomic_helper_connector_hdmi_priv *priv; + struct drm_connector *conn; + struct drm_display_mode *preferred; + int ret; + + priv =3D drm_atomic_helper_connector_hdmi_init(test, + BIT(HDMI_COLORSPACE_RGB), + 8); + KUNIT_ASSERT_NOT_NULL(test, priv); + + conn =3D &priv->connector; + + /* You shouldn't be doing that at home. */ + conn->hdmi.funcs =3D &reject_100_MHz_connector_hdmi_funcs; + + ret =3D set_connector_edid(test, conn, + test_edid_hdmi_1080p_rgb_max_200mhz, + ARRAY_SIZE(test_edid_hdmi_1080p_rgb_max_200mhz)); + KUNIT_ASSERT_EQ(test, ret, 0); + + preferred =3D find_preferred_mode(conn); + KUNIT_ASSERT_NOT_NULL(test, preferred); + KUNIT_EXPECT_EQ(test, preferred->hdisplay, 640); + KUNIT_EXPECT_EQ(test, preferred->vdisplay, 480); + KUNIT_EXPECT_EQ(test, preferred->clock, 25200); +} + +/* + * Verify that the framework filters modes based on info.max_tmds_clock, w= hile + * allowing small resolution modes. + */ +static void drm_test_check_mode_valid_reject_max_clock(struct kunit *test) +{ + struct drm_atomic_helper_connector_hdmi_priv *priv; + struct drm_connector *conn; + struct drm_display_mode *preferred; + int ret; + + priv =3D drm_atomic_helper_connector_hdmi_init(test, + BIT(HDMI_COLORSPACE_RGB), + 8); + KUNIT_ASSERT_NOT_NULL(test, priv); + + conn =3D &priv->connector; + + drm_connector_helper_add(conn, &dummy_connector_helper_funcs_max_tmds_clo= ck); + + ret =3D set_connector_edid(test, conn, + test_edid_hdmi_1080p_rgb_max_200mhz, + ARRAY_SIZE(test_edid_hdmi_1080p_rgb_max_200mhz)); + KUNIT_ASSERT_EQ(test, ret, 0); + + preferred =3D find_preferred_mode(conn); + KUNIT_ASSERT_NOT_NULL(test, preferred); + KUNIT_EXPECT_EQ(test, preferred->hdisplay, 640); + KUNIT_EXPECT_EQ(test, preferred->vdisplay, 480); + KUNIT_EXPECT_EQ(test, preferred->clock, 25200); +} + +static struct kunit_case drm_atomic_helper_connector_hdmi_mode_valid_tests= [] =3D { + KUNIT_CASE(drm_test_check_mode_valid), + KUNIT_CASE(drm_test_check_mode_valid_reject), + KUNIT_CASE(drm_test_check_mode_valid_reject_rate), + KUNIT_CASE(drm_test_check_mode_valid_reject_max_clock), + { } +}; + +static struct kunit_suite drm_atomic_helper_connector_hdmi_mode_valid_test= _suite =3D { + .name =3D "drm_atomic_helper_connector_hdmi_mode_valid", + .test_cases =3D drm_atomic_helper_connector_hdmi_mode_valid_tests, +}; + kunit_test_suites( &drm_atomic_helper_connector_hdmi_check_test_suite, &drm_atomic_helper_connector_hdmi_reset_test_suite, + &drm_atomic_helper_connector_hdmi_mode_valid_test_suite, ); =20 MODULE_AUTHOR("Maxime Ripard "); diff --git a/include/drm/display/drm_hdmi_state_helper.h b/include/drm/disp= lay/drm_hdmi_state_helper.h index 2d45fcfa461985065a5e5ad67eddc0b1c556d526..d6d65da6d8f9ee46de33114cce2= d6fbe6098a862 100644 --- a/include/drm/display/drm_hdmi_state_helper.h +++ b/include/drm/display/drm_hdmi_state_helper.h @@ -20,4 +20,8 @@ int drm_atomic_helper_connector_hdmi_clear_audio_infofram= e(struct drm_connector int drm_atomic_helper_connector_hdmi_update_infoframes(struct drm_connecto= r *connector, struct drm_atomic_state *state); =20 +enum drm_mode_status +drm_hdmi_connector_mode_valid(struct drm_connector *connector, + struct drm_display_mode *mode); + #endif // DRM_HDMI_STATE_HELPER_H_ --=20 2.39.5