2N/A/* kern/i386/tsc.c - x86 TSC time source implementation
2N/A * Requires Pentium or better x86 CPU that supports the RDTSC instruction.
2N/A * This module uses the RTC (via grub_get_rtc()) to calibrate the TSC to
2N/A * real time.
2N/A *
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2008 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/types.h>
2N/A#include <grub/time.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/i386/tsc.h>
2N/A#include <grub/i386/pit.h>
2N/A
2N/A/* This defines the value TSC had at the epoch (that is, when we calibrated it). */
2N/Astatic grub_uint64_t tsc_boot_time;
2N/A
2N/A/* Calibrated TSC rate. (In TSC ticks per millisecond.) */
2N/Astatic grub_uint64_t tsc_ticks_per_ms;
2N/A
2N/A
2N/Agrub_uint64_t
2N/Agrub_tsc_get_time_ms (void)
2N/A{
2N/A return tsc_boot_time + grub_divmod64 (grub_get_tsc (), tsc_ticks_per_ms, 0);
2N/A}
2N/A
2N/A
2N/A/* How many RTC ticks to use for calibration loop. (>= 1) */
2N/A#define CALIBRATION_TICKS 2
2N/A
2N/A/* Calibrate the TSC based on the RTC. */
2N/Astatic void
2N/Acalibrate_tsc (void)
2N/A{
2N/A /* First calibrate the TSC rate (relative, not absolute time). */
2N/A grub_uint64_t start_tsc;
2N/A grub_uint64_t end_tsc;
2N/A
2N/A start_tsc = grub_get_tsc ();
2N/A grub_pit_wait (0xffff);
2N/A end_tsc = grub_get_tsc ();
2N/A
2N/A tsc_ticks_per_ms = grub_divmod64 (end_tsc - start_tsc, 55, 0);
2N/A}
2N/A
2N/Avoid
2N/Agrub_tsc_init (void)
2N/A{
2N/A if (grub_cpu_is_tsc_supported ())
2N/A {
2N/A tsc_boot_time = grub_get_tsc ();
2N/A calibrate_tsc ();
2N/A grub_install_get_time_ms (grub_tsc_get_time_ms);
2N/A }
2N/A else
2N/A {
2N/A grub_install_get_time_ms (grub_rtc_get_time_ms);
2N/A }
2N/A}