Detecting x86 LAPIC timer frequency from CPUID data

From: Daniel Drake
Date: Wed Apr 17 2019 - 01:28:17 EST


The CPUID.0x16 leaf provides "Bus (Reference) Frequency (in MHz)".

In the thread "No 8254 PIT & no HPET on new Intel N3350 platforms
causes kernel panic during early boot" we are exploring ways to have
the kernel avoid using the PIT/HPET IRQ0 timer in more cases, and
Thomas Gleixner suggested that we could use this CPUID data to set
lapic_timer_frequency, avoiding the need for calibrate_APIC_clock()
to measure the APIC clock against the IRQ0 timer.

I'm thinking of the the following code change, however I get
unexpected results on Intel i7-8565U (Whiskey Lake). When
booting without this change, and with apic=notscdeadline (so that
APIC clock gets calibrated and used), the bus speed is detected as
23MHz:

... lapic delta = 149994
... PM-Timer delta = 357939
... PM-Timer result ok
..... delta 149994
..... mult: 6442193
..... calibration result: 23999
..... CPU clock speed is 1991.0916 MHz.
..... host bus clock speed is 23.0999 MHz.

However the CPUID.0x16 ECX reports a 100MHz bus speed on this device,
so this code change would produce a significantly different calibration.

Am I doing anything obviously wrong?

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 3fae23834069..6c51ce842f86 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -679,6 +679,16 @@ static unsigned long cpu_khz_from_cpuid(void)

cpuid(0x16, &eax_base_mhz, &ebx_max_mhz, &ecx_bus_mhz, &edx);

+#ifdef CONFIG_X86_LOCAL_APIC
+ /*
+ * If bus frequency is provided in CPUID data, set
+ * global lapic_timer_frequency to bus_clock_cycles/jiffy.
+ * This avoids having to calibrate the APIC timer later.
+ */
+ if (ecx_bus_mhz)
+ lapic_timer_frequency = (ecx_bus_mhz * 1000000) / HZ;
+#endif
+
return eax_base_mhz * 1000;
}

--
2.19.1