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
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue