Blob Blame History Raw
From 637ede531e110b92cf5e8fd5be8561c5a5bd3429 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 201/482] 	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/pit.c     | 49 ------------------------------------------
 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 ++
 10 files changed, 70 insertions(+), 93 deletions(-)
 delete mode 100644 grub-core/kern/i386/pit.c

diff --git a/ChangeLog b/ChangeLog
index c604d8d..bc51ae9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2013-03-10  Vladimir Serbinenko  <phcoder@gmail.com>
 
+	Avoid costly 64-bit division in grub_get_time_ms on most platforms.
+
+2013-03-10  Vladimir Serbinenko  <phcoder@gmail.com>
+
 	* grub-core/fs/fshelp.c (grub_fshelp_log2blksize): Remove now unused
 	function.
 
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index 9cb14e2..221466b 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 93ff2a8..3bcf662 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;
@@ -123,8 +122,6 @@ kernel = {
   i386_coreboot_multiboot_qemu = kern/i386/coreboot/init.c;
   i386_coreboot_multiboot_qemu = term/i386/pc/vga_text.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 730e04a..5b68504 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/pit.c b/grub-core/kern/i386/pit.c
deleted file mode 100644
index 092481a..0000000
--- a/grub-core/kern/i386/pit.c
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2008  Free Software Foundation, Inc.
- *
- *  GRUB is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
- *
- *  GRUB is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <grub/types.h>
-#include <grub/i386/io.h>
-#include <grub/i386/pit.h>
-
-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);
-}
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.2.1