diff mbox series

[v2,5/8] timer: Allow delays with a 32-bit microsecond timer

Message ID 20200703163711.1658000-5-sjg@chromium.org
State Superseded
Headers show
Series [v2,1/8] linux/kconfig.h: simplify logic for choosing CONFIG_{SPL_, TPL_, }* | expand

Commit Message

Simon Glass July 3, 2020, 4:37 p.m. UTC
The current get_timer_us() uses 64-bit arithmetic. When implementing
microsecond-level timeouts, 32-bits is plenty. Add a new function to
support this.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

(no changes since v1)

 include/time.h | 11 +++++++++++
 lib/time.c     |  5 +++++
 2 files changed, 16 insertions(+)

Comments

Bin Meng July 7, 2020, 7:09 a.m. UTC | #1
Hi Simon,

On Sat, Jul 4, 2020 at 12:38 AM Simon Glass <sjg at chromium.org> wrote:
>
> The current get_timer_us() uses 64-bit arithmetic. When implementing
> microsecond-level timeouts, 32-bits is plenty. Add a new function to
> support this.
>
> Signed-off-by: Simon Glass <sjg at chromium.org>
> ---
>
> (no changes since v1)
>
>  include/time.h | 11 +++++++++++
>  lib/time.c     |  5 +++++
>  2 files changed, 16 insertions(+)
>
> diff --git a/include/time.h b/include/time.h
> index e99f9c8012..434e63b075 100644
> --- a/include/time.h
> +++ b/include/time.h
> @@ -17,6 +17,17 @@ unsigned long get_timer(unsigned long base);
>  unsigned long timer_get_us(void);
>  uint64_t get_timer_us(uint64_t base);
>
> +/**
> + * get_timer_us_long() - Get the number of elapsed microseconds
> + *
> + * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
> + * delays of over an hour.
> + *
> + *@base: Base time to consider
> + *@return elapsed time since @base
> + */
> +unsigned long get_timer_us_long(unsigned long base);

The function name does not clear indicates this is 32-bit value
because unsigned long is still 64-bit when building for 64-bit U-Boot.

> +
>  /*
>   * timer_test_add_offset()
>   *
> diff --git a/lib/time.c b/lib/time.c
> index 65db0f6cda..47f8c84327 100644
> --- a/lib/time.c
> +++ b/lib/time.c
> @@ -152,6 +152,11 @@ uint64_t __weak get_timer_us(uint64_t base)
>         return tick_to_time_us(get_ticks()) - base;
>  }
>
> +unsigned long __weak get_timer_us_long(unsigned long base)
> +{
> +       return timer_get_us() - base;
> +}
> +
>  unsigned long __weak notrace timer_get_us(void)
>  {
>         return tick_to_time(get_ticks() * 1000);
> --

Regards,
Bin
Bin Meng July 7, 2020, 7:20 a.m. UTC | #2
Hi Simon,

On Tue, Jul 7, 2020 at 3:09 PM Bin Meng <bmeng.cn at gmail.com> wrote:
>
> Hi Simon,
>
> On Sat, Jul 4, 2020 at 12:38 AM Simon Glass <sjg at chromium.org> wrote:
> >
> > The current get_timer_us() uses 64-bit arithmetic. When implementing
> > microsecond-level timeouts, 32-bits is plenty. Add a new function to
> > support this.
> >
> > Signed-off-by: Simon Glass <sjg at chromium.org>
> > ---
> >
> > (no changes since v1)
> >
> >  include/time.h | 11 +++++++++++
> >  lib/time.c     |  5 +++++
> >  2 files changed, 16 insertions(+)
> >
> > diff --git a/include/time.h b/include/time.h
> > index e99f9c8012..434e63b075 100644
> > --- a/include/time.h
> > +++ b/include/time.h
> > @@ -17,6 +17,17 @@ unsigned long get_timer(unsigned long base);
> >  unsigned long timer_get_us(void);
> >  uint64_t get_timer_us(uint64_t base);
> >
> > +/**
> > + * get_timer_us_long() - Get the number of elapsed microseconds
> > + *
> > + * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
> > + * delays of over an hour.
> > + *
> > + *@base: Base time to consider
> > + *@return elapsed time since @base
> > + */
> > +unsigned long get_timer_us_long(unsigned long base);
>
> The function name does not clear indicates this is 32-bit value
> because unsigned long is still 64-bit when building for 64-bit U-Boot.
>
> > +
> >  /*
> >   * timer_test_add_offset()
> >   *
> > diff --git a/lib/time.c b/lib/time.c
> > index 65db0f6cda..47f8c84327 100644
> > --- a/lib/time.c
> > +++ b/lib/time.c
> > @@ -152,6 +152,11 @@ uint64_t __weak get_timer_us(uint64_t base)
> >         return tick_to_time_us(get_ticks()) - base;
> >  }
> >
> > +unsigned long __weak get_timer_us_long(unsigned long base)
> > +{
> > +       return timer_get_us() - base;
> > +}
> > +
> >  unsigned long __weak notrace timer_get_us(void)
> >  {
> >         return tick_to_time(get_ticks() * 1000);
> > --

I've applied the first 4 patches in this series to u-boot-x86.

Regards,
Bin
Simon Glass July 10, 2020, 12:28 a.m. UTC | #3
Hi Bin,

On Tue, 7 Jul 2020 at 01:09, Bin Meng <bmeng.cn at gmail.com> wrote:
>
> Hi Simon,
>
> On Sat, Jul 4, 2020 at 12:38 AM Simon Glass <sjg at chromium.org> wrote:
> >
> > The current get_timer_us() uses 64-bit arithmetic. When implementing
> > microsecond-level timeouts, 32-bits is plenty. Add a new function to
> > support this.
> >
> > Signed-off-by: Simon Glass <sjg at chromium.org>
> > ---
> >
> > (no changes since v1)
> >
> >  include/time.h | 11 +++++++++++
> >  lib/time.c     |  5 +++++
> >  2 files changed, 16 insertions(+)
> >
> > diff --git a/include/time.h b/include/time.h
> > index e99f9c8012..434e63b075 100644
> > --- a/include/time.h
> > +++ b/include/time.h
> > @@ -17,6 +17,17 @@ unsigned long get_timer(unsigned long base);
> >  unsigned long timer_get_us(void);
> >  uint64_t get_timer_us(uint64_t base);
> >
> > +/**
> > + * get_timer_us_long() - Get the number of elapsed microseconds
> > + *
> > + * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
> > + * delays of over an hour.
> > + *
> > + *@base: Base time to consider
> > + *@return elapsed time since @base
> > + */
> > +unsigned long get_timer_us_long(unsigned long base);
>
> The function name does not clear indicates this is 32-bit value
> because unsigned long is still 64-bit when building for 64-bit U-Boot.

Yes that's right. My purpose is to use the natural long time.

Shall I update the commit message?

Regards,
Simon
Bin Meng July 10, 2020, 12:44 a.m. UTC | #4
Hi Simon,

On Fri, Jul 10, 2020 at 8:28 AM Simon Glass <sjg at chromium.org> wrote:
>
> Hi Bin,
>
> On Tue, 7 Jul 2020 at 01:09, Bin Meng <bmeng.cn at gmail.com> wrote:
> >
> > Hi Simon,
> >
> > On Sat, Jul 4, 2020 at 12:38 AM Simon Glass <sjg at chromium.org> wrote:
> > >
> > > The current get_timer_us() uses 64-bit arithmetic. When implementing
> > > microsecond-level timeouts, 32-bits is plenty. Add a new function to
> > > support this.
> > >
> > > Signed-off-by: Simon Glass <sjg at chromium.org>
> > > ---
> > >
> > > (no changes since v1)
> > >
> > >  include/time.h | 11 +++++++++++
> > >  lib/time.c     |  5 +++++
> > >  2 files changed, 16 insertions(+)
> > >
> > > diff --git a/include/time.h b/include/time.h
> > > index e99f9c8012..434e63b075 100644
> > > --- a/include/time.h
> > > +++ b/include/time.h
> > > @@ -17,6 +17,17 @@ unsigned long get_timer(unsigned long base);
> > >  unsigned long timer_get_us(void);
> > >  uint64_t get_timer_us(uint64_t base);
> > >
> > > +/**
> > > + * get_timer_us_long() - Get the number of elapsed microseconds
> > > + *
> > > + * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
> > > + * delays of over an hour.
> > > + *
> > > + *@base: Base time to consider
> > > + *@return elapsed time since @base
> > > + */
> > > +unsigned long get_timer_us_long(unsigned long base);
> >
> > The function name does not clear indicates this is 32-bit value
> > because unsigned long is still 64-bit when building for 64-bit U-Boot.
>
> Yes that's right. My purpose is to use the natural long time.
>
> Shall I update the commit message?

I think we need to explicitly declare the return value to u32 or
uint32_t to make a 32-bit variant function.

Regards,
Bin
Simon Glass July 10, 2020, 12:56 a.m. UTC | #5
Hi Bin,

On Thu, 9 Jul 2020 at 18:44, Bin Meng <bmeng.cn at gmail.com> wrote:
>
> Hi Simon,
>
> On Fri, Jul 10, 2020 at 8:28 AM Simon Glass <sjg at chromium.org> wrote:
> >
> > Hi Bin,
> >
> > On Tue, 7 Jul 2020 at 01:09, Bin Meng <bmeng.cn at gmail.com> wrote:
> > >
> > > Hi Simon,
> > >
> > > On Sat, Jul 4, 2020 at 12:38 AM Simon Glass <sjg at chromium.org> wrote:
> > > >
> > > > The current get_timer_us() uses 64-bit arithmetic. When implementing
> > > > microsecond-level timeouts, 32-bits is plenty. Add a new function to
> > > > support this.
> > > >
> > > > Signed-off-by: Simon Glass <sjg at chromium.org>
> > > > ---
> > > >
> > > > (no changes since v1)
> > > >
> > > >  include/time.h | 11 +++++++++++
> > > >  lib/time.c     |  5 +++++
> > > >  2 files changed, 16 insertions(+)
> > > >
> > > > diff --git a/include/time.h b/include/time.h
> > > > index e99f9c8012..434e63b075 100644
> > > > --- a/include/time.h
> > > > +++ b/include/time.h
> > > > @@ -17,6 +17,17 @@ unsigned long get_timer(unsigned long base);
> > > >  unsigned long timer_get_us(void);
> > > >  uint64_t get_timer_us(uint64_t base);
> > > >
> > > > +/**
> > > > + * get_timer_us_long() - Get the number of elapsed microseconds
> > > > + *
> > > > + * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
> > > > + * delays of over an hour.
> > > > + *
> > > > + *@base: Base time to consider
> > > > + *@return elapsed time since @base
> > > > + */
> > > > +unsigned long get_timer_us_long(unsigned long base);
> > >
> > > The function name does not clear indicates this is 32-bit value
> > > because unsigned long is still 64-bit when building for 64-bit U-Boot.
> >
> > Yes that's right. My purpose is to use the natural long time.
> >
> > Shall I update the commit message?
>
> I think we need to explicitly declare the return value to u32 or
> uint32_t to make a 32-bit variant function.

But that is not my objective here. I just want something that is
efficient on both 32- and 64-bit systems.

Regards,
Simon
diff mbox series

Patch

diff --git a/include/time.h b/include/time.h
index e99f9c8012..434e63b075 100644
--- a/include/time.h
+++ b/include/time.h
@@ -17,6 +17,17 @@  unsigned long get_timer(unsigned long base);
 unsigned long timer_get_us(void);
 uint64_t get_timer_us(uint64_t base);
 
+/**
+ * get_timer_us_long() - Get the number of elapsed microseconds
+ *
+ * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
+ * delays of over an hour.
+ *
+ *@base: Base time to consider
+ *@return elapsed time since @base
+ */
+unsigned long get_timer_us_long(unsigned long base);
+
 /*
  * timer_test_add_offset()
  *
diff --git a/lib/time.c b/lib/time.c
index 65db0f6cda..47f8c84327 100644
--- a/lib/time.c
+++ b/lib/time.c
@@ -152,6 +152,11 @@  uint64_t __weak get_timer_us(uint64_t base)
 	return tick_to_time_us(get_ticks()) - base;
 }
 
+unsigned long __weak get_timer_us_long(unsigned long base)
+{
+	return timer_get_us() - base;
+}
+
 unsigned long __weak notrace timer_get_us(void)
 {
 	return tick_to_time(get_ticks() * 1000);