From: "Sören Brinkmann" <soren.brinkmann@xilinx.com>
To: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
Cc: Mike Turquette <mturquette@linaro.org>,
Russell King <linux@arm.linux.org.uk>,
Stephen Boyd <sboyd@codeaurora.org>,
linux-pm@vger.kernel.org, Viresh Kumar <viresh.kumar@linaro.org>,
"Rafael J. Wysocki" <rjw@rjwysocki.net>,
Michal Simek <michal.simek@xilinx.com>,
cpufreq@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org
Subject: Re: [RFC PATCH 2/5] clk: Introduce 'clk_round_rate_nearest()'
Date: Fri, 6 Jun 2014 17:44:57 -0700 [thread overview]
Message-ID: <fabca95a-fca0-4a8e-b79a-c7d6437de9f0@BL2FFO11FD042.protection.gbl> (raw)
In-Reply-To: <20140522182040.GB20155@pengutronix.de>
On Thu, 2014-05-22 at 08:20PM +0200, Uwe Kleine-König wrote:
> Hello Sören,
>
> On Thu, May 22, 2014 at 11:03:00AM -0700, Sören Brinkmann wrote:
> > On Wed, 2014-05-21 at 01:33PM -0700, Mike Turquette wrote:
> > > Quoting Uwe Kleine-König (2014-05-21 11:23:08)
> > > > Hello Sören,
> > > >
> > > > On Wed, May 21, 2014 at 08:58:10AM -0700, Sören Brinkmann wrote:
> > > > > On Wed, 2014-05-21 at 09:34AM +0200, Uwe Kleine-König wrote:
> > > > > > On Tue, May 20, 2014 at 02:48:20PM -0700, Sören Brinkmann wrote:
> > > > > > > On Tue, 2014-05-20 at 10:48AM -0700, Stephen Boyd wrote:
> > > > > > > > On 05/20/14 09:01, Sören Brinkmann wrote:
> > > > > > > > >
> > > > > > > > >>>>> +{
> > > > > > > > >>>>> + unsigned long lower, upper, cur, lower_last, upper_last;
> > > > > > > > >>>>> +
> > > > > > > > >>>>> + lower = clk_round_rate(clk, rate);
> > > > > > > > >>>>> + if (lower >= rate)
> > > > > > > > >>>>> + return lower;
> > > > > > > > >>>> Is the >-case worth a warning?
> > > > > > > > >>> No, it's correct behavior. If you request a rate that is way lower than what the
> > > > > > > > >>> clock can generate, returning something larger is perfectly valid, IMHO.
> > > > > > > > >>> Which reveals one problem in this whole discussion. The API does not
> > > > > > > > >>> require clk_round_rate() to round down. It is actually an implementation
> > > > > > > > >>> choice that had been made for clk-divider.
> > > > > > > > >> I'm sure it's more than an implementation choice for clk-divider. But I
> > > > > > > > >> don't find any respective documentation (but I didn't try hard).
> > > > > > > > > A similar discussion - without final conclusion:
> > > > > > > > > https://lkml.org/lkml/2010/7/14/260
> > > > > > > > >
> > > > > > > > >
> > > > > > > >
> > > > > > > > Please call this new API something like clk_find_nearest_rate() or
> > > > > > > > something. clk_round_rate() is supposed to return the rate that will be
> > > > > > > > set if you call clk_set_rate() with the same arguments. It's up to the
> > > > > > > > implementation to decide if that means rounding the rate up or down or
> > > > > > > > to the nearest value.
> > > > > > >
> > > > > > > Sounds good to me. Are there any cases of clocks that round up? I think
> > > > > > > that case would not be handled correctly. But I also don't see a use
> > > > > > > case for such an implementation.
> > > > > > I don't really care which semantic (i.e. round up, round down or round
> > > > > > closest) is picked, but I'd vote that all should pick up the same. I
> > > > > > think the least surprising definition is to choose rounding down and add
> > > > > > the function that is under discussion here to get a nearest match.
> > > > > >
> > > > > > So I suggest:
> > > > > >
> > > > > > - if round_rate is given a rate that is smaller than the
> > > > > > smallest available rate, return 0
> > > > > > - add WARN_ONCE to round_rate and set_rate if they return with a
> > > > > > rate bigger than requested
> > > > >
> > > > > Why do you think 0 is always valid? I think for a clock that can
> > > > > generate 40, 70, 120, clk_round_rate(20) should return 40.
> > > > I didn't say it's a valid value. It just makes the it possible to check
> > > > for clk_round_rate(clk, rate) <= rate.
> > > >
> > > > I grepped a bit around and found da850_round_armrate which implements a
> > > > round_rate callback returning the best match.
> > > > omap1_clk_round_rate_ckctl_arm can return a value < 0.
> > > > s3c2412_roundrate_usbsrc can return values that are bigger than
> > > > requested. (I wonder if that is a bug though.)
> > > >
> > > > > > - change the return values to unsigned long
> > > > >
> > > > > Yep, I agree, this should happen.
> > > > And we're using 0 as error value? e.g. for the case where
> > > > omap1_clk_round_rate_ckctl_arm returns -EIO now?
> > >
> > > No. clk_round_rate returns long for a reason, which is that we can
> > > provide an error code to the caller. From include/linux/clk.h:
> > >
> > > /**
> > > * clk_round_rate - adjust a rate to the exact rate a clock can provide
> > > * @clk: clock source
> > > * @rate: desired clock rate in Hz
> > > *
> > > * Returns rounded clock rate in Hz, or negative errno.
> > > */
> > >
> > > This has the unfortunate side effect that the max value we can return
> > > safely is 2147483647 (~2GHz). So another issue here is converting clock
> > > rates to 64-bit values.
> >
> > So, let's assume
> > - a clock does either of these
> > - round down
> > - round nearest
> > - round up (is there any such case? I don't see a use-case for this)
> > - or return an error
> >
> > I think my latest try handles such cases, with the limitation of
> > for a clock that rounds up, the up-rounded value is found instead of the
> > nearest.
> >
> >
> > static long clk_find_nearest_rate(struct clk *clk, unsigned long rate)
> > {
> > long ret;
> > unsigned long lower, upper;
> >
> > clk_prepare_lock();
> >
> > lower = __clk_round_rate(clk, rate);
> this is CCF specific while I don't see a need for it. (But yes, a
> lock-less clk_find_nearest_rate is of course racy.)
>
> > if (lower >= rate || (long)lower < 0) {
> If you made lower and upper a signed long, you could drop the casting
> here. BTW, why does __clk_round_rate return an unsigned long??
> There seem to be several more type mismatches in that area.
> Maybe we should add a waring if rate is > LONG_MAX?
>
> (And ISTR that the C standard doesn't specify what the result of
> (long)lower is given that lower is of type unsigned long and holding a
> value > LONG_MAX.)
I have another iteration. I kept the locking part, switched to assuming
'long' being used for frequencies and hopefully got rid of constructs
that are not defined by the C standard:
long clk_find_nearest_rate(struct clk *clk, unsigned long rate)
{
long ret, lower, upper;
unsigned long tmp;
clk_prepare_lock();
lower = __clk_round_rate(clk, rate);
if (lower >= rate || lower < 0) {
ret = lower;
goto unlock;
}
tmp = rate + (rate - lower) - 1;
if (tmp > LONG_MAX)
upper = LONG_MAX;
else
upper = tmp;
upper = __clk_round_rate(clk, upper);
if (upper <= lower || upper < 0) {
ret = lower;
goto unlock;
}
lower = rate + 1;
while (lower < upper) {
long rounded, mid;
mid = lower + ((upper - lower) >> 1);
rounded = __clk_round_rate(clk, mid);
if (rounded < lower)
lower = mid + 1;
else
upper = rounded;
}
ret = upper;
unlock:
clk_prepare_unlock();
return ret;
}
Is there a more elegant way to check for the arithmetic overflow?
Thanks,
Sören
next prev parent reply other threads:[~2014-06-07 0:44 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-14 22:30 [RFC PATCH 0/5] Frequency resolution in CCF vs. cpufreq Soren Brinkmann
2014-05-14 22:30 ` [RFC PATCH 1/5] cpufreq: stats: Allow small rounding errors Soren Brinkmann
2014-05-14 22:30 ` [RFC PATCH 2/5] clk: Introduce 'clk_round_rate_nearest()' Soren Brinkmann
2014-05-15 7:38 ` Uwe Kleine-König
2014-05-15 14:10 ` Sören Brinkmann
2014-05-19 0:51 ` Sören Brinkmann
2014-05-19 16:19 ` Uwe Kleine-König
2014-05-19 16:41 ` Sören Brinkmann
2014-05-19 17:29 ` Sören Brinkmann
2014-05-20 7:51 ` Uwe Kleine-König
2014-05-20 7:33 ` Uwe Kleine-König
2014-05-20 16:01 ` Sören Brinkmann
2014-05-20 17:48 ` Stephen Boyd
2014-05-20 21:48 ` Sören Brinkmann
2014-05-21 7:34 ` Uwe Kleine-König
2014-05-21 15:58 ` Sören Brinkmann
2014-05-21 18:23 ` Uwe Kleine-König
2014-05-21 20:19 ` Sören Brinkmann
2014-05-21 20:33 ` Mike Turquette
2014-05-22 18:03 ` Sören Brinkmann
2014-05-22 18:20 ` Uwe Kleine-König
2014-05-22 20:32 ` Sören Brinkmann
2014-05-22 21:03 ` Mike Turquette
2014-05-22 23:44 ` Sören Brinkmann
2014-05-23 1:37 ` Mike Turquette
2014-05-23 16:14 ` Sören Brinkmann
2014-05-26 6:29 ` Viresh Kumar
2014-05-26 11:22 ` Rafael J. Wysocki
2014-05-26 11:07 ` Viresh Kumar
2014-05-26 11:47 ` Rafael J. Wysocki
2014-05-26 21:52 ` Sören Brinkmann
2014-05-28 2:05 ` Mike Turquette
2014-05-28 16:17 ` Rafael J. Wysocki
2014-06-07 0:44 ` Sören Brinkmann [this message]
2014-05-14 22:30 ` [RFC PATCH 3/5] cpufreq: cpu0: Use clk_round_rate_nearest() Soren Brinkmann
2014-05-14 22:30 ` [RFC PATCH 4/5] ARM: zynq: dt: Use properly rounded frequencies in OPPs Soren Brinkmann
2014-05-14 22:30 ` [RFC PATCH 5/5] net: macb: Use clk_round_rate_nearest() API Soren Brinkmann
2014-05-15 6:12 ` [RFC PATCH 0/5] Frequency resolution in CCF vs. cpufreq Viresh Kumar
2014-05-15 14:05 ` Sören Brinkmann
2014-05-15 7:47 ` Uwe Kleine-König
2014-05-15 12:14 ` Nishanth Menon
2014-05-15 14:00 ` Sören Brinkmann
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=fabca95a-fca0-4a8e-b79a-c7d6437de9f0@BL2FFO11FD042.protection.gbl \
--to=soren.brinkmann@xilinx.com \
--cc=cpufreq@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux@arm.linux.org.uk \
--cc=michal.simek@xilinx.com \
--cc=mturquette@linaro.org \
--cc=rjw@rjwysocki.net \
--cc=sboyd@codeaurora.org \
--cc=u.kleine-koenig@pengutronix.de \
--cc=viresh.kumar@linaro.org \
/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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).