tsc.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef KERNEL_CPU_TSC_HEADER
  19. #define KERNEL_CPU_TSC_HEADER 1
  20. #include <grub/types.h>
  21. #include <grub/i386/cpuid.h>
  22. void grub_tsc_init (void);
  23. /* In ms per 2^32 ticks. */
  24. extern grub_uint32_t EXPORT_VAR(grub_tsc_rate);
  25. int
  26. grub_tsc_calibrate_from_xen (void);
  27. int
  28. grub_tsc_calibrate_from_efi (void);
  29. int
  30. grub_tsc_calibrate_from_pmtimer (void);
  31. int
  32. grub_tsc_calibrate_from_pit (void);
  33. /* Read the TSC value, which increments with each CPU clock cycle. */
  34. static __inline grub_uint64_t
  35. grub_get_tsc (void)
  36. {
  37. grub_uint32_t lo, hi;
  38. grub_uint32_t a,b,c,d;
  39. /* The CPUID instruction is a 'serializing' instruction, and
  40. avoids out-of-order execution of the RDTSC instruction. */
  41. grub_cpuid (0,a,b,c,d);
  42. /* Read TSC value. We cannot use "=A", since this would use
  43. %rax on x86_64. */
  44. asm volatile ("rdtsc":"=a" (lo), "=d" (hi));
  45. /*
  46. * Run cpuid after rdtsc instruction to ensure rdtsc is executed before
  47. * subsequent instruction.
  48. */
  49. grub_cpuid (0, a, b, c, d);
  50. return (((grub_uint64_t) hi) << 32) | lo;
  51. }
  52. static __inline int
  53. grub_cpu_is_tsc_supported (void)
  54. {
  55. #if !defined(GRUB_MACHINE_XEN) && !defined(GRUB_MACHINE_XEN_PVH)
  56. grub_uint32_t a,b,c,d;
  57. if (! grub_cpu_is_cpuid_supported ())
  58. return 0;
  59. grub_cpuid(1,a,b,c,d);
  60. return (d & (1 << 4)) != 0;
  61. #else
  62. return 1;
  63. #endif
  64. }
  65. #endif /* ! KERNEL_CPU_TSC_HEADER */