Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1675640pxb; Thu, 4 Nov 2021 06:37:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwcgkYlHRiJ9+opKCyg5a9zxD9whrDM5brZUD4X+v5zLpwNN3g7HAKEQN5ifzRCtU3pGvtZ X-Received: by 2002:a17:906:4f8c:: with SMTP id o12mr62097337eju.115.1636033078182; Thu, 04 Nov 2021 06:37:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1636033078; cv=none; d=google.com; s=arc-20160816; b=wQSjSLgFPIdkPRetZcqQXiwCinKxreO9nc9JxAapMzOvsukvIKmTejfJhywHeIbQgF uz3aI0AyI27mmkDIVjjndwiXoZ06wcGFiAuv8MPxJKGdayRhV9wTmuLw38DNEDwIbAB+ 7RArk4acTsuGhWTnCcGSyGY940fKfJ7kpVN/G0sCJWsBH6oU0dCivghs/SimIkd6vSbU g8LXQIPVt8G/mL9R+Td4n/WYLTT4e5NK22EDshECyVkBxW4otYpJ3lcBxOcXyefQF2gj uKo5wDAGKI/r5Ri0dHoTjPvl2qrILsFguf4d3b5gz/HIIOP8VV6dG76yz4JnzhM52Wmg SPdw== 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=TPD7WxH/rkOJ8vkex5BN2bQSGLeTm+I9vqOqjHwGE90QCoqx611u/W1ePHPmEux2et jhs0qxkCnr1HYIkV3V1hTjPBikdcXn6mXMfMivqWwEM4lOUNsXUVoGg2/JtlKI/qc9Qp DDD+uhl+UuE9/c9PvidJzPUC2YEHUfGVIJu8l031h8C5gFxKoLwKQAM130XoizqemD6j e3G2vznj9r0CYYZYNCl4/Y8UvrN3hb85AL3AfBskP/mZAS74W02bh17d9sA8m9Lia9s5 ItshdQupPNGz61sdjAU06DLvE2b5lZ/NKTw4baCYkwtz70Fg0C/6anHk0Guksd28Nfhh WGuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=2Mr9cPo2; dkim=neutral (no key) header.i=@linutronix.de header.b=w4Embh4P; 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 h18si6598168edb.602.2021.11.04.06.37.33; Thu, 04 Nov 2021 06:37:58 -0700 (PDT) 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=2Mr9cPo2; dkim=neutral (no key) header.i=@linutronix.de header.b=w4Embh4P; 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 S231586AbhKDNfk (ORCPT + 99 others); Thu, 4 Nov 2021 09:35:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231403AbhKDNfi (ORCPT ); Thu, 4 Nov 2021 09:35:38 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0061AC061203; Thu, 4 Nov 2021 06:32:59 -0700 (PDT) From: Martin Kaistra DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1636032778; 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=2Mr9cPo20V5pIEMblKwrNHc73uQcRTrJZgZazSAHgtSONRXdawRbRGSqdueJI32p6SjYJN tgZKvokMyYCTbHGjfNDnvTuSUMm/BJh4Gy6cT7W85Wl+S+B18DUVNEcZ++mCYgkkUmd2bd fgkVeUbvkcNTv+r6iRtjl82gMvp6/5E/76XqAzNag6Mfy/2q1kTzNntlJymG9BQ5IaIo/R HglRnN6oweg6Zuug//Z7knByWTvHY4DrCrcpHJGrmhsDcwDnsFYeTK/Cxea5ReMDiDIYvs pdS26mKAaBB2vPiXZfRHAOJ3S2iPWq1RmBN+Wq9P1nSW/e+bJS47ZZYN+pigzw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1636032778; 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=w4Embh4PsX/WCOMqxcaQUCJG7l18xHIiS2d0MivABo/HRnsqsnJeMx/EvPc0oDD/Gydzx2 3tbIC7X8t/++ihBg== 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 3/7] timecounter: allow for non-power of two overflow Date: Thu, 4 Nov 2021 14:31:57 +0100 Message-Id: <20211104133204.19757-4-martin.kaistra@linutronix.de> In-Reply-To: <20211104133204.19757-1-martin.kaistra@linutronix.de> References: <20211104133204.19757-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