a5bd9f6
From d47c03e078f39034d1843dfe8a84aedff40638ba Mon Sep 17 00:00:00 2001
a5bd9f6
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
a5bd9f6
Date: Sun, 10 Mar 2013 17:45:38 +0100
a5bd9f6
Subject: [PATCH 200/364] 	Avoid costly 64-bit division in
a5bd9f6
 grub_get_time_ms on most platforms.
a5bd9f6
a5bd9f6
---
a5bd9f6
 ChangeLog                     |  4 ++++
a5bd9f6
 grub-core/Makefile.am         | 14 ++++++------
a5bd9f6
 grub-core/Makefile.core.def   |  3 ---
a5bd9f6
 grub-core/kern/i386/pc/init.c |  6 +++---
a5bd9f6
 grub-core/kern/i386/pit.c     | 49 ------------------------------------------
a5bd9f6
 grub-core/kern/i386/tsc.c     | 50 +++++++++++++++++++++++++++++++++----------
a5bd9f6
 grub-core/loader/i386/xnu.c   | 28 +++++++++++++-----------
a5bd9f6
 include/grub/i386/pc/time.h   |  5 -----
a5bd9f6
 include/grub/i386/pit.h       |  2 --
a5bd9f6
 include/grub/i386/tsc.h       |  2 ++
a5bd9f6
 10 files changed, 70 insertions(+), 93 deletions(-)
a5bd9f6
 delete mode 100644 grub-core/kern/i386/pit.c
a5bd9f6
a5bd9f6
diff --git a/ChangeLog b/ChangeLog
a5bd9f6
index c604d8d..bc51ae9 100644
a5bd9f6
--- a/ChangeLog
a5bd9f6
+++ b/ChangeLog
a5bd9f6
@@ -1,5 +1,9 @@
a5bd9f6
 2013-03-10  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
 
a5bd9f6
+	Avoid costly 64-bit division in grub_get_time_ms on most platforms.
a5bd9f6
+
a5bd9f6
+2013-03-10  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
+
a5bd9f6
 	* grub-core/fs/fshelp.c (grub_fshelp_log2blksize): Remove now unused
a5bd9f6
 	function.
a5bd9f6
 
a5bd9f6
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
a5bd9f6
index 9cb14e2..221466b 100644
a5bd9f6
--- a/grub-core/Makefile.am
a5bd9f6
+++ b/grub-core/Makefile.am
a5bd9f6
@@ -98,29 +98,29 @@ if COND_i386_pc
a5bd9f6
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
a5bd9f6
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/pxe.h
a5bd9f6
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/int.h
a5bd9f6
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
a5bd9f6
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
a5bd9f6
 endif
a5bd9f6
 
a5bd9f6
 if COND_i386_efi
a5bd9f6
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
a5bd9f6
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
a5bd9f6
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
a5bd9f6
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
a5bd9f6
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/pci.h
a5bd9f6
 endif
a5bd9f6
 
a5bd9f6
 if COND_i386_coreboot
a5bd9f6
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
a5bd9f6
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
a5bd9f6
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/i386/pc/int.h
a5bd9f6
 endif
a5bd9f6
 
a5bd9f6
 if COND_i386_multiboot
a5bd9f6
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
a5bd9f6
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
a5bd9f6
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/i386/pc/int.h
a5bd9f6
 endif
a5bd9f6
 
a5bd9f6
 if COND_i386_qemu
a5bd9f6
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/pci.h
a5bd9f6
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
a5bd9f6
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
a5bd9f6
 endif
a5bd9f6
 
a5bd9f6
 if COND_i386_ieee1275
a5bd9f6
@@ -128,13 +128,13 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
a5bd9f6
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
a5bd9f6
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
a5bd9f6
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
a5bd9f6
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
a5bd9f6
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
a5bd9f6
 endif
a5bd9f6
 
a5bd9f6
 if COND_x86_64_efi
a5bd9f6
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
a5bd9f6
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
a5bd9f6
-KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/pit.h
a5bd9f6
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
a5bd9f6
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/pci.h
a5bd9f6
 endif
a5bd9f6
 
a5bd9f6
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
a5bd9f6
index 93ff2a8..3bcf662 100644
a5bd9f6
--- a/grub-core/Makefile.core.def
a5bd9f6
+++ b/grub-core/Makefile.core.def
a5bd9f6
@@ -102,7 +102,6 @@ kernel = {
a5bd9f6
 
a5bd9f6
   noemu_nodist = symlist.c;
a5bd9f6
 
a5bd9f6
-  i386_pc = kern/generic/rtc_get_time_ms.c;
a5bd9f6
   mips = kern/generic/rtc_get_time_ms.c;
a5bd9f6
 
a5bd9f6
   ieee1275 = disk/ieee1275/ofdisk.c;
a5bd9f6
@@ -123,8 +122,6 @@ kernel = {
a5bd9f6
   i386_coreboot_multiboot_qemu = kern/i386/coreboot/init.c;
a5bd9f6
   i386_coreboot_multiboot_qemu = term/i386/pc/vga_text.c;
a5bd9f6
 
a5bd9f6
-  x86 = kern/i386/pit.c;
a5bd9f6
-
a5bd9f6
   efi = disk/efi/efidisk.c;
a5bd9f6
   efi = kern/efi/efi.c;
a5bd9f6
   efi = kern/efi/init.c;
a5bd9f6
diff --git a/grub-core/kern/i386/pc/init.c b/grub-core/kern/i386/pc/init.c
a5bd9f6
index 730e04a..5b68504 100644
a5bd9f6
--- a/grub-core/kern/i386/pc/init.c
a5bd9f6
+++ b/grub-core/kern/i386/pc/init.c
a5bd9f6
@@ -52,8 +52,8 @@ void (*grub_pc_net_config) (char **device, char **path);
a5bd9f6
  *	return the real time in ticks, of which there are about
a5bd9f6
  *	18-20 per second
a5bd9f6
  */
a5bd9f6
-grub_uint32_t
a5bd9f6
-grub_get_rtc (void)
a5bd9f6
+grub_uint64_t
a5bd9f6
+grub_rtc_get_time_ms (void)
a5bd9f6
 {
a5bd9f6
   struct grub_bios_int_registers regs;
a5bd9f6
 
a5bd9f6
@@ -61,7 +61,7 @@ grub_get_rtc (void)
a5bd9f6
   regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
a5bd9f6
   grub_bios_interrupt (0x1a, ®s;;
a5bd9f6
 
a5bd9f6
-  return (regs.ecx << 16) | (regs.edx & 0xffff);
a5bd9f6
+  return ((regs.ecx << 16) | (regs.edx & 0xffff)) * 55ULL;
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
 void
a5bd9f6
diff --git a/grub-core/kern/i386/pit.c b/grub-core/kern/i386/pit.c
a5bd9f6
deleted file mode 100644
a5bd9f6
index 092481a..0000000
a5bd9f6
--- a/grub-core/kern/i386/pit.c
a5bd9f6
+++ /dev/null
a5bd9f6
@@ -1,49 +0,0 @@
a5bd9f6
-/*
a5bd9f6
- *  GRUB  --  GRand Unified Bootloader
a5bd9f6
- *  Copyright (C) 2008  Free Software Foundation, Inc.
a5bd9f6
- *
a5bd9f6
- *  GRUB is free software: you can redistribute it and/or modify
a5bd9f6
- *  it under the terms of the GNU General Public License as published by
a5bd9f6
- *  the Free Software Foundation, either version 3 of the License, or
a5bd9f6
- *  (at your option) any later version.
a5bd9f6
- *
a5bd9f6
- *  GRUB is distributed in the hope that it will be useful,
a5bd9f6
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
a5bd9f6
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a5bd9f6
- *  GNU General Public License for more details.
a5bd9f6
- *
a5bd9f6
- *  You should have received a copy of the GNU General Public License
a5bd9f6
- *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
a5bd9f6
- */
a5bd9f6
-
a5bd9f6
-#include <grub/types.h>
a5bd9f6
-#include <grub/i386/io.h>
a5bd9f6
-#include <grub/i386/pit.h>
a5bd9f6
-
a5bd9f6
-void
a5bd9f6
-grub_pit_wait (grub_uint16_t tics)
a5bd9f6
-{
a5bd9f6
-  /* Disable timer2 gate and speaker.  */
a5bd9f6
-  grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
a5bd9f6
-	     & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
a5bd9f6
-             GRUB_PIT_SPEAKER_PORT);
a5bd9f6
-
a5bd9f6
-  /* Set tics.  */
a5bd9f6
-  grub_outb (GRUB_PIT_CTRL_SELECT_2 | GRUB_PIT_CTRL_READLOAD_WORD,
a5bd9f6
-	     GRUB_PIT_CTRL);
a5bd9f6
-  grub_outb (tics & 0xff, GRUB_PIT_COUNTER_2);
a5bd9f6
-  grub_outb (tics >> 8, GRUB_PIT_COUNTER_2);
a5bd9f6
-
a5bd9f6
-  /* Enable timer2 gate, keep speaker disabled.  */
a5bd9f6
-  grub_outb ((grub_inb (GRUB_PIT_SPEAKER_PORT) & ~ GRUB_PIT_SPK_DATA)
a5bd9f6
-	     | GRUB_PIT_SPK_TMR2,
a5bd9f6
-             GRUB_PIT_SPEAKER_PORT);
a5bd9f6
-
a5bd9f6
-  /* Wait.  */
a5bd9f6
-  while ((grub_inb (GRUB_PIT_SPEAKER_PORT) & GRUB_PIT_SPK_TMR2_LATCH) == 0x00);
a5bd9f6
-
a5bd9f6
-  /* Disable timer2 gate and speaker.  */
a5bd9f6
-  grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
a5bd9f6
-	     & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
a5bd9f6
-             GRUB_PIT_SPEAKER_PORT);
a5bd9f6
-}
a5bd9f6
diff --git a/grub-core/kern/i386/tsc.c b/grub-core/kern/i386/tsc.c
a5bd9f6
index c4645f0..9efd633 100644
a5bd9f6
--- a/grub-core/kern/i386/tsc.c
a5bd9f6
+++ b/grub-core/kern/i386/tsc.c
a5bd9f6
@@ -25,37 +25,66 @@
a5bd9f6
 #include <grub/misc.h>
a5bd9f6
 #include <grub/i386/tsc.h>
a5bd9f6
 #include <grub/i386/pit.h>
a5bd9f6
+#include <grub/cpu/io.h>
a5bd9f6
 
a5bd9f6
 /* This defines the value TSC had at the epoch (that is, when we calibrated it). */
a5bd9f6
 static grub_uint64_t tsc_boot_time;
a5bd9f6
 
a5bd9f6
-/* Calibrated TSC rate.  (In TSC ticks per millisecond.) */
a5bd9f6
-static grub_uint64_t tsc_ticks_per_ms;
a5bd9f6
+/* Calibrated TSC rate.  (In ms per 2^32 ticks) */
a5bd9f6
+/* We assume that the tick is less than 1 ms and hence this value fits
a5bd9f6
+   in 32-bit.  */
a5bd9f6
+grub_uint32_t grub_tsc_rate;
a5bd9f6
 
a5bd9f6
+static void
a5bd9f6
+grub_pit_wait (grub_uint16_t tics)
a5bd9f6
+{
a5bd9f6
+  /* Disable timer2 gate and speaker.  */
a5bd9f6
+  grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
a5bd9f6
+	     & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
a5bd9f6
+             GRUB_PIT_SPEAKER_PORT);
a5bd9f6
+
a5bd9f6
+  /* Set tics.  */
a5bd9f6
+  grub_outb (GRUB_PIT_CTRL_SELECT_2 | GRUB_PIT_CTRL_READLOAD_WORD,
a5bd9f6
+	     GRUB_PIT_CTRL);
a5bd9f6
+  grub_outb (tics & 0xff, GRUB_PIT_COUNTER_2);
a5bd9f6
+  grub_outb (tics >> 8, GRUB_PIT_COUNTER_2);
a5bd9f6
+
a5bd9f6
+  /* Enable timer2 gate, keep speaker disabled.  */
a5bd9f6
+  grub_outb ((grub_inb (GRUB_PIT_SPEAKER_PORT) & ~ GRUB_PIT_SPK_DATA)
a5bd9f6
+	     | GRUB_PIT_SPK_TMR2,
a5bd9f6
+             GRUB_PIT_SPEAKER_PORT);
a5bd9f6
+
a5bd9f6
+  /* Wait.  */
a5bd9f6
+  while ((grub_inb (GRUB_PIT_SPEAKER_PORT) & GRUB_PIT_SPK_TMR2_LATCH) == 0x00);
a5bd9f6
+
a5bd9f6
+  /* Disable timer2 gate and speaker.  */
a5bd9f6
+  grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
a5bd9f6
+	     & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
a5bd9f6
+             GRUB_PIT_SPEAKER_PORT);
a5bd9f6
+}
a5bd9f6
 
a5bd9f6
 grub_uint64_t
a5bd9f6
 grub_tsc_get_time_ms (void)
a5bd9f6
 {
a5bd9f6
-  return tsc_boot_time + grub_divmod64 (grub_get_tsc (), tsc_ticks_per_ms, 0);
a5bd9f6
-}
a5bd9f6
+  grub_uint64_t a = grub_get_tsc () - tsc_boot_time;
a5bd9f6
+  grub_uint64_t ah = a >> 32;
a5bd9f6
+  grub_uint64_t al = a & 0xffffffff;
a5bd9f6
 
a5bd9f6
-
a5bd9f6
-/* How many RTC ticks to use for calibration loop. (>= 1) */
a5bd9f6
-#define CALIBRATION_TICKS 2
a5bd9f6
+  return ((al * grub_tsc_rate) >> 32) + ah * grub_tsc_rate;
a5bd9f6
+}
a5bd9f6
 
a5bd9f6
 /* Calibrate the TSC based on the RTC.  */
a5bd9f6
 static void
a5bd9f6
 calibrate_tsc (void)
a5bd9f6
 {
a5bd9f6
   /* First calibrate the TSC rate (relative, not absolute time). */
a5bd9f6
-  grub_uint64_t start_tsc;
a5bd9f6
   grub_uint64_t end_tsc;
a5bd9f6
 
a5bd9f6
-  start_tsc = grub_get_tsc ();
a5bd9f6
+  tsc_boot_time = grub_get_tsc ();
a5bd9f6
   grub_pit_wait (0xffff);
a5bd9f6
   end_tsc = grub_get_tsc ();
a5bd9f6
 
a5bd9f6
-  tsc_ticks_per_ms = grub_divmod64 (end_tsc - start_tsc, 55, 0);
a5bd9f6
+  grub_tsc_rate = grub_divmod64 ((55ULL << 32), end_tsc - tsc_boot_time, 0);
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
 void
a5bd9f6
@@ -63,7 +92,6 @@ grub_tsc_init (void)
a5bd9f6
 {
a5bd9f6
   if (grub_cpu_is_tsc_supported ())
a5bd9f6
     {
a5bd9f6
-      tsc_boot_time = grub_get_tsc ();
a5bd9f6
       calibrate_tsc ();
a5bd9f6
       grub_install_get_time_ms (grub_tsc_get_time_ms);
a5bd9f6
     }
a5bd9f6
diff --git a/grub-core/loader/i386/xnu.c b/grub-core/loader/i386/xnu.c
a5bd9f6
index 4e5ce09..497529b 100644
a5bd9f6
--- a/grub-core/loader/i386/xnu.c
a5bd9f6
+++ b/grub-core/loader/i386/xnu.c
a5bd9f6
@@ -125,9 +125,6 @@ guessfsb (void)
a5bd9f6
 {
a5bd9f6
   const grub_uint64_t sane_value = 100000000;
a5bd9f6
   grub_uint32_t manufacturer[3], max_cpuid, capabilities, msrlow;
a5bd9f6
-  grub_uint64_t start_tsc;
a5bd9f6
-  grub_uint64_t end_tsc;
a5bd9f6
-  grub_uint64_t tsc_ticks_per_ms;
a5bd9f6
 
a5bd9f6
   if (! grub_cpu_is_cpuid_supported ())
a5bd9f6
     return sane_value;
a5bd9f6
@@ -192,14 +189,6 @@ guessfsb (void)
a5bd9f6
   if (! (capabilities & (1 << 7)))
a5bd9f6
     return sane_value;
a5bd9f6
 
a5bd9f6
-  /* Calibrate the TSC rate. */
a5bd9f6
-
a5bd9f6
-  start_tsc = grub_get_tsc ();
a5bd9f6
-  grub_pit_wait (0xffff);
a5bd9f6
-  end_tsc = grub_get_tsc ();
a5bd9f6
-
a5bd9f6
-  tsc_ticks_per_ms = grub_divmod64 (end_tsc - start_tsc, 55, 0);
a5bd9f6
-
a5bd9f6
   /* Read the multiplier. */
a5bd9f6
   asm volatile ("movl $0x198, %%ecx\n"
a5bd9f6
 		"rdmsr"
a5bd9f6
@@ -207,8 +196,21 @@ guessfsb (void)
a5bd9f6
 		:
a5bd9f6
 		: "%ecx", "%eax");
a5bd9f6
 
a5bd9f6
-  return grub_divmod64 (2000 * tsc_ticks_per_ms,
a5bd9f6
-			((msrlow >> 7) & 0x3e) + ((msrlow >> 14) & 1), 0);
a5bd9f6
+  grub_uint64_t v;
a5bd9f6
+  grub_uint32_t r;
a5bd9f6
+
a5bd9f6
+  /* (2000ULL << 32) / grub_tsc_rate  */
a5bd9f6
+  /* Assumption: TSC frequency is over 2 MHz.  */
a5bd9f6
+  v = 0xffffffff / grub_tsc_rate;
a5bd9f6
+  v *= 2000;
a5bd9f6
+  /* v is at most 2000 off from (2000ULL << 32) / grub_tsc_rate.
a5bd9f6
+     Since grub_tsc_rate < 2^32/2^11=2^21, so no overflow.
a5bd9f6
+   */
a5bd9f6
+  r = (2000ULL << 32) - v * grub_tsc_rate;
a5bd9f6
+  v += r / grub_tsc_rate;
a5bd9f6
+
a5bd9f6
+  return grub_divmod64 (v, ((msrlow >> 7) & 0x3e) | ((msrlow >> 14) & 1),
a5bd9f6
+			 0);
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
 struct property_descriptor
a5bd9f6
diff --git a/include/grub/i386/pc/time.h b/include/grub/i386/pc/time.h
a5bd9f6
index ba227ca..e93320f 100644
a5bd9f6
--- a/include/grub/i386/pc/time.h
a5bd9f6
+++ b/include/grub/i386/pc/time.h
a5bd9f6
@@ -21,9 +21,4 @@
a5bd9f6
 
a5bd9f6
 #include <grub/symbol.h>
a5bd9f6
 
a5bd9f6
-#define GRUB_TICKS_PER_SECOND	18
a5bd9f6
-
a5bd9f6
-/* Return the real time in ticks.  */
a5bd9f6
-grub_uint32_t grub_get_rtc (void);
a5bd9f6
-
a5bd9f6
 #endif /* ! KERNEL_MACHINE_TIME_HEADER */
a5bd9f6
diff --git a/include/grub/i386/pit.h b/include/grub/i386/pit.h
a5bd9f6
index e1c92cd..4bd49d4 100644
a5bd9f6
--- a/include/grub/i386/pit.h
a5bd9f6
+++ b/include/grub/i386/pit.h
a5bd9f6
@@ -100,6 +100,4 @@ enum
a5bd9f6
     GRUB_PIT_CTRL_COUNT_BCD = 0x01	/* 4-decade BCD counter.  */
a5bd9f6
   };
a5bd9f6
 
a5bd9f6
-void EXPORT_FUNC(grub_pit_wait) (grub_uint16_t tics);
a5bd9f6
-
a5bd9f6
 #endif /* ! KERNEL_CPU_PIT_HEADER */
a5bd9f6
diff --git a/include/grub/i386/tsc.h b/include/grub/i386/tsc.h
a5bd9f6
index 2442d7e..d25d0e3 100644
a5bd9f6
--- a/include/grub/i386/tsc.h
a5bd9f6
+++ b/include/grub/i386/tsc.h
a5bd9f6
@@ -137,5 +137,7 @@ grub_cpu_is_tsc_supported (void)
a5bd9f6
 
a5bd9f6
 void grub_tsc_init (void);
a5bd9f6
 grub_uint64_t grub_tsc_get_time_ms (void);
a5bd9f6
+/* In ms per 2^32 ticks.  */
a5bd9f6
+extern grub_uint32_t EXPORT_VAR(grub_tsc_rate);
a5bd9f6
 
a5bd9f6
 #endif /* ! KERNEL_CPU_TSC_HEADER */
a5bd9f6
-- 
a5bd9f6
1.8.1.4
a5bd9f6