public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Marco Nenciarini <mnencia@kcore.it>
Cc: linux-media@vger.kernel.org,
	Sakari Ailus <sakari.ailus@linux.intel.com>,
	Bingbu Cao <bingbu.cao@intel.com>,
	Tianshu Qiu <tian.shu.qiu@intel.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	stable@vger.kernel.org
Subject: Re: [PATCH v3] media: intel/ipu6: Improve DWC PHY HSFREQRANGE band selection for overlapping ranges
Date: Thu, 2 Apr 2026 11:54:21 +0300	[thread overview]
Message-ID: <ac4uvf_a-4-fLBPV@ashevche-desk.local> (raw)
In-Reply-To: <20260401162547.1597975-1-mnencia@kcore.it>

On Wed, Apr 01, 2026 at 06:25:47PM +0200, Marco Nenciarini wrote:
> The get_hsfreq_by_mbps() function searches the freqranges[] table
> backward (from highest to lowest index). Because adjacent frequency
> bands overlap, a data rate that falls in the overlap region always
> lands on the higher-indexed band.
> 
> For data rates up to 1500 Mbps (index 42) every band uses
> osc_freq_target 335. Starting at index 43 (1461-1640 Mbps) the
> osc_freq_target drops to 208. A sensor running at 1498 Mbps sits in
> the overlap between index 42 (1414-1588, osc 335) and index 43
> (1461-1640, osc 208). The backward search picks index 43, programming
> the lower osc_freq_target of 208 instead of the optimal 335.
> 
> This causes DDL lock instability and CSI-2 CRC errors on affected
> configurations, such as the OmniVision OV08X40 sensor on Intel Arrow
> Lake platforms (Dell Pro Max 16).
> 
> Rewrite get_hsfreq_by_mbps() to select the optimal band:
> 
> 1. Among bands whose min/max range covers the data rate, prefer
>    the one with the higher osc_freq_target.
> 2. If osc_freq_target is equal, prefer the band whose default_mbps
>    is closest to the requested rate.
> 
> Since the frequency ranges are monotonically increasing, the loop
> exits early once min exceeds the requested rate.
> 
> For 1498 Mbps this now correctly selects index 42 (osc_freq_target
> 335, range 1414-1588) instead of index 43 (osc_freq_target 208,
> range 1461-1640).

Below just a couple of remarks, no need to be addressed, JFYI.

...

>  static u16 get_hsfreq_by_mbps(u32 mbps)
>  {
> -	unsigned int i = DPHY_FREQ_RANGE_NUM;
> -
> -	while (i--) {
> -		if (freqranges[i].default_mbps == mbps ||
> -		    (mbps >= freqranges[i].min && mbps <= freqranges[i].max))
> -			return i;
> +	u16 best = DPHY_FREQ_RANGE_INVALID_INDEX;
> +	unsigned int i;
> +
> +	for (i = 0; i < DPHY_FREQ_RANGE_NUM; i++) {
> +		if (mbps > freqranges[i].max)
> +			continue;
> +
> +		if (mbps < freqranges[i].min)
> +			break;

Wondering if this can use bsearch() algo or any linear ranges.

> +		if (best == DPHY_FREQ_RANGE_INVALID_INDEX ||
> +		    freqranges[i].osc_freq_target >
> +		    freqranges[best].osc_freq_target ||
> +		    (freqranges[i].osc_freq_target ==
> +		     freqranges[best].osc_freq_target &&
> +		     abs((int)mbps - (int)freqranges[i].default_mbps) <
> +		     abs((int)mbps - (int)freqranges[best].default_mbps)))

Note, abs(INT_MIN) is UB, I hope this won't be the case IRL in this code.

> +			best = i;
>  	}
>  
> -	return DPHY_FREQ_RANGE_INVALID_INDEX;
> +	return best;
>  }

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2026-04-02  8:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-01 16:25 [PATCH v3] media: intel/ipu6: Improve DWC PHY HSFREQRANGE band selection for overlapping ranges Marco Nenciarini
2026-04-02  8:54 ` Andy Shevchenko [this message]
2026-04-02  9:35   ` Marco Nenciarini
2026-04-07 19:12 ` Marco Nenciarini
2026-04-08 19:33   ` Sakari Ailus
2026-05-05 12:19     ` Marco Nenciarini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ac4uvf_a-4-fLBPV@ashevche-desk.local \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=bingbu.cao@intel.com \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=mnencia@kcore.it \
    --cc=sakari.ailus@linux.intel.com \
    --cc=stable@vger.kernel.org \
    --cc=tian.shu.qiu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox