From owner-svn-src-stable-12@freebsd.org Wed Apr 15 00:36:01 2020 Return-Path: Delivered-To: svn-src-stable-12@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 1F5AC2CE075; Wed, 15 Apr 2020 00:36:01 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4923LS70TCz425d; Wed, 15 Apr 2020 00:36:00 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id E720DE591; Wed, 15 Apr 2020 00:36:00 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 03F0a0Ep052281; Wed, 15 Apr 2020 00:36:00 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 03F0a0n8052280; Wed, 15 Apr 2020 00:36:00 GMT (envelope-from kib@FreeBSD.org) Message-Id: <202004150036.03F0a0n8052280@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Wed, 15 Apr 2020 00:36:00 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r359947 - stable/12/sys/x86/x86 X-SVN-Group: stable-12 X-SVN-Commit-Author: kib X-SVN-Commit-Paths: stable/12/sys/x86/x86 X-SVN-Commit-Revision: 359947 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-12@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for only the 12-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Apr 2020 00:36:01 -0000 Author: kib Date: Wed Apr 15 00:36:00 2020 New Revision: 359947 URL: https://svnweb.freebsd.org/changeset/base/359947 Log: MFC r359520: x86 tsc: fall back to CPUID if calibration results looks unbelievable. Modified: stable/12/sys/x86/x86/tsc.c Directory Properties: stable/12/ (props changed) Modified: stable/12/sys/x86/x86/tsc.c ============================================================================== --- stable/12/sys/x86/x86/tsc.c Wed Apr 15 00:18:19 2020 (r359946) +++ stable/12/sys/x86/x86/tsc.c Wed Apr 15 00:36:00 2020 (r359947) @@ -142,7 +142,7 @@ tsc_freq_vmware(void) * tsc_freq_intel(), when available. */ static bool -tsc_freq_cpuid(void) +tsc_freq_cpuid(uint64_t *res) { u_int regs[4]; @@ -150,7 +150,7 @@ tsc_freq_cpuid(void) return (false); do_cpuid(0x15, regs); if (regs[0] != 0 && regs[1] != 0 && regs[2] != 0) { - tsc_freq = (uint64_t)regs[2] * regs[1] / regs[0]; + *res = (uint64_t)regs[2] * regs[1] / regs[0]; return (true); } @@ -158,7 +158,7 @@ tsc_freq_cpuid(void) return (false); do_cpuid(0x16, regs); if (regs[0] != 0) { - tsc_freq = (uint64_t)regs[0] * 1000000; + *res = (uint64_t)regs[0] * 1000000; return (true); } @@ -228,7 +228,8 @@ static void probe_tsc_freq(void) { u_int regs[4]; - uint64_t tsc1, tsc2; + uint64_t tmp_freq, tsc1, tsc2; + int no_cpuid_override; uint16_t bootflags; if (cpu_high >= 6) { @@ -303,15 +304,15 @@ probe_tsc_freq(void) */ if (acpi_get_fadt_bootflags(&bootflags) && (bootflags & ACPI_FADT_LEGACY_DEVICES) == 0 && - tsc_freq_cpuid()) { + tsc_freq_cpuid(&tmp_freq)) { printf("Skipping TSC calibration since no legacy " "devices reported by FADT and CPUID works\n"); tsc_skip_calibration = 1; } } if (tsc_skip_calibration) { - if (tsc_freq_cpuid()) - ; + if (tsc_freq_cpuid(&tmp_freq)) + tsc_freq = tmp_freq; else if (cpu_vendor_id == CPU_VENDOR_INTEL) tsc_freq_intel(); } else { @@ -321,6 +322,32 @@ probe_tsc_freq(void) DELAY(1000000); tsc2 = rdtsc(); tsc_freq = tsc2 - tsc1; + + /* + * If the difference between calibrated frequency and + * the frequency reported by CPUID 0x15/0x16 leafs + * differ significantly, this probably means that + * calibration is bogus. It happens on machines + * without 8254 timer and with BIOS not properly + * reporting it in FADT boot flags. + */ + if (tsc_freq_cpuid(&tmp_freq) && qabs(tsc_freq - tmp_freq) > + uqmin(tsc_freq, tmp_freq)) { + no_cpuid_override = 0; + TUNABLE_INT_FETCH("machdep.disable_tsc_cpuid_override", + &no_cpuid_override); + if (!no_cpuid_override) { + if (bootverbose) { + printf( + "TSC clock: calibration freq %ju Hz, CPUID freq %ju Hz%s\n", + (uintmax_t)tsc_freq, + (uintmax_t)tmp_freq, + no_cpuid_override ? "" : + ", doing CPUID override"); + } + tsc_freq = tmp_freq; + } + } } if (bootverbose) printf("TSC clock: %ju Hz\n", (intmax_t)tsc_freq);