From mboxrd@z Thu Jan 1 00:00:00 1970 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 35253ECAAD3 for ; Thu, 1 Sep 2022 09:27:43 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C1CE584118; Thu, 1 Sep 2022 11:27:40 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1662024460; bh=PcaeAuqX1vDnrjNQLcZbr9pOseG3wUZaYx2OUHYUfik=; h=Date:Subject:To:Cc:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=qi8Gg4dJLSdvaXLfPDmIe1/8hxLBR1T4B0WG6Brs2fNG+hB15IWilhfBGtJGXtNWq a2TUjkwrUtjfiGYV1Ug9AvkQEReK2ZoUXWiSO5jeX91afBMqWNjnXfu5AB7IrRwa5X IHSwAo46HxtzaxRU++tDI/M4eA2RLNNE9OrGFlm988rqULg6A/zHPQAC1GrGup8Uli 72nFNzNEg/kNjQlzUmWIiouaZLBcJ+YILRvDe6vd3fQy9VZT3k1ew9rBMxO2lqkNu2 YJ7J0xHgYxVAiJjxc5VG2uww6GfWbGvHSAxnBcNv2Hy45FVl20/c95LbwsLFWdSlgg l4z99cj1Ooo5w== Received: by phobos.denx.de (Postfix, from userid 109) id 3E02284118; Thu, 1 Sep 2022 11:27:39 +0200 (CEST) Received: from mout-u-107.mailbox.org (mout-u-107.mailbox.org [IPv6:2001:67c:2050:101:465::107]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id CBC508499D for ; Thu, 1 Sep 2022 11:27:34 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sr@denx.de Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4MJG0h6ZTXz9sQL; Thu, 1 Sep 2022 11:27:32 +0200 (CEST) Message-ID: Date: Thu, 1 Sep 2022 11:27:32 +0200 MIME-Version: 1.0 Subject: Re: [PATCH 0/6] Enable CONFIG_TIMER for all Kirwood / MVEBU boards Content-Language: en-US To: Tony Dinh , Simon Glass Cc: U-Boot Mailing List , =?UTF-8?Q?Pali_Roh=c3=a1r?= , Michael Walle References: <20220830115317.410812-1-sr@denx.de> <282eefe1-58d4-89cd-f940-2428bdd42b59@denx.de> From: Stefan Roese In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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.6 at phobos.denx.de X-Virus-Status: Clean Hi Tony, On 01.09.22 09:39, Tony Dinh wrote: >>> Some ideas. >>> >>> The get_timer() function looks wrong assigning an uint64_t to ulong. >>> >>> lib/time.c >>> >>> static uint64_t notrace tick_to_time(uint64_t tick) >>> uint64_t notrace get_ticks(void) >>> uint64_t __weak notrace get_ticks(void) >>> >>> ulong __weak get_timer(ulong base) >>> { >>> return tick_to_time(get_ticks()) - base; >>> } >>> >>> Most of the timer infrastructure is using uint64_t. I'm seeing this >>> __weak function get_timer was invoked in Kirkwood boards. Both in >>> sleep and timer commands. >> >> The get_ticks() thing can run at 1MHz but the timer is 1KHz, so that >> is why we don't need a u64 for the timer. > > Thanks for your explanation! However, would you agree that code is > problematic and needed some improvement ? IOW, depending what the > compiler does, it might return the 1st 32 bit of the 64-bit integer > result? It will return the lower 32 bits if the system is 32bit, yes. To check if we have a problem here, please add this (totally untested) code and extend it if it makes sense: diff --git a/lib/time.c b/lib/time.c index bbf191f67323..ef5252419f3b 100644 --- a/lib/time.c +++ b/lib/time.c @@ -146,7 +146,15 @@ int __weak timer_init(void) /* Returns time in milliseconds */ ulong __weak get_timer(ulong base) { - return tick_to_time(get_ticks()) - base; + u64 ticks = get_ticks(); + u64 time_ms = tick_to_time(ticks); + + if (time_ms & 0xffffffff00000000ULL) + printf("ticks=%lld time_ms=%lld\n", ticks, time_ms); + if ((time_ms - base) & 0xffffffff80000000ULL) + printf("ticks=%lld time_ms=%lld base=%ld ret=%lld\n", ticks, time_ms, base, time_ms - base); + + return time_ms - base; } At least here, you seem to have a wrap around with the 32bits AFAICT: > GoFlexHome> sleep 20.5 > do_sleep got a timer start = 15031 > do_sleep delay = 20000 > do_sleep delay = 20500 > do_sleep sleeping... > do_sleep start 15031 current 100 > > do_sleep start 15031 current 6400 > do_sleep end of sleep ... current = 4294952265 > > *** Something strange happened here. current should be 6500, but it > seems to have garbage. So the loop exits prematurely. 4294952265 = 0xFFFFC549! Thanks, Stefan