Received: by 2002:ab2:4a89:0:b0:1f4:a8b6:6e69 with SMTP id w9csp76454lqj; Wed, 10 Apr 2024 04:43:06 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW5JSFE2LcgsOKA47wF2ZtC0paz5u7f7+z+tsHIsPOCsV906NEx1qCEVmKWBjIK7ko25KEQBmgaoROa0RCTFo3njmQnaWIrXNNn3McOWA== X-Google-Smtp-Source: AGHT+IGfVr3QY9nMjqAGIaMXhw/XHRYoRpvZTHDcwo4HCr5u6qkTXmg/mODdmhANlOKbPKfPYE8q X-Received: by 2002:a05:620a:13fa:b0:78d:6318:7fe3 with SMTP id h26-20020a05620a13fa00b0078d63187fe3mr2633052qkl.8.1712749386725; Wed, 10 Apr 2024 04:43:06 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712749386; cv=pass; d=google.com; s=arc-20160816; b=mo9t5untIpqX7fvMMkcQqypexWt54Rh2jNwtloNVxnGp4mdrcW6tEP8rnwVlUX0N1M uLWxlKUsfm8ibqzGdoH+JMCZORvL7z5iQ6vkQgFX9klqoeptwVNnRmRF5Vj4Z9mL77Xs 0UqmElbQ4vNds6oHiPvmiCRr4cyRk0Zd5I9ZAv992I9cTgv1teoqRAndW3XLPQL8e94z b8QAqD+bscj+20WIyg0269rj0Sy+1neDD4wYcFNilwSajWcUVcwDD6XidrXLP+nqFExJ fdJAsa/YbU9tw2MVgQRbGApmk4zkZHRE/tXMtuYjdTjseSw0xw2H9usUjwUfWoHbdPRc 2sCA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=Orwcw5Ia2AB+nDmlojgten3vjJ8b47E/GgQ/goc3VCs=; fh=q58blXoWGtV7ql1FSFt1zn7YywXb+4/ZcwxYN2JB1ww=; b=Lcit4WLltCzAKcw7Z8LqPJYcjm8Mzj00KSBCNu0IVUA7pre2MI8ypVdsRsD3H1Uvib V3XrdcPnMg1gdrAtaF/XkajOJsh8/pTgiXsAm9IF44e/PLUX99jTTWi8nmfbuUC3W7Kl ak67e8kfxyRjpItkvL6d+ak6ePnDRHbqbi8C4ZpWuWNPk2HDJzvOrmBT7Y2SHW23yojQ xfY6YJCcHqNigiOhLCI9kOLZ/xDq69IQrJ6c0zTPau/GrOpLLq9cKr/EebMJhq6hV+16 1U3x7gpqvS+Oj191TDHTDsdR6bw7a2E9qfNI3m9XkZyP4oZJL7GnINh8F0cSUEtm7G2y JJsQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=Irffg5GU; arc=pass (i=1 spf=pass spfdomain=ti.com dkim=pass dkdomain=ti.com dmarc=pass fromdomain=ti.com); spf=pass (google.com: domain of linux-kernel+bounces-138425-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-138425-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id ou35-20020a05620a622300b0078a0b02960fsi12008140qkn.232.2024.04.10.04.43.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 04:43:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-138425-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=Irffg5GU; arc=pass (i=1 spf=pass spfdomain=ti.com dkim=pass dkdomain=ti.com dmarc=pass fromdomain=ti.com); spf=pass (google.com: domain of linux-kernel+bounces-138425-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-138425-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4FE231C22B54 for ; Wed, 10 Apr 2024 11:43:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C9A2F15AAAB; Wed, 10 Apr 2024 11:42:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b="Irffg5GU" Received: from lelv0142.ext.ti.com (lelv0142.ext.ti.com [198.47.23.249]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E41815957C for ; Wed, 10 Apr 2024 11:42:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.47.23.249 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712749378; cv=none; b=gMHS8OsLgunr0DCI9ftEyeg//7OI1IzzPNH1RNXcZz/XFuA30WrG22ZqlUFe2+NdkIysld82367xXcKMuCmci0pX0l6yHdnEYPhhZ3TkbW5LIedSqHbqd+eLStK7mTTfwmFURBID+VvNhIYx4HxALd841EISmBJy8qnGSpvRXWA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712749378; c=relaxed/simple; bh=U6JdwqaccJyIBc8d1EGruAonoz/SMcqtAoxEhtehO7g=; h=Message-ID:Date:MIME-Version:Subject:To:CC:References:From: In-Reply-To:Content-Type; b=a1uM/B1nCFgR69bNJi2jCdn9/xqCcVMuqXrKOQ1tpo5tFNSNRr1WKrScsTN3YwQXWyMHapCRCZEIMKDwHhKYpap7CzxZxB+Xc+XUy+IxKmmfT47h+AHkSM0zeFsUxZ0gmF/pkqmbaGyGg0rdsmjzUQBV/RA9ZcgFTMIpg7UA61o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com; spf=pass smtp.mailfrom=ti.com; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b=Irffg5GU; arc=none smtp.client-ip=198.47.23.249 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ti.com Received: from lelv0266.itg.ti.com ([10.180.67.225]) by lelv0142.ext.ti.com (8.15.2/8.15.2) with ESMTP id 43ABgb77088904; Wed, 10 Apr 2024 06:42:37 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1712749357; bh=Orwcw5Ia2AB+nDmlojgten3vjJ8b47E/GgQ/goc3VCs=; h=Date:Subject:To:CC:References:From:In-Reply-To; b=Irffg5GUmt1x5SB5XAssmEXglhk4JTfwjhwxNUahC4Jt/FU0S0ptXlDoVgqqoVWnN sYpSHpHIkFZnTHjliZuxwo8ZrCoTqBUTTRTacWGNCzCou7fn49W8jYh0V2rxd/HF3t bgh5SJ6O5Ig6fIGTo4U7EtpSbjPlDQ1gRfHQnnMQ= Received: from DFLE103.ent.ti.com (dfle103.ent.ti.com [10.64.6.24]) by lelv0266.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 43ABgbCK022407 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 10 Apr 2024 06:42:37 -0500 Received: from flwvowa02.ent.ti.com (10.64.41.53) by DFLE103.ent.ti.com (10.64.6.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Wed, 10 Apr 2024 06:42:36 -0500 Received: from DFLE109.ent.ti.com (10.64.6.30) by flwvowa02.ent.ti.com (10.64.41.53) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.2507.34; Wed, 10 Apr 2024 06:42:36 -0500 Received: from lelvsmtp5.itg.ti.com (10.180.75.250) by DFLE109.ent.ti.com (10.64.6.30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23 via Frontend Transport; Wed, 10 Apr 2024 06:42:36 -0500 Received: from [172.24.227.252] (jayesh-hp-probook-440-g8-notebook-pc.dhcp.ti.com [172.24.227.252]) by lelvsmtp5.itg.ti.com (8.15.2/8.15.2) with ESMTP id 43ABgVU9078615; Wed, 10 Apr 2024 06:42:32 -0500 Message-ID: <279a1467-9ba4-449c-9076-9b2acef9336c@ti.com> Date: Wed, 10 Apr 2024 17:12:30 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] drm/bridge: ti-sn65dsi86: Fix ti_sn_bridge_set_dsi_rate function To: Doug Anderson CC: , , , , , , , , , , , , , , References: <20240408073623.186489-1-j-choudhary@ti.com> Content-Language: en-US From: Jayesh Choudhary In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Hello Doug, Thanks for the review. On 08/04/24 14:33, Doug Anderson wrote: > Hi, > > On Mon, Apr 8, 2024 at 12:36 AM Jayesh Choudhary wrote: >> >> Due to integer calculations, the rounding off can cause errors in the final >> value propagated in the registers. >> Considering the example of 1080p (very common resolution), the mode->clock >> is 148500, dsi->lanes = 4, and bpp = 24, with the previous logic, the DSI >> clock frequency would come as 444 when we are expecting the value 445.5 >> which would reflect in SN_DSIA_CLK_FREQ_REG. >> So move the division to be the last operation where rounding off will not >> impact the register value. > > Given that this driver is used on a whole pile of shipping Chromebooks > and those devices have been working just fine (with 1080p resolution) > for years, I'm curious how you noticed this. Was it actually causing > real problems for you, or did you notice it just from code inspection? > You should include this information in the commit message. I am trying to add display support for TI SoC which uses this particular bridge. While debugging, I was trying to get all the register value in sync with the datasheet and it was then that I observed this issue while inspecting the code. Maybe Chromebooks are using different set of parameters which does not expose this issue. Since parameters for my display (mentioned in commit message) yields the frequency at the border, I saw this issue. My debug is still ongoing but since the value is not in sync with the documentation, I sent out this patch. > > I'm travelling for the next two weeks so I can't actually check on a > device to see if your patch makes any difference on hardware I have, > but I'd presume that things were working "well enough" with the old > value and they'll still work with the new value? > > Yes, ideally they should still work well with this change. >> Also according to the SN65DSI86 datasheet[0], the minimum value for that >> reg is 0x08 (inclusive) and the maximum value is 0x97 (exclusive). So add >> check for that. > > Maybe the range checking should be a separate patch? Check should be done before propagating the register value so I added it in the same function and hence in the same patch. > > >> [0]: >> >> Fixes: ca1b885cbe9e ("drm/bridge: ti-sn65dsi86: Split the setting of the dp and dsi rates") > > Are you sure that's the commit you're fixing? In the commit text of > that commit I wrote that it was supposed to "have zero functional > change". Looking back at the change I still believe it had zero > functional change. The rounding error looks like it predates the > patch. > > As far as I can tell the rounding error has been there since commit > a095f15c00e2 ("drm/bridge: add support for sn65dsi86 bridge driver"). > Yes. Now I see that it fixes the initial support patch. I will fix that. > >> Signed-off-by: Jayesh Choudhary > > It's great to see a TI engineer contributing to this driver! Awesome! > > >> --- >> drivers/gpu/drm/bridge/ti-sn65dsi86.c | 48 +++++++++++++++++++++------ >> 1 file changed, 37 insertions(+), 11 deletions(-) >> >> diff --git a/drivers/gpu/drm/bridge/ti-sn65dsi86.c b/drivers/gpu/drm/bridge/ti-sn65dsi86.c >> index 84698a0b27a8..f9cf6b14d85e 100644 >> --- a/drivers/gpu/drm/bridge/ti-sn65dsi86.c >> +++ b/drivers/gpu/drm/bridge/ti-sn65dsi86.c >> @@ -111,7 +111,14 @@ >> #define AUX_IRQ_STATUS_AUX_SHORT BIT(5) >> #define AUX_IRQ_STATUS_NAT_I2C_FAIL BIT(6) >> >> -#define MIN_DSI_CLK_FREQ_MHZ 40 >> +/* >> + * NOTE: DSI clock frequency range: [40MHz,755MHz) >> + * DSI clock frequency range is in 5-MHz increments >> + * So minimum frequency 40MHz translates to 0x08 >> + * And maximum frequency 755MHz translates to 0x97 >> + */ >> +#define MIN_DSI_CLK_RANGE 0x8 >> +#define MAX_DSI_CLK_RANGE 0x97 > > It's a little weird to call this min/max and have one be inclusive and > one be exclusive. Be consistent and say that this is the minimum legal > value and the maximum legal value. I think that means the MAX should > be 0x96. The comment above does specify the inclusive/exclusive behavior. Since a value corresponds to 5MHz range, associating the value with the range makes more sense if I keep it 0x97 (0x97 * 5 -> 755MHz) 0x96 corresponds to the range of [750Mz,755MHz). If this argument does not make sense, I can change it to 0x96 and handle it with the inequalities in the function call. > > >> /* fudge factor required to account for 8b/10b encoding */ >> #define DP_CLK_FUDGE_NUM 10 >> @@ -820,22 +827,37 @@ static void ti_sn_bridge_atomic_disable(struct drm_bridge *bridge, >> regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE, 0); >> } >> >> -static void ti_sn_bridge_set_dsi_rate(struct ti_sn65dsi86 *pdata) >> +static int ti_sn_bridge_set_dsi_rate(struct ti_sn65dsi86 *pdata) >> { >> - unsigned int bit_rate_mhz, clk_freq_mhz; >> + unsigned int bit_rate_khz; >> unsigned int val; >> struct drm_display_mode *mode = >> &pdata->bridge.encoder->crtc->state->adjusted_mode; >> >> - /* set DSIA clk frequency */ >> - bit_rate_mhz = (mode->clock / 1000) * >> - mipi_dsi_pixel_format_to_bpp(pdata->dsi->format); >> - clk_freq_mhz = bit_rate_mhz / (pdata->dsi->lanes * 2); >> + /* >> + * Set DSIA clk frequency >> + * Maximum supported value of bit_rate_khz turns out to be >> + * 6040000 which can be put in 32-bit variable so no overflow >> + * possible in this calculation. > > The way you've written this comment makes me worried. You're saying > that the only supported result of the math has to fit in 32-bits so > we're OK. ...and then _after_ you do the math you check to see if the > clock rate is within the supported range. It makes me feel like you > could still overflow. I did use reverse calculation for the value that I used in comments. xD > > I think your comment here should say something like: > > The maximum value returned by mipi_dsi_pixel_format_to_bpp() is 24. > That means that as long as "mode->clock" is less than 178,956,971 kHz > then the calculation can't overflow and can fit in 32-bits. > > If you wanted to be really good you could even put a check earlier in > the function to make sure that mode->clock wasn't something totally > crazy (could confirm it's < 100GHz maybe?) so you absolutely knew it > couldn't overflow. Okay. This makes sense. Will take this into account for v2. > >> + */ >> + bit_rate_khz = mode->clock * >> + mipi_dsi_pixel_format_to_bpp(pdata->dsi->format); >> + >> + /* >> + * For each increment in val, frequency increases by 5MHz >> + * and the factor of 1000 comes from kHz to MHz conversion >> + */ >> + val = (bit_rate_khz / (pdata->dsi->lanes * 2 * 1000 * 5)) & 0xFF; >> + >> + if (val >= MAX_DSI_CLK_RANGE || val < MIN_DSI_CLK_RANGE) { >> + drm_err(pdata->bridge.dev, >> + "DSI clock frequency not in the supported range\n"); >> + return -EINVAL; >> + } > > Shouldn't the above be in atomic_check()? There's a reason why > atomic_enable() can't return error codes. Oops. I will handle it how we are handling errors in case of link_training: https://github.com/torvalds/linux/blob/master/drivers/gpu/drm/bridge/ti-sn65dsi86.c#L1152 That should be okay I guess? Warm Regards, Jayesh > > -Doug