Received: from list by lists.gnu.org with archive (Exim 4.71) id 1Zvgj0-000414-35 for mharc-grub-devel@gnu.org; Mon, 09 Nov 2015 02:18:46 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43442) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zvgix-00040t-Vm for grub-devel@gnu.org; Mon, 09 Nov 2015 02:18:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zvgit-0001D7-Tn for grub-devel@gnu.org; Mon, 09 Nov 2015 02:18:43 -0500 Received: from victor.provo.novell.com ([137.65.250.26]:35165 helo=prv3-mh.provo.novell.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zvgit-0001Cv-MM for grub-devel@gnu.org; Mon, 09 Nov 2015 02:18:39 -0500 Received: from linux-dsax.tai.apac.novell.com (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (TLS encrypted); Mon, 09 Nov 2015 00:18:29 -0700 Date: Mon, 9 Nov 2015 15:07:46 +0800 From: Michael Chang To: grub-devel@gnu.org Subject: [PATCH] Use UEFI Time Service to calibrate TSC Message-ID: <20151109070746.GA21701@linux-dsax.tai.apac.novell.com> Mail-Followup-To: grub-devel@gnu.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 137.65.250.26 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 07:18:45 -0000 This patch tries to detect PIT timer is broken and use UEFI Time Service to calibrate TSC. --- 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) == 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 = 0; + } + else + { + *is_started = 1; + /* Wait. */ + while ((grub_inb (GRUB_PIT_SPEAKER_PORT) & GRUB_PIT_SPK_TMR2_LATCH) == 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 = grub_get_tsc (); - grub_pit_wait (0xffff); + grub_pit_wait (0xffff, &is_started); end_tsc = 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 = grub_get_tsc (); + efi_call_1 (grub_efi_system_table->boot_services->stall, 54925); + end_tsc = grub_get_tsc (); + } +#endif + grub_tsc_rate = 0; if (end_tsc > tsc_boot_time) grub_tsc_rate = grub_divmod64 ((55ULL << 32), end_tsc - tsc_boot_time, 0); -- 1.7.3.4