]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
Avoid costly 64-bit division in grub_get_time_ms on most platforms.
authorVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sun, 10 Mar 2013 16:45:38 +0000 (17:45 +0100)
committerVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sun, 10 Mar 2013 16:45:38 +0000 (17:45 +0100)
ChangeLog
grub-core/Makefile.am
grub-core/Makefile.core.def
grub-core/kern/i386/pc/init.c
grub-core/kern/i386/pit.c [deleted file]
grub-core/kern/i386/tsc.c
grub-core/loader/i386/xnu.c
include/grub/i386/pc/time.h
include/grub/i386/pit.h
include/grub/i386/tsc.h

index c604d8d7cdc2ae552b357a2095f18de3da0334ab..bc51ae9c16e83d344e26fca9767829672cbf6659 100644 (file)
--- 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-10  Vladimir Serbinenko  <phcoder@gmail.com>
 
        * grub-core/fs/fshelp.c (grub_fshelp_log2blksize): Remove now unused
index 9cb14e27a6257a4b8685bb6e569e4415699fb19d..221466b3678444af2a5ddedf70d836de1f7ab0b9 100644 (file)
@@ -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
 
index 93ff2a8a3970d661a87f0f42271b8b516d1dee6f..3bcf66232ff0469c3efb7a76f94a6f43017dd71b 100644 (file)
@@ -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;
index 730e04ac461aa15119f8066b46e9f291313bd1e3..5b685040a7861eb548fe47dc6516117b08c45567 100644 (file)
@@ -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 (file)
index 092481a..0000000
+++ /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);
-}
index c4645f04369c4d1f3c9907a659ac6fdd411c22d3..9efd6338cd294b73fa35564485ed13636601e3c5 100644 (file)
 #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);
     }
index 4e5ce097592bed41c2400e63c7a925b072c462f8..497529bc3868cc4a4a66edb1e3fd09ef4703f5f8 100644 (file)
@@ -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
index ba227ca6f2b804d14fe5e84ff5a6895dabac4be5..e93320f7126fd6cb67d0fcf69664d24a11a1c148 100644 (file)
@@ -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 */
index e1c92cd167a0030433603258e394774228087023..4bd49d4cad14a8d3e803a1a484f8a78e04e412ad 100644 (file)
@@ -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 */
index 2442d7e24921fa906203d76480079fc14fc7ee6c..d25d0e3e9d9eaf7f3f1ed3c5ecb4a92e4bdebfb0 100644 (file)
@@ -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 */