From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.71) id 1ZvmPn-0005Cc-3e for mharc-grub-devel@gnu.org; Mon, 09 Nov 2015 08:23:19 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51276) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZvmPj-00058Q-3L for grub-devel@gnu.org; Mon, 09 Nov 2015 08:23:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZvmPh-0001wt-RC for grub-devel@gnu.org; Mon, 09 Nov 2015 08:23:15 -0500 Received: from mail-wm0-x236.google.com ([2a00:1450:400c:c09::236]:37215) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZvmPh-0001wd-G9 for grub-devel@gnu.org; Mon, 09 Nov 2015 08:23:13 -0500 Received: by wmww144 with SMTP id w144so32064968wmw.0 for ; Mon, 09 Nov 2015 05:23:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type; bh=iyfXoSJ3LaG9Dq7JP3BhnlslR2582wYSEGB82Ox2jZs=; b=B5h0dRoq9tjsbGArwxmvhmVpiRYWihSAdjIFG1uTY4fLnIJMRm/Vd8h/q3VsFj6+VQ IVUlcJ8mM9buGGDnU3InjD1Gqa4EzNWfiy01oxM5ZsQ0efF0Wl7+M2OFVSvZkxnpJgb2 LR+/S2if7ZWd5GqBiWtnz2Fsufd9UL8c6n3KSoJZQc4mCyVoE1oyYQBNhXnN0otjW7LL iJuaZmG8U/AKbt6aCSSYQvUwzJ94rxx5jHGn1j+OtrFL7x/hU2uIgeODM1c1W6+Vc2Rl AmhafVWyQlEC8Jt/zjIWv2eOEHMu/OvbCvgxmyFhtto9sjIBZ5cG3L7bi+CeDx9anyTK Vs4Q== X-Received: by 10.28.8.205 with SMTP id 196mr27373856wmi.50.1447075392773; Mon, 09 Nov 2015 05:23:12 -0800 (PST) Received: from ?IPv6:2620:0:105f:fd00:863a:4bff:fe50:abc4? ([2620:0:105f:fd00:863a:4bff:fe50:abc4]) by smtp.gmail.com with ESMTPSA id p10sm15418350wjx.36.2015.11.09.05.23.11 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Nov 2015 05:23:11 -0800 (PST) Subject: Re: [PATCH] Use UEFI Time Service to calibrate TSC To: The development of GNU GRUB References: <20151109070746.GA21701@linux-dsax.tai.apac.novell.com> <20151109080356.GA20546@linux-dsax.tai.apac.novell.com> From: =?UTF-8?Q?Vladimir_'=cf=86-coder/phcoder'_Serbinenko?= X-Enigmail-Draft-Status: N1110 Message-ID: <56409E38.8010804@gmail.com> Date: Mon, 9 Nov 2015 14:23:04 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.3.0 MIME-Version: 1.0 In-Reply-To: <20151109080356.GA20546@linux-dsax.tai.apac.novell.com> Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="rdvNV6ljeM67ostnr1f3WWgDoeLG2dQIv" X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c09::236 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 09 Nov 2015 13:23:16 -0000 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --rdvNV6ljeM67ostnr1f3WWgDoeLG2dQIv Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 09.11.2015 09:03, Michael Chang wrote: > On Mon, Nov 09, 2015 at 10:29:55AM +0300, Andrei Borzenkov wrote: >> On Mon, Nov 9, 2015 at 10:07 AM, Michael Chang wrote= : >>> This patch tries to detect PIT timer is broken and use UEFI Time Serv= ice >>> to calibrate TSC. >> >> Second try :) >> >> https://lists.gnu.org/archive/html/grub-devel/2014-11/msg00079.html >> >> Although I think that this one is acceptable - it is used as fallback >> only. Will it also catch the case when PIT is not present at all? >=20 > I think yes, actually this patch was tested and can fix the timeout > problem on hyper-v Generation 2 VMs with UEFI, which is known to ship > without PIT. >=20 > In addition to that, the linux kernel also calibrates the tsc via PIT a= s > default on x86. >=20 > http://lxr.free-electrons.com/source/arch/x86/kernel/tsc.c#L441 >=20 > But with some sanity checks to detect the SMI storm interfering the > result and will fallback to other timer sources if the sanity check > fails. This patch is inspired by that one of that check with much more > restricted loopmin to "1" that's basically a insane condition.=20 >=20 I like some aspects of this patch, i.a. that it's unlikely to break compatibility. ut I feel that we can do a bit better: 1) Returning through pointed variable is expensive in terms of binary size. Just plain return is better. 2) More modern calibrations can calibrate in 1ms, not 55ms. This is one that slows down coreboot boot (I think only USB init is slower). 3) We could have a cascade of methods. E.g. on EFI: PIT -> PM -> Stall -> hardcoded 1GHz (putting PM as first would be slightly more risk on coreboot: PM -> PIT -> hardcoded 1GHz rest: PIT -> hardcoded 1GHz (need to keep size down) I'm going to prepare proof-of-concept patch > Thanks, > Michael >=20 >> >>> --- >>> grub-core/kern/i386/tsc.c | 33 +++++++++++++++++++++++++++++---- >>> 1 files changed, 29 insertions(+), 4 deletions(-) >>> >>> diff --git a/grub-core/kern/i386/tsc.c b/grub-core/kern/i386/tsc.c >>> index bc441d0..bd24cea 100644 >>> --- a/grub-core/kern/i386/tsc.c >>> +++ b/grub-core/kern/i386/tsc.c >>> @@ -29,6 +29,10 @@ >>> #include >>> #else >>> #include >>> +#ifdef GRUB_MACHINE_EFI >>> +#include >>> +#include >>> +#endif >>> #endif >>> #include >>> >>> @@ -72,7 +76,7 @@ grub_cpu_is_tsc_supported (void) >>> } >>> >>> static void >>> -grub_pit_wait (grub_uint16_t tics) >>> +grub_pit_wait (grub_uint16_t tics, int *is_started) >>> { >>> /* Disable timer2 gate and speaker. */ >>> grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT) >>> @@ -90,8 +94,17 @@ grub_pit_wait (grub_uint16_t tics) >>> | GRUB_PIT_SPK_TMR2, >>> GRUB_PIT_SPEAKER_PORT); >>> >>> - /* Wait. */ >>> - while ((grub_inb (GRUB_PIT_SPEAKER_PORT) & GRUB_PIT_SPK_TMR2_LATCH= ) =3D=3D 0x00); >>> + if ((grub_inb (GRUB_PIT_SPEAKER_PORT) & GRUB_PIT_SPK_TMR2_LATCH)) >>> + { >>> + /* The ticks have expired too fast to know the counting really= started or not */ >>> + *is_started =3D 0; >>> + } >>> + else >>> + { >>> + *is_started =3D 1; >>> + /* Wait. */ >>> + while ((grub_inb (GRUB_PIT_SPEAKER_PORT) & GRUB_PIT_SPK_TMR2_L= ATCH) =3D=3D 0x00); >>> + } >>> >>> /* Disable timer2 gate and speaker. */ >>> grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT) >>> @@ -117,11 +130,23 @@ calibrate_tsc (void) >>> { >>> /* First calibrate the TSC rate (relative, not absolute time). */ >>> grub_uint64_t end_tsc; >>> + int is_started; >>> >>> tsc_boot_time =3D grub_get_tsc (); >>> - grub_pit_wait (0xffff); >>> + grub_pit_wait (0xffff, &is_started); >>> end_tsc =3D grub_get_tsc (); >>> >>> +#ifdef GRUB_MACHINE_EFI >>> + /* The PIT is broken as 55ms is too sufficent to any cpu to catch = it */ >>> + if (!is_started) >>> + { >>> + /* Use EFI Time Service to calibrate TSC */ >>> + tsc_boot_time =3D grub_get_tsc (); >>> + efi_call_1 (grub_efi_system_table->boot_services->stall, 54925= ); >>> + end_tsc =3D grub_get_tsc (); >>> + } >>> +#endif >>> + >>> grub_tsc_rate =3D 0; >>> if (end_tsc > tsc_boot_time) >>> grub_tsc_rate =3D grub_divmod64 ((55ULL << 32), end_tsc - tsc_bo= ot_time, 0); >>> -- >>> 1.7.3.4 >>> >>> >>> _______________________________________________ >>> Grub-devel mailing list >>> Grub-devel@gnu.org >>> https://lists.gnu.org/mailman/listinfo/grub-devel >> >> _______________________________________________ >> Grub-devel mailing list >> Grub-devel@gnu.org >> https://lists.gnu.org/mailman/listinfo/grub-devel >=20 > _______________________________________________ > Grub-devel mailing list > Grub-devel@gnu.org > https://lists.gnu.org/mailman/listinfo/grub-devel >=20 --rdvNV6ljeM67ostnr1f3WWgDoeLG2dQIv Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iF4EAREKAAYFAlZAnjgACgkQmBXlbbo5nOtV2QD/XP2nZn/6ryqZnXdK6bAjwNbW qvzZ7G2K9WW8Tfx/nGwBAJ6leLcJjyqjyE6GHeU80PtbmO/D31TKoHLJ3RlNv5z1 =64UA -----END PGP SIGNATURE----- --rdvNV6ljeM67ostnr1f3WWgDoeLG2dQIv--