bc092b
From 703ac12cc7b710cf5fd8ed08fdaaa760ffdff501 Mon Sep 17 00:00:00 2001
bc092b
From: Laszlo Ersek <lersek@redhat.com>
bc092b
Date: Fri, 10 Oct 2014 11:11:09 +0200
bc092b
Subject: [PATCH 116/176] calibrate_tsc(): use the Stall() EFI boot service on
bc092b
 GRUB_MACHINE_EFI
bc092b
bc092b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1150698
bc092b
bc092b
HyperV Gen2 virtual machines have no PIT; guest code should rely on UEFI
bc092b
services instead.
bc092b
bc092b
Signed-off-by: RHEL Ninjas <example@example.com>
bc092b
---
bc092b
 grub-core/kern/i386/tsc.c | 59 +++++++++++++++++++++++++++++++++++++++++++++--
bc092b
 1 file changed, 57 insertions(+), 2 deletions(-)
bc092b
bc092b
diff --git a/grub-core/kern/i386/tsc.c b/grub-core/kern/i386/tsc.c
bc092b
index 2e85289d8..0787b6f3e 100644
bc092b
--- a/grub-core/kern/i386/tsc.c
bc092b
+++ b/grub-core/kern/i386/tsc.c
bc092b
@@ -24,6 +24,17 @@
bc092b
 #include <grub/misc.h>
bc092b
 #include <grub/i386/tsc.h>
bc092b
 #include <grub/i386/cpuid.h>
bc092b
+#ifdef GRUB_MACHINE_XEN
bc092b
+# include <grub/xen.h>
bc092b
+#else
bc092b
+# ifdef GRUB_MACHINE_EFI
bc092b
+#  include <grub/efi/efi.h>
bc092b
+#  include <grub/efi/api.h>
bc092b
+# else
bc092b
+#  include <grub/i386/pit.h>
bc092b
+# endif
bc092b
+#endif
bc092b
+#include <grub/cpu/io.h>
bc092b
 
bc092b
 /* This defines the value TSC had at the epoch (that is, when we calibrated it). */
bc092b
 static grub_uint64_t tsc_boot_time;
bc092b
@@ -33,6 +44,44 @@ static grub_uint64_t tsc_boot_time;
bc092b
    in 32-bit.  */
bc092b
 grub_uint32_t grub_tsc_rate;
bc092b
 
bc092b
+#ifndef GRUB_MACHINE_XEN
bc092b
+
bc092b
+static void
bc092b
+grub_stall (grub_uint16_t tics)
bc092b
+{
bc092b
+# ifdef GRUB_MACHINE_EFI
bc092b
+  grub_uint64_t microseconds;
bc092b
+
bc092b
+  microseconds = (grub_uint64_t)tics * 1000 * 1000 * 3 / 3579545;
bc092b
+  efi_call_1 (grub_efi_system_table->boot_services->stall, microseconds);
bc092b
+# else
bc092b
+  /* Disable timer2 gate and speaker.  */
bc092b
+  grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
bc092b
+	     & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
bc092b
+             GRUB_PIT_SPEAKER_PORT);
bc092b
+
bc092b
+  /* Set tics.  */
bc092b
+  grub_outb (GRUB_PIT_CTRL_SELECT_2 | GRUB_PIT_CTRL_READLOAD_WORD,
bc092b
+	     GRUB_PIT_CTRL);
bc092b
+  grub_outb (tics & 0xff, GRUB_PIT_COUNTER_2);
bc092b
+  grub_outb (tics >> 8, GRUB_PIT_COUNTER_2);
bc092b
+
bc092b
+  /* Enable timer2 gate, keep speaker disabled.  */
bc092b
+  grub_outb ((grub_inb (GRUB_PIT_SPEAKER_PORT) & ~ GRUB_PIT_SPK_DATA)
bc092b
+	     | GRUB_PIT_SPK_TMR2,
bc092b
+             GRUB_PIT_SPEAKER_PORT);
bc092b
+
bc092b
+  /* Wait.  */
bc092b
+  while ((grub_inb (GRUB_PIT_SPEAKER_PORT) & GRUB_PIT_SPK_TMR2_LATCH) == 0x00);
bc092b
+
bc092b
+  /* Disable timer2 gate and speaker.  */
bc092b
+  grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
bc092b
+	     & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
bc092b
+             GRUB_PIT_SPEAKER_PORT);
bc092b
+# endif
bc092b
+}
bc092b
+#endif
bc092b
+
bc092b
 static grub_uint64_t
bc092b
 grub_tsc_get_time_ms (void)
bc092b
 {
bc092b
@@ -46,8 +95,14 @@ grub_tsc_get_time_ms (void)
bc092b
 static int
bc092b
 calibrate_tsc_hardcode (void)
bc092b
 {
bc092b
-  grub_tsc_rate = 5368;/* 800 MHz */
bc092b
-  return 1;
bc092b
+  /* First calibrate the TSC rate (relative, not absolute time). */
bc092b
+  grub_uint64_t end_tsc;
bc092b
+
bc092b
+  tsc_boot_time = grub_get_tsc ();
bc092b
+  grub_stall (0xffff);
bc092b
+  end_tsc = grub_get_tsc ();
bc092b
+
bc092b
+  grub_tsc_rate = grub_divmod64 ((55ULL << 32), end_tsc - tsc_boot_time, 0);
bc092b
 }
bc092b
 
bc092b
 void
bc092b
-- 
bc092b
2.13.0
bc092b