[PATCH v2] x86: cpu: x86_64: Detect CPU vendor and device ID using cpuid

Desapogu Jayaramudu <[email protected]>
Newsgroups org.u-boot-project.lists.u-boot
Message-ID <[email protected]>
When U-Boot is configured as a coreboot payload on x86_64, the current
code leaves the CPU identity at the default Intel vendor and device ID
0x0, even on non-Intel platforms.

Read CPUID leaf 0 and build the 12-byte vendor string from EBX:EDX:ECX
to identify the CPU vendor at runtime. Set gd->arch.x86_vendor to Intel/AMD
when matched, with fallback to X86_VENDOR_ANY for unknown vendors. Also
stores cpuid_eax(1) in gd->arch.x86_device so later x86 code can use the
detected CPU indentity.

This avoids relying on a fixed vendor value and keeps vendor-sensitive
paths(e.g. TSC calibration) aligned with the actual CPU.

Signed-off-by: Desapogu Jayaramudu <[email protected]>
---
v2:
- fixed type GenuineIntel

 arch/x86/cpu/x86_64/cpu.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/arch/x86/cpu/x86_64/cpu.c b/arch/x86/cpu/x86_64/cpu.c
index 25ae92c702fbca..57d11499437fa3 100644
--- a/arch/x86/cpu/x86_64/cpu.c
+++ b/arch/x86/cpu/x86_64/cpu.c
@@ -50,8 +50,21 @@ static void setup_sse_features(void)
 
 int x86_cpu_reinit_f(void)
 {
-	/* set the vendor to Intel so that native_calibrate_tsc() works */
-	gd->arch.x86_vendor = X86_VENDOR_INTEL;
+	struct cpuid_result res;
+	char vendor[13];
+
+	res = cpuid(0x00000000);
+	memcpy(&vendor[0], &res.ebx, 4);
+	memcpy(&vendor[4], &res.edx, 4);
+	memcpy(&vendor[8], &res.ecx, 4);
+	vendor[12] = '\0';
+	if (!strcmp(vendor, "GenuineIntel"))
+		gd->arch.x86_vendor = X86_VENDOR_INTEL; /* native_calibrate_tsc() works*/
+	else if (!strcmp(vendor, "AuthenticAMD"))
+		gd->arch.x86_vendor = X86_VENDOR_AMD;
+	else
+		gd->arch.x86_vendor = X86_VENDOR_ANY;
+	gd->arch.x86_device = cpuid_eax(1);
 	gd->arch.has_mtrr = true;
 	if (IS_ENABLED(CONFIG_X86_HARDFP))
 		setup_sse_features();
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.