mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: "Sascha Hauer" <s.hauer@pengutronix.de>
To: "Ahmad Fatoum" <a.fatoum@pengutronix.de>
Cc: "open list:BAREBOX" <barebox@lists.infradead.org>,
	"Stefan Kerkmann" <s.kerkmann@pengutronix.de>
Subject: Re: [PATCH v2 2/3] ARM: lib32: pbl: implement get_time_ns and is_timeout
Date: Wed, 12 Aug 2026 05:56:22 +0000	[thread overview]
Message-ID: <20260812055622.FV9CYMelzjD8hmsYUpL7otBk7O9qDKWJvTsFpfNNlTk@z> (raw)
In-Reply-To: <a2b7c9d2-b6b0-4c64-818c-f945b117fb94@pengutronix.de>

On 2026-08-11 12:21, Ahmad Fatoum wrote:
> Hi,
> 
> On 8/11/26 12:13 PM, Stefan Kerkmann wrote:
> > ARMv7-A cores can implement the optional 64bit wide generic timer
> > CNTPCT[1]. If it is present we can use it to implement the get_time_ns
> > and is_timeout helpers which in turn enable the whole read_poll_timeout
> > class of functions in the PBL. As it is guaranteed that the timer won't
> > wrap for 40 years no overflow handling is necessary.
> > 
> > [1]: See "ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition
> > (rev C.d)", Chapter B8 "The Generic Timer"
> > 
> > Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
> > ---
> >  arch/arm/lib32/arm_architected_timer.c | 15 ++++++++++++++-
> >  1 file changed, 14 insertions(+), 1 deletion(-)
> > 
> > diff --git a/arch/arm/lib32/arm_architected_timer.c b/arch/arm/lib32/arm_architected_timer.c
> > index 54eca13f8b..75f74cbc15 100644
> > --- a/arch/arm/lib32/arm_architected_timer.c
> > +++ b/arch/arm/lib32/arm_architected_timer.c
> > @@ -10,8 +10,21 @@ void arm_architected_timer_udelay(unsigned long us)
> >  	unsigned long long ticks, cntfrq = get_cntfrq();
> >  	unsigned long long start = get_cntpct();
> >  
> > -	ticks = DIV_ROUND_DOWN_ULL((us * cntfrq), 1000000);
> > +	ticks = DIV_ROUND_DOWN_ULL((us * cntfrq), MSECOND);
> >  
> >  	while ((long)(start + ticks - get_cntpct()) > 0)
> >  		;
> >  }
> > +
> > +uint64_t get_time_ns(void)
> > +{
> > +	return get_cntpct() * SECOND / get_cntfrq();
> 
> I think you need to use one of the 64-bit division macros here.

Also the corresponding barebox proper code doesn't depend on the timer
being initialized to 0. I don't know if we can guarantee this for the
architected timer, but otherwise get_cntpct() * SECOND could easily
overflow.

> 
> > +int is_timeout(uint64_t start, uint64_t time_offset_ns)
> > +{
> > +	if ((int64_t)(start + time_offset_ns - get_time_ns()) < 0)
> > +		return 1;
> > +	else
> > +		return 0;
> 
> That's a convoluted way to write
> 
>   return get_time_ns() >= start + time_offset_ns;
> 
> Why did you structure it this way with the cast?
> 
> (A 64-bit nanosecond timestamp overflows after hundreds of years).

It's a direct copy of the non-pbl is_timeout implementation. It's
agnostic to the timer initialization value which is not necessarily 0.

Sascha

--
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




  reply	other threads:[~2026-08-12  7:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-11 10:13 [PATCH v2 0/3] PBL: enable timeouts in read_poll_timeout macros Stefan Kerkmann
2026-08-11 10:13 ` [PATCH v2 1/3] ARM64: lib64: pbl: implement get_time_ns and is_timeout Stefan Kerkmann
2026-08-11 10:13 ` [PATCH v2 2/3] ARM: lib32: " Stefan Kerkmann
2026-08-11 10:21   ` Ahmad Fatoum
2026-08-12  5:56     ` Sascha Hauer [this message]
2026-08-11 10:13 ` [PATCH v2 3/3] pbl: introduce HAS_PBL_CLOCKSOURCE marker Stefan Kerkmann
2026-08-12  5:32   ` Sascha Hauer

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=20260812055622.FV9CYMelzjD8hmsYUpL7otBk7O9qDKWJvTsFpfNNlTk@z \
    --to=s.hauer@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.kerkmann@pengutronix.de \
    /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