Re: [PATCH v2] x86/tsc: Have tsc=recalibrate override things

Peter Zijlstra
2023-11-01T17:50:55Z

On Wed, Nov 01, 2023 at 12:16:21PM +0100, Peter Zijlstra wrote:
> Subject: x86/tsc: Have tsc=recalibrate override things
> From: Peter Zijlstra <peterz@infradead.org>
> Date: Mon, 30 Oct 2023 17:00:50 +0100
>
> My brand-spanking new SPR supermicro workstation was reporting NTP
> failures:
>
> Oct 30 13:00:26 spr ntpd[3517]: CLOCK: kernel reports TIME_ERROR: 0x41: Clock Unsynchronized
> Oct 30 13:00:58 spr ntpd[3517]: CLOCK: time stepped by 32.316775
> Oct 30 13:00:58 spr ntpd[3517]: CLOCK: frequency error 41699 PPM exceeds tolerance 500 PPM
>
> CPUID provides:
>
> Time Stamp Counter/Core Crystal Clock Information (0x15):
> TSC/clock ratio = 200/2
> nominal core crystal clock = 25000000 Hz
> Processor Frequency Information (0x16):
> Core Base Frequency (MHz) = 0x9c4 (2500)
> Core Maximum Frequency (MHz) = 0x12c0 (4800)
> Bus (Reference) Frequency (MHz) = 0x64 (100)
>
> and the kernel believes this. Since commit a7ec817d5542 ("x86/tsc: Add
> option to force frequency recalibration with HW timer") there is the
> tsc=recalibrate option, which forces the recalibrate.
>
> This duely reports:
>
> Oct 30 12:42:39 spr kernel: tsc: Warning: TSC freq calibrated by CPUID/MSR differs from what is calibrated by HW timer, please check with vendor!!
> Oct 30 12:42:39 spr kernel: tsc: Previous calibrated TSC freq: 2500.000 MHz
> Oct 30 12:42:39 spr kernel: tsc: TSC freq recalibrated by [HPET]: 2399.967 MHz

Additionally, we could consider something like the below. This makes the
machine print:

[ 0.000000] DMI: Supermicro SYS-531A-I/X13SRA-TF, BIOS 1.1b 08/01/2023
[ 0.000000] tsc: [Firmware Bug]: DMI based CPUID-15h crystal frequency override: 24000000

This way I can boot without additional parameters.

---
Index: linux-2.6/arch/x86/kernel/tsc.c
===================================================================
--- linux-2.6.orig/arch/x86/kernel/tsc.c
+++ linux-2.6/arch/x86/kernel/tsc.c
@@ -15,6 +15,7 @@
#include <linux/timex.h>
#include <linux/static_key.h>
#include <linux/static_call.h>
+#include <linux/dmi.h>

#include <asm/hpet.h>
#include <asm/timer.h>
@@ -651,6 +652,35 @@ success:
return delta;
}

+static unsigned long dmi_crystal_hz;
+
+static int dmi_crystal_hz_override(const struct dmi_system_id *d)
+{
+ if (dmi_crystal_hz != (unsigned long)d->driver_data) {
+ dmi_crystal_hz = (unsigned long)d->driver_data;
+ pr_err(FW_BUG "DMI based CPUID-15h crystal frequency override: %lu\n",
+ dmi_crystal_hz);
+ }
+ return 1;
+}
+
+/*
+ * List of systems that managed to screw up CPUID-15h :-(
+ */
+static const struct dmi_system_id tsc_dmi_table[] = {
+ {
+ .callback = dmi_crystal_hz_override,
+ .ident = "Supermicro X13SRA-TF",
+ .matches = {
+ DMI_MATCH(DMI_BOARD_VENDOR, "Supermicro"),
+ DMI_MATCH(DMI_BOARD_NAME, "X13SRA-TF"),
+ },
+ /* The board advertises a 25MHz crystal, in reality it has 24MHz */
+ .driver_data = (void *)24000000UL,
+ },
+ {}
+};
+
/**
* native_calibrate_tsc
* Determine TSC frequency via CPUID, else return 0.
@@ -671,6 +701,10 @@ unsigned long native_calibrate_tsc(void)
/* CPUID 15H TSC/Crystal ratio, plus optionally Crystal Hz */
cpuid(0x15, &eax_denominator, &ebx_numerator, &ecx_hz, &edx);

+ dmi_crystal_hz = ecx_hz;
+ if (ecx_hz && dmi_check_system(tsc_dmi_table))
+ ecx_hz = dmi_crystal_hz;
+
if (ebx_numerator == 0 || eax_denominator == 0)
return 0;