From e8adfa81c1a25284cc4125827e5063ad25899e88 Mon Sep 17 00:00:00 2001
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Date: Sun, 10 Mar 2013 17:45:38 +0100
Subject: [PATCH 04/14] Avoid costly 64-bit division in grub_get_time_ms on
 most platforms.

---
 ChangeLog                     |  4 ++++
 grub-core/Makefile.am         | 14 ++++++------
 grub-core/Makefile.core.def   |  3 ---
 grub-core/kern/i386/pc/init.c |  6 +++---
 grub-core/kern/i386/tsc.c     | 50 +++++++++++++++++++++++++++++++++----------
 grub-core/loader/i386/xnu.c   | 28 +++++++++++++-----------
 include/grub/i386/pc/time.h   |  5 -----
 include/grub/i386/pit.h       |  2 --
 include/grub/i386/tsc.h       |  2 ++
 9 files changed, 70 insertions(+), 44 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d1e6f77..04b57ca 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2013-03-10  Vladimir Serbinenko  <phcoder@gmail.com>
+
+	Avoid costly 64-bit division in grub_get_time_ms on most platforms.
+
 2013-03-07  Vladimir Serbinenko  <phcoder@gmail.com>
 
 	Lift up core size limits on some platforms. Fix potential memory
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index 7dc2519..664c9ec 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -98,29 +98,29 @@ if COND_i386_pc
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/pxe.h
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/int.h
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
 if COND_i386_efi
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/pci.h
 endif
 
 if COND_i386_coreboot
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/i386/pc/int.h
 endif
 
 if COND_i386_multiboot
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/i386/pc/int.h
 endif
 
 if COND_i386_qemu
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/pci.h
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
 if COND_i386_ieee1275
@@ -128,13 +128,13 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
 if COND_x86_64_efi
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/pci.h
 endif
 
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 7482dd6..3e4af5d 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -102,7 +102,6 @@ kernel = {
 
   noemu_nodist = symlist.c;
 
-  i386_pc = kern/generic/rtc_get_time_ms.c;
   mips = kern/generic/rtc_get_time_ms.c;
 
   ieee1275 = disk/ieee1275/ofdisk.c;
@@ -126,8 +125,6 @@ kernel = {
   i386_coreboot_multiboot_qemu = term/i386/vga_common.c;
   i386_pc = term/i386/vga_common.c;
 
-  x86 = kern/i386/pit.c;
-
   efi = disk/efi/efidisk.c;
   efi = kern/efi/efi.c;
   efi = kern/efi/init.c;
diff --git a/grub-core/kern/i386/pc/init.c b/grub-core/kern/i386/pc/init.c
index d34cb43..7befb6b 100644
--- a/grub-core/kern/i386/pc/init.c
+++ b/grub-core/kern/i386/pc/init.c
@@ -52,8 +52,8 @@ void (*grub_pc_net_config) (char **device, char **path);
  *	return the real time in ticks, of which there are about
  *	18-20 per second
  */
-grub_uint32_t
-grub_get_rtc (void)
+grub_uint64_t
+grub_rtc_get_time_ms (void)
 {
   struct grub_bios_int_registers regs;
 
@@ -61,7 +61,7 @@ grub_get_rtc (void)
   regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
   grub_bios_interrupt (0x1a, &regs);
 
-  return (regs.ecx << 16) | (regs.edx & 0xffff);
+  return ((regs.ecx << 16) | (regs.edx & 0xffff)) * 55ULL;
 }
 
 void
diff --git a/grub-core/kern/i386/tsc.c b/grub-core/kern/i386/tsc.c
index c4645f0..9efd633 100644
--- a/grub-core/kern/i386/tsc.c
+++ b/grub-core/kern/i386/tsc.c
@@ -25,37 +25,66 @@
 #include <grub/misc.h>
 #include <grub/i386/tsc.h>
 #include <grub/i386/pit.h>
+#include <grub/cpu/io.h>
 
 /* This defines the value TSC had at the epoch (that is, when we calibrated it). */
 static grub_uint64_t tsc_boot_time;
 
-/* Calibrated TSC rate.  (In TSC ticks per millisecond.) */
-static grub_uint64_t tsc_ticks_per_ms;
+/* Calibrated TSC rate.  (In ms per 2^32 ticks) */
+/* We assume that the tick is less than 1 ms and hence this value fits
+   in 32-bit.  */
+grub_uint32_t grub_tsc_rate;
 
+static void
+grub_pit_wait (grub_uint16_t tics)
+{
+  /* Disable timer2 gate and speaker.  */
+  grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
+	     & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
+             GRUB_PIT_SPEAKER_PORT);
+
+  /* Set tics.  */
+  grub_outb (GRUB_PIT_CTRL_SELECT_2 | GRUB_PIT_CTRL_READLOAD_WORD,
+	     GRUB_PIT_CTRL);
+  grub_outb (tics & 0xff, GRUB_PIT_COUNTER_2);
+  grub_outb (tics >> 8, GRUB_PIT_COUNTER_2);
+
+  /* Enable timer2 gate, keep speaker disabled.  */
+  grub_outb ((grub_inb (GRUB_PIT_SPEAKER_PORT) & ~ GRUB_PIT_SPK_DATA)
+	     | GRUB_PIT_SPK_TMR2,
+             GRUB_PIT_SPEAKER_PORT);
+
+  /* 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)
+	     & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
+             GRUB_PIT_SPEAKER_PORT);
+}
 
 grub_uint64_t
 grub_tsc_get_time_ms (void)
 {
-  return tsc_boot_time + grub_divmod64 (grub_get_tsc (), tsc_ticks_per_ms, 0);
-}
+  grub_uint64_t a = grub_get_tsc () - tsc_boot_time;
+  grub_uint64_t ah = a >> 32;
+  grub_uint64_t al = a & 0xffffffff;
 
-
-/* How many RTC ticks to use for calibration loop. (>= 1) */
-#define CALIBRATION_TICKS 2
+  return ((al * grub_tsc_rate) >> 32) + ah * grub_tsc_rate;
+}
 
 /* Calibrate the TSC based on the RTC.  */
 static void
 calibrate_tsc (void)
 {
   /* First calibrate the TSC rate (relative, not absolute time). */
-  grub_uint64_t start_tsc;
   grub_uint64_t end_tsc;
 
-  start_tsc = grub_get_tsc ();
+  tsc_boot_time = grub_get_tsc ();
   grub_pit_wait (0xffff);
   end_tsc = grub_get_tsc ();
 
-  tsc_ticks_per_ms = grub_divmod64 (end_tsc - start_tsc, 55, 0);
+  grub_tsc_rate = grub_divmod64 ((55ULL << 32), end_tsc - tsc_boot_time, 0);
 }
 
 void
@@ -63,7 +92,6 @@ grub_tsc_init (void)
 {
   if (grub_cpu_is_tsc_supported ())
     {
-      tsc_boot_time = grub_get_tsc ();
       calibrate_tsc ();
       grub_install_get_time_ms (grub_tsc_get_time_ms);
     }
diff --git a/grub-core/loader/i386/xnu.c b/grub-core/loader/i386/xnu.c
index 4e5ce09..497529b 100644
--- a/grub-core/loader/i386/xnu.c
+++ b/grub-core/loader/i386/xnu.c
@@ -125,9 +125,6 @@ guessfsb (void)
 {
   const grub_uint64_t sane_value = 100000000;
   grub_uint32_t manufacturer[3], max_cpuid, capabilities, msrlow;
-  grub_uint64_t start_tsc;
-  grub_uint64_t end_tsc;
-  grub_uint64_t tsc_ticks_per_ms;
 
   if (! grub_cpu_is_cpuid_supported ())
     return sane_value;
@@ -192,14 +189,6 @@ guessfsb (void)
   if (! (capabilities & (1 << 7)))
     return sane_value;
 
-  /* Calibrate the TSC rate. */
-
-  start_tsc = grub_get_tsc ();
-  grub_pit_wait (0xffff);
-  end_tsc = grub_get_tsc ();
-
-  tsc_ticks_per_ms = grub_divmod64 (end_tsc - start_tsc, 55, 0);
-
   /* Read the multiplier. */
   asm volatile ("movl $0x198, %%ecx\n"
 		"rdmsr"
@@ -207,8 +196,21 @@ guessfsb (void)
 		:
 		: "%ecx", "%eax");
 
-  return grub_divmod64 (2000 * tsc_ticks_per_ms,
-			((msrlow >> 7) & 0x3e) + ((msrlow >> 14) & 1), 0);
+  grub_uint64_t v;
+  grub_uint32_t r;
+
+  /* (2000ULL << 32) / grub_tsc_rate  */
+  /* Assumption: TSC frequency is over 2 MHz.  */
+  v = 0xffffffff / grub_tsc_rate;
+  v *= 2000;
+  /* v is at most 2000 off from (2000ULL << 32) / grub_tsc_rate.
+     Since grub_tsc_rate < 2^32/2^11=2^21, so no overflow.
+   */
+  r = (2000ULL << 32) - v * grub_tsc_rate;
+  v += r / grub_tsc_rate;
+
+  return grub_divmod64 (v, ((msrlow >> 7) & 0x3e) | ((msrlow >> 14) & 1),
+			 0);
 }
 
 struct property_descriptor
diff --git a/include/grub/i386/pc/time.h b/include/grub/i386/pc/time.h
index ba227ca..e93320f 100644
--- a/include/grub/i386/pc/time.h
+++ b/include/grub/i386/pc/time.h
@@ -21,9 +21,4 @@
 
 #include <grub/symbol.h>
 
-#define GRUB_TICKS_PER_SECOND	18
-
-/* Return the real time in ticks.  */
-grub_uint32_t grub_get_rtc (void);
-
 #endif /* ! KERNEL_MACHINE_TIME_HEADER */
diff --git a/include/grub/i386/pit.h b/include/grub/i386/pit.h
index e1c92cd..4bd49d4 100644
--- a/include/grub/i386/pit.h
+++ b/include/grub/i386/pit.h
@@ -100,6 +100,4 @@ enum
     GRUB_PIT_CTRL_COUNT_BCD = 0x01	/* 4-decade BCD counter.  */
   };
 
-void EXPORT_FUNC(grub_pit_wait) (grub_uint16_t tics);
-
 #endif /* ! KERNEL_CPU_PIT_HEADER */
diff --git a/include/grub/i386/tsc.h b/include/grub/i386/tsc.h
index 2442d7e..d25d0e3 100644
--- a/include/grub/i386/tsc.h
+++ b/include/grub/i386/tsc.h
@@ -137,5 +137,7 @@ grub_cpu_is_tsc_supported (void)
 
 void grub_tsc_init (void);
 grub_uint64_t grub_tsc_get_time_ms (void);
+/* In ms per 2^32 ticks.  */
+extern grub_uint32_t EXPORT_VAR(grub_tsc_rate);
 
 #endif /* ! KERNEL_CPU_TSC_HEADER */
-- 
1.8.1.4

