From patchwork Mon Aug 14 13:56:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Ripard X-Patchwork-Id: 135464 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp2774601vqi; Mon, 14 Aug 2023 07:16:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH27WF+1jFiD92iU/co3j03ojNLzjX8h6VI6O/0YbBxseaWyRLKOaClfOpXSpLCg3Q9fxpA X-Received: by 2002:a05:6a21:1f30:b0:137:9622:17d0 with SMTP id ry48-20020a056a211f3000b00137962217d0mr7873960pzb.58.1692022563225; Mon, 14 Aug 2023 07:16:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692022563; cv=none; d=google.com; s=arc-20160816; b=nEW9Emo502/8g6iwpdabrctqzIxsPBmEWXVb/fvIFQWn4o9KmxsD0liT3NgWKz5vzk 86PQe8cJH85Ts5bmdPzVeoFRm4e/m76GigSgAgK+Q1cvCPXMLKIiI+jeJYA+NjqsWyhv 3PsgLTUYb8YMmHyJTy4evHOtNvuv9l4wOwQl9KQivzwXolWPTnAXguLsZx2irsIqaqy3 YyqqVh5rfHrQJv1JOQmz+BfKWX3R/JP4iTLvGVIor5BsqSkB9NzDOcOtennWuKyvp9Wi yj7uViaNISoN/YbXyrFsaqLw/DkJHkKB1p6ndGTZrbq7Ugf3I06apidx1/AEmqQuhCGE iFqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=dBegZVTBQumT3pbeTF2tTv25/2xmuIyMFF2r/Ob2j7M=; fh=XsS7fCnS6jyV48OHq4AWazZQaFUKJmjcq7fZaQkmGOY=; b=pUPWRp+xGql85VkY77A/7aMgNDqtfW8+rfPZJDyPkS/RHBR0RdzOkt1PG+DnuPiOL+ cKaDFgxBoy7cqzcOWGl+pc7NI+OJmytoEvnZCJBKM7oN/RrTu4Y6UMpYchWp5gq+H2rq Tx1ri/C52egJ9ZTTE0+evH6pM+JSD5kc12HphCzCggsvhXSyTxiyyJBa/IicfBF4mt1l k4StA5XJdz/TNy+M6ETrz3bOoAtHpvvk4kxpVC2E9U5YvWPHgNiZOxHPt08DiuwlQgk5 xBOHaxW1b9xge+TQGNYJOH9BVSf7VCxKAI+f8qTNqMK+CnGBPxPDvZgnxrs2/qNz0g2y rHBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UrLEtfa3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v135-20020a63618d000000b005641fc2684bsi8242334pgb.527.2023.08.14.07.15.49; Mon, 14 Aug 2023 07:16:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=UrLEtfa3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231775AbjHNN50 (ORCPT + 99 others); Mon, 14 Aug 2023 09:57:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231691AbjHNN4z (ORCPT ); Mon, 14 Aug 2023 09:56:55 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 825F710D5 for ; Mon, 14 Aug 2023 06:56:52 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 11CA961796 for ; Mon, 14 Aug 2023 13:56:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24ECCC433C7; Mon, 14 Aug 2023 13:56:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692021411; bh=5FwIWakECN1Fb8VdEDsYOpvXNTYQGx7JKvYanIQ8ciM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=UrLEtfa3ndkkl9o39vjy3XqAnLqKAgiL0Z0S+hr1T0xLP5/2txg0cu+Fuy7UkyNt6 71qiEYLzFXGj6B8QgXaX6k4c9SFODQSQ5mqHacMqqu2xPhIk6UYZbhMXynCeFqCRR5 kK92mVoDjyZjtlvzhY+mVy0h2biK3c+UCLUcurB5NUQ0dmbi7ADejDfMXfTwoSwq/K URRRi6BPFRGcqHL9FcNOijWl1A0BkhZbcghXfQHPZL2fKpwxX5w+1JOrsanDqlWZth 0A9ldENcKAkG3O44gGgqtAOnOvAtwTybFELOUVWoZwVYFc4X8nhHafBth9O3zEEmmM NW/5cAVz6ZFOg== From: Maxime Ripard Date: Mon, 14 Aug 2023 15:56:21 +0200 Subject: [PATCH RFC 09/13] drm/connector: hdmi: Compute bpc and format automatically MIME-Version: 1.0 Message-Id: <20230814-kms-hdmi-connector-state-v1-9-048054df3654@kernel.org> References: <20230814-kms-hdmi-connector-state-v1-0-048054df3654@kernel.org> In-Reply-To: <20230814-kms-hdmi-connector-state-v1-0-048054df3654@kernel.org> To: Maarten Lankhorst , Thomas Zimmermann , David Airlie , Daniel Vetter , Emma Anholt Cc: Hans Verkuil , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, Maxime Ripard X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=7067; i=mripard@kernel.org; h=from:subject:message-id; bh=5FwIWakECN1Fb8VdEDsYOpvXNTYQGx7JKvYanIQ8ciM=; b=owGbwMvMwCX2+D1vfrpE4FHG02pJDCm3jNrerOP5daLYesOKX95d3t8lX57fc3rxlsLye/n6O ZoCfE/XdZSyMIhxMciKKbLECJsviTs163UnG988mDmsTCBDGLg4BWAiF5IY/qkkaFXs27dnUs6V lW4b0yd6rFKZo/9CdHf8xinH247cnbaLkWHqpjc5Be+in68xcX+2LEZw3WfDio2nUzLOq+Q8coi 7a8sFAA== X-Developer-Key: i=mripard@kernel.org; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774214251048244962 X-GMAIL-MSGID: 1774214251048244962 Now that we have all the infrastructure needed, we can add some code that will, for a given connector state and mode, compute the best output format and bpc. The algorithm is the same one than the one already found in i915 and vc4. Signed-off-by: Maxime Ripard --- drivers/gpu/drm/drm_hdmi_connector.c | 182 ++++++++++++++++++++++++++++++++++- 1 file changed, 177 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/drm_hdmi_connector.c b/drivers/gpu/drm/drm_hdmi_connector.c index d94ceeb6a8ef..22c49906dfb5 100644 --- a/drivers/gpu/drm/drm_hdmi_connector.c +++ b/drivers/gpu/drm/drm_hdmi_connector.c @@ -258,6 +258,91 @@ connector_state_get_adjusted_mode(const struct drm_connector_state *state) return &crtc_state->adjusted_mode; } +static bool +sink_supports_format_bpc(const struct drm_hdmi_connector *hdmi_connector, + const struct drm_display_info *info, + const struct drm_display_mode *mode, + unsigned int format, unsigned int bpc) +{ + const struct drm_connector *connector = &hdmi_connector->base; + struct drm_device *dev = connector->dev; + u8 vic = drm_match_cea_mode(mode); + + if (vic == 1 && bpc != 8) { + drm_dbg(dev, "VIC1 requires a bpc of 8, got %u\n", bpc); + return false; + } + + if (!info->is_hdmi && + (format != HDMI_COLORSPACE_RGB || bpc != 8)) { + drm_dbg(dev, "DVI Monitors require an RGB output at 8 bpc\n"); + return false; + } + + switch (format) { + case HDMI_COLORSPACE_RGB: + drm_dbg(dev, "RGB Format, checking the constraints.\n"); + + if (!(info->color_formats & DRM_COLOR_FORMAT_RGB444)) + return false; + + if (bpc == 10 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_30)) { + drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n"); + return false; + } + + if (bpc == 12 && !(info->edid_hdmi_rgb444_dc_modes & DRM_EDID_HDMI_DC_36)) { + drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n"); + return false; + } + + drm_dbg(dev, "RGB format supported in that configuration.\n"); + + return true; + + case HDMI_COLORSPACE_YUV422: + drm_dbg(dev, "YUV422 format, checking the constraints.\n"); + + if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR422)) { + drm_dbg(dev, "Sink doesn't support YUV422.\n"); + return false; + } + + if (bpc != 12) { + drm_dbg(dev, "YUV422 only supports 12 bpc.\n"); + return false; + } + + drm_dbg(dev, "YUV422 format supported in that configuration.\n"); + + return true; + + case HDMI_COLORSPACE_YUV444: + drm_dbg(dev, "YUV444 format, checking the constraints.\n"); + + if (!(info->color_formats & DRM_COLOR_FORMAT_YCBCR444)) { + drm_dbg(dev, "Sink doesn't support YUV444.\n"); + return false; + } + + if (bpc == 10 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_30)) { + drm_dbg(dev, "10 BPC but sink doesn't support Deep Color 30.\n"); + return false; + } + + if (bpc == 12 && !(info->edid_hdmi_ycbcr444_dc_modes & DRM_EDID_HDMI_DC_36)) { + drm_dbg(dev, "12 BPC but sink doesn't support Deep Color 36.\n"); + return false; + } + + drm_dbg(dev, "YUV444 format supported in that configuration.\n"); + + return true; + } + + return false; +} + static enum drm_mode_status drm_hdmi_connector_clock_valid(const struct drm_hdmi_connector *hdmi_connector, const struct drm_display_mode *mode, @@ -325,6 +410,95 @@ drm_hdmi_connector_compute_clock(const struct drm_hdmi_connector *hdmi_connector return 0; } +static bool +drm_hdmi_connector_try_format_bpc(const struct drm_hdmi_connector *hdmi_connector, + struct drm_hdmi_connector_state *hdmi_state, + const struct drm_display_mode *mode, + unsigned int bpc, enum hdmi_colorspace fmt) +{ + const struct drm_connector *connector = &hdmi_connector->base; + const struct drm_display_info *info = &connector->display_info; + struct drm_device *dev = connector->dev; + int ret; + + drm_dbg(dev, "Trying output format %s\n", + drm_hdmi_connector_get_output_format_name(fmt)); + + if (!sink_supports_format_bpc(hdmi_connector, info, mode, fmt, bpc)) + return false; + + ret = drm_hdmi_connector_compute_clock(hdmi_connector, hdmi_state, + mode, bpc, fmt); + if (ret) + return false; + + return true; +} + +static int +drm_hdmi_connector_compute_format(const struct drm_hdmi_connector *hdmi_connector, + struct drm_hdmi_connector_state *hdmi_state, + const struct drm_display_mode *mode, + unsigned int bpc) +{ + const struct drm_connector *connector = &hdmi_connector->base; + struct drm_device *dev = connector->dev; + + if (drm_hdmi_connector_try_format_bpc(hdmi_connector, hdmi_state, + mode, bpc, HDMI_COLORSPACE_RGB)) { + hdmi_state->output_format = HDMI_COLORSPACE_RGB; + return 0; + } + + if (drm_hdmi_connector_try_format_bpc(hdmi_connector, hdmi_state, + mode, bpc, HDMI_COLORSPACE_YUV422)) { + hdmi_state->output_format = HDMI_COLORSPACE_YUV422; + return 0; + } + + drm_dbg(dev, "Failed. No Format Supported for that bpc count.\n"); + + return -EINVAL; +} + +static int +drm_hdmi_connector_compute_config(const struct drm_hdmi_connector *hdmi_connector, + struct drm_hdmi_connector_state *hdmi_state, + const struct drm_display_mode *mode) +{ + const struct drm_connector *connector = &hdmi_connector->base; + struct drm_connector_state *conn_state = &hdmi_state->base; + struct drm_device *dev = connector->dev; + unsigned int max_bpc = clamp_t(unsigned int, + conn_state->max_bpc, + 8, hdmi_connector->max_bpc); + unsigned int bpc; + int ret; + + for (bpc = max_bpc; bpc >= 8; bpc -= 2) { + drm_dbg(dev, "Trying with a %d bpc output\n", bpc); + + ret = drm_hdmi_connector_compute_format(hdmi_connector, + hdmi_state, + mode, bpc); + if (ret) + continue; + + hdmi_state->output_bpc = bpc; + + drm_dbg(dev, + "Mode %ux%u @ %uHz: Found configuration: bpc: %u, fmt: %s, clock: %llu\n", + mode->hdisplay, mode->vdisplay, drm_mode_vrefresh(mode), + hdmi_state->output_bpc, + drm_hdmi_connector_get_output_format_name(hdmi_state->output_format), + hdmi_state->tmds_char_rate); + + return 0; + } + + return -EINVAL; +} + /** * drm_atomic_helper_hdmi_connector_atomic_check() - Helper to check HDMI connector atomic state * @connector: the parent connector this state refers to @@ -357,11 +531,9 @@ int drm_atomic_helper_hdmi_connector_atomic_check(struct drm_connector *connecto connector_state_get_adjusted_mode(new_state); int ret; - ret = drm_hdmi_connector_compute_clock(hdmi_connector, new_hdmi_state, - mode, - new_hdmi_state->output_bpc, - new_hdmi_state->output_format); - if (!ret) + ret = drm_hdmi_connector_compute_config(hdmi_connector, new_hdmi_state, + mode); + if (ret) return ret; if (old_hdmi_state->broadcast_rgb != new_hdmi_state->broadcast_rgb ||