Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp29082rdb; Thu, 21 Dec 2023 01:39:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IEHZu2LDg7n4U/zAu4VnPwcTKRCvyUQM8Kwh+aMWqQZbzm3ArGoTE4RCjHpyonK+FxXFGpq X-Received: by 2002:a05:6808:1786:b0:3bb:7bde:3bd4 with SMTP id bg6-20020a056808178600b003bb7bde3bd4mr793959oib.12.1703151571391; Thu, 21 Dec 2023 01:39:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703151571; cv=none; d=google.com; s=arc-20160816; b=y738B9UCWOX7rEqbOEwor82rzv2SeY6SW1qxLFhftOub55pglCVsO6cNytYBgivKc8 /jlGLUuHZUwP7ThixUWEWu7MDUfXBAieJg5UXxFicHeueTqxShsARQYitI16qKlkRd0x Qfm4FHN6LGuyIyztbtsTNlMerk9TjO288hkmK32M+jMgJOQWzmElpeI+ZPenqFZU8zmy OLb/kP6JSgctg1ScXXiMaD01ESWCNO6KW6+uaco9OZwYdHBLesLnY/W1PdDpTn9KqiUa z1JPW4VnMu/oQNsNBY3h3MUSN32zzSTXoQUbrJaFH2oV2NKj+2Sa6tIVM1KWtkHO9QOj QwEQ== ARC-Message-Signature: i=1; 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=iKXTcezb/3a0w+NiCVNwBnrWfz7ZYrcx9iCAE/fVtgc=; fh=4NrVLy9e1N+ecgT+cYXeaYo3IQFJO3N8B6ul/oSBUd8=; b=WDbMcrHl3pYw6hTOc4rZ8bV5R0gWI/eb/n11Je7vi9mze9dhuAgKnkcNZo9IPl/H4R sDZi8Nvt90B9kwenfWWVu620ZUE+PkIrv6LBNVVYT1i954V+YvzIqG8yGjhqgWaAaTDY jjc8KSh7x749xO7XWNGkxoC+89bY1bYkpNo8NjcrAv67ufaSb7+Q6Cvgx4ykEzQH+0QM 65CF8DuhdsqFvKRtHZSfPgvVPOYkC1TdfnDqyNHhEd23jSYUlo2ik38V9/cBi3Q7u06V G+32sEl5pobktulLgW6qmmKICSlOQKJCi9Gwz4H2hShrWbJnST2yWh6fDZxEv/GVjyB/ dIkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XC4QY3a5; spf=pass (google.com: domain of linux-kernel+bounces-8144-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8144-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id c12-20020a056a00008c00b006d94573e781si1260834pfj.380.2023.12.21.01.39.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 01:39:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-8144-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XC4QY3a5; spf=pass (google.com: domain of linux-kernel+bounces-8144-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8144-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 5FD2B285439 for ; Thu, 21 Dec 2023 09:39:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C8E544E1A4; Thu, 21 Dec 2023 09:33:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="XC4QY3a5" X-Original-To: linux-kernel@vger.kernel.org Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 5DC4D4D595; Thu, 21 Dec 2023 09:33:15 +0000 (UTC) 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=1703151195; x=1734687195; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=w+aeq0HJvcCZ+KS3uPOjGfoNyocIUqM1MwjS66a3tiI=; b=XC4QY3a5A6+zQcUxTwHu1uxvDbzsWZ80OurfOOGznVs0l3rf5vfFQOrR mHNHrLnanQn1MCgttgqhnHPLr+RlaM/Hko+4hcznSJAieTHKoFse3EWdI j5l1Hg4y+RUzjPHJ58rM8zwLtH4SOCd0uVOPpaFM1G/zPR05cA8p5q8Y7 jWEk2lrEmjZK3h+lnG25x2wxKliULUFJdpNjRByH5nwQ+KFvA5riGgATn umXY4Zn2Xjw3EqlOYga2APafC0aDmmhduMz9uxNNYi2buHoS+6h90VCrT wbaT7ntxb2hvgyO4fyd3wrgH2Hh8asVv94rmBPo3PVPtRcWrU/hbvdqm5 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10930"; a="3188474" X-IronPort-AV: E=Sophos;i="6.04,293,1695711600"; d="scan'208";a="3188474" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Dec 2023 01:33:15 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10930"; a="810918161" X-IronPort-AV: E=Sophos;i="6.04,293,1695711600"; d="scan'208";a="810918161" Received: from inlubt0316.iind.intel.com ([10.191.20.213]) by orsmga001.jf.intel.com with ESMTP; 21 Dec 2023 01:33:08 -0800 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, pandith.n@intel.com, mallikarjunappa.sangannavar@intel.com, thejesh.reddy.t.r@intel.com, lakshmi.sowjanya.d@intel.com Subject: [RFC PATCH v2 02/10] timekeeping: Add function to convert realtime to base clock Date: Thu, 21 Dec 2023 15:02:46 +0530 Message-Id: <20231221093254.9599-3-lakshmi.sowjanya.d@intel.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231221093254.9599-1-lakshmi.sowjanya.d@intel.com> References: <20231221093254.9599-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 Introduce an interface ktime_real_to_base_clock(), to convert realtime to base clock. Convert base clock to system clock using convert_base_to_cs() in get_device_system_crosststamp(). Add a helper function to check whether the current clocksource has same base clock. 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 | 6 ++ kernel/time/timekeeping.c | 112 +++++++++++++++++++++++++++++++++++- 2 files changed, 116 insertions(+), 2 deletions(-) diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h index 0f00f382bb5d..2c3bc2eac974 100644 --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -274,12 +274,18 @@ struct system_device_crosststamp { * timekeeping code to verify comparability of two cycle values. * The default ID, CSID_GENERIC, does not identify a specific * clocksource. + * @nsecs: @cycles is in nanoseconds. */ struct system_counterval_t { u64 cycles; enum clocksource_ids cs_id; + bool 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 0ff065c5d25b..e68ecd77ee34 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1191,6 +1191,89 @@ static bool cycle_between(u64 before, u64 test, u64 after) return false; } +static bool convert_clock(u64 *val, u32 numerator, u32 denominator) +{ + u64 rem, res; + + if (numerator == 0 || denominator == 0) + return false; + + res = div64_u64_rem(*val, denominator, &rem) * numerator; + *val = res + div_u64(rem * numerator, denominator); + return true; +} + +static bool convert_base_to_cs(struct system_counterval_t *scv) +{ + struct clocksource *cs = tk_core.timekeeper.tkr_mono.clock; + struct clocksource_base *base = cs->base; + + /* The timestamp was taken from the time keeper clock source */ + if (cs->id == scv->cs_id) + return true; + + /* Check whether cs_id matches the base clock */ + if (!base || base->id != scv->cs_id) + return false; + + /* Avoid conversion to a less precise clock */ + if (scv->nsecs && cs->freq_khz != 0 && base->freq_khz < cs->freq_khz) { + if (!convert_clock(&scv->cycles, cs->freq_khz, USEC_PER_SEC)) + return false; + } else { + if (scv->nsecs) { + if (!convert_clock(&scv->cycles, base->freq_khz, USEC_PER_SEC)) + return false; + } + if (!convert_clock(&scv->cycles, base->numerator, base->denominator)) + return false; + } + scv->cycles += base->offset; + 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); + delta = (u64)treal - tk->tkr_mono.base_real; + if (delta > tk->tkr_mono.clock->max_idle_ns) + return false; + *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 @@ -1236,8 +1319,7 @@ int get_device_system_crosststamp(int (*get_time_fn) * system counter value is the same as for the currently * installed timekeeper clocksource */ - if (system_counterval.cs_id == CSID_GENERIC || - tk->tkr_mono.clock->id != system_counterval.cs_id) + if (!convert_base_to_cs(&system_counterval)) return -ENODEV; cycles = system_counterval.cycles; @@ -1304,6 +1386,32 @@ 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); + ret = tk_core.timekeeper.tkr_mono.clock->base ? + tk_core.timekeeper.tkr_mono.clock->base->id == id : 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