All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pavel Tatashin <pasha.tatashin@oracle.com>
To: x86@kernel.org, linux-kernel@vger.kernel.org, mingo@redhat.com,
	peterz@infradead.org, tglx@linutronix.de, hpa@zytor.com
Subject: [v2 6/9] x86/tsc: use cpuid to determine TSC frequency
Date: Fri, 24 Mar 2017 11:21:36 -0400	[thread overview]
Message-ID: <1490368899-877997-7-git-send-email-pasha.tatashin@oracle.com> (raw)
In-Reply-To: <1490368899-877997-1-git-send-email-pasha.tatashin@oracle.com>

One of the newer methods to determine TSC frequency, is to use one of cpuid
extensions to get TSC/Crystal ratio. This method is preferred on CPUs that
implements it. This patch adds a new function calibrate_tsc_early() that
can be called early in boot to determine the TSC by using this method.

Signed-off-by: Pavel Tatashin <pasha.tatashin@oracle.com>
---
 arch/x86/kernel/tsc.c | 39 ++++++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 13 deletions(-)

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 5add503..1c9fc23 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -26,6 +26,9 @@
 #include <asm/apic.h>
 #include <asm/intel-family.h>
 
+/* CPUID 15H TSC/Crystal ratio, plus optionally Crystal Hz */
+#define CPUID_TSC_LEAF		0x15
+
 unsigned int __read_mostly cpu_khz;	/* TSC clocks / usec, not used here */
 EXPORT_SYMBOL(cpu_khz);
 
@@ -671,24 +674,16 @@ static unsigned long quick_pit_calibrate(bool early_boot)
 }
 
 /**
- * native_calibrate_tsc
- * Determine TSC frequency via CPUID, else return 0.
+ * The caller already checked that TSC leaf capability can be read from cpuid
  */
-unsigned long native_calibrate_tsc(void)
+static unsigned long calibrate_tsc_early(int model)
 {
 	unsigned int eax_denominator, ebx_numerator, ecx_hz, edx;
 	unsigned int crystal_khz;
 
-	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
-		return 0;
-
-	if (boot_cpu_data.cpuid_level < 0x15)
-		return 0;
-
 	eax_denominator = ebx_numerator = ecx_hz = edx = 0;
 
-	/* CPUID 15H TSC/Crystal ratio, plus optionally Crystal Hz */
-	cpuid(0x15, &eax_denominator, &ebx_numerator, &ecx_hz, &edx);
+	cpuid(CPUID_TSC_LEAF, &eax_denominator, &ebx_numerator, &ecx_hz, &edx);
 
 	if (ebx_numerator == 0 || eax_denominator == 0)
 		return 0;
@@ -696,7 +691,7 @@ unsigned long native_calibrate_tsc(void)
 	crystal_khz = ecx_hz / 1000;
 
 	if (crystal_khz == 0) {
-		switch (boot_cpu_data.x86_model) {
+		switch (model) {
 		case INTEL_FAM6_SKYLAKE_MOBILE:
 		case INTEL_FAM6_SKYLAKE_DESKTOP:
 		case INTEL_FAM6_KABYLAKE_MOBILE:
@@ -713,6 +708,24 @@ unsigned long native_calibrate_tsc(void)
 		}
 	}
 
+	return crystal_khz * ebx_numerator / eax_denominator;
+}
+
+/**
+ * native_calibrate_tsc
+ * Determine TSC frequency via CPUID, else return 0.
+ */
+unsigned long native_calibrate_tsc(void)
+{
+	unsigned int tsc_khz;
+
+	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
+		return 0;
+
+	if (boot_cpu_data.cpuid_level < CPUID_TSC_LEAF)
+		return 0;
+
+	tsc_khz = calibrate_tsc_early(boot_cpu_data.x86_model);
 	/*
 	 * TSC frequency determined by CPUID is a "hardware reported"
 	 * frequency and is the most accurate one so far we have. This
@@ -727,7 +740,7 @@ unsigned long native_calibrate_tsc(void)
 	if (boot_cpu_data.x86_model == INTEL_FAM6_ATOM_GOLDMONT)
 		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
 
-	return crystal_khz * ebx_numerator / eax_denominator;
+	return tsc_khz;
 }
 
 static unsigned long cpu_khz_from_cpuid(void)
-- 
1.8.3.1

  parent reply	other threads:[~2017-03-24 15:22 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-24 15:21 [v2 0/9] Early boot time stamps for x86 Pavel Tatashin
2017-03-24 15:21 ` [v2 1/9] sched/clock: broken stable to unstable transfer Pavel Tatashin
2017-04-06  2:47   ` [lkp-robot] [sched/clock] 30c5d7a296: [No primary change] -77.8% netperf.time.percent_of_cpu_this_job_got kernel test robot
2017-03-24 15:21 ` [v2 2/9] sched/clock: interface to allow timestamps early in boot Pavel Tatashin
2017-03-24 15:21 ` [v2 3/9] x86/cpu: determining x86 vendor early Pavel Tatashin
2017-03-24 15:21 ` [v2 4/9] x86/tsc: early MSR-based CPU/TSC frequency discovery Pavel Tatashin
2017-03-24 15:21 ` [v2 5/9] x86/tsc: disable early messages from quick_pit_calibrate Pavel Tatashin
2017-03-24 15:21 ` Pavel Tatashin [this message]
2017-03-24 15:21 ` [v2 7/9] x86/tsc: use cpuid to determine CPU frequency Pavel Tatashin
2017-03-24 15:21 ` [v2 8/9] x86/tsc: tsc early Pavel Tatashin
2017-03-24 15:21 ` [v2 9/9] x86/tsc: use " Pavel Tatashin
2017-03-28  1:08   ` [lkp-robot] [x86/tsc] 07e7eaa3bc: BUG:kernel_reboot-without-warning_in_early-boot_stage kernel test robot
2017-03-28  1:08     ` kernel test robot
2017-03-25 10:25 ` [v2 0/9] Early boot time stamps for x86 Thomas Gleixner
2017-03-25 13:55   ` Pasha Tatashin
2017-03-25 18:36     ` Thomas Gleixner
2017-03-25 19:45       ` Pasha Tatashin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1490368899-877997-7-git-send-email-pasha.tatashin@oracle.com \
    --to=pasha.tatashin@oracle.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.