Received: by 2002:ab2:4a89:0:b0:1f4:a8b6:6e69 with SMTP id w9csp80202lqj; Wed, 10 Apr 2024 04:51:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWBxCgjoFUZGjO5iG9B7a4/iUVWCzBqxSKNzBqgUVgjxPm1aGD0cfq33yCSCfIjTGZimwTAumfDFUpdT0eaYtYVHzjz+/1x7VqdXByWEg== X-Google-Smtp-Source: AGHT+IGyGz1rBQiB5xuaByUHRCchvIuFuF3q5+pal+PkPH9KQ8IDY9yvw7+9tmQDgP16WDxq3avW X-Received: by 2002:a17:906:f0a:b0:a51:b23c:1187 with SMTP id z10-20020a1709060f0a00b00a51b23c1187mr1410039eji.12.1712749916389; Wed, 10 Apr 2024 04:51:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712749916; cv=pass; d=google.com; s=arc-20160816; b=C8CGrwcbf8y5/rkRfkZ18AbixK3zfamO6gQBaAN8dkbMsUYvYYK9f38qlXFmQOvCmI m857ELgKBA0ifEDlR24yd85Uxnyv/KACKkwlAtyiD4lEh6zkcCyX9OLpihyMZlOPeZTb IsfNp7XUOkGLuD4xHklR/JE9V81/Wh6NYlJt88ejTnmWiWkttjT8nNxm3e6bMDkfQdKU xR7yjdAfYbMQy9bROrONnAscImh1Vr38zZDIvkSp2CHPvjMiiHLSIgnYlAPpuPc92DBn XmO+1ysllTMyco+ubfhr8q1qQbCVNrw3ECofaacf7E+RWkyyjHyq6yCkDIXXUgOSH86Z TNhw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=a9KL4FFM6ot70rSYiEBryK6oaQwzLc+0vo9pmhWnNOU=; fh=9Ba0DaKzo2I1dMve9dlPa9QXf3otfKRHKeoWQOhs61U=; b=UpiNYsl3sIvABDcI8ix40PMZaRLjG8L+EiGupAC4Z1he8rmCF5YYTgh28yiSuZXeg3 91VXrIj+e1EsW904TdruFKLzzUlsS7a0Ab4oVDOEUNOMieonmSwIsM2qHWs1HW8qGgtR FGR6yNPz2YEGYklWb9GyVI5wyx5uSBKLCO3xiLytjNMoKG9K82ur5JunqOcl4Lyp9MaV 7QLeKFG1m2ho/96PaY570sBQ/fzU+AlXwGJA4tegytMKGhuLpDNuIrF20loNPtAI7rg3 BYIrfeLLyX6r0JtsmsWpKaig54yFpdogSgqeEagjHuQSanWWz50xdqV3SfJJ4/jD1Kdt z0Gw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Co3uJtdH; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-138444-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-138444-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id d24-20020a170906041800b00a52028eedfdsi1018435eja.654.2024.04.10.04.51.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 04:51:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-138444-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Co3uJtdH; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-138444-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-138444-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id BD4C51F22E98 for ; Wed, 10 Apr 2024 11:51:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 79CDC15AABA; Wed, 10 Apr 2024 11:49:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Co3uJtdH" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03BD8159909; Wed, 10 Apr 2024 11:49:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.14 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712749773; cv=none; b=mA5oRTtsT69bsjJGwG6c7d4r/s+70GHLZ+jPd/QvbGKQsADvWl0xEr9EZ7y/0S3LdhjrZSKeztet1JvKWow7Zu7ZNLVTDG3wMBDeQxegqw2BSF2HLDzG3pFrjb/yTeTVoiyBPsyP2K1Xj6me4FoPK8j/b40rlnjSBZHNXH09IwI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712749773; c=relaxed/simple; bh=4yFG2epkMTZfSUzUaCaIYzcSlYJ6SfR0gapuEZJcQ7g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ks1B/cIDCABpJMI6tEFJbjgqrp0KixXeIE3m0QuPNMULXHFAGsYR3H64a9oJxnYvmfAvXgfmuScZ3QukbC82sRvnb49Y3z1qUK0qVTrBzew78EKCjGIIT98WvRWbhM/YPi00I7TG4rgUuhuWhURYHMto2n80O1ufN7Q6G0zvIQs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Co3uJtdH; arc=none smtp.client-ip=198.175.65.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1712749773; x=1744285773; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4yFG2epkMTZfSUzUaCaIYzcSlYJ6SfR0gapuEZJcQ7g=; b=Co3uJtdHcwdWqXlqdjCoBNxeZ8mjO6BO9EJ4g+yVG/o7oB/vYrILAKt3 UvXBx9OlpbcQolYISJaylZdIobxtIHy9FDkCGGa2S7a1OHk0BIHipvh+A 8MrNWeASyypPDkOxts9oLfuBcOygDjPO6M+Wsy/YKD2sey2NQUmcxGItC eLWo8FuZF37q+L6097A8rtVahU5WKENAQJj3NFbi8lb10rwVO/nRTKFtd +GERyYQAdu0Yv6kfiSwqVqGU7QXYcw2TkOddbBiS+oTc1k54FJqlI06UB kODlWLZBRStHBLeWUch0gGiGYG3oqBpCrFazkitbz95SlwNzkfu/lnyti w==; X-CSE-ConnectionGUID: QCHZKlxDR8aRcQPGJEFefA== X-CSE-MsgGUID: vmtNCXbXScutjf3Jq7AQ1w== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="11944185" X-IronPort-AV: E=Sophos;i="6.07,190,1708416000"; d="scan'208";a="11944185" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Apr 2024 04:49:30 -0700 X-CSE-ConnectionGUID: cv+Xk9N7SI+tpBWojhhwTg== X-CSE-MsgGUID: h15+G8CxQD6pKKxlg/oWUQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,190,1708416000"; d="scan'208";a="43769286" Received: from inlubt0316.iind.intel.com ([10.191.20.213]) by fmviesa002.fm.intel.com with ESMTP; 10 Apr 2024 04:49:23 -0700 From: lakshmi.sowjanya.d@intel.com To: tglx@linutronix.de, jstultz@google.com, giometti@enneenne.com, corbet@lwn.net, linux-kernel@vger.kernel.org Cc: x86@kernel.org, netdev@vger.kernel.org, linux-doc@vger.kernel.org, intel-wired-lan@lists.osuosl.org, andriy.shevchenko@linux.intel.com, eddie.dong@intel.com, christopher.s.hall@intel.com, jesse.brandeburg@intel.com, davem@davemloft.net, alexandre.torgue@foss.st.com, joabreu@synopsys.com, mcoquelin.stm32@gmail.com, perex@perex.cz, linux-sound@vger.kernel.org, anthony.l.nguyen@intel.com, peter.hilber@opensynergy.com, pandith.n@intel.com, subramanian.mohan@intel.com, thejesh.reddy.t.r@intel.com, lakshmi.sowjanya.d@intel.com Subject: [PATCH v6 08/11] timekeeping: Add function to convert realtime to base clock Date: Wed, 10 Apr 2024 17:18:25 +0530 Message-Id: <20240410114828.25581-9-lakshmi.sowjanya.d@intel.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20240410114828.25581-1-lakshmi.sowjanya.d@intel.com> References: <20240410114828.25581-1-lakshmi.sowjanya.d@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Lakshmi Sowjanya D PPS(Pulse Per Second) generates signals in realtime, but Timed IO hardware understands time in base clock reference. Add an interface, ktime_real_to_base_clock() to convert realtime to base clock. Add the helper function timekeeping_clocksource_has_base(), to check whether the current clocksource has the same base clock. This will be used by Timed IO device to check if the base clock is X86_ART(Always Running Timer). Co-developed-by: Thomas Gleixner Signed-off-by: Thomas Gleixner Co-developed-by: Christopher S. Hall Signed-off-by: Christopher S. Hall Signed-off-by: Lakshmi Sowjanya D --- include/linux/timekeeping.h | 4 +++ kernel/time/timekeeping.c | 70 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h index b2ee182d891e..fc12a9ba2c88 100644 --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -318,6 +318,10 @@ struct system_counterval_t { bool use_nsecs; }; +extern bool ktime_real_to_base_clock(ktime_t treal, + enum clocksource_ids base_id, u64 *cycles); +extern bool timekeeping_clocksource_has_base(enum clocksource_ids id); + /* * Get cross timestamp between system clock and device clock */ diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 2542cfefbdee..e4ecce722969 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1227,6 +1227,48 @@ static bool convert_base_to_cs(struct system_counterval_t *scv) return true; } +static bool convert_cs_to_base(u64 *cycles, enum clocksource_ids base_id) +{ + struct clocksource *cs = tk_core.timekeeper.tkr_mono.clock; + struct clocksource_base *base = cs->base; + + /* Check whether base_id matches the base clock */ + if (!base || base->id != base_id) + return false; + + *cycles -= base->offset; + if (!convert_clock(cycles, base->denominator, base->numerator)) + return false; + return true; +} + +static u64 convert_ns_to_cs(u64 delta) +{ + struct tk_read_base *tkr = &tk_core.timekeeper.tkr_mono; + + return div_u64((delta << tkr->shift) - tkr->xtime_nsec, tkr->mult); +} + +bool ktime_real_to_base_clock(ktime_t treal, enum clocksource_ids base_id, u64 *cycles) +{ + struct timekeeper *tk = &tk_core.timekeeper; + unsigned int seq; + u64 delta; + + do { + seq = read_seqcount_begin(&tk_core.seq); + if ((u64)treal < tk->tkr_mono.base_real) + return false; + delta = (u64)treal - tk->tkr_mono.base_real; + *cycles = tk->tkr_mono.cycle_last + convert_ns_to_cs(delta); + if (!convert_cs_to_base(cycles, base_id)) + return false; + } while (read_seqcount_retry(&tk_core.seq, seq)); + + return true; +} +EXPORT_SYMBOL_GPL(ktime_real_to_base_clock); + /** * get_device_system_crosststamp - Synchronously capture system/device timestamp * @get_time_fn: Callback to get simultaneous device time and @@ -1337,6 +1379,34 @@ int get_device_system_crosststamp(int (*get_time_fn) } EXPORT_SYMBOL_GPL(get_device_system_crosststamp); +/** + * timekeeping_clocksource_has_base - Check whether the current clocksource + * has a base clock + * @id: The clocksource ID to check for + * + * Note: The return value is a snapshot which can become invalid right + * after the function returns. + * + * Return: true if the timekeeper clocksource has a base clock with @id, + * false otherwise + */ +bool timekeeping_clocksource_has_base(enum clocksource_ids id) +{ + unsigned int seq; + bool ret; + + do { + seq = read_seqcount_begin(&tk_core.seq); + if (tk_core.timekeeper.tkr_mono.clock->base) + ret = (tk_core.timekeeper.tkr_mono.clock->base->id == id); + else + ret = false; + } while (read_seqcount_retry(&tk_core.seq, seq)); + + return ret; +} +EXPORT_SYMBOL_GPL(timekeeping_clocksource_has_base); + /** * do_settimeofday64 - Sets the time of day. * @ts: pointer to the timespec64 variable containing the new time -- 2.35.3