From patchwork Wed Apr 10 11:48:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "D, Lakshmi Sowjanya" X-Patchwork-Id: 13624155 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Co3uJtdH" 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: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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