diff mbox

[U-Boot] lib/time - fix "usec_to_tick" calculation for hi freq system timers

Message ID 1386593830-3197-1-git-send-email-abrodkin@synopsys.com
State Deferred
Delegated to: Tom Rini
Headers show

Commit Message

Alexey Brodkin Dec. 9, 2013, 12:57 p.m. UTC
Current implementation works fine if "usec * get_tbclk()" fits in 32
bits. Otherwise result will be cut down to 32-bit.

Fix is obvious - first extend data type of either operand.

Signed-off-by: Alexey Brodkin <abrodkin@synopsys.com>

Cc: Rob Herring <rob.herring@calxeda.com>
Cc: Mischa Jonker <mjonker@synopsys.com>
---
 lib/time.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Alexey Brodkin Dec. 13, 2013, 7:01 a.m. UTC | #1
Hi Tom,

On Mon, 2013-12-09 at 16:57 +0400, Alexey Brodkin wrote:
> Current implementation works fine if "usec * get_tbclk()" fits in 32
> bits. Otherwise result will be cut down to 32-bit.
> 
> Fix is obvious - first extend data type of either operand.
> 
> Signed-off-by: Alexey Brodkin <abrodkin@synopsys.com>
> 
> Cc: Rob Herring <rob.herring@calxeda.com>
> Cc: Mischa Jonker <mjonker@synopsys.com>
> ---
>  lib/time.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/lib/time.c b/lib/time.c
> index 09bb05a..80003c3 100644
> --- a/lib/time.c
> +++ b/lib/time.c
> @@ -71,7 +71,7 @@ unsigned long __weak notrace timer_get_us(void)
>  }
>  static unsigned long long usec_to_tick(unsigned long usec)
>  {
> -	uint64_t tick = usec * get_tbclk();
> +	uint64_t tick = (uint64_t)usec * get_tbclk();
>  	usec *= get_tbclk();
>  	do_div(tick, 1000000);
>  	return tick;

Any chance to get it applied?

Regards,
Alexey
Tom Rini Dec. 13, 2013, 1:07 p.m. UTC | #2
On Fri, Dec 13, 2013 at 07:01:07AM +0000, Alexey Brodkin wrote:

> Hi Tom,
> 
> On Mon, 2013-12-09 at 16:57 +0400, Alexey Brodkin wrote:
> > Current implementation works fine if "usec * get_tbclk()" fits in 32
> > bits. Otherwise result will be cut down to 32-bit.
> > 
> > Fix is obvious - first extend data type of either operand.
> > 
> > Signed-off-by: Alexey Brodkin <abrodkin@synopsys.com>
> > 
> > Cc: Rob Herring <rob.herring@calxeda.com>
> > Cc: Mischa Jonker <mjonker@synopsys.com>
> > ---
> >  lib/time.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/lib/time.c b/lib/time.c
> > index 09bb05a..80003c3 100644
> > --- a/lib/time.c
> > +++ b/lib/time.c
> > @@ -71,7 +71,7 @@ unsigned long __weak notrace timer_get_us(void)
> >  }
> >  static unsigned long long usec_to_tick(unsigned long usec)
> >  {
> > -	uint64_t tick = usec * get_tbclk();
> > +	uint64_t tick = (uint64_t)usec * get_tbclk();
> >  	usec *= get_tbclk();
> >  	do_div(tick, 1000000);
> >  	return tick;
> 
> Any chance to get it applied?

Yes, it's on my TODO list shortly, thanks.
Tom Rini Dec. 13, 2013, 2:09 p.m. UTC | #3
On Fri, Dec 13, 2013 at 08:07:51AM -0500, Tom Rini wrote:
> On Fri, Dec 13, 2013 at 07:01:07AM +0000, Alexey Brodkin wrote:
> 
> > Hi Tom,
> > 
> > On Mon, 2013-12-09 at 16:57 +0400, Alexey Brodkin wrote:
> > > Current implementation works fine if "usec * get_tbclk()" fits in 32
> > > bits. Otherwise result will be cut down to 32-bit.
> > > 
> > > Fix is obvious - first extend data type of either operand.
> > > 
> > > Signed-off-by: Alexey Brodkin <abrodkin@synopsys.com>
> > > 
> > > Cc: Rob Herring <rob.herring@calxeda.com>
> > > Cc: Mischa Jonker <mjonker@synopsys.com>
> > > ---
> > >  lib/time.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/lib/time.c b/lib/time.c
> > > index 09bb05a..80003c3 100644
> > > --- a/lib/time.c
> > > +++ b/lib/time.c
> > > @@ -71,7 +71,7 @@ unsigned long __weak notrace timer_get_us(void)
> > >  }
> > >  static unsigned long long usec_to_tick(unsigned long usec)
> > >  {
> > > -	uint64_t tick = usec * get_tbclk();
> > > +	uint64_t tick = (uint64_t)usec * get_tbclk();
> > >  	usec *= get_tbclk();
> > >  	do_div(tick, 1000000);
> > >  	return tick;
> > 
> > Any chance to get it applied?
> 
> Yes, it's on my TODO list shortly, thanks.

Please note that I'm taking http://patchwork.ozlabs.org/patch/297361/ as
the solution for this problem, thanks!
Alexey Brodkin Dec. 13, 2013, 2:20 p.m. UTC | #4
On Fri, 2013-12-13 at 09:09 -0500, Tom Rini wrote:
> Please note that I'm taking http://patchwork.ozlabs.org/patch/297361/ as
> the solution for this problem, thanks!
> 

As long as it resolves overflow issue (and from that patch I'd say it
should) I'm totally fine with it.

Thanks.

P.S. funny enough this overflow issue bites multiple people these days
(but earlier it seems like everybody was happy though).
diff mbox

Patch

diff --git a/lib/time.c b/lib/time.c
index 09bb05a..80003c3 100644
--- a/lib/time.c
+++ b/lib/time.c
@@ -71,7 +71,7 @@  unsigned long __weak notrace timer_get_us(void)
 }
 static unsigned long long usec_to_tick(unsigned long usec)
 {
-	uint64_t tick = usec * get_tbclk();
+	uint64_t tick = (uint64_t)usec * get_tbclk();
 	usec *= get_tbclk();
 	do_div(tick, 1000000);
 	return tick;