Blame SOURCES/0201-Avoid-costly-64-bit-division-in-grub_get_time_ms-on-.patch

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