Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp303983ybl; Thu, 30 Jan 2020 22:42:05 -0800 (PST) X-Google-Smtp-Source: APXvYqxFNucgKz3X0CiUVWFFNQmLX1sDJsSIGGAR28PPjfIhutaXnaM8Am75OBLcUMWFQsnr9K0Q X-Received: by 2002:a05:6830:15c5:: with SMTP id j5mr6377743otr.289.1580452925228; Thu, 30 Jan 2020 22:42:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580452925; cv=none; d=google.com; s=arc-20160816; b=v2RdyeNhfGbMg/Yw+OzoeCeJ+/nW7qTBbh0Hoh+xGi1KtEdx5Gwo5phJtc10e6k2+5 axpVLoZSUZpjJWiObI+g0PjjLhNrTuaoPDHrLsTaES8wJhLYgL6Nc2mvKWfniNQOuKCI 0bVpkQkGalpYqEFKE/+e4tdt9+nSb8i6UbYBWr36l0qanp5WYYO5jGPL3psZzXtOKuUW k7Qe0p/12QFEcvXask8TAaW8iyKv7O7iZnEwD15mVT3JVDUligtF0Tn/s3sDGytdicKU nT/eVo8AmKdGwk7UTpZUh5ryGoUuShJU558/G33tvZ4fJTKHKL6x3QAOy0Air8Scc6j2 EyIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=bpP+aFifqX9zQIH2JOO91tVxNYNKqjYuUkCN43O9cZs=; b=Q1CYVs4OjWYFRw1jLz/pQhhEO0rZtxkG3jYkV2JczT1k3OoIKLO+HLt0RiFvh9CPHZ sWWP3MTC9PiXsXlcNoy465SPIPqhujMUTMC+7xFGDVezxz+dS353pzMCKF5rhzbFutg+ 5T067X7K8dT6v/ARm3tACjwC+dzXqpXx/E8W7WNEz/Zwz0CokMHEV2kE1lBOdJpYgokS kE9LkKYJtv0vd+LpHBWPQE0Ugm/wjyQ7BrUMBIael9NoR+DZ3Iq0ek44aHiP/fHSsy7H ur0g/2JIZtChLp7RoN/euYX0Rq5OFgHu6Rd3eeSb60SwHvYCnEJfm/zpyjU4Or93OeHz GQxA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z26si2993632oid.247.2020.01.30.22.41.53; Thu, 30 Jan 2020 22:42:05 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728128AbgAaGkx (ORCPT + 99 others); Fri, 31 Jan 2020 01:40:53 -0500 Received: from mga02.intel.com ([134.134.136.20]:64035 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725815AbgAaGkv (ORCPT ); Fri, 31 Jan 2020 01:40:51 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Jan 2020 22:40:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,384,1574150400"; d="scan'208";a="309925522" Received: from wtczc53028gn.jf.intel.com (HELO localhost.localdomain) ([10.54.87.17]) by orsmga001.jf.intel.com with ESMTP; 30 Jan 2020 22:40:50 -0800 From: christopher.s.hall@intel.com To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, tglx@linutronix.de, hpa@zytor.com, mingo@redhat.com, x86@kernel.org, jacob.e.keller@intel.com, richardcochran@gmail.com, davem@davemloft.net, sean.v.kelley@intel.com Cc: Christopher Hall Subject: [Intel PMC TGPIO Driver 4/5] x86/tsc: Add TSC support functions to support ART driven Time-Aware GPIO Date: Wed, 11 Dec 2019 13:48:51 -0800 Message-Id: <20191211214852.26317-5-christopher.s.hall@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191211214852.26317-1-christopher.s.hall@intel.com> References: <20191211214852.26317-1-christopher.s.hall@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Christopher Hall The PMC Time-Aware GPIO (TGPIO) logic is driven by ART. Several support functions are required to translate ART to/from nanoseconds units which is required by the PHC clock interface. A function is also added to get the current value of ART/TSC using get_cycles() (RDTSC). This avoids multiple MMIO reads required to retrieve ART. Note that rather than ART nanoseconds, TSC nanoseconds are used. These are related by TSC = ART * CPUID[15H].EBX/CPUID[15H].EAX + K. The value of K is the distinction between between the ART and TSC nanoseconds. The advantage of using TSC nanoseconds to represent the device clock because it can easily be calculated in user-space using only CPUID[15H]. The value of ART in general can't be since K isn't necessarily known to the application. Signed-off-by: Christopher Hall --- arch/x86/include/asm/tsc.h | 6 ++ arch/x86/kernel/tsc.c | 116 ++++++++++++++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/tsc.h b/arch/x86/include/asm/tsc.h index 8a0c25c6bf09..f44c92fe3cd5 100644 --- a/arch/x86/include/asm/tsc.h +++ b/arch/x86/include/asm/tsc.h @@ -32,6 +32,12 @@ static inline cycles_t get_cycles(void) extern struct system_counterval_t convert_art_to_tsc(u64 art); extern struct system_counterval_t convert_art_ns_to_tsc(u64 art_ns); +extern struct timespec64 get_tsc_ns_now(struct system_counterval_t + *system_counter); +extern u64 convert_tsc_ns_to_art(struct timespec64 *tsc_ns); +extern u64 convert_tsc_ns_to_art_duration(struct timespec64 *tsc_ns); +extern struct timespec64 convert_art_to_tsc_ns(u64 art); +extern struct timespec64 convert_art_to_tsc_ns_duration(u64 art); extern void tsc_early_init(void); extern void tsc_init(void); diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 7e322e2daaf5..fb0dc3169e6b 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -1215,7 +1215,7 @@ int unsynchronized_tsc(void) /* * Convert ART to TSC given numerator/denominator found in detect_art() */ -struct system_counterval_t convert_art_to_tsc(u64 art) +static struct system_counterval_t _convert_art_to_tsc(u64 art, bool dur) { u64 tmp, res, rem; @@ -1225,13 +1225,125 @@ struct system_counterval_t convert_art_to_tsc(u64 art) tmp = rem * art_to_tsc_numerator; do_div(tmp, art_to_tsc_denominator); - res += tmp + art_to_tsc_offset; + res += tmp; + if (!dur) + res += art_to_tsc_offset; return (struct system_counterval_t) {.cs = art_related_clocksource, .cycles = res}; } + +struct system_counterval_t convert_art_to_tsc(u64 art) +{ + return _convert_art_to_tsc(art, false); +} EXPORT_SYMBOL(convert_art_to_tsc); +static +struct timespec64 get_tsc_ns(struct system_counterval_t *system_counter) +{ + u64 tmp, res, rem; + u64 cycles; + + cycles = system_counter->cycles; + rem = do_div(cycles, tsc_khz); + + res = cycles * USEC_PER_SEC; + tmp = rem * USEC_PER_SEC; + + rem = do_div(tmp, tsc_khz); + tmp += rem > tsc_khz >> 2 ? 1 : 0; + res += tmp; + + rem = do_div(res, NSEC_PER_SEC); + + return (struct timespec64) {.tv_sec = res, .tv_nsec = rem}; +} + +struct timespec64 get_tsc_ns_now(struct system_counterval_t *system_counter) +{ + struct system_counterval_t counterval; + + if (system_counter == NULL) + system_counter = &counterval; + + system_counter->cycles = clocksource_tsc.read(NULL); + system_counter->cs = art_related_clocksource; + + return get_tsc_ns(system_counter); +} +EXPORT_SYMBOL(get_tsc_ns_now); + +struct timespec64 convert_art_to_tsc_ns(u64 art) +{ + struct system_counterval_t counterval; + + counterval = _convert_art_to_tsc(art, false); + + return get_tsc_ns(&counterval); +} +EXPORT_SYMBOL(convert_art_to_tsc_ns); + +struct timespec64 convert_art_to_tsc_ns_duration(u64 art) +{ + struct system_counterval_t counterval; + + counterval = _convert_art_to_tsc(art, true); + + return get_tsc_ns(&counterval); +} +EXPORT_SYMBOL(convert_art_to_tsc_ns_duration); + +static u64 convert_tsc_ns_to_tsc(struct timespec64 *tsc_ns) +{ + u64 tmp, res, rem; + u64 cycles; + + cycles = timespec64_to_ns(tsc_ns); + + rem = do_div(cycles, USEC_PER_SEC); + + res = cycles * tsc_khz; + tmp = rem * tsc_khz; + + do_div(tmp, USEC_PER_SEC); + + return res + tmp; +} + + +static u64 _convert_tsc_ns_to_art(struct timespec64 *tsc_ns, bool dur) +{ + u64 tmp, res, rem; + u64 cycles; + + cycles = convert_tsc_ns_to_tsc(tsc_ns); + if (!dur) + cycles -= art_to_tsc_offset; + + rem = do_div(cycles, art_to_tsc_numerator); + + res = cycles * art_to_tsc_denominator; + tmp = rem * art_to_tsc_denominator; + + rem = do_div(tmp, art_to_tsc_numerator); + tmp += rem > art_to_tsc_numerator >> 2 ? 1 : 0; + + return res + tmp; +} + +u64 convert_tsc_ns_to_art(struct timespec64 *tsc_ns) +{ + return _convert_tsc_ns_to_art(tsc_ns, false); +} +EXPORT_SYMBOL(convert_tsc_ns_to_art); + +u64 convert_tsc_ns_to_art_duration(struct timespec64 *tsc_ns) +{ + return _convert_tsc_ns_to_art(tsc_ns, true); +} +EXPORT_SYMBOL(convert_tsc_ns_to_art_duration); + /** * convert_art_ns_to_tsc() - Convert ART in nanoseconds to TSC. * @art_ns: ART (Always Running Timer) in unit of nanoseconds -- 2.21.0