From nobody Sat Mar 05 00:49:04 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id EBA0819E03EC; Sat, 5 Mar 2022 00:49:04 +0000 (UTC) (envelope-from git@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) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4K9R1X5KF7z4TQC; Sat, 5 Mar 2022 00:49:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646441344; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=QPjJPyvXyzN7D17tfzyxjCoGUyZd9CASJdw5nk5mcuI=; b=RoVRRYDdkUwrshBfbUjRFkpzuh/tJkSzZAeZLGmJnsTQNePbCfThQmdnHjcpBF7WrSCqiW UczisUZ6qywj+vjuoZy/FQ8lO2AUgDuJiFYc3SYmQjcI0aPePKHrgudyC2o/V/RrwLtxOy eSjIBIsA6I7sZaqVwOk+KvkYSLpt95es3+ZPppueg9wb+N1C8Gkzkd9qTovgMooJXh8QCV mv7uDg+Rk3lk11aywa2GqGD963ce9B8Pj5muIxQuFMDmGwZk8ZrEsiJSWhZmrtpVuwNR+i NKA12COvNmeBVkaeb63ybgOuHEKBZzioPWR7YWerW6zd+sJCgN59GDepZYjhiA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 78321169C5; Sat, 5 Mar 2022 00:49:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2250n4Rl072518; Sat, 5 Mar 2022 00:49:04 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2250n4ka072517; Sat, 5 Mar 2022 00:49:04 GMT (envelope-from git) Date: Sat, 5 Mar 2022 00:49:04 GMT Message-Id: <202203050049.2250n4ka072517@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 075e2779aca7 - main - x86: Defer early TSC timecounter calibration to SI_SUB_CPU List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 075e2779aca7cbd8f201ce0e1bb60318d0cbd8b8 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1646441344; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=QPjJPyvXyzN7D17tfzyxjCoGUyZd9CASJdw5nk5mcuI=; b=r1XaJQfLQGQHZCYAAN1FpeOgOOUNSR4pfC//R8R/ARp76mxynr9A5dCUCykGi7lqEuoLd5 W7xI9JsFxtaYWWOwQEiS8RCOmoQmJhGuKrbnyBzJqBsu1yC1UblTIECl/xEz6mA+zCMQd6 8hcr/4voFpnOLBcatFQrRY/RRtPoScFtZMjbPmpMKstPyHUy7yCyF+1e4hXjymMTfsonaE JWmGTAi0GB8b1HowVFoQiMaVKgpCUYBH4IPMRW8VAuqIleTMmFDylPYSLjUpwEUaTJwiKj aokqJTrKQv1C8G1W2hUUO0Y87bX/VP+z7RKQvmxBryrUGoI9c0pM9zo4lBoaCg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1646441344; a=rsa-sha256; cv=none; b=v7REi9cheSk+JCytxeGIvRgI+B4cj3NCsFb8A/Lf5uFtnCP8QhKXUTW+96EW+sqmzBtMQ7 6hNg39GUPRGFxU24buLsFQN+yotmZulkkLk4V296XPq9hvaG2Bt0+VKAKAhve+4Hk9pJA4 vQn7cMRnnYmU/qQahLYMJFa2kU6P0E+Jw4qrs2Fg6GKvulOGSTeicuwFgcHcYA+zNhbXbl LOYI75D3L+qlBu1YHkW0pAlhqaB2+GT5ltKFEAKKikggvMzU7BvDjpii8MD+xTdOqX6PL5 ZJJpRO1v2B2iRdoHpYIY7I60aeXOOmgcLN2bq2v25oMKzZbybJ5xPChXEM9LAQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=075e2779aca7cbd8f201ce0e1bb60318d0cbd8b8 commit 075e2779aca7cbd8f201ce0e1bb60318d0cbd8b8 Author: Mark Johnston AuthorDate: 2022-03-05 00:34:43 +0000 Commit: Mark Johnston CommitDate: 2022-03-05 00:34:43 +0000 x86: Defer early TSC timecounter calibration to SI_SUB_CPU If we can't determine the TSC frequency using CPU registers, we need to give a chance for Hyper-V drivers to register a timecounter (during SI_SUB_HYPERVISOR) since an emulated 8254 might not be available. Thus, split probe_tsc_freq() into early and late stages, and wait until the latter to attempt calibration using a reference clock. Fixes: 84369dd52369 ("x86: Probe the TSC frequency earlier") Reported and tested by: khng, Shawn Webb Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D34444 --- sys/x86/x86/tsc.c | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/sys/x86/x86/tsc.c b/sys/x86/x86/tsc.c index 82ee358b6895..0fdd70a690a3 100644 --- a/sys/x86/x86/tsc.c +++ b/sys/x86/x86/tsc.c @@ -240,7 +240,7 @@ tsc_freq_intel_brand(uint64_t *res) } static void -tsc_freq_8254(uint64_t *res) +tsc_freq_tc(uint64_t *res) { uint64_t tsc1, tsc2; int64_t overhead; @@ -262,8 +262,15 @@ tsc_freq_8254(uint64_t *res) tsc_freq = (tsc2 - tsc1 - overhead) * 10; } +/* + * Try to determine the TSC frequency using CPUID or hypercalls. If successful, + * this lets use the TSC for early DELAY() calls instead of the 8254 timer, + * which may be unreliable or entirely absent on contemporary systems. However, + * avoid calibrating using the 8254 here so as to give hypervisors a chance to + * register a timecounter that can be used instead. + */ static void -probe_tsc_freq(void) +probe_tsc_freq_early(void) { #ifdef __i386__ /* The TSC is known to be broken on certain CPUs. */ @@ -364,7 +371,20 @@ probe_tsc_freq(void) if (bootverbose) printf("Early TSC frequency %juHz derived from CPUID\n", (uintmax_t)tsc_freq); - } else if (tsc_skip_calibration) { + } +} + +/* + * If we were unable to determine the TSC frequency via CPU registers, try + * to calibrate against a known clock. + */ +static void +probe_tsc_freq_late(void) +{ + if (tsc_freq != 0) + return; + + if (tsc_skip_calibration) { /* * Try to parse the brand string to obtain the nominal TSC * frequency. @@ -380,15 +400,24 @@ probe_tsc_freq(void) } } else { /* - * Calibrate against the 8254 PIT. This estimate will be - * refined later in tsc_calib(). + * Calibrate against a timecounter or the 8254 PIT. This + * estimate will be refined later in tsc_calib(). */ - tsc_freq_8254(&tsc_freq); + tsc_freq_tc(&tsc_freq); if (bootverbose) printf( "Early TSC frequency %juHz calibrated from 8254 PIT\n", (uintmax_t)tsc_freq); } +} + +void +start_TSC(void) +{ + if ((cpu_feature & CPUID_TSC) == 0 || tsc_disabled) + return; + + probe_tsc_freq_late(); if (cpu_power_ecx & CPUID_PERF_STAT) { /* @@ -401,13 +430,6 @@ probe_tsc_freq(void) if (rdmsr(MSR_MPERF) > 0 && rdmsr(MSR_APERF) > 0) tsc_perf_stat = 1; } -} - -void -start_TSC(void) -{ - if ((cpu_feature & CPUID_TSC) == 0 || tsc_disabled) - return; /* * Inform CPU accounting about our boot-time clock rate. This will @@ -718,7 +740,7 @@ tsc_init(void) if ((cpu_feature & CPUID_TSC) == 0 || tsc_disabled) return; - probe_tsc_freq(); + probe_tsc_freq_early(); } /*