1 /* kern/i386/tsc.c - x86 TSC time source implementation
2 * Requires Pentium or better x86 CPU that supports the RDTSC instruction.
3 * This module uses the RTC (via grub_get_rtc()) to calibrate the TSC to
6 * GRUB -- GRand Unified Bootloader
7 * Copyright (C) 2008 Free Software Foundation, Inc.
9 * GRUB is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * GRUB is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
23 #include <grub/types.h>
24 #include <grub/time.h>
25 #include <grub/misc.h>
26 #include <grub/i386/tsc.h>
27 #include <grub/i386/pit.h>
29 /* This defines the value TSC had at the epoch (that is, when we calibrated it). */
30 static grub_uint64_t tsc_boot_time
;
32 /* Calibrated TSC rate. (In TSC ticks per millisecond.) */
33 static grub_uint64_t tsc_ticks_per_ms
;
37 grub_tsc_get_time_ms (void)
39 return tsc_boot_time
+ grub_divmod64 (grub_get_tsc (), tsc_ticks_per_ms
, 0);
43 /* How many RTC ticks to use for calibration loop. (>= 1) */
44 #define CALIBRATION_TICKS 2
46 /* Calibrate the TSC based on the RTC. */
50 /* First calibrate the TSC rate (relative, not absolute time). */
51 grub_uint64_t start_tsc
;
52 grub_uint64_t end_tsc
;
54 start_tsc
= grub_get_tsc ();
55 grub_pit_wait (0xffff);
56 end_tsc
= grub_get_tsc ();
58 tsc_ticks_per_ms
= grub_divmod64 (end_tsc
- start_tsc
, 55, 0);
64 if (grub_cpu_is_tsc_supported ())
66 tsc_boot_time
= grub_get_tsc ();
68 grub_install_get_time_ms (grub_tsc_get_time_ms
);
72 grub_install_get_time_ms (grub_rtc_get_time_ms
);