[PATCH 3/6] x86/cpuid: Add smp helper

From: Sandipan Das
Date: Wed Jul 19 2023 - 02:57:36 EST


Depending on which CPU the CPUID instruction is executed, some leaves
can report different values. There are cases where it may be required
to know all possible values.

E.g. for AMD Zen 4 processors, the ActiveUmcMask field from leaf
0x80000022 ECX, which provides a way to determine the active memory
controllers, can have different masks on CPUs belonging to different
sockets as each socket can follow a different DIMM population scheme.
Each memory channel is assigned a memory controller (UMC) and if no
DIMMs are attached to a channel, the corresponding memory controller
is inactive. There are performance monitoring counters exclusive to
each memory controller which need to be represented under separate
PMUs. So, it will be necessary to know the active memory controllers
on each socket during the initialization of the UMC PMUs irrespective
of where the uncore driver's module init runs.

Add a new helper that executes CPUID on a particular CPU and returns
the EAX, EBX, ECX and EDX values.

Signed-off-by: Sandipan Das <sandipan.das@xxxxxxx>
---
arch/x86/include/asm/cpuid.h | 14 ++++++++++++++
arch/x86/lib/Makefile | 2 +-
arch/x86/lib/cpuid-smp.c | 36 ++++++++++++++++++++++++++++++++++++
3 files changed, 51 insertions(+), 1 deletion(-)
create mode 100644 arch/x86/lib/cpuid-smp.c

diff --git a/arch/x86/include/asm/cpuid.h b/arch/x86/include/asm/cpuid.h
index 9bee3e7bf973..17e74d4584f5 100644
--- a/arch/x86/include/asm/cpuid.h
+++ b/arch/x86/include/asm/cpuid.h
@@ -150,6 +150,20 @@ static __always_inline bool cpuid_function_is_indexed(u32 function)
return false;
}

+#ifdef CONFIG_SMP
+int cpuid_on_cpu(unsigned int cpu, unsigned int op,
+ unsigned int *eax, unsigned int *ebx,
+ unsigned int *ecx, unsigned int *edx);
+#else /* CONFIG_SMP */
+static inline int cpuid_on_cpu(unsigned int cpu, unsigned int op,
+ unsigned int *eax, unsigned int *ebx,
+ unsigned int *ecx, unsigned int *edx)
+{
+ cpuid(op, eax, ebx, ecx, edx);
+ return 0;
+}
+#endif /* CONFIG_SMP */
+
#define for_each_possible_hypervisor_cpuid_base(function) \
for (function = 0x40000000; function < 0x40010000; function += 0x100)

diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile
index ea3a28e7b613..e0097ae55edf 100644
--- a/arch/x86/lib/Makefile
+++ b/arch/x86/lib/Makefile
@@ -39,7 +39,7 @@ $(obj)/inat.o: $(obj)/inat-tables.c

clean-files := inat-tables.c

-obj-$(CONFIG_SMP) += msr-smp.o cache-smp.o
+obj-$(CONFIG_SMP) += msr-smp.o cache-smp.o cpuid-smp.o

lib-y := delay.o misc.o cmdline.o cpu.o
lib-y += usercopy_$(BITS).o usercopy.o getuser.o putuser.o
diff --git a/arch/x86/lib/cpuid-smp.c b/arch/x86/lib/cpuid-smp.c
new file mode 100644
index 000000000000..87340893ff61
--- /dev/null
+++ b/arch/x86/lib/cpuid-smp.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/export.h>
+#include <linux/smp.h>
+#include <asm/cpuid.h>
+
+struct cpuid_info {
+ u32 op;
+ struct cpuid_regs regs;
+};
+
+static void __cpuid_smp(void *info)
+{
+ struct cpuid_info *rv = info;
+
+ cpuid(rv->op, &rv->regs.eax, &rv->regs.ebx, &rv->regs.ecx, &rv->regs.edx);
+}
+
+int cpuid_on_cpu(unsigned int cpu, unsigned int op,
+ unsigned int *eax, unsigned int *ebx,
+ unsigned int *ecx, unsigned int *edx)
+{
+ struct cpuid_info rv;
+ int err;
+
+ memset(&rv, 0, sizeof(rv));
+
+ rv.op = op;
+ err = smp_call_function_single(cpu, __cpuid_smp, &rv, 1);
+ *eax = rv.regs.eax;
+ *ebx = rv.regs.ebx;
+ *ecx = rv.regs.ecx;
+ *edx = rv.regs.edx;
+
+ return err;
+}
+EXPORT_SYMBOL(cpuid_on_cpu);
--
2.34.1