From nobody Thu Mar 31 16:07:51 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 B860D1A3D7F4; Thu, 31 Mar 2022 16:07:52 +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 4KTp9h1WVSz4Ysx; Thu, 31 Mar 2022 16:07:52 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648742872; 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=Me9/iatEllTLiYuD8/bm8gBJlWGF6KIpX2PMHCRBdEM=; b=mrmkPQFIqoURJuLBbMr0MkVZHkfHKQ1NSxeuv9dR1zJ7eS0TeyTU/hSAVk4bKVnOYFU15b 1zGTblVqRTeO6Q25Ti6Ff9mRr8Ii3FBAYeY858FtDeN/LjjbX8WXVkoBIo3i8Fu4ygUCUY xCXVfXjgnsLV1xitZOqfwunUdFcy/yrr2TKJmAy3P7EIGT9cll3W3TZFxhYLgaKLYMXYyB tSvND5+WYxs2g9b4rSG3+GZTfuWix8G87nkvInFWf7Z/a1WVctqjmFSLqSFlyXmiGG18EV gONF7+gxvxGshrW3hslzuO+VXdAPIMRj3u4v6y7iujwZrsLmj740OfhoxUSPiQ== 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 DE67B2AB27; Thu, 31 Mar 2022 16:07:51 +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 22VG7p4c095175; Thu, 31 Mar 2022 16:07:51 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22VG7pts095174; Thu, 31 Mar 2022 16:07:51 GMT (envelope-from git) Date: Thu, 31 Mar 2022 16:07:51 GMT Message-Id: <202203311607.22VG7pts095174@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: b6c207a8301e - stable/13 - 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/stable/13 X-Git-Reftype: branch X-Git-Commit: b6c207a8301e3da94b6282bdf4a798f1e898fc29 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648742872; 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=Me9/iatEllTLiYuD8/bm8gBJlWGF6KIpX2PMHCRBdEM=; b=AupBj3XtA885WiyM4hPh+FcGHmb3JNcMehcPKAFrtX54q9IMeA1aY/ejDl2pgA4DWA9CL1 NNJikbywLEf60hr1q+5DtqlFCtgqT2gEy9aO/3Cg9qik0WDEoS1nm3kGrlBTvMd6ynj/m2 lVGErrmzqkaroBuwVmr1uNkh57PNBWQsXHD/5o4AK07L2cPbzQiNTY+1YC965Ia6eKAYt/ nXvR32wKcsJAeWXoK7M90tF092DAE1UmSQgNWBjq0ATws+fwt+8eyDYyH+NlvhOuKAPZZ5 72bEM5TpbWGCqmRkMKFaqSkPJ0MEX9oPz/1ijNCr+C8Mb5pq9/QVxcs1rl8xKg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1648742872; a=rsa-sha256; cv=none; b=QEiGjyy9Dg5SnF3QY/a3hnPPjIpQc1os2a1LAwSAE5RBw+zyi8adnYs9XM2NI86jSHJ/vi rCENMvqS21DO6P9QEFmtAv9rvKQdOhFhVlW7MdKbHWu0COrXbuOyKbPwbCJeg8zFiqSX3S 0ewqaGyhImm1oh8MTl8IoO0xjsO8Qwq4Ci5D9TS9G8/loYotd5y0Hp1cqFZZF/JJSF0AlS RJhslQA2U/YKrhYyKwnagqCCbQieaXTB+doMTODtAO915GfuU8XRo591HWJb/XJm4dMg1v /1lhn9It07DvjB0LV5GSPwheCtF+cgrcR4Uqr48oL8fNGVJ2U1cS/5S8M9jv3A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=b6c207a8301e3da94b6282bdf4a798f1e898fc29 commit b6c207a8301e3da94b6282bdf4a798f1e898fc29 Author: Mark Johnston AuthorDate: 2022-03-05 00:34:43 +0000 Commit: Mark Johnston CommitDate: 2022-03-31 16:05:46 +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 (cherry picked from commit 075e2779aca7cbd8f201ce0e1bb60318d0cbd8b8) --- 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 024800077e86..e821246ae293 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 @@ -716,7 +738,7 @@ tsc_init(void) if ((cpu_feature & CPUID_TSC) == 0 || tsc_disabled) return; - probe_tsc_freq(); + probe_tsc_freq_early(); } /*