Optimize udelay for aarch64 boards
Subsystems (3)
cover Optimize udelay for aarch64 boards
Arm boards currently use the default implementation of __udelay which consists of a spin-loop. AArch64 however provides an architectural timer with an event stream which can be enabled and waited upon with a few lines of assembly. This is more efficient and also avoids an issue on Arm FVPs where polling the counter frequently takes much longer than real-time.
Therefore, overrride __udelay for all armv8 boards to use the event stream.
Macros for the relevant instructions already existed for mach-exynos, so pull these up to a common header file.
I have tested this patch chain on qemu-system-aarch64 (virt,secure=on), FVP_Base_RevC and a Raspberry Pi 4.
Peter Hoyes (2):
arm: Move sev() and wfe() definitions to common Arm header file
armv8: generic_timer: Use event stream for udelay
arch/arm/cpu/armv8/Kconfig | 8 +++++++ arch/arm/cpu/armv8/generic_timer.c | 27 ++++++++++++++++++++++ arch/arm/include/asm/system.h | 15 ++++++++++-- arch/arm/mach-exynos/include/mach/system.h | 19 --------------- 4 files changed, 48 insertions(+), 21 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 arm: Move sev() and wfe() definitions to common Arm header file
From: Peter Hoyes <[email protected]>
The sev() and wfe() asm macros are currently defined only for mach-exynos. As these are common Arm instructions, move them to the common asm/system.h header file, for both Armv7 and Armv8, so they can be used by other machines.
wfe may theoretically trigger a context switch if an interrupt occurs so add a memory barrier to this call.
--- arch/arm/include/asm/system.h | 9 +++++++++ arch/arm/mach-exynos/include/mach/system.h | 19 ------------------- 2 files changed, 9 insertions(+), 19 deletions(-)
arch/arm/include/asm/system.h +9 -0
@@ -154,6 +154,13 @@ enum dcache_option { "wfi" : : : "memory"); \ }) +#define wfe () \ + ({asm volatile( \ + "wfe" : : : "memory"); \ + }) + +#define sev () asm volatile("sev") +static inline unsigned int current_el (void ) {unsigned long el;
@@ -369,6 +376,8 @@ void switch_to_hypervisor_ret(void);#ifdef __ARM_ARCH_7A__#define wfi () __asm__ __volatile__ ("wfi" : : : "memory") +#define wfe () __asm__ __volatile__ ("wfe" : : : "memory") +#define sev () __asm__ __volatile__ ("sev")#else #define wfi ()#endif
arch/arm/mach-exynos/include/mach/system.h +0 -19
@@ -36,25 +36,6 @@ struct exynos5_sysreg {#define USB20_PHY_CFG_HOST_LINK_EN (1 << 0) -/* -* This instruction causes an event to be signaled to all cores -* within a multiprocessor system. If SEV is implemented, -* WFE must also be implemented. -*/ -#define sev () __asm__ __volatile__ ("sev\n\t" : : ); -/* -* If the Event Register is not set, WFE suspends execution until -* one of the following events occurs: -* - an IRQ interrupt, unless masked by the CPSR I-bit -* - an FIQ interrupt, unless masked by the CPSR F-bit -* - an Imprecise Data abort, unless masked by the CPSR A-bit -* - a Debug Entry request, if Debug is enabled -* - an Event signaled by another processor using the SEV instruction. -* If the Event Register is set, WFE clears it and returns immediately. -* If WFE is implemented, SEV must also be implemented. -*/ -#define wfe () __asm__ __volatile__ ("wfe\n\t" : : ); -/* Move 0xd3 value to CPSR register to enable SVC mode */ #define svc32_mode_en () __asm__ __volatile__ \ ("@ I&F disable, Mode: 0x13 - SVC\n\t" \
Signature · 1 line
2.34.1
Re: [PATCH 1/2] arm: Move sev() and wfe() definitions to common Arm header file
On Tue, 23 Apr 2024 09:10:04 +0100
Peter Hoyes <[email protected]> wrote:
Hi,
Quoted 11 lines
From: Peter Hoyes <[email protected]>
The
sev()andwfe()asm macros are currently defined only for mach-exynos. As these are common Arm instructions, move them to the commonasm/system.hheader file, for both Armv7 and Armv8, so they can be used by other machines.wfe may theoretically trigger a context switch if an interrupt occurs so add a memory barrier to this call.
Signed-off-by Peter Hoyes <[email protected]>
Looks alright to me, and the memory barrier for wfe looks justified.
For the records: we couldn't come up with a reason why "sev" would need one as well, so even while the kernel has this, this seems to be more a copy&paste incident rather than something intentional.
Cheers,
Andre
Quoted 62 lines
--- arch/arm/include/asm/system.h | 9 +++++++++ arch/arm/mach-exynos/include/mach/system.h | 19 ------------------- 2 files changed, 9 insertions(+), 19 deletions(-)arch/arm/include/asm/system.h +9
-0@@ -154,6 +154,13 @@ enum dcache_option { "wfi" : : : "memory"); \ }) +#define wfe () \ + ({asm volatile( \ + "wfe" : : : "memory"); \ + }) + +#define sev () asm volatile("sev") +static inline unsigned int current_el (void ) {unsigned long el;@@ -369,6 +376,8 @@ void switch_to_hypervisor_ret(void);#ifdef __ARM_ARCH_7A__#define wfi () __asm__ __volatile__ ("wfi" : : : "memory") +#define wfe () __asm__ __volatile__ ("wfe" : : : "memory") +#define sev () __asm__ __volatile__ ("sev")#else #define wfi ()#endif arch/arm/mach-exynos/include/mach/system.h +0
-19@@ -36,25 +36,6 @@ struct exynos5_sysreg {#define USB20_PHY_CFG_HOST_LINK_EN (1 << 0) -/* -* This instruction causes an event to be signaled to all cores -* within a multiprocessor system. If SEV is implemented, -* WFE must also be implemented. -*/ -#define sev () __asm__ __volatile__ ("sev\n\t" : : ); -/* -* If the Event Register is not set, WFE suspends execution until -* one of the following events occurs: -* - an IRQ interrupt, unless masked by the CPSR I-bit -* - an FIQ interrupt, unless masked by the CPSR F-bit -* - an Imprecise Data abort, unless masked by the CPSR A-bit -* - a Debug Entry request, if Debug is enabled -* - an Event signaled by another processor using the SEV instruction. -* If the Event Register is set, WFE clears it and returns immediately. -* If WFE is implemented, SEV must also be implemented. -*/ -#define wfe () __asm__ __volatile__ ("wfe\n\t" : : ); -/* Move 0xd3 value to CPSR register to enable SVC mode */ #define svc32_mode_en () __asm__ __volatile__ \ ("@ I&F disable, Mode: 0x13 - SVC\n\t" \
PATCH 2/2 armv8: generic_timer: Use event stream for udelay
In reply to Peter HoyesFrom: Peter Hoyes <[email protected]>
Polling cntpct_el0 in a tight loop for delays is inefficient. This is particularly apparent on Arm FVPs, which do not simulate real time, meaning that a 1s sleep can take a couple of orders of magnitude longer to execute in wall time.
If running at EL2 or above (where CNTHCTL_EL2 is available), enable the cntpct_el0 event stream temporarily and use wfe to implement the delay more efficiently. The event period is chosen as a trade-off between efficiency and the fact that Arm FVPs do not typically simulate real time.
This is only implemented for Armv8 boards, where an architectural timer exists.
Some mach-socfpga AArch64 boards already override __udelay to make it always inline, so guard the functionality with a new ARMV8_UDELAY_EVENT_STREAM Kconfig, enabled by default.
--- arch/arm/cpu/armv8/Kconfig | 8 ++++++++ arch/arm/cpu/armv8/generic_timer.c | 27 +++++++++++++++++++++++++++ arch/arm/include/asm/system.h | 6 ++++-- 3 files changed, 39 insertions(+), 2 deletions(-)
arch/arm/cpu/armv8/Kconfig +8 -0
@@ -191,6 +191,14 @@ config ARMV8_EA_EL3_FIRST Exception handling at all exception levels for External Abort and SError interrupt exception are taken in EL3. +config ARMV8_UDELAY_EVENT_STREAM + bool "Use the event stream for udelay" + default y if !ARCH_SOCFPGA + help + Use the event stream provided by the AArch64 architectural timer for + delays. This is more efficient than the default polling + implementation. + menuconfig ARMV8_CRYPTO bool "ARM64 Accelerated Cryptographic Algorithms"
arch/arm/cpu/armv8/generic_timer.c +27 -0
@@ -115,3 +115,30 @@ ulong timer_get_boot_us(void)return val /get_tbclk (); } + +#if CONFIG_IS_ENABLED (ARMV8_UDELAY_EVENT_STREAM ) +void __udelay (unsigned long usec) +{ +u64 target =get_ticks () +usec_to_tick (usec); + +/* At EL2 or above, use the event stream to avoid polling CNTPCT_EL0 so often */ +if (current_el () >=2 ) { +u32 cnthctl_val; +const u8 event_period =0x7 ; + + asmvolatile ("mrs %0, cnthctl_el2" :"=r" (cnthctl_val)); + asmvolatile ("msr cnthctl_el2, %0" : :"r" + (cnthctl_val |CNTHCTL_EL2_EVNT_EN |CNTHCTL_EL2_EVNT_I (event_period))); + +while (get_ticks () + (1ULL << event_period) <= target) +wfe (); + +/* Reset the event stream */ + asmvolatile ("msr cnthctl_el2, %0" : :"r" (cnthctl_val)); + } + +/* Fall back to polling CNTPCT_EL0 */ +while (get_ticks () <= target) + ; +} +#endif
arch/arm/include/asm/system.h +4 -2
@@ -69,8 +69,10 @@/* * CNTHCTL_EL2 bits definitions */ -#define CNTHCTL_EL2_EL1PCEN_EN (1 << 1)/* Physical timer regs accessible */ -#define CNTHCTL_EL2_EL1PCTEN_EN (1 << 0)/* Physical counter accessible */ +#define CNTHCTL_EL2_EVNT_EN BIT(2)/* Enable the event stream */ +#define CNTHCTL_EL2_EVNT_I (val) ((val) << 4)/* Event stream trigger bits */ +#define CNTHCTL_EL2_EL1PCEN_EN (1 << 1)/* Physical timer regs accessible */ +#define CNTHCTL_EL2_EL1PCTEN_EN (1 << 0)/* Physical counter accessible */ /* * HCR_EL2 bits definitions
Signature · 1 line
2.34.1
Re: [PATCH 2/2] armv8: generic_timer: Use event stream for udelay
Hi Peter,
Quoted 51 lines · Peter Hoyes
From: Peter Hoyes <[email protected]>
Polling
cntpct_el0in a tight loop for delays is inefficient. This is particularly apparent on Arm FVPs, which do not simulate real time, meaning that a 1s sleep can take a couple of orders of magnitude longer to execute in wall time.If running at EL2 or above (where
CNTHCTL_EL2is available), enable thecntpct_el0event stream temporarily and use wfe to implement the delay more efficiently. The event period is chosen as a trade-off between efficiency and the fact that Arm FVPs do not typically simulate real time.This is only implemented for Armv8 boards, where an architectural timer exists.
Some mach-socfpga AArch64 boards already override
__udelayto make it always inline, so guard the functionality with a newARMV8_UDELAY_EVENT_STREAMKconfig, enabled by default.Signed-off-by Peter Hoyes <[email protected]>--- arch/arm/cpu/armv8/Kconfig | 8 ++++++++ arch/arm/cpu/armv8/generic_timer.c | 27 +++++++++++++++++++++++++++ arch/arm/include/asm/system.h | 6 ++++-- 3 files changed, 39 insertions(+), 2 deletions(-)arch/arm/cpu/armv8/Kconfig +8
-0@@ -191,6 +191,14 @@ config ARMV8_EA_EL3_FIRST Exception handling at all exception levels for External Abort and SError interrupt exception are taken in EL3. +config ARMV8_UDELAY_EVENT_STREAM + bool "Use the event stream for udelay" + default y if !ARCH_SOCFPGA + help + Use the event stream provided by the AArch64 architectural timer for + delays. This is more efficient than the default polling + implementation. + menuconfig ARMV8_CRYPTO bool "ARM64 Accelerated Cryptographic Algorithms"arch/arm/cpu/armv8/generic_timer.c +0
-0
arch/arm/cpu/armv8/generic_timer.c ulong timer_get_boot_us(void) · line 123
Quoted 9 lines
arch/arm/cpu/armv8/generic_timer.c +6
-0@@ -115,3 +115,30 @@ ulong timer_get_boot_us(void)return val /get_tbclk (); } + +#if CONFIG_IS_ENABLED (ARMV8_UDELAY_EVENT_STREAM ) +void __udelay (unsigned long usec) +{ +u64 target =get_ticks () +usec_to_tick (usec); +
This can theoretically overflow, do we have any guarantee this cannot happen in real life, like... we would need U-Boot to be running for 100 years without being powered-down/reset or something like that? Can we document this assumption? Does this make sense?
arch/arm/cpu/armv8/generic_timer.c ulong timer_get_boot_us(void) · line 133
Quoted 10 lines
arch/arm/cpu/armv8/generic_timer.c +10
-0+/* At EL2 or above, use the event stream to avoid polling CNTPCT_EL0 so often */ +if (current_el () >=2 ) { +u32 cnthctl_val; +const u8 event_period =0x7 ; + + asmvolatile ("mrs %0, cnthctl_el2" :"=r" (cnthctl_val)); + asmvolatile ("msr cnthctl_el2, %0" : :"r" + (cnthctl_val |CNTHCTL_EL2_EVNT_EN |CNTHCTL_EL2_EVNT_I (event_period))); + +while (get_ticks () + (1ULL << event_period) <= target)
This could be an overflow as well.
arch/arm/cpu/armv8/generic_timer.c ulong timer_get_boot_us(void) · line 141
Quoted 8 lines
arch/arm/cpu/armv8/generic_timer.c +8
-0+wfe (); + +/* Reset the event stream */ + asmvolatile ("msr cnthctl_el2, %0" : :"r" (cnthctl_val)); + } + +/* Fall back to polling CNTPCT_EL0 */ +while (get_ticks () <= target)
get_ticks() could wrap around here maybe?
Cheers,
Quentin
Re: [PATCH 2/2] armv8: generic_timer: Use event stream for udelay
Hi Quentin,
Quoted 79 lines · Quentin Schulz
Hi Peter,
Quoted 50 lines · Peter Hoyes
From: Peter Hoyes <[email protected]>
Polling
cntpct_el0in a tight loop for delays is inefficient. This is particularly apparent on Arm FVPs, which do not simulate real time, meaning that a 1s sleep can take a couple of orders of magnitude longer to execute in wall time.If running at EL2 or above (where
CNTHCTL_EL2is available), enable thecntpct_el0event stream temporarily and use wfe to implement the delay more efficiently. The event period is chosen as a trade-off between efficiency and the fact that Arm FVPs do not typically simulate real time.This is only implemented for Armv8 boards, where an architectural timer exists.
Some mach-socfpga AArch64 boards already override
__udelayto make it always inline, so guard the functionality with a newARMV8_UDELAY_EVENT_STREAMKconfig, enabled by default.Signed-off-by Peter Hoyes <[email protected]>---
arch/arm/cpu/armv8/Kconfig | 8 ++++++++arch/arm/cpu/armv8/generic_timer.c| 27 +++++++++++++++++++++++++++ arch/arm/include/asm/system.h | 6 ++++--3 files changed, 39 insertions(+), 2 deletions(-)arch/arm/cpu/armv8/Kconfig +0
-0@@ -191,6 +191,14 @@ config ARMV8_EA_EL3_FIRSTException handling at all exception levels for External Abort and SError interrupt exception are taken in EL3.
+configARMV8_UDELAY_EVENT_STREAM
+ bool "Use the event stream for udelay"
+ default y if !ARCH_SOCFPGA
+ help
+ Use the event stream provided by the AArch64 architectural
timer for
+ delays. This is more efficient than the default polling
+ implementation.
+
menuconfigARMV8_CRYPTO
bool "ARM64 Accelerated Cryptographic Algorithms"
diff --gita/arch/arm/cpu/armv8/generic_timer.cb/arch/arm/cpu/armv8/generic_timer.cindex8f83372cbc..e18b5c8187100644arch/arm/cpu/armv8/generic_timer.c ulong timer_get_boot_us(void) · line 122
Quoted 8 lines
arch/arm/cpu/armv8/generic_timer.c +6
-0@@ -115,3 +115,30 @@ ulong timer_get_boot_us(void) return val / get_tbclk(); } + +#if CONFIG_IS_ENABLED (ARMV8_UDELAY_EVENT_STREAM ) +void __udelay (unsigned long usec) +{ + u64 target = get_ticks() + usec_to_tick(usec); +This can theoretically overflow, do we have any guarantee this cannot happen in real life, like... we would need U-Boot to be running for 100 years without being powered-down/reset or something like that? Can we document this assumption? Does this make sense?
Quoted 4 lines
arch/arm/cpu/armv8/generic_timer.c +1
-0+ /* At EL2 or above, use the event stream to avoid polling
CNTPCT_EL0so often */
+ if (current_el()>= 2) {
+ u32cnthctl_val;Quoted 8 lines
+ const u8
event_period=0x7;
+
+ asmvolatile("mrs %0, cnthctl_el2" : "=r" (cnthctl_val)); + asm volatile("msrcnthctl_el2, %0" : : "r"
+ (cnthctl_val|CNTHCTL_EL2_EVNT_EN|CNTHCTL_EL2_EVNT_I(event_period)));
+
+ while (get_ticks()+ (1ULL <<event_period) <= target)This could be an overflow as well.
Quoted 10 lines
arch/arm/cpu/armv8/generic_timer.c ulong timer_get_boot_us(void) · line 141
Quoted 8 lines
+
wfe();arch/arm/cpu/armv8/generic_timer.c +7
-0+ + /* Reset the event stream */ + asm volatile("msr cnthctl_el2, %0" : : "r" (cnthctl_val)); + } + + /* Fall back to polling CNTPCT_EL0 */ + while (get_ticks() <= target)
get_ticks()could wrap around here maybe?
I don't see a problem here. It's using u64s and the maximum COUNTER_FREQUENCY in the U-Boot source is 200MHz, which gives enough ticks for about 3 millennia.
Additionally, the underlying "while" condition is the same as the existing weak __udelay implementation
(https://source.denx.de/u-boot/u-boot/-/blob/master/lib/time.c?ref_type=heads#L181), so this change doesn't affect the overflow limit.
Thanks,
Peter
Re: [PATCH 2/2] armv8: generic_timer: Use event stream for udelay
In reply to Quentin SchulzQuoted 55 lines · On Tue, 23 Apr 2024 12:55:55 +0200 Quentin Schulz…
Hi Peter,
Quoted 52 lines · Peter Hoyes
From: Peter Hoyes <[email protected]>
Polling
cntpct_el0in a tight loop for delays is inefficient. This is particularly apparent on Arm FVPs, which do not simulate real time, meaning that a 1s sleep can take a couple of orders of magnitude longer to execute in wall time.If running at EL2 or above (where
CNTHCTL_EL2is available), enable thecntpct_el0event stream temporarily and use wfe to implement the delay more efficiently. The event period is chosen as a trade-off between efficiency and the fact that Arm FVPs do not typically simulate real time.This is only implemented for Armv8 boards, where an architectural timer exists.
Some mach-socfpga AArch64 boards already override
__udelayto make it always inline, so guard the functionality with a newARMV8_UDELAY_EVENT_STREAMKconfig, enabled by default.Signed-off-by Peter Hoyes <[email protected]>--- arch/arm/cpu/armv8/Kconfig | 8 ++++++++ arch/arm/cpu/armv8/generic_timer.c | 27 +++++++++++++++++++++++++++ arch/arm/include/asm/system.h | 6 ++++-- 3 files changed, 39 insertions(+), 2 deletions(-)arch/arm/cpu/armv8/Kconfig +8
-0@@ -191,6 +191,14 @@ config ARMV8_EA_EL3_FIRST Exception handling at all exception levels for External Abort and SError interrupt exception are taken in EL3. +config ARMV8_UDELAY_EVENT_STREAM + bool "Use the event stream for udelay" + default y if !ARCH_SOCFPGA + help + Use the event stream provided by the AArch64 architectural timer for + delays. This is more efficient than the default polling + implementation. + menuconfig ARMV8_CRYPTO bool "ARM64 Accelerated Cryptographic Algorithms"arch/arm/cpu/armv8/generic_timer.c +0
-0@@ -115,3 +115,30 @@ ulong timer_get_boot_us(void)
Quoted 13 lines
arch/arm/cpu/armv8/generic_timer.c ulong timer_get_boot_us(void) · line 123
Quoted 8 lines
arch/arm/cpu/armv8/generic_timer.c +6
-0return val /get_tbclk (); } + +#if CONFIG_IS_ENABLED (ARMV8_UDELAY_EVENT_STREAM ) +void __udelay (unsigned long usec) +{ +u64 target =get_ticks () +usec_to_tick (usec); +This can theoretically overflow, do we have any guarantee this cannot happen in real life, like... we would need U-Boot to be running for 100 years without being powered-down/reset or something like that? Can we document this assumption? Does this make sense?
The Arm ARM guarantees a "Roll-over time of not less than 40 years." (Armv8 ARM 0487K.a D12.1.2 "The system counter").
So that's not the 100 years you are asking for, but I guess still good enough?
Cheers,
Andre
Quoted 27 lines
arch/arm/cpu/armv8/generic_timer.c ulong timer_get_boot_us(void) · line 133
Quoted 11 lines
arch/arm/cpu/armv8/generic_timer.c +10
-0+/* At EL2 or above, use the event stream to avoid polling CNTPCT_EL0 so often */ +if (current_el () >=2 ) { +u32 cnthctl_val; +const u8 event_period =0x7 ; + + asmvolatile ("mrs %0, cnthctl_el2" :"=r" (cnthctl_val)); + asmvolatile ("msr cnthctl_el2, %0" : :"r" + (cnthctl_val |CNTHCTL_EL2_EVNT_EN |CNTHCTL_EL2_EVNT_I (event_period))); + +while (get_ticks () + (1ULL << event_period) <= target)This could be an overflow as well.
arch/arm/cpu/armv8/generic_timer.c ulong timer_get_boot_us(void) · line 141
Quoted 8 lines
arch/arm/cpu/armv8/generic_timer.c +8
-0+wfe (); + +/* Reset the event stream */ + asmvolatile ("msr cnthctl_el2, %0" : :"r" (cnthctl_val)); + } + +/* Fall back to polling CNTPCT_EL0 */ +while (get_ticks () <= target)
get_ticks()could wrap around here maybe?Cheers,
Quentin
Re: [PATCH 2/2] armv8: generic_timer: Use event stream for udelay
Hi Peter, Andre,
Quoted 72 lines · Andre Przywara
Quoted 55 lines · On Tue, 23 Apr 2024 12:55:55 +0200 Quentin Schulz…
Hi Peter,
Quoted 52 lines · Peter Hoyes
From: Peter Hoyes <[email protected]>
Polling
cntpct_el0in a tight loop for delays is inefficient. This is particularly apparent on Arm FVPs, which do not simulate real time, meaning that a 1s sleep can take a couple of orders of magnitude longer to execute in wall time.If running at EL2 or above (where
CNTHCTL_EL2is available), enable thecntpct_el0event stream temporarily and use wfe to implement the delay more efficiently. The event period is chosen as a trade-off between efficiency and the fact that Arm FVPs do not typically simulate real time.This is only implemented for Armv8 boards, where an architectural timer exists.
Some mach-socfpga AArch64 boards already override
__udelayto make it always inline, so guard the functionality with a newARMV8_UDELAY_EVENT_STREAMKconfig, enabled by default.Signed-off-by Peter Hoyes <[email protected]>--- arch/arm/cpu/armv8/Kconfig | 8 ++++++++ arch/arm/cpu/armv8/generic_timer.c | 27 +++++++++++++++++++++++++++ arch/arm/include/asm/system.h | 6 ++++-- 3 files changed, 39 insertions(+), 2 deletions(-)arch/arm/cpu/armv8/Kconfig +8
-0@@ -191,6 +191,14 @@ config ARMV8_EA_EL3_FIRST Exception handling at all exception levels for External Abort and SError interrupt exception are taken in EL3. +config ARMV8_UDELAY_EVENT_STREAM + bool "Use the event stream for udelay" + default y if !ARCH_SOCFPGA + help + Use the event stream provided by the AArch64 architectural timer for + delays. This is more efficient than the default polling + implementation. + menuconfig ARMV8_CRYPTO bool "ARM64 Accelerated Cryptographic Algorithms"arch/arm/cpu/armv8/generic_timer.c +0
-0@@ -115,3 +115,30 @@ ulong timer_get_boot_us(void)Quoted 13 lines
arch/arm/cpu/armv8/generic_timer.c ulong timer_get_boot_us(void) · line 123
Quoted 8 lines
arch/arm/cpu/armv8/generic_timer.c +6
-0return val /get_tbclk (); } + +#if CONFIG_IS_ENABLED (ARMV8_UDELAY_EVENT_STREAM ) +void __udelay (unsigned long usec) +{ +u64 target =get_ticks () +usec_to_tick (usec); +This can theoretically overflow, do we have any guarantee this cannot happen in real life, like... we would need U-Boot to be running for 100 years without being powered-down/reset or something like that? Can we document this assumption? Does this make sense?
Quoted 5 lines
The Arm ARM guarantees a "Roll-over time of not less than 40 years." (Armv8 ARM 0487K.a D12.1.2 "The system counter").
So that's not the 100 years you are asking for, but I guess still good enough?
I guess it is, since it is also stored in a u64 and is reset to 0 upon start-up according to the ARM. I also assume nobody is going to add a udelay of years in their code :) (and if they do, they would probably figure out something's wrong before it reaches the final products :) ).
Thanks all for the pointers to reference manuals and current implementations.
Cheers,
Quentin
Re: [PATCH 2/2] armv8: generic_timer: Use event stream for udelay
In reply to Peter HoyesOn Tue, 23 Apr 2024 09:10:05 +0100
Peter Hoyes <[email protected]> wrote:
Hi,
Quoted 13 lines
From: Peter Hoyes <[email protected]>
Polling
cntpct_el0in a tight loop for delays is inefficient. This is particularly apparent on Arm FVPs, which do not simulate real time, meaning that a 1s sleep can take a couple of orders of magnitude longer to execute in wall time.If running at EL2 or above (where
CNTHCTL_EL2is available), enable thecntpct_el0event stream temporarily and use wfe to implement the delay more efficiently. The event period is chosen as a trade-off between efficiency and the fact that Arm FVPs do not typically simulate real time.
Quoted 6 lines
This is only implemented for Armv8 boards, where an architectural timer exists.
Some mach-socfpga AArch64 boards already override
__udelayto make it always inline, so guard the functionality with a newARMV8_UDELAY_EVENT_STREAMKconfig, enabled by default.
So the code looks alright, and it works for me, tested on some hardware I just had at hand.
However I am a bit worried about the scope of this patch. While it's indeed purely architectural and should work on all systems, I think we need to be careful with messing with the arch timer *while the OS is running*. U-Boot code will run for UEFI runtime services and for serving PSCI on some platforms, and I guess messing with CNTHCTL_EL2 is not a good idea then. And udelay sounds like a basic function that this code might use.
So I wonder if we should limit this to the Arm FVPs for now? To not create subtle and hard-to-diagnose problems for a lot of boards?
Or maybe there is some mechanism to limit this to U-Boot boot time/UEFI boot services?
Quoted 12 lines
Signed-off-by Peter Hoyes <[email protected]>--- arch/arm/cpu/armv8/Kconfig | 8 ++++++++ arch/arm/cpu/armv8/generic_timer.c | 27 +++++++++++++++++++++++++++ arch/arm/include/asm/system.h | 6 ++++-- 3 files changed, 39 insertions(+), 2 deletions(-)arch/arm/cpu/armv8/Kconfig +0
-0
arch/arm/cpu/armv8/Kconfig config ARMV8_EA_EL3_FIRST · line 196
Quoted 6 lines
arch/arm/cpu/armv8/Kconfig +3
-0@@ -191,6 +191,14 @@ config ARMV8_EA_EL3_FIRST Exception handling at all exception levels for External Abort and SError interrupt exception are taken in EL3. +config ARMV8_UDELAY_EVENT_STREAM + bool "Use the event stream for udelay" + default y if !ARCH_SOCFPGA
As described above, change this to something like "... if ARCH_VEXPRESS64".
Cheers,
Andre
Quoted 60 lines
arch/arm/cpu/armv8/Kconfig +5
-0+ help + Use the event stream provided by the AArch64 architectural timer for + delays. This is more efficient than the default polling + implementation. + menuconfig ARMV8_CRYPTO bool "ARM64 Accelerated Cryptographic Algorithms"arch/arm/cpu/armv8/generic_timer.c +27
-0@@ -115,3 +115,30 @@ ulong timer_get_boot_us(void)return val /get_tbclk (); } + +#if CONFIG_IS_ENABLED (ARMV8_UDELAY_EVENT_STREAM ) +void __udelay (unsigned long usec) +{ +u64 target =get_ticks () +usec_to_tick (usec); + +/* At EL2 or above, use the event stream to avoid polling CNTPCT_EL0 so often */ +if (current_el () >=2 ) { +u32 cnthctl_val; +const u8 event_period =0x7 ; + + asmvolatile ("mrs %0, cnthctl_el2" :"=r" (cnthctl_val)); + asmvolatile ("msr cnthctl_el2, %0" : :"r" + (cnthctl_val |CNTHCTL_EL2_EVNT_EN |CNTHCTL_EL2_EVNT_I (event_period))); + +while (get_ticks () + (1ULL << event_period) <= target) +wfe (); + +/* Reset the event stream */ + asmvolatile ("msr cnthctl_el2, %0" : :"r" (cnthctl_val)); + } + +/* Fall back to polling CNTPCT_EL0 */ +while (get_ticks () <= target) + ; +} +#endif arch/arm/include/asm/system.h +4
-2@@ -69,8 +69,10 @@/* * CNTHCTL_EL2 bits definitions */ -#define CNTHCTL_EL2_EL1PCEN_EN (1 << 1)/* Physical timer regs accessible */ -#define CNTHCTL_EL2_EL1PCTEN_EN (1 << 0)/* Physical counter accessible */ +#define CNTHCTL_EL2_EVNT_EN BIT(2)/* Enable the event stream */ +#define CNTHCTL_EL2_EVNT_I (val) ((val) << 4)/* Event stream trigger bits */ +#define CNTHCTL_EL2_EL1PCEN_EN (1 << 1)/* Physical timer regs accessible */ +#define CNTHCTL_EL2_EL1PCTEN_EN (1 << 0)/* Physical counter accessible */ /* * HCR_EL2 bits definitions
Re: [PATCH 2/2] armv8: generic_timer: Use event stream for udelay
Quoted 54 lines · Andre Przywara
On Tue, 23 Apr 2024 09:10:05 +0100
Peter Hoyes <[email protected]> wrote:Hi,
Quoted 13 lines
From: Peter Hoyes <[email protected]>
Polling
cntpct_el0in a tight loop for delays is inefficient. This is particularly apparent on Arm FVPs, which do not simulate real time, meaning that a 1s sleep can take a couple of orders of magnitude longer to execute in wall time.If running at EL2 or above (where
CNTHCTL_EL2is available), enable thecntpct_el0event stream temporarily and use wfe to implement the delay more efficiently. The event period is chosen as a trade-off between efficiency and the fact that Arm FVPs do not typically simulate real time.Quoted 6 lines
This is only implemented for Armv8 boards, where an architectural timer exists.
Some mach-socfpga AArch64 boards already override
__udelayto make it always inline, so guard the functionality with a newARMV8_UDELAY_EVENT_STREAMKconfig, enabled by default.So the code looks alright, and it works for me, tested on some hardware I just had at hand.
However I am a bit worried about the scope of this patch. While it's indeed purely architectural and should work on all systems, I think we need to be careful with messing with the arch timer *while the OS is running*. U-Boot code will run for UEFI runtime services and for serving PSCI on some platforms, and I guess messing withCNTHCTL_EL2is not a good idea then. And udelay sounds like a basic function that this code might use.
So I wonder if we should limit this to the Arm FVPs for now? To not create subtle and hard-to-diagnose problems for a lot of boards?Or maybe there is some mechanism to limit this to U-Boot boot time/UEFI boot services?
Quoted 15 lines
Signed-off-by Peter Hoyes <[email protected]>--- arch/arm/cpu/armv8/Kconfig | 8 ++++++++ arch/arm/cpu/armv8/generic_timer.c | 27 +++++++++++++++++++++++++++ arch/arm/include/asm/system.h | 6 ++++-- 3 files changed, 39 insertions(+), 2 deletions(-)arch/arm/cpu/armv8/Kconfig +0
-0@@ -191,6 +191,14 @@ config ARMV8_EA_EL3_FIRST Exception handling at all exception levels for External Abort and SError interrupt exception are taken in EL3.
Quoted 8 lines
arch/arm/cpu/armv8/Kconfig config ARMV8_EA_EL3_FIRST · line 196
Quoted 3 lines
arch/arm/cpu/armv8/Kconfig +3
-0+config ARMV8_UDELAY_EVENT_STREAM + bool "Use the event stream for udelay" + default y if !ARCH_SOCFPGAAs described above, change this to something like "... if
ARCH_VEXPRESS64".Cheers,
Andre
Agreed, this is limited to ARCH_VEXPRESS64 in v2.
Peter
Quoted 60 lines
Quoted 60 lines
arch/arm/cpu/armv8/Kconfig +5
-0+ help + Use the event stream provided by the AArch64 architectural timer for + delays. This is more efficient than the default polling + implementation. + menuconfig ARMV8_CRYPTO bool "ARM64 Accelerated Cryptographic Algorithms"arch/arm/cpu/armv8/generic_timer.c +27
-0@@ -115,3 +115,30 @@ ulong timer_get_boot_us(void)return val /get_tbclk (); } + +#if CONFIG_IS_ENABLED (ARMV8_UDELAY_EVENT_STREAM ) +void __udelay (unsigned long usec) +{ +u64 target =get_ticks () +usec_to_tick (usec); + +/* At EL2 or above, use the event stream to avoid polling CNTPCT_EL0 so often */ +if (current_el () >=2 ) { +u32 cnthctl_val; +const u8 event_period =0x7 ; + + asmvolatile ("mrs %0, cnthctl_el2" :"=r" (cnthctl_val)); + asmvolatile ("msr cnthctl_el2, %0" : :"r" + (cnthctl_val |CNTHCTL_EL2_EVNT_EN |CNTHCTL_EL2_EVNT_I (event_period))); + +while (get_ticks () + (1ULL << event_period) <= target) +wfe (); + +/* Reset the event stream */ + asmvolatile ("msr cnthctl_el2, %0" : :"r" (cnthctl_val)); + } + +/* Fall back to polling CNTPCT_EL0 */ +while (get_ticks () <= target) + ; +} +#endif arch/arm/include/asm/system.h +4
-2@@ -69,8 +69,10 @@/* * CNTHCTL_EL2 bits definitions */ -#define CNTHCTL_EL2_EL1PCEN_EN (1 << 1)/* Physical timer regs accessible */ -#define CNTHCTL_EL2_EL1PCTEN_EN (1 << 0)/* Physical counter accessible */ +#define CNTHCTL_EL2_EVNT_EN BIT(2)/* Enable the event stream */ +#define CNTHCTL_EL2_EVNT_I (val) ((val) << 4)/* Event stream trigger bits */ +#define CNTHCTL_EL2_EL1PCEN_EN (1 << 1)/* Physical timer regs accessible */ +#define CNTHCTL_EL2_EL1PCTEN_EN (1 << 0)/* Physical counter accessible */ /* * HCR_EL2 bits definitions