Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D667EC4345F for ; Wed, 24 Apr 2024 10:25:46 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0EEC988E23; Wed, 24 Apr 2024 12:25:45 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id B324788E23; Wed, 24 Apr 2024 12:25:43 +0200 (CEST) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id CE43488E0B for ; Wed, 24 Apr 2024 12:25:40 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=peter.hoyes@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CBF6F339; Wed, 24 Apr 2024 03:26:07 -0700 (PDT) Received: from [10.1.198.41] (unknown [10.1.198.41]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9FD1B3F73F; Wed, 24 Apr 2024 03:25:38 -0700 (PDT) Message-ID: <51da86c6-ab77-4967-b879-e2261fec14e0@arm.com> Date: Wed, 24 Apr 2024 11:25:29 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] armv8: generic_timer: Use event stream for udelay To: Quentin Schulz , u-boot@lists.denx.de Cc: trini@konsulko.com, andre.przywara@arm.com, mk7.kang@samsung.com, ilias.apalodimas@linaro.org, wqu@suse.com, neil.armstrong@linaro.org, sr@denx.de, michal.simek@amd.com, patrick.delaunay@foss.st.com, sjg@chromium.org, patrice.chotard@foss.st.com References: <20240423081005.23218-1-peter.hoyes@arm.com> <20240423081005.23218-3-peter.hoyes@arm.com> <8ccd1c8a-5ab2-44ad-b49a-26bff13187bd@theobroma-systems.com> Content-Language: en-US From: Peter Hoyes In-Reply-To: <8ccd1c8a-5ab2-44ad-b49a-26bff13187bd@theobroma-systems.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Hi Quentin, On 4/23/24 11:55, Quentin Schulz wrote: > Hi Peter, > > On 4/23/24 10:10, Peter Hoyes wrote: >> From: Peter Hoyes >> >> 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. >> >> Signed-off-by: Peter Hoyes >> --- >>   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(-) >> >> diff --git a/arch/arm/cpu/armv8/Kconfig b/arch/arm/cpu/armv8/Kconfig >> index 9f0fb369f7..544c5e2d74 100644 >> --- a/arch/arm/cpu/armv8/Kconfig >> +++ b/arch/arm/cpu/armv8/Kconfig >> @@ -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" >>   diff --git a/arch/arm/cpu/armv8/generic_timer.c >> b/arch/arm/cpu/armv8/generic_timer.c >> index 8f83372cbc..e18b5c8187 100644 >> --- a/arch/arm/cpu/armv8/generic_timer.c >> +++ b/arch/arm/cpu/armv8/generic_timer.c >> @@ -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? > >> +    /* 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; >> + >> +        asm volatile("mrs %0, cnthctl_el2" : "=r" (cnthctl_val)); >> +        asm volatile("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. > >> +            wfe(); >> + >> +        /* 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