mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: zhenwei pi <pizhenwei@bytedance.com>
To: tglx@linutronix.de, pbonzini@redhat.com
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	x86@kernel.org, zhenwei pi <pizhenwei@bytedance.com>
Subject: [PATCH v2 1/2] x86/cpu: Introduce x86_get_cpufreq_khz()
Date: Wed,  1 Dec 2021 10:46:49 +0800	[thread overview]
Message-ID: <20211201024650.88254-2-pizhenwei@bytedance.com> (raw)
In-Reply-To: <20211201024650.88254-1-pizhenwei@bytedance.com>

Wrapper function x86_get_cpufreq_khz() to get frequency on a x86
platform, hide detailed implementation from proc routine.

Also export this function for the further use, a typical case is that
kvm module gets the frequency of the host and tell the guest side by
kvmclock.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
---
 arch/x86/include/asm/processor.h |  2 ++
 arch/x86/kernel/cpu/common.c     | 19 +++++++++++++++++++
 arch/x86/kernel/cpu/proc.c       | 13 +++----------
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
index 355d38c0cf60..22f183dee593 100644
--- a/arch/x86/include/asm/processor.h
+++ b/arch/x86/include/asm/processor.h
@@ -855,4 +855,6 @@ enum mds_mitigations {
 	MDS_MITIGATION_VMWERV,
 };
 
+unsigned int x86_get_cpufreq_khz(unsigned int cpu);
+
 #endif /* _ASM_X86_PROCESSOR_H */
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 0083464de5e3..997026fedbb4 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -22,6 +22,7 @@
 #include <linux/io.h>
 #include <linux/syscore_ops.h>
 #include <linux/pgtable.h>
+#include <linux/cpufreq.h>
 
 #include <asm/cmdline.h>
 #include <asm/stackprotector.h>
@@ -2104,3 +2105,21 @@ void arch_smt_update(void)
 	/* Check whether IPI broadcasting can be enabled */
 	apic_smt_update();
 }
+
+unsigned int x86_get_cpufreq_khz(unsigned int cpu)
+{
+	unsigned int freq = 0;
+
+	if (!cpu_feature_enabled(X86_FEATURE_TSC))
+		return 0;
+
+	freq = aperfmperf_get_khz(cpu);
+	if (!freq)
+		freq = cpufreq_quick_get(cpu);
+
+	if (!freq)
+		freq = cpu_khz;
+
+	return freq;
+}
+EXPORT_SYMBOL_GPL(x86_get_cpufreq_khz);
diff --git a/arch/x86/kernel/cpu/proc.c b/arch/x86/kernel/cpu/proc.c
index 4eec8889b0ff..8ed17f969f72 100644
--- a/arch/x86/kernel/cpu/proc.c
+++ b/arch/x86/kernel/cpu/proc.c
@@ -3,7 +3,6 @@
 #include <linux/timex.h>
 #include <linux/string.h>
 #include <linux/seq_file.h>
-#include <linux/cpufreq.h>
 
 #include "cpu.h"
 
@@ -61,7 +60,7 @@ static void show_cpuinfo_misc(struct seq_file *m, struct cpuinfo_x86 *c)
 static int show_cpuinfo(struct seq_file *m, void *v)
 {
 	struct cpuinfo_x86 *c = v;
-	unsigned int cpu;
+	unsigned int cpu, freq;
 	int i;
 
 	cpu = c->cpu_index;
@@ -83,16 +82,10 @@ static int show_cpuinfo(struct seq_file *m, void *v)
 	if (c->microcode)
 		seq_printf(m, "microcode\t: 0x%x\n", c->microcode);
 
-	if (cpu_has(c, X86_FEATURE_TSC)) {
-		unsigned int freq = aperfmperf_get_khz(cpu);
-
-		if (!freq)
-			freq = cpufreq_quick_get(cpu);
-		if (!freq)
-			freq = cpu_khz;
+	freq = x86_get_cpufreq_khz(cpu);
+	if (freq)
 		seq_printf(m, "cpu MHz\t\t: %u.%03u\n",
 			   freq / 1000, (freq % 1000));
-	}
 
 	/* Cache size */
 	if (c->x86_cache_size)
-- 
2.25.1


  reply	other threads:[~2021-12-01  2:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-01  2:46 [PATCH v2 0/2] " zhenwei pi
2021-12-01  2:46 ` zhenwei pi [this message]
2021-12-02 22:25   ` [PATCH v2 1/2] x86/cpu: " Peter Zijlstra
2021-12-03  7:34     ` zhenwei pi
2021-12-04 10:46       ` Peter Zijlstra
2021-12-01  2:46 ` [PATCH v2 2/2] KVM: x86: use x86_get_freq to get freq for kvmclock zhenwei pi
2021-12-02  2:48   ` Thomas Gleixner
2021-12-02  5:26     ` zhenwei pi
2021-12-02  7:19       ` Maxim Levitsky
2021-12-02 22:45         ` Peter Zijlstra
2021-12-03  7:53           ` Maxim Levitsky
2021-12-04 10:44             ` Peter Zijlstra

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=20211201024650.88254-2-pizhenwei@bytedance.com \
    --to=pizhenwei@bytedance.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®