* [PATCH v2 1/3] ARM64: lib64: pbl: implement get_time_ns and is_timeout
2026-08-11 10:13 [PATCH v2 0/3] PBL: enable timeouts in read_poll_timeout macros Stefan Kerkmann
@ 2026-08-11 10:13 ` Stefan Kerkmann
2026-08-11 10:13 ` [PATCH v2 2/3] ARM: lib32: " Stefan Kerkmann
2026-08-11 10:13 ` [PATCH v2 3/3] pbl: introduce HAS_PBL_CLOCKSOURCE marker Stefan Kerkmann
2 siblings, 0 replies; 7+ messages in thread
From: Stefan Kerkmann @ 2026-08-11 10:13 UTC (permalink / raw)
To: Sascha Hauer, open list:BAREBOX; +Cc: Stefan Kerkmann
All ARMv8-A cores implement the 64bit wide generic timer CNTPCT[1], thus
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 will not wrap for 40 years no
overflow handling is necessary.
[1]: See "ARM Architecture Reference Manual for A-profile architecture
(rev L.a)", chapter D12 "The Generic Timer in AArch64 state"
Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
arch/arm/lib64/delay.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/arch/arm/lib64/delay.c b/arch/arm/lib64/delay.c
index 78eab33f8d..00b20c5db1 100644
--- a/arch/arm/lib64/delay.c
+++ b/arch/arm/lib64/delay.c
@@ -7,7 +7,7 @@
void udelay(unsigned long us)
{
unsigned long cntfrq = get_cntfrq();
- unsigned long ticks = (us * cntfrq) / 1000000;
+ unsigned long ticks = (us * cntfrq) / MSECOND;
unsigned long start = get_cntpct();
while ((long)(start + ticks - get_cntpct()) > 0);
@@ -15,5 +15,18 @@ void udelay(unsigned long us)
void mdelay(unsigned long ms)
{
- udelay(ms * 1000);
+ udelay(ms * USECOND);
+}
+
+uint64_t get_time_ns(void)
+{
+ return get_cntpct() * SECOND / get_cntfrq();
+}
+
+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;
}
--
2.47.3
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v2 2/3] ARM: lib32: pbl: implement get_time_ns and is_timeout
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 ` Stefan Kerkmann
2026-08-11 10:21 ` Ahmad Fatoum
2026-08-11 10:13 ` [PATCH v2 3/3] pbl: introduce HAS_PBL_CLOCKSOURCE marker Stefan Kerkmann
2 siblings, 1 reply; 7+ messages in thread
From: Stefan Kerkmann @ 2026-08-11 10:13 UTC (permalink / raw)
To: Sascha Hauer, open list:BAREBOX; +Cc: Stefan Kerkmann
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();
+}
+
+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;
+}
--
2.47.3
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v2 2/3] ARM: lib32: pbl: implement get_time_ns and is_timeout
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
0 siblings, 1 reply; 7+ messages in thread
From: Ahmad Fatoum @ 2026-08-11 10:21 UTC (permalink / raw)
To: Stefan Kerkmann, Sascha Hauer, open list:BAREBOX
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.
> +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).
Cheers,
Ahmad
> +}
>
--
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 |
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v2 2/3] ARM: lib32: pbl: implement get_time_ns and is_timeout
2026-08-11 10:21 ` Ahmad Fatoum
@ 2026-08-12 5:56 ` Sascha Hauer
0 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2026-08-12 5:56 UTC (permalink / raw)
To: Ahmad Fatoum; +Cc: open list:BAREBOX, Stefan Kerkmann
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 |
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 3/3] pbl: introduce HAS_PBL_CLOCKSOURCE marker
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:13 ` Stefan Kerkmann
2026-08-12 5:32 ` Sascha Hauer
2 siblings, 1 reply; 7+ messages in thread
From: Stefan Kerkmann @ 2026-08-11 10:13 UTC (permalink / raw)
To: Sascha Hauer, open list:BAREBOX; +Cc: Stefan Kerkmann
This finally enables the usage of the polled timeout functions in the
barebox pbl with real timeouts for supported architectures. Currently
only ARMv7 and AARCH64 are enabled.
Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
arch/arm/cpu/Kconfig | 2 ++
arch/arm/lib32/Makefile | 2 +-
include/linux/iopoll.h | 2 +-
pbl/Kconfig | 3 +++
4 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/arch/arm/cpu/Kconfig b/arch/arm/cpu/Kconfig
index 47c116ea98..684c7eade9 100644
--- a/arch/arm/cpu/Kconfig
+++ b/arch/arm/cpu/Kconfig
@@ -82,6 +82,7 @@ config CPU_V7
bool "Generic ARMv7 support"
depends on 32BIT
select CPU_32v7
+ select HAS_PBL_CLOCKSOURCE if !CPU_32v4T && !CPU_32v5 && !CPU_32v6
config CPU_V7VE
bool "ARMv7 Virtualization Extensions Support"
@@ -135,6 +136,7 @@ config CPU_32v7
config CPU_64v8
bool
select CPU_64
+ select HAS_PBL_CLOCKSOURCE
comment "processor features"
diff --git a/arch/arm/lib32/Makefile b/arch/arm/lib32/Makefile
index 67c4f16f76..796ea6c742 100644
--- a/arch/arm/lib32/Makefile
+++ b/arch/arm/lib32/Makefile
@@ -34,7 +34,7 @@ extra-y += barebox.lds
pbl-y += lib1funcs.o
pbl-y += ashldi3.o
pbl-y += div0.o
-pbl-$(CONFIG_CPU_32v7) += arm_architected_timer.o
+pbl-$(CONFIG_HAS_PBL_CLOCKSOURCE) += arm_architected_timer.o
CFLAGS_arm_architected_timer.o := -march=armv7-a
obj-pbl-y += setjmp.o
diff --git a/include/linux/iopoll.h b/include/linux/iopoll.h
index a6fade2a11..d86069f348 100644
--- a/include/linux/iopoll.h
+++ b/include/linux/iopoll.h
@@ -12,7 +12,7 @@
#include <clock.h>
#include <pbl.h>
-#if IN_PROPER
+#if IN_PROPER || IS_ENABLED(CONFIG_HAS_PBL_CLOCKSOURCE)
# define read_poll_get_time_ns() get_time_ns()
# define read_poll_is_timeout(s, t) is_timeout(s, t)
#else
diff --git a/pbl/Kconfig b/pbl/Kconfig
index 63f29cd613..e5fe5328d2 100644
--- a/pbl/Kconfig
+++ b/pbl/Kconfig
@@ -43,6 +43,9 @@ config PBL_SINGLE_IMAGE
depends on !HAVE_PBL_MULTI_IMAGES
default y
+config HAS_PBL_CLOCKSOURCE
+ bool
+
if PBL_IMAGE
config USE_COMPRESSED_DTB
--
2.47.3
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 3/3] pbl: introduce HAS_PBL_CLOCKSOURCE marker
2026-08-11 10:13 ` [PATCH v2 3/3] pbl: introduce HAS_PBL_CLOCKSOURCE marker Stefan Kerkmann
@ 2026-08-12 5:32 ` Sascha Hauer
0 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2026-08-12 5:32 UTC (permalink / raw)
To: Stefan Kerkmann; +Cc: open list:BAREBOX, Stefan Kerkmann
On 2026-08-11 12:13, Stefan Kerkmann wrote:
> This finally enables the usage of the polled timeout functions in the
> barebox pbl with real timeouts for supported architectures. Currently
> only ARMv7 and AARCH64 are enabled.
>
> Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
> ---
> arch/arm/cpu/Kconfig | 2 ++
> arch/arm/lib32/Makefile | 2 +-
> include/linux/iopoll.h | 2 +-
> pbl/Kconfig | 3 +++
> 4 files changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/arch/arm/cpu/Kconfig b/arch/arm/cpu/Kconfig
> index 47c116ea98..684c7eade9 100644
> --- a/arch/arm/cpu/Kconfig
> +++ b/arch/arm/cpu/Kconfig
> @@ -82,6 +82,7 @@ config CPU_V7
> bool "Generic ARMv7 support"
> depends on 32BIT
> select CPU_32v7
> + select HAS_PBL_CLOCKSOURCE if !CPU_32v4T && !CPU_32v5 && !CPU_32v6
>
> config CPU_V7VE
> bool "ARMv7 Virtualization Extensions Support"
> @@ -135,6 +136,7 @@ config CPU_32v7
> config CPU_64v8
> bool
> select CPU_64
> + select HAS_PBL_CLOCKSOURCE
>
> comment "processor features"
>
> diff --git a/arch/arm/lib32/Makefile b/arch/arm/lib32/Makefile
> index 67c4f16f76..796ea6c742 100644
> --- a/arch/arm/lib32/Makefile
> +++ b/arch/arm/lib32/Makefile
> @@ -34,7 +34,7 @@ extra-y += barebox.lds
> pbl-y += lib1funcs.o
> pbl-y += ashldi3.o
> pbl-y += div0.o
> -pbl-$(CONFIG_CPU_32v7) += arm_architected_timer.o
> +pbl-$(CONFIG_HAS_PBL_CLOCKSOURCE) += arm_architected_timer.o
This change makes arm_architected_timer_udelay() available for builds
which exclusively support ARMv7a, so breaks builds that have ARMv4/5/6
enabled as well.
You could leave pbl-$(CONFIG_CPU_32v7) untouched and put get_time_ns()
inside #ifdef CONFIG_HAS_PBL_CLOCKSOURCE
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 |
^ permalink raw reply [flat|nested] 7+ messages in thread