From nobody Fri Feb 11 06:52:09 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 B9A2C19C80C0; Fri, 11 Feb 2022 06:52:10 +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 4Jw46f3KgYz4sH1; Fri, 11 Feb 2022 06:52:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644562330; 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=Y7xDoaFRAjr2912X+wwCFxQ9Z3vo0OogpMYGArsi9Vs=; b=hlt24yRC38ImQ2te8eCMms0bz7zpZyRUhVm8k3z/7bf42qsOetWMOQWxyDGB1ydjrMq+kl 0Vxf8nHRusBsx+MmkXTkgoocTz3j0mFRpgPH9ZFAeJR9lhn722bg3aqf33gQEQavsEFmdf 3hBBoSFpp0Mz3TJ6iDtGXD8jUw/6usbT0hum25tZAbLi2dO+YnIcqgnC9wQ6wZ4gyuH/68 IeZL+FyudDiQiI59RRBxfPitFReiPgY5V+nzhOSPoTsRHakD7Z4C03LQXJlSSCaNXVzUlc a/tYIjPLj/nZoY+WZrCrU3FhqApXsgAg9xFQagztkp0N2bj35UhEqttFap59SA== 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 DF1E37A09; Fri, 11 Feb 2022 06:52:09 +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 21B6q9FQ031231; Fri, 11 Feb 2022 06:52:09 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 21B6q9K2031230; Fri, 11 Feb 2022 06:52:09 GMT (envelope-from git) Date: Fri, 11 Feb 2022 06:52:09 GMT Message-Id: <202202110652.21B6q9K2031230@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Colin Percival Subject: git: a605a66017ec - stable/13 - Use CPUID leaf 0x40000010 for local APIC freq 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: cperciva X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: a605a66017ec53e42c06b35a1786edf1b33cd8ab Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1644562330; 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=Y7xDoaFRAjr2912X+wwCFxQ9Z3vo0OogpMYGArsi9Vs=; b=q+qBNxiIlxrxPCkU63oWf0asR3wNhZlZ/HzBJZ/NJxN7LAo6yOvaMOmjVdcUuGisZwTtjj IdQcKRtotzMjej/enAfaHPPQVHJMKq48OFe6wMIEsS8HL67CMXq0GtUujXxvBDgHQratRA xnIIuvQJjfn5o2lHyqCl85PpWsqO6fpcfJuCxuFrYX1JEAmEKP7qXLq4qTp8+YVLV4huXm 2/TJaASmXdLOw39VssgKmk8l1oRh/IRzt0NaK3MLyunECey+EbMi1RYrzelsKdm9ETM/CY t2oA43W/mX7VLwKnB1bHb7+69P4yQztaQLon0uI8tXn3tljV+DaCnaq77fwumQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1644562330; a=rsa-sha256; cv=none; b=Y0q6c6r++V09oVna/gtbVJXFNdJEgTBMr6vuADgADsboqBAUmixU7yv/W2V0oO+neRfkvi hwV1nPZGFBz24+zxAmoPuOJJsWCl6HgvCKzKFZCjrhNmZTwwfHqvBWbFf83WkQBg/m5k8G N7rvIJ/BgTTjAnjZe6a4MnRjABRmvtYsIbh/HYIYPsfML2MalN5c+b6pWcu1j6jE2zmMxU jhaOZBtsDCnFjLTSF/XHssth8ov/cssQZPuZq2ajTyFhPIYcEnlXm61EG4aiXJWPJzgftK upXBra68Jvwdk+jOYRrUzo3Nh//U1S0znLSpirVznHiad0YH/Q+4kVa9ou5fyA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by cperciva: URL: https://cgit.FreeBSD.org/src/commit/?id=a605a66017ec53e42c06b35a1786edf1b33cd8ab commit a605a66017ec53e42c06b35a1786edf1b33cd8ab Author: Colin Percival AuthorDate: 2022-01-09 00:59:31 +0000 Commit: Colin Percival CommitDate: 2022-02-11 06:52:00 +0000 Use CPUID leaf 0x40000010 for local APIC freq Some VM systems announce the frequency of the local APIC via the CPUID leaf 0x40000010. Using this allows us to boot slightly faster by avoiding the need for timer calibration. Reviewed by: markj Sponsored by: https://www.patreon.com/cperciva (cherry picked from commit de1292c6ff8a445fd453effba8cc23c38cea223f) --- sys/x86/x86/local_apic.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/sys/x86/x86/local_apic.c b/sys/x86/x86/local_apic.c index c5f72a7ff43c..59b5cb600725 100644 --- a/sys/x86/x86/local_apic.c +++ b/sys/x86/x86/local_apic.c @@ -991,6 +991,35 @@ native_lapic_disable_pmc(void) #endif } +static int +lapic_calibrate_initcount_cpuid_vm(void) +{ + u_int regs[4]; + uint64_t freq; + + /* Get value from CPUID leaf if possible. */ + if (vm_guest == VM_GUEST_NO) + return (false); + if (hv_high < 0x40000010) + return (false); + do_cpuid(0x40000010, regs); + freq = (uint64_t)(regs[1]) * 1000; + + /* Pick timer divisor. */ + lapic_timer_divisor = 2; + do { + if (freq / lapic_timer_divisor < APIC_TIMER_MAX_COUNT) + break; + lapic_timer_divisor <<= 1; + } while (lapic_timer_divisor <= 128); + if (lapic_timer_divisor > 128) + return (false); + + /* Record divided frequency. */ + count_freq = freq / lapic_timer_divisor; + return (true); +} + static uint64_t cb_lapic_getcount(void) { @@ -1003,6 +1032,9 @@ lapic_calibrate_initcount(struct lapic *la) { uint64_t freq; + if (lapic_calibrate_initcount_cpuid_vm()) + goto done; + /* Calibrate the APIC timer frequency. */ lapic_timer_set_divisor(2); lapic_timer_oneshot_nointr(la, APIC_TIMER_MAX_COUNT); @@ -1020,6 +1052,7 @@ lapic_calibrate_initcount(struct lapic *la) if (lapic_timer_divisor > 128) panic("lapic: Divisor too big"); count_freq = freq * 2 / lapic_timer_divisor; +done: if (bootverbose) { printf("lapic: Divisor %lu, Frequency %lu Hz\n", lapic_timer_divisor, count_freq);