[PATCH 5/6] x86: Add a cpuid command

Simon Glass sjg at chromium.org
Wed Aug 28 03:44:28 CEST 2024


It is useful to obtain the results of cpuid queries, so add a command
for this.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 cmd/x86/Makefile        |  2 +-
 cmd/x86/cpuid.c         | 37 ++++++++++++++++++++++
 doc/usage/cmd/cpuid.rst | 68 +++++++++++++++++++++++++++++++++++++++++
 doc/usage/index.rst     |  1 +
 test/cmd/Makefile       |  1 +
 test/cmd/cpuid.c        | 22 +++++++++++++
 6 files changed, 130 insertions(+), 1 deletion(-)
 create mode 100644 cmd/x86/cpuid.c
 create mode 100644 doc/usage/cmd/cpuid.rst
 create mode 100644 test/cmd/cpuid.c

diff --git a/cmd/x86/Makefile b/cmd/x86/Makefile
index b1f39d3bfde..1648907ac2d 100644
--- a/cmd/x86/Makefile
+++ b/cmd/x86/Makefile
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0+
 
 obj-$(CONFIG_CMD_CBSYSINFO) += cbsysinfo.o
-obj-y += mtrr.o
+obj-y += cpuid.o mtrr.o
 obj-$(CONFIG_CMD_EXCEPTION) += exception.o
 obj-$(CONFIG_USE_HOB) += hob.o
 obj-$(CONFIG_HAVE_FSP) += fsp.o
diff --git a/cmd/x86/cpuid.c b/cmd/x86/cpuid.c
new file mode 100644
index 00000000000..222754b5d15
--- /dev/null
+++ b/cmd/x86/cpuid.c
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * The 'cpuid' command provides access to the CPU's cpuid information
+ *
+ * Copyright 2024 Google, LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#include <command.h>
+#include <vsprintf.h>
+#include <asm/cpu.h>
+
+static int do_cpuid(struct cmd_tbl *cmdtp, int flag, int argc,
+		    char *const argv[])
+{
+	struct cpuid_result res;
+	ulong op;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	op = hextoul(argv[1], NULL);
+	res = cpuid(op);
+	printf("eax %08x\n", res.eax);
+	printf("ebx %08x\n", res.ebx);
+	printf("ecx %08x\n", res.ecx);
+	printf("edx %08x\n", res.edx);
+
+	return 0;
+}
+
+U_BOOT_LONGHELP(cpuid, "Show CPU Identification information");
+
+U_BOOT_CMD(
+	cpuid,	2,	1,	do_cpuid,
+	"cpuid <op>", cpuid_help_text
+);
diff --git a/doc/usage/cmd/cpuid.rst b/doc/usage/cmd/cpuid.rst
new file mode 100644
index 00000000000..cccf9262ed4
--- /dev/null
+++ b/doc/usage/cmd/cpuid.rst
@@ -0,0 +1,68 @@
+.. SPDX-License-Identifier: GPL-2.0+
+
+.. index::
+   single: cpuid (command)
+
+cpuid command
+=============
+
+Synopsis
+--------
+
+::
+
+    cpuid <op>
+
+Description
+-----------
+
+The cpuid command requests CPU-identification information on x86 CPUs. The
+operation <op> selects what information is returned. Up to four 32-bit registers
+can be update (eax-edx) depending on the operation.
+
+Configuration
+-------------
+
+The cpuid command is only available on x86.
+
+Return value
+------------
+
+The return value $? is 0 (true).
+
+Example
+-------
+
+::
+
+    => cpuid 1
+    eax 00060fb1
+    ebx 00040800
+    ecx 80002001
+    edx 178bfbfd
+
+This shows checking for 64-bit 'long' mode::
+
+    => cpuid 80000000
+    eax 8000000a
+    ebx 68747541
+    ecx 444d4163
+    edx 69746e65
+    => cpuid 80000001
+    eax 00060fb1
+    ebx 00000000
+    ecx 00000007
+    edx 2193fbfd   # Bit 29 is set in edx, so long mode is available
+
+On a 32-bit-only CPU::
+
+    => cpuid 80000000
+    eax 80000004
+    ebx 756e6547
+    ecx 6c65746e
+    edx 49656e69
+    => cpuid 80000001
+    eax 00000663
+    ebx 00000000
+    ecx 00000000
+    edx 00000000   # Bit 29 is not set in edx, so long mode is not available
diff --git a/doc/usage/index.rst b/doc/usage/index.rst
index b058c2254fb..986554090af 100644
--- a/doc/usage/index.rst
+++ b/doc/usage/index.rst
@@ -52,6 +52,7 @@ Shell commands
    cmd/conitrace
    cmd/cp
    cmd/cpu
+   cmd/cpuid
    cmd/cyclic
    cmd/dm
    cmd/ebtupdate
diff --git a/test/cmd/Makefile b/test/cmd/Makefile
index 8f2134998ad..dedcec33c8e 100644
--- a/test/cmd/Makefile
+++ b/test/cmd/Makefile
@@ -12,6 +12,7 @@ ifdef CONFIG_CONSOLE_RECORD
 obj-$(CONFIG_CMD_PAUSE) += test_pause.o
 endif
 obj-y += exit.o mem.o
+obj-$(CONFIG_X86) += cpuid.o
 obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
 obj-$(CONFIG_CMD_BDI) += bdinfo.o
 obj-$(CONFIG_CMD_FDT) += fdt.o
diff --git a/test/cmd/cpuid.c b/test/cmd/cpuid.c
new file mode 100644
index 00000000000..e07f5fd4696
--- /dev/null
+++ b/test/cmd/cpuid.c
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for cpuid command
+ *
+ * Copyright 2024 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#include <test/cmd.h>
+#include <test/ut.h>
+
+static int cmd_test_cpuid(struct unit_test_state *uts)
+{
+	ut_assertok(run_commandf("cpuid 1"));
+	ut_assert_nextline("eax 00060fb1");
+	ut_assert_nextline("ebx 00000800");
+	ut_assert_nextline("ecx 80002001");
+	ut_assert_nextline("edx 078bfbfd");
+
+	return 0;
+}
+CMD_TEST(cmd_test_cpuid, UTF_CONSOLE);
-- 
2.34.1



More information about the U-Boot mailing list