x86: tsc: Read the base frequency from CPUID leaf 0x16
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.
---
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
@@ -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
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 leaf0x16EAX. Commitecf31113f178("x86: Sync uptsc_timerwith Linux") changed it to read leaf0x15instead, while still checking that leaf0x16exists. Leaf0x15EAX 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, becausegd->arch.x86_modelis never set thereU-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
0x15EAX and 3700 in leaf0x16EAX.
Quoted 7 lines
Read leaf
0x16again, ascpu_khz_from_cpuid()does in Linux.Fixes ecf31113f178 ("x86: Sync up tsc_timer with Linux")Signed-off-by Dmitrii Merkurev <[email protected]>drivers/timer/tsc_timer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 23 lines · Dmitrii Merkurev
cpu_mhz_from_cpuid()should return the base frequency in MHz from CPUID leaf0x16EAX. Commitecf31113f178("x86: Sync uptsc_timerwith Linux") changed it to read leaf0x15instead, while still checking that leaf0x16exists. Leaf0x15EAX 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, becausegd->arch.x86_modelis never set thereU-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
0x15EAX and 3700 in leaf0x16EAX.Read leaf
0x16again, ascpu_khz_from_cpuid()does in Linux.Fixes ecf31113f178 ("x86: Sync up tsc_timer with Linux")Signed-off-by Dmitrii Merkurev <[email protected]>
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 leaf0x15and0x16values 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(-)
Quoted 23 lines · Dmitrii Merkurev
cpu_mhz_from_cpuid()should return the base frequency in MHz from CPUID leaf0x16EAX. Commitecf31113f178("x86: Sync uptsc_timerwith Linux") changed it to read leaf0x15instead, while still checking that leaf0x16exists. Leaf0x15EAX 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, becausegd->arch.x86_modelis never set thereU-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
0x15EAX and 3700 in leaf0x16EAX.Read leaf
0x16again, ascpu_khz_from_cpuid()does in Linux.Fixes ecf31113f178 ("x86: Sync up tsc_timer with Linux")Signed-off-by Dmitrii Merkurev <[email protected]>
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 leaf0x15and0x16values 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!