Received: by 2002:a05:6a10:8395:0:0:0:0 with SMTP id n21csp371666pxh; Tue, 9 Nov 2021 12:06:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJxCj9iKrPnWh6eVuTpBhbdfUwp+kzAilk0NeLKgRzW/SaN0CvDAJCUc+60Y83FMxy7g1MVB X-Received: by 2002:a02:c761:: with SMTP id k1mr7992243jao.74.1636488377390; Tue, 09 Nov 2021 12:06:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1636488377; cv=none; d=google.com; s=arc-20160816; b=iUPWiFPEeVwSLrIiL0BAeg2AFhbaM6VqONv4jke2f1CxpvSzEmSi85gEvOh1xfcgPS Q+gW3IbT1CSeiF4Ugm7KlPjmxU+bbzjKiApnDLYdpYoNqImUW4atOMA9k7zjI4e8Wyio oUW9tlHf6qk9uvUxEjYmjobth1O4aYO4p4iDH+wyIPdsPmqxXCYWi0Xwy8bqi8WBrGdu jFhoH6sJqqtCBvZ24XyzJIK/Rhoh8cgTz3Qw7S6XCb+3+Id+pnAXSL1/NB1ufcER4Ifw qzC/dbLcSGBdJBor4o4C5FpsgoITn8RugLH4mzznBsfrfP1CVkcdq6WpO6V5EVa6L2Qh Gdiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=aW6ZznmXqA8zEDZbkYNLIXCfqLwCLA1w/y4b1/dzeTg=; b=rNrlR9CmRK1oL+3ZKK1cbJtKfZ01/l7HOLdkBbzhNova0BxHudAHXcNMb8Tz/9vHjw 71T2HRNOR6pyQgd1cz1Vzz+3fnza5IwPFI+0sk90lZLhkI5MRSfQX+C5G8QUh3lGb4cZ wa0daOeWy/VoPy5bsESUDvrH6RfPK1z6CHn5HQUccZaWg7ryiWawRe4EnTjJbJD5YQuS a9rqVci82FBbFxfwmi1l79Qymr8SfrSu7cNV17gnfAqSXTBZzSKzEI+M13JHXsaedUsf Obvm/2x1PaDZzrlZkVvvjg71Jm8NLkYHSZSggXjrkX0xH1/uJLrtxSrCpDFUNFVPta82 4FYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=m3zlKfKy; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=TUOEXKdG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u5si911661jak.40.2021.11.09.12.05.58; Tue, 09 Nov 2021 12:06:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=m3zlKfKy; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=TUOEXKdG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245093AbhKIJxw (ORCPT + 99 others); Tue, 9 Nov 2021 04:53:52 -0500 Received: from Galois.linutronix.de ([193.142.43.55]:34980 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245096AbhKIJxm (ORCPT ); Tue, 9 Nov 2021 04:53:42 -0500 From: Martin Kaistra DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1636451454; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aW6ZznmXqA8zEDZbkYNLIXCfqLwCLA1w/y4b1/dzeTg=; b=m3zlKfKywp4n3ayFQdZDSqn2H/wfESZNm2Q/nl3EIJpTpmBfm8abioTbu3+rkyDm7lArAE RDLRiyAwTMhj95XRd2cgMBpMyrOloh0Xd2pkK5tQM+tDUHURoS9vlnixuMpFwwQsVwtYkU a7S5ysCRwwzXzcDSaw+3HEgyx3JYckf/D3A/+1TFQAO0FZawE3ouJuqLZfKeV1Q4kK8AnW 0xD5Dm4VXD4lz6Ev4xzP/wejgxR63Vb/RZ7Hn/zRdLO8u8z7TWObw5bxS/Hzq9ZpD+7gC6 QKk1j1w24M+WqpqO677BzBzYA2kHcoyUDlO0R7V/dTUt6BLULERl/EPPvf62VA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1636451454; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aW6ZznmXqA8zEDZbkYNLIXCfqLwCLA1w/y4b1/dzeTg=; b=TUOEXKdGjBNqOQnGPSRh99U599f1AuTHSpAt0pJnQucHgbOv+kldDhBi+Dxu/70jswABDE I6+SkObPLqr6EaBw== To: Florian Fainelli , Andrew Lunn , Vivien Didelot Cc: martin.kaistra@linutronix.de, Richard Cochran , Vladimir Oltean , "David S. Miller" , Jakub Kicinski , John Stultz , Thomas Gleixner , Stephen Boyd , Russell King , Marc Kleine-Budde , linux-kernel@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v2 3/7] timecounter: allow for non-power of two overflow Date: Tue, 9 Nov 2021 10:50:05 +0100 Message-Id: <20211109095013.27829-4-martin.kaistra@linutronix.de> In-Reply-To: <20211109095013.27829-1-martin.kaistra@linutronix.de> References: <20211109095013.27829-1-martin.kaistra@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some hardware counters which are used as clocks have an overflow point which is not a power of two. In order to be able to use the cycle counter infrastructure with such hardware, add support for more generic overflow logic. Signed-off-by: Martin Kaistra --- include/linux/timecounter.h | 3 +++ kernel/time/timecounter.c | 3 +++ 2 files changed, 6 insertions(+) diff --git a/include/linux/timecounter.h b/include/linux/timecounter.h index c6540ceea143..c71196a742b3 100644 --- a/include/linux/timecounter.h +++ b/include/linux/timecounter.h @@ -26,12 +26,15 @@ * see CYCLECOUNTER_MASK() helper macro * @mult: cycle to nanosecond multiplier * @shift: cycle to nanosecond divisor (power of two) + * @overflow_point: non-power of two overflow point (optional), + * smaller than mask */ struct cyclecounter { u64 (*read)(const struct cyclecounter *cc); u64 mask; u32 mult; u32 shift; + u64 overflow_point; }; /** diff --git a/kernel/time/timecounter.c b/kernel/time/timecounter.c index e6285288d765..afd2910a9724 100644 --- a/kernel/time/timecounter.c +++ b/kernel/time/timecounter.c @@ -39,6 +39,9 @@ static u64 timecounter_read_delta(struct timecounter *tc) /* calculate the delta since the last timecounter_read_delta(): */ cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask; + if (tc->cc->overflow_point && (cycle_now - tc->cycle_last) > tc->cc->mask) + cycle_delta -= tc->cc->mask - tc->cc->overflow_point; + /* convert to nanoseconds: */ ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta, tc->mask, &tc->frac); -- 2.20.1