Skip to contentSkip to conversation

x86: tsc: Read the base frequency from CPUID leaf 0x16

PATCH In review Dmitrii Merkurev 3 participants 4 messages Oct 22 – Oct 28

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: tsc: Read the base frequency from CPUID leaf 0x16

cpu_mhz_from_cpuid() should return the base frequency in MHz from CPUID leaf 0x16 EAX. Commit ecf31113f178 ("x86: Sync up tsc_timer with Linux") changed it to read leaf 0x15 instead, while still checking that leaf 0x16 exists. Leaf 0x15 EAX is the denominator of the TSC / crystal clock ratio, typically 2.

The function is used when native_calibrate_tsc() fails on an Intel CPU that reports this ratio but not the crystal frequency:

- in 32-bit U-Boot, for models missing from its table, e.g. Skylake-X, Ice Lake-SP or Comet Lake
- in 64-bit U-Boot proper, always, because gd->arch.x86_model is never set there

U-Boot then assumes a 2 MHz TSC, so delays and timeouts are about 1000 times too short. For example, a Core i9-10900K reports 2 in leaf 0x15 EAX and 3700 in leaf 0x16 EAX.

Read leaf 0x16 again, as cpu_khz_from_cpuid() does in Linux.

Fixes ecf31113f178 ("x86: Sync up tsc_timer with Linux")

---
QEMU reports zero in both leaves, so I tested qemu-x86_64 with a debug change that makes the timer code see the leaf 0x15 and 0x16 values of the host, a Core i9-9900K: the TSC rate goes from 2 MHz to 3600 MHz and 'sleep 2' takes 2.0 s instead of 0.008 s.

 drivers/timer/tsc_timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/timer/tsc_timer.c +1 -1
index dd16ab320b..fe1888eb2c 100644
@@ -83,7 +83,7 @@ static unsigned long cpu_mhz_from_cpuid(void)
 	if (cpuid_eax(0) < 0x16)
 		return 0;
 
-	return cpuid_eax(0x15);
+	return cpuid_eax(0x16);
 }
 
 /*

base-commit: 903c9578eeb36d72f087082da494c605ddaed3b2

Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Dmitrii Merkurev

x86: tsc: Read the base frequency from CPUID leaf 0x16

cpu_mhz_from_cpuid() should return the base frequency in MHz from CPUID leaf 0x16 EAX. Commit ecf31113f178 ("x86: Sync up tsc_timer with Linux") changed it to read leaf 0x15 instead, while still checking that leaf 0x16 exists. Leaf 0x15 EAX is the denominator of the TSC / crystal clock ratio, typically 2.

The function is used when native_calibrate_tsc() fails on an Intel CPU that reports this ratio but not the crystal frequency:

- in 32-bit U-Boot, for models missing from its table, e.g. Skylake-X, Ice Lake-SP or Comet Lake
- in 64-bit U-Boot proper, always, because gd->arch.x86_model is never set there

U-Boot then assumes a 2 MHz TSC, so delays and timeouts are about 1000 times too short. For example, a Core i9-10900K reports 2 in leaf 0x15 EAX and 3700 in leaf 0x16 EAX.

Quoted 7 lines

Read leaf 0x16 again, as cpu_khz_from_cpuid() does in Linux.

Fixes ecf31113f178 ("x86: Sync up tsc_timer with Linux")
 drivers/timer/tsc_timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Dmitrii Merkurev
Quoted 23 lines · Dmitrii Merkurev

cpu_mhz_from_cpuid() should return the base frequency in MHz from CPUID leaf 0x16 EAX. Commit ecf31113f178 ("x86: Sync up tsc_timer with Linux") changed it to read leaf 0x15 instead, while still checking that leaf 0x16 exists. Leaf 0x15 EAX is the denominator of the TSC / crystal clock ratio, typically 2.

The function is used when native_calibrate_tsc() fails on an Intel CPU that reports this ratio but not the crystal frequency:

- in 32-bit U-Boot, for models missing from its table, e.g. Skylake-X, Ice Lake-SP or Comet Lake
- in 64-bit U-Boot proper, always, because gd->arch.x86_model is never set there

U-Boot then assumes a 2 MHz TSC, so delays and timeouts are about 1000 times too short. For example, a Core i9-10900K reports 2 in leaf 0x15 EAX and 3700 in leaf 0x16 EAX.

Read leaf 0x16 again, as cpu_khz_from_cpuid() does in Linux.

Fixes ecf31113f178 ("x86: Sync up tsc_timer with Linux")
Quoted 9 lines

---
QEMU reports zero in both leaves, so I tested qemu-x86_64 with a debug change that makes the timer code see the leaf 0x15 and 0x16 values of the host, a Core i9-9900K: the TSC rate goes from 2 MHz to 3600 MHz and 'sleep 2' takes 2.0 s instead of 0.008 s.

 drivers/timer/tsc_timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#3 lore raw
In reply to Dmitrii Merkurev
Quoted 23 lines · Dmitrii Merkurev

cpu_mhz_from_cpuid() should return the base frequency in MHz from CPUID leaf 0x16 EAX. Commit ecf31113f178 ("x86: Sync up tsc_timer with Linux") changed it to read leaf 0x15 instead, while still checking that leaf 0x16 exists. Leaf 0x15 EAX is the denominator of the TSC / crystal clock ratio, typically 2.

The function is used when native_calibrate_tsc() fails on an Intel CPU that reports this ratio but not the crystal frequency:

- in 32-bit U-Boot, for models missing from its table, e.g. Skylake-X, Ice Lake-SP or Comet Lake
- in 64-bit U-Boot proper, always, because gd->arch.x86_model is never set there

U-Boot then assumes a 2 MHz TSC, so delays and timeouts are about 1000 times too short. For example, a Core i9-10900K reports 2 in leaf 0x15 EAX and 3700 in leaf 0x16 EAX.

Read leaf 0x16 again, as cpu_khz_from_cpuid() does in Linux.

Fixes ecf31113f178 ("x86: Sync up tsc_timer with Linux")
Quoted 9 lines

---
QEMU reports zero in both leaves, so I tested qemu-x86_64 with a debug change that makes the timer code see the leaf 0x15 and 0x16 values of the host, a Core i9-9900K: the TSC rate goes from 2 MHz to 3600 MHz and 'sleep 2' takes 2.0 s instead of 0.008 s.

 drivers/timer/tsc_timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

applied to u-boot-x86, thanks!