mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/3] PBL: enable timeouts in read_poll_timeout macros
@ 2025-01-21 16:49 Stefan Kerkmann
  2025-01-21 16:49 ` [PATCH 1/3] ARM64: lib64: pbl: implement get_time_ns and is_timeout Stefan Kerkmann
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Stefan Kerkmann @ 2025-01-21 16:49 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

Without a time source the read_poll_timeout functions will deadlock in the PBL
if the break condition is never met. This series introduces the necessary
timing functions in the PBL for ARMv7 and ARMv8 based on the ARM architected
timer and enable their usage if available.

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
Stefan Kerkmann (3):
      ARM64: lib64: pbl: implement get_time_ns and is_timeout
      ARM: lib32: pbl: implement get_time_ns and is_timeout
      pbl: introduce HAS_PBL_CLOCKSOURCE marker

 arch/arm/cpu/Kconfig                   |  2 ++
 arch/arm/lib32/Makefile                |  2 +-
 arch/arm/lib32/arm_architected_timer.c | 15 ++++++++++++++-
 arch/arm/lib64/pbl.c                   | 17 +++++++++++++++--
 include/linux/iopoll.h                 |  8 ++++----
 pbl/Kconfig                            |  3 +++
 6 files changed, 39 insertions(+), 8 deletions(-)
---
base-commit: 5acd59882f02d8f8da002b939756c1739d917ac2
change-id: 20250121-feature-pbl-get-time-ns-6d9e8874d582

Best regards,
-- 
Stefan Kerkmann <s.kerkmann@pengutronix.de>




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] ARM64: lib64: pbl: implement get_time_ns and is_timeout
  2025-01-21 16:49 [PATCH 0/3] PBL: enable timeouts in read_poll_timeout macros Stefan Kerkmann
@ 2025-01-21 16:49 ` Stefan Kerkmann
  2025-01-21 16:49 ` [PATCH 2/3] ARM: lib32: " Stefan Kerkmann
  2025-01-21 16:49 ` [PATCH 3/3] pbl: introduce HAS_PBL_CLOCKSOURCE marker Stefan Kerkmann
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Kerkmann @ 2025-01-21 16:49 UTC (permalink / raw)
  To: Sascha Hauer, 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/pbl.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/arch/arm/lib64/pbl.c b/arch/arm/lib64/pbl.c
index 78eab33f8d68598554c07f347938cc84608c6716..00b20c5db1bd347ae4c4a3ce9eaa792fec3d07c9 100644
--- a/arch/arm/lib64/pbl.c
+++ b/arch/arm/lib64/pbl.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.39.5




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] ARM: lib32: pbl: implement get_time_ns and is_timeout
  2025-01-21 16:49 [PATCH 0/3] PBL: enable timeouts in read_poll_timeout macros Stefan Kerkmann
  2025-01-21 16:49 ` [PATCH 1/3] ARM64: lib64: pbl: implement get_time_ns and is_timeout Stefan Kerkmann
@ 2025-01-21 16:49 ` Stefan Kerkmann
  2025-01-21 16:49 ` [PATCH 3/3] pbl: introduce HAS_PBL_CLOCKSOURCE marker Stefan Kerkmann
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Kerkmann @ 2025-01-21 16:49 UTC (permalink / raw)
  To: Sascha Hauer, 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 54eca13f8b9d1c940d54f0e34c1dd261c5eec8c7..75f74cbc15ccf09b2fae1fc287138118c3c3c22c 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.39.5




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] pbl: introduce HAS_PBL_CLOCKSOURCE marker
  2025-01-21 16:49 [PATCH 0/3] PBL: enable timeouts in read_poll_timeout macros Stefan Kerkmann
  2025-01-21 16:49 ` [PATCH 1/3] ARM64: lib64: pbl: implement get_time_ns and is_timeout Stefan Kerkmann
  2025-01-21 16:49 ` [PATCH 2/3] ARM: lib32: " Stefan Kerkmann
@ 2025-01-21 16:49 ` Stefan Kerkmann
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Kerkmann @ 2025-01-21 16:49 UTC (permalink / raw)
  To: Sascha Hauer, 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 ARMv8 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  | 8 ++++----
 pbl/Kconfig             | 3 +++
 4 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/arch/arm/cpu/Kconfig b/arch/arm/cpu/Kconfig
index 84fe770b6da892723528eeea89ae2b0eda122d07..3569f3d3483a6289d7a6eca3d1715e06144df8d4 100644
--- a/arch/arm/cpu/Kconfig
+++ b/arch/arm/cpu/Kconfig
@@ -84,6 +84,7 @@ config CPU_V6
 config CPU_V7
 	bool
 	select CPU_32v7
+	select HAS_PBL_CLOCKSOURCE
 
 # ARMv8
 config CPU_V8
@@ -125,6 +126,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 a139a80fb84973e01e24b063eb6b1d774a60a056..4509d483b27ec7da038b17729f552d6f09fd39ec 100644
--- a/arch/arm/lib32/Makefile
+++ b/arch/arm/lib32/Makefile
@@ -31,7 +31,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 9350a3d05007fbf760e2dea2a981cb86641343fd..df7fdbfa4ae0aec825cc02afa8f14d1ea1f1e535 100644
--- a/include/linux/iopoll.h
+++ b/include/linux/iopoll.h
@@ -26,19 +26,19 @@
  * When available, you'll probably want to use one of the specialized
  * macros defined below rather than this macro directly.
  *
- * We do not have timing functions in the PBL, so ignore the timeout value and
- * loop infinitely here.
+ * In the PBL the timeout is ignored if timing functions are not implemented.
+ * This potentially means looping infinitely!
  */
 #define read_poll_timeout(op, val, cond, timeout_us, args...)	\
 ({ \
 	uint64_t start = 0; \
-	if (IN_PROPER && (timeout_us) != 0) \
+	if ((IN_PROPER || IS_ENABLED(CONFIG_HAS_PBL_CLOCKSOURCE)) && (timeout_us) != 0) \
 		start = get_time_ns(); \
 	for (;;) { \
 		(val) = op(args); \
 		if (cond) \
 			break; \
-		if (IN_PROPER && (timeout_us) != 0 && \
+		if ((IN_PROPER || IS_ENABLED(CONFIG_HAS_PBL_CLOCKSOURCE)) && (timeout_us) != 0 && \
 		    is_timeout(start, ((timeout_us) * USECOND))) { \
 			(val) = op(args); \
 			break; \
diff --git a/pbl/Kconfig b/pbl/Kconfig
index 98d71791454b1a31f315ca3fdffa675e6d3dcb50..c2697a2bd00d807a78ac6bc9f0bcf7b28d150c59 100644
--- a/pbl/Kconfig
+++ b/pbl/Kconfig
@@ -26,6 +26,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.39.5




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-01-21 16:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-21 16:49 [PATCH 0/3] PBL: enable timeouts in read_poll_timeout macros Stefan Kerkmann
2025-01-21 16:49 ` [PATCH 1/3] ARM64: lib64: pbl: implement get_time_ns and is_timeout Stefan Kerkmann
2025-01-21 16:49 ` [PATCH 2/3] ARM: lib32: " Stefan Kerkmann
2025-01-21 16:49 ` [PATCH 3/3] pbl: introduce HAS_PBL_CLOCKSOURCE marker Stefan Kerkmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox