Avoid costly 64-bit division in grub_get_time_ms on most platforms.
This commit is contained in:
parent
fc3ff2a2ad
commit
5341c0fbfc
10 changed files with 70 additions and 93 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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, ®s);
|
||||
|
||||
return (regs.ecx << 16) | (regs.edx & 0xffff);
|
||||
return ((regs.ecx << 16) | (regs.edx & 0xffff)) * 55ULL;
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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;
|
||||
|
||||
return ((al * grub_tsc_rate) >> 32) + ah * grub_tsc_rate;
|
||||
}
|
||||
|
||||
|
||||
/* How many RTC ticks to use for calibration loop. (>= 1) */
|
||||
#define CALIBRATION_TICKS 2
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Reference in a new issue