2019-05-28 10:48:56

by Nick Hu

[permalink] [raw]
Subject: [PATCH] riscv: Fix udelay in RV32.

In RV32, udelay would delay the wrong cycle.
When it shifts right "UDELAY_SHITFT" bits, it
either delays 0 cycle or 1 cycle. It only works
correctly in RV64. Because the 'ucycles' always
needs to be 64 bits variable.

Signed-off-by: Nick Hu <[email protected]>
---
arch/riscv/lib/delay.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/lib/delay.c b/arch/riscv/lib/delay.c
index dce8ae24c6d3..da847f49fb74 100644
--- a/arch/riscv/lib/delay.c
+++ b/arch/riscv/lib/delay.c
@@ -88,7 +88,7 @@ EXPORT_SYMBOL(__delay);

void udelay(unsigned long usecs)
{
- unsigned long ucycles = usecs * lpj_fine * UDELAY_MULT;
+ unsigned long long ucycles = (unsigned long long)usecs * lpj_fine * UDELAY_MULT;

if (unlikely(usecs > MAX_UDELAY_US)) {
__delay((u64)usecs * riscv_timebase / 1000000ULL);
--
2.17.0


2019-05-30 04:12:00

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH] riscv: Fix udelay in RV32.

On Tue, 28 May 2019 02:26:49 PDT (-0700), [email protected] wrote:
> In RV32, udelay would delay the wrong cycle.
> When it shifts right "UDELAY_SHITFT" bits, it
> either delays 0 cycle or 1 cycle. It only works
> correctly in RV64. Because the 'ucycles' always
> needs to be 64 bits variable.
>
> Signed-off-by: Nick Hu <[email protected]>
> ---
> arch/riscv/lib/delay.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/riscv/lib/delay.c b/arch/riscv/lib/delay.c
> index dce8ae24c6d3..da847f49fb74 100644
> --- a/arch/riscv/lib/delay.c
> +++ b/arch/riscv/lib/delay.c
> @@ -88,7 +88,7 @@ EXPORT_SYMBOL(__delay);
>
> void udelay(unsigned long usecs)
> {
> - unsigned long ucycles = usecs * lpj_fine * UDELAY_MULT;
> + unsigned long long ucycles = (unsigned long long)usecs * lpj_fine * UDELAY_MULT;
>
> if (unlikely(usecs > MAX_UDELAY_US)) {
> __delay((u64)usecs * riscv_timebase / 1000000ULL);

Reviewed-by: Palmer Dabbelt <[email protected]>

2019-05-30 05:54:17

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH] riscv: Fix udelay in RV32.

On Tue, May 28, 2019 at 05:26:49PM +0800, Nick Hu wrote:
> In RV32, udelay would delay the wrong cycle.
> When it shifts right "UDELAY_SHITFT" bits, it
> either delays 0 cycle or 1 cycle. It only works
> correctly in RV64. Because the 'ucycles' always
> needs to be 64 bits variable.

Please use up all your ~72 chars per line in the commit log.

> diff --git a/arch/riscv/lib/delay.c b/arch/riscv/lib/delay.c
> index dce8ae24c6d3..da847f49fb74 100644
> --- a/arch/riscv/lib/delay.c
> +++ b/arch/riscv/lib/delay.c
> @@ -88,7 +88,7 @@ EXPORT_SYMBOL(__delay);
>
> void udelay(unsigned long usecs)
> {
> - unsigned long ucycles = usecs * lpj_fine * UDELAY_MULT;
> + unsigned long long ucycles = (unsigned long long)usecs * lpj_fine * UDELAY_MULT;

And this creates a way too long line. Pleaase use u64 instead of
unsigned long long to clarify the intention while also fixing the long
lines.

2019-05-30 06:34:33

by Nick Hu

[permalink] [raw]
Subject: Re: [PATCH] riscv: Fix udelay in RV32.

On Thu, May 30, 2019 at 01:52:58PM +0800, Christoph Hellwig wrote:
> On Tue, May 28, 2019 at 05:26:49PM +0800, Nick Hu wrote:
> > In RV32, udelay would delay the wrong cycle.
> > When it shifts right "UDELAY_SHITFT" bits, it
> > either delays 0 cycle or 1 cycle. It only works
> > correctly in RV64. Because the 'ucycles' always
> > needs to be 64 bits variable.
>
> Please use up all your ~72 chars per line in the commit log.
>

OK, Thanks!

> > diff --git a/arch/riscv/lib/delay.c b/arch/riscv/lib/delay.c
> > index dce8ae24c6d3..da847f49fb74 100644
> > --- a/arch/riscv/lib/delay.c
> > +++ b/arch/riscv/lib/delay.c
> > @@ -88,7 +88,7 @@ EXPORT_SYMBOL(__delay);
> >
> > void udelay(unsigned long usecs)
> > {
> > - unsigned long ucycles = usecs * lpj_fine * UDELAY_MULT;
> > + unsigned long long ucycles = (unsigned long long)usecs * lpj_fine * UDELAY_MULT;
>
> And this creates a way too long line. Pleaase use u64 instead of
> unsigned long long to clarify the intention while also fixing the long
> lines.
>

Sure, I will fix it and send another patch. Thanks.

> _______________________________________________
> linux-riscv mailing list
> [email protected]
> http://lists.infradead.org/mailman/listinfo/linux-riscv