Skip to contentSkip to conversation

x86: Do CPU identification in the early phase

PATCH New Bin Meng 2 participants 3 messages Nov 9, 2014 – Nov 11, 2014 x86

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH v2 1/4 x86: Do CPU identification in the early phase

The CPU identification happens in x86_cpu_init_f() and corresponding fields are saved in the global data for later use.

---
Changes for v2:
- Update the patch per review comments from Simon Glass
- cpu_vendor_name() and fill_processor_name() are not static now

 arch/x86/cpu/cpu.c                 | 277 +++++++++++++++++++++++++++++++------
 arch/x86/include/asm/cpu.h         | 168 +++++++++++++++++++++-
 arch/x86/include/asm/global_data.h |   5 +
 3 files changed, 402 insertions(+), 48 deletions(-)
arch/x86/cpu/cpu.c +233 -44
index 6441dde..e3fa21d 100644
@@ -13,6 +13,9 @@
  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  * Alex Zuepke <[email protected]>
  *
+ * Part of this file is adapted from coreboot
+ * src/arch/x86/lib/cpu.c
+ *
  * SPDX-License-Identifier:	GPL-2.0+
  */
 
@@ -27,6 +30,8 @@
 #include <asm/interrupt.h>
 #include <linux/compiler.h>
 
+DECLARE_GLOBAL_DATA_PTR;
+
 /*
  * Constructor for a conventional segment GDT (or LDT) entry
  * This is a macro so it can be used in initialisers
@@ -43,6 +48,52 @@ struct gdt_ptr {
 	u32 ptr;
 } __packed;
 
+struct cpu_device_id {
+	unsigned vendor;
+	unsigned device;
+};
+
+struct cpuinfo_x86 {
+        uint8_t    x86;            /* CPU family */
+        uint8_t    x86_vendor;     /* CPU vendor */
+        uint8_t    x86_model;
+        uint8_t    x86_mask;
+};
+
+/*
+ * List of cpu vendor strings along with their normalized
+ * id values.
+ */
+static struct {
+	int vendor;
+	const char *name;
+} x86_vendors[] = {
+	{ X86_VENDOR_INTEL,     "GenuineIntel", },
+	{ X86_VENDOR_CYRIX,     "CyrixInstead", },
+	{ X86_VENDOR_AMD,       "AuthenticAMD", },
+	{ X86_VENDOR_UMC,       "UMC UMC UMC ", },
+	{ X86_VENDOR_NEXGEN,    "NexGenDriven", },
+	{ X86_VENDOR_CENTAUR,   "CentaurHauls", },
+	{ X86_VENDOR_RISE,      "RiseRiseRise", },
+	{ X86_VENDOR_TRANSMETA, "GenuineTMx86", },
+	{ X86_VENDOR_TRANSMETA, "TransmetaCPU", },
+	{ X86_VENDOR_NSC,       "Geode by NSC", },
+	{ X86_VENDOR_SIS,       "SiS SiS SiS ", },
+};
+
+static const char *x86_vendor_name[] = {
+	[X86_VENDOR_INTEL]     = "Intel",
+	[X86_VENDOR_CYRIX]     = "Cyrix",
+	[X86_VENDOR_AMD]       = "AMD",
+	[X86_VENDOR_UMC]       = "UMC",
+	[X86_VENDOR_NEXGEN]    = "NexGen",
+	[X86_VENDOR_CENTAUR]   = "Centaur",
+	[X86_VENDOR_RISE]      = "Rise",
+	[X86_VENDOR_TRANSMETA] = "Transmeta",
+	[X86_VENDOR_NSC]       = "NSC",
+	[X86_VENDOR_SIS]       = "SiS",
+};
+
 static void load_ds(u32 segment)
 {
 	asm volatile("movl %0, %%ds" : : "r" (segment * X86_GDT_ENTRY_SIZE));
@@ -115,6 +166,131 @@ int __weak x86_cleanup_before_linux(void)
 	return 0;
 }
 
+/*
+ * Cyrix CPUs without cpuid or with cpuid not yet enabled can be detected
+ * by the fact that they preserve the flags across the division of 5/2.
+ * PII and PPro exhibit this behavior too, but they have cpuid available.
+ */
+
+/*
+ * Perform the Cyrix 5/2 test. A Cyrix won't change
+ * the flags, while other 486 chips will.
+ */
+static inline int test_cyrix_52div(void)
+{
+	unsigned int test;
+
+	__asm__ __volatile__(
+	     "sahf\n\t"		/* clear flags (%eax = 0x0005) */
+	     "div %b2\n\t"	/* divide 5 by 2 */
+	     "lahf"		/* store flags into %ah */
+	     : "=a" (test)
+	     : "0" (5), "q" (2)
+	     : "cc");
+
+	/* AH is 0x02 on Cyrix after the divide.. */
+	return (unsigned char) (test >> 8) == 0x02;
+}
+
+/*
+ *	Detect a NexGen CPU running without BIOS hypercode new enough
+ *	to have CPUID. (Thanks to Herbert Oppmann)
+ */
+
+static int deep_magic_nexgen_probe(void)
+{
+	int ret;
+
+	__asm__ __volatile__ (
+		"	movw	$0x5555, %%ax\n"
+		"	xorw	%%dx,%%dx\n"
+		"	movw	$2, %%cx\n"
+		"	divw	%%cx\n"
+		"	movl	$0, %%eax\n"
+		"	jnz	1f\n"
+		"	movl	$1, %%eax\n"
+		"1:\n"
+		: "=a" (ret) : : "cx", "dx" );
+	return  ret;
+}
+
+static bool has_cpuid(void)
+{
+	return flag_is_changeable_p(X86_EFLAGS_ID);
+}
+
+static int build_vendor_name(char *vendor_name)
+{
+	struct cpuid_result result;
+	result = cpuid(0x00000000);
+	unsigned int *name_as_ints = (unsigned int *)vendor_name;
+
+	name_as_ints[0] = result.ebx;
+	name_as_ints[1] = result.edx;
+	name_as_ints[2] = result.ecx;
+
+	return result.eax;
+}
+
+static void identify_cpu(struct cpu_device_id *cpu)
+{
+	char vendor_name[16];
+	int i;
+
+	vendor_name[0] = '\0'; /* Unset */
+
+	/* Find the id and vendor_name */
+	if (!has_cpuid()) {
+		/* Its a 486 if we can modify the AC flag */
+		if (flag_is_changeable_p(X86_EFLAGS_AC)) {
+			cpu->device = 0x00000400; /* 486 */
+		} else {
+			cpu->device = 0x00000300; /* 386 */
+		}
+		if ((cpu->device == 0x00000400) && test_cyrix_52div()) {
+			memcpy(vendor_name, "CyrixInstead", 13);
+			/* If we ever care we can enable cpuid here */
+		}
+		/* Detect NexGen with old hypercode */
+		else if (deep_magic_nexgen_probe()) {
+			memcpy(vendor_name, "NexGenDriven", 13);
+		}
+	}
+	if (has_cpuid()) {
+		int  cpuid_level;
+
+		cpuid_level = build_vendor_name(vendor_name);
+		vendor_name[12] = '\0';
+
+		/* Intel-defined flags: level 0x00000001 */
+		if (cpuid_level >= 0x00000001) {
+			cpu->device = cpuid_eax(0x00000001);
+		}
+		else {
+			/* Have CPUID level 0 only unheard of */
+			cpu->device = 0x00000400;
+		}
+	}
+	cpu->vendor = X86_VENDOR_UNKNOWN;
+	for(i = 0; i < ARRAY_SIZE(x86_vendors); i++) {
+		if (memcmp(vendor_name, x86_vendors[i].name, 12) == 0) {
+			cpu->vendor = x86_vendors[i].vendor;
+			break;
+		}
+	}
+}
+
+static inline void get_fms(struct cpuinfo_x86 *c, uint32_t tfms)
+{
+	c->x86 = (tfms >> 8) & 0xf;
+	c->x86_model = (tfms >> 4) & 0xf;
+	c->x86_mask = tfms & 0xf;
+	if (c->x86 == 0xf)
+		c->x86 += (tfms >> 20) & 0xff;
+	if (c->x86 >= 0x6)
+		c->x86_model += ((tfms >> 16) & 0xF) << 4;
+}
+
 int x86_cpu_init_f(void)
 {
 	const u32 em_rst = ~X86_CR0_EM;
@@ -128,6 +304,20 @@ int x86_cpu_init_f(void)
 	     "movl %%eax, %%cr0\n" \
 	     : : "i" (em_rst), "i" (mp_ne_set) : "eax");
 
+	/* identify CPU via cpuid and store the decoded info into gd->arch */
+	if (has_cpuid()) {
+		struct cpu_device_id cpu;
+		struct cpuinfo_x86 c;
+
+		identify_cpu(&cpu);
+		get_fms(&c, cpu.device);
+		gd->arch.x86 = c.x86;
+		gd->arch.x86_vendor = cpu.vendor;
+		gd->arch.x86_model = c.x86_model;
+		gd->arch.x86_mask = c.x86_mask;
+		gd->arch.x86_device = cpu.device;
+	}
+
 	return 0;
 }
 
@@ -277,55 +467,14 @@ void cpu_disable_paging_pae(void)
 		: "eax");
 }
 
-static bool has_cpuid(void)
-{
-	unsigned long flag;
-
-	asm volatile("pushf\n" \
-		"pop %%eax\n"
-		"mov %%eax, %%ecx\n"	/* ecx = flags */
-		"xor %1, %%eax\n"
-		"push %%eax\n"
-		"popf\n"		/* flags ^= $2 */
-		"pushf\n"
-		"pop %%eax\n"		/* eax = flags */
-		"push %%ecx\n"
-		"popf\n"		/* flags = ecx */
-		"xor %%ecx, %%eax\n"
-		"mov %%eax, %0"
-		: "=r" (flag)
-		: "i" (1 << 21)
-		: "eax", "ecx", "memory");
-
-	return flag != 0;
-}
-
 static bool can_detect_long_mode(void)
 {
-	unsigned long flag;
-
-	asm volatile("mov $0x80000000, %%eax\n"
-		"cpuid\n"
-		"mov %%eax, %0"
-		: "=r" (flag)
-		:
-		: "eax", "ebx", "ecx", "edx", "memory");
-
-	return flag > 0x80000000UL;
+	return cpuid_eax(0x80000000) > 0x80000000UL;
 }
 
 static bool has_long_mode(void)
 {
-	unsigned long flag;
-
-	asm volatile("mov $0x80000001, %%eax\n"
-		"cpuid\n"
-		"mov %%edx, %0"
-		: "=r" (flag)
-		:
-		: "eax", "ebx", "ecx", "edx", "memory");
-
-	return flag & (1 << 29) ? true : false;
+	return cpuid_edx(0x80000001) & (1 << 29) ? true : false;
 }
 
 int cpu_has_64bit(void)
@@ -334,9 +483,49 @@ int cpu_has_64bit(void)
 		has_long_mode();
 }
 
+const char *cpu_vendor_name(int vendor)
+{
+	const char *name;
+	name = "<invalid cpu vendor>";
+	if ((vendor < (ARRAY_SIZE(x86_vendor_name))) &&
+		(x86_vendor_name[vendor] != 0))
+	{
+		name = x86_vendor_name[vendor];
+	}
+	return name;
+}
+
+void fill_processor_name(char *processor_name)
+{
+	struct cpuid_result regs;
+	char temp_processor_name[49];
+	char *processor_name_start;
+	unsigned int *name_as_ints = (unsigned int *)temp_processor_name;
+	int i;
+
+	for (i = 0; i < 3; i++) {
+		regs = cpuid(0x80000002 + i);
+		name_as_ints[i * 4 + 0] = regs.eax;
+		name_as_ints[i * 4 + 1] = regs.ebx;
+		name_as_ints[i * 4 + 2] = regs.ecx;
+		name_as_ints[i * 4 + 3] = regs.edx;
+	}
+
+	temp_processor_name[48] = 0;
+
+	/* Skip leading spaces. */
+	processor_name_start = temp_processor_name;
+	while (*processor_name_start == ' ')
+		processor_name_start++;
+
+	memset(processor_name, 0, 49);
+	strcpy(processor_name, processor_name_start);
+}
+
 int print_cpuinfo(void)
 {
-	printf("CPU:   %s\n", cpu_has_64bit() ? "x86_64" : "x86");
+	printf("CPU: %s, vendor %s, device %xh\n", cpu_has_64bit() ? "x86_64" : "x86",
+		cpu_vendor_name(gd->arch.x86_vendor), gd->arch.x86_device);
 
 	return 0;
 }
arch/x86/include/asm/cpu.h +164 -4

186 lines not on this page · raw message

arch/x86/include/asm/global_data.h +5 -0
index 9eae228..3e3073a 100644
@@ -13,6 +13,11 @@
 /* Architecture-specific global data */
 struct arch_global_data {
 	struct global_data *gd_addr;		/* Location of Global Data */
+	uint8_t  x86;			/* CPU family */
+	uint8_t  x86_vendor;		/* CPU vendor */
+	uint8_t  x86_model;
+	uint8_t  x86_mask;
+	uint32_t x86_device;
 	uint64_t tsc_base;		/* Initial value returned by rdtsc() */
 	uint32_t tsc_base_kclocks;	/* Initial tsc as a kclocks value */
 	uint32_t tsc_prev;		/* For show_boot_progress() */
Signature · 1 line
1.8.2.1
#1 lore raw

PATCH v2 1/4 x86: Do CPU identification in the early phase

Hi Bin,

Quoted 9 lines · Bin Meng

The CPU identification happens in x86_cpu_init_f() and corresponding fields are saved in the global data for later use.

---
Changes for v2:
- Update the patch per review comments from Simon Glass
- cpu_vendor_name() and fill_processor_name() are not static now

Quoted 4 lines
 arch/x86/cpu/cpu.c                 | 277 +++++++++++++++++++++++++++++++------
 arch/x86/include/asm/cpu.h         | 168 +++++++++++++++++++++-
 arch/x86/include/asm/global_data.h |   5 +
 3 files changed, 402 insertions(+), 48 deletions(-)

Applied to u-boot-x86/master, thanks!

Note I had to clean up style problems in these patches. I decided to do this instead of asking for a respin since I didn't check it for version 1.

$ patman -nm -c4
Cleaned 4 patches
8 errors, 8 warnings, 2 checks for
0001-x86-Do-CPU-identification-in-the-early-phase.patch:
error: arch/x86/cpu/cpu.c,57: code indent should use tabs where possible
warning: arch/x86/cpu/cpu.c,57: please, no spaces at the start of a line
error: arch/x86/cpu/cpu.c,58: code indent should use tabs where possible
warning: arch/x86/cpu/cpu.c,58: please, no spaces at the start of a line
error: arch/x86/cpu/cpu.c,59: code indent should use tabs where possible
warning: arch/x86/cpu/cpu.c,59: please, no spaces at the start of a line
error: arch/x86/cpu/cpu.c,60: code indent should use tabs where possible
warning: arch/x86/cpu/cpu.c,60: please, no spaces at the start of a line
warning: arch/x86/cpu/cpu.c,84: static const char * array should
probably be static const char * const
error: arch/x86/cpu/cpu.c,213: space prohibited before that close
parenthesis ')'
warning: arch/x86/cpu/cpu.c,245: braces {} are not necessary for any
arm of this statement
warning: arch/x86/cpu/cpu.c,255: braces {} are not necessary for
single statement blocks
error: arch/x86/cpu/cpu.c,269: else should follow close brace '}'
error: arch/x86/cpu/cpu.c,275: space required before the open parenthesis '('
error: arch/x86/cpu/cpu.c,490: that open brace { should be on the previous line
check: arch/x86/cpu/cpu.c,491: Alignment should match open parenthesis
warning: arch/x86/cpu/cpu.c,527: line over 80 characters
check: arch/x86/cpu/cpu.c,528: Alignment should match open parenthesis

0 errors, 0 warnings, 2 checks for 0002-x86-Do-TSC-MSR-calibration-only-for-known-supported-.patch:
check: arch/x86/lib/tsc_timer.c,62: Alignment should match open parenthesis
check: arch/x86/lib/tsc_timer.c,105: Alignment should match open parenthesis

0 errors, 1 warnings, 0 checks for 0003-x86-Add-quick-TSC-calibration-via-PIT.patch:
warning: arch/x86/lib/tsc_timer.c,162: line over 80 characters

Please can you make sure you submit them with patman so that these problems are checked?

Regards,
Simon

author
#2 lore raw

PATCH v2 1/4 x86: Do CPU identification in the early phase

Hi Simon,

Quoted 55 lines · Simon Glass

Hi Bin,

Quoted 9 lines · Bin Meng

The CPU identification happens in x86_cpu_init_f() and corresponding fields are saved in the global data for later use.

---
Changes for v2:
- Update the patch per review comments from Simon Glass
- cpu_vendor_name() and fill_processor_name() are not static now

Quoted 4 lines
 arch/x86/cpu/cpu.c                 | 277 +++++++++++++++++++++++++++++++------
 arch/x86/include/asm/cpu.h         | 168 +++++++++++++++++++++-
 arch/x86/include/asm/global_data.h |   5 +
 3 files changed, 402 insertions(+), 48 deletions(-)

Applied to u-boot-x86/master, thanks!

Note I had to clean up style problems in these patches. I decided to do this instead of asking for a respin since I didn't check it for version 1.

$ patman -nm -c4
Cleaned 4 patches
8 errors, 8 warnings, 2 checks for
0001-x86-Do-CPU-identification-in-the-early-phase.patch:
error: arch/x86/cpu/cpu.c,57: code indent should use tabs where possible
warning: arch/x86/cpu/cpu.c,57: please, no spaces at the start of a line
error: arch/x86/cpu/cpu.c,58: code indent should use tabs where possible
warning: arch/x86/cpu/cpu.c,58: please, no spaces at the start of a line
error: arch/x86/cpu/cpu.c,59: code indent should use tabs where possible
warning: arch/x86/cpu/cpu.c,59: please, no spaces at the start of a line
error: arch/x86/cpu/cpu.c,60: code indent should use tabs where possible
warning: arch/x86/cpu/cpu.c,60: please, no spaces at the start of a line
warning: arch/x86/cpu/cpu.c,84: static const char * array should
probably be static const char * const
error: arch/x86/cpu/cpu.c,213: space prohibited before that close
parenthesis ')'
warning: arch/x86/cpu/cpu.c,245: braces {} are not necessary for any
arm of this statement
warning: arch/x86/cpu/cpu.c,255: braces {} are not necessary for
single statement blocks
error: arch/x86/cpu/cpu.c,269: else should follow close brace '}'
error: arch/x86/cpu/cpu.c,275: space required before the open parenthesis '('
error: arch/x86/cpu/cpu.c,490: that open brace { should be on the previous line
check: arch/x86/cpu/cpu.c,491: Alignment should match open parenthesis
warning: arch/x86/cpu/cpu.c,527: line over 80 characters
check: arch/x86/cpu/cpu.c,528: Alignment should match open parenthesis

0 errors, 0 warnings, 2 checks for 0002-x86-Do-TSC-MSR-calibration-only-for-known-supported-.patch:
check: arch/x86/lib/tsc_timer.c,62: Alignment should match open parenthesis
check: arch/x86/lib/tsc_timer.c,105: Alignment should match open parenthesis

Quoted 7 lines

0 errors, 1 warnings, 0 checks for 0003-x86-Add-quick-TSC-calibration-via-PIT.patch:
warning: arch/x86/lib/tsc_timer.c,162: line over 80 characters

Please can you make sure you submit them with patman so that these problems are checked?

I am really sorry for not checking the patches before sending them out. I will do it in the future. Thanks again!

Regards,
Bin