tsc.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 calibrates the TSC to real time.
  4. *
  5. * GRUB -- GRand Unified Bootloader
  6. * Copyright (C) 2008 Free Software Foundation, Inc.
  7. *
  8. * GRUB is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * GRUB is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <grub/types.h>
  22. #include <grub/time.h>
  23. #include <grub/misc.h>
  24. #include <grub/i386/tsc.h>
  25. #include <grub/i386/cpuid.h>
  26. /* This defines the value TSC had at the epoch (that is, when we calibrated it). */
  27. static grub_uint64_t tsc_boot_time;
  28. /* Calibrated TSC rate. (In ms per 2^32 ticks) */
  29. /* We assume that the tick is less than 1 ms and hence this value fits
  30. in 32-bit. */
  31. grub_uint32_t grub_tsc_rate;
  32. static grub_uint64_t
  33. grub_tsc_get_time_ms (void)
  34. {
  35. grub_uint64_t a = grub_get_tsc () - tsc_boot_time;
  36. grub_uint64_t ah = a >> 32;
  37. grub_uint64_t al = a & 0xffffffff;
  38. return ((al * grub_tsc_rate) >> 32) + ah * grub_tsc_rate;
  39. }
  40. static int
  41. calibrate_tsc_hardcode (void)
  42. {
  43. grub_tsc_rate = 5368;/* 800 MHz */
  44. return 1;
  45. }
  46. void
  47. grub_tsc_init (void)
  48. {
  49. if (!grub_cpu_is_tsc_supported ())
  50. {
  51. #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_IEEE1275)
  52. grub_install_get_time_ms (grub_rtc_get_time_ms);
  53. #else
  54. grub_fatal ("no TSC found");
  55. #endif
  56. return;
  57. }
  58. tsc_boot_time = grub_get_tsc ();
  59. #if defined (GRUB_MACHINE_XEN) || defined (GRUB_MACHINE_XEN_PVH)
  60. (void) (grub_tsc_calibrate_from_xen () || calibrate_tsc_hardcode());
  61. #elif defined (GRUB_MACHINE_EFI)
  62. (void) (grub_tsc_calibrate_from_pmtimer () || grub_tsc_calibrate_from_pit () || grub_tsc_calibrate_from_efi() || calibrate_tsc_hardcode());
  63. #elif defined (GRUB_MACHINE_COREBOOT)
  64. (void) (grub_tsc_calibrate_from_pmtimer () || grub_tsc_calibrate_from_pit () || calibrate_tsc_hardcode());
  65. #else
  66. (void) (grub_tsc_calibrate_from_pit () || calibrate_tsc_hardcode());
  67. #endif
  68. grub_install_get_time_ms (grub_tsc_get_time_ms);
  69. }