tsc.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * x86 TSC related functions
  3. */
  4. #ifndef _ASM_X86_TSC_H
  5. #define _ASM_X86_TSC_H
  6. #include <asm/processor.h>
  7. #define NS_SCALE 10 /* 2^10, carefully chosen */
  8. #define US_SCALE 32 /* 2^32, arbitralrily chosen */
  9. /*
  10. * Standard way to access the cycle counter.
  11. */
  12. typedef unsigned long long cycles_t;
  13. extern unsigned int cpu_khz;
  14. extern unsigned int tsc_khz;
  15. static inline cycles_t get_cycles(void)
  16. {
  17. unsigned long long ret = 0;
  18. #ifndef CONFIG_X86_TSC
  19. if (!cpu_has_tsc)
  20. return 0;
  21. #endif
  22. #if defined(CONFIG_X86_GENERIC) || defined(CONFIG_X86_TSC)
  23. rdtscll(ret);
  24. #endif
  25. return ret;
  26. }
  27. /* Like get_cycles, but make sure the CPU is synchronized. */
  28. static __always_inline cycles_t get_cycles_sync(void)
  29. {
  30. unsigned long long ret;
  31. unsigned eax, edx;
  32. /*
  33. * Use RDTSCP if possible; it is guaranteed to be synchronous
  34. * and doesn't cause a VMEXIT on Hypervisors
  35. */
  36. alternative_io(ASM_NOP3, ".byte 0x0f,0x01,0xf9", X86_FEATURE_RDTSCP,
  37. ASM_OUTPUT2("=a" (eax), "=d" (edx)),
  38. "a" (0U), "d" (0U) : "ecx", "memory");
  39. ret = (((unsigned long long)edx) << 32) | ((unsigned long long)eax);
  40. if (ret)
  41. return ret;
  42. /*
  43. * Don't do an additional sync on CPUs where we know
  44. * RDTSC is already synchronous:
  45. */
  46. alternative_io("cpuid", ASM_NOP2, X86_FEATURE_SYNC_RDTSC,
  47. "=a" (eax), "0" (1) : "ebx","ecx","edx","memory");
  48. rdtscll(ret);
  49. return ret;
  50. }
  51. extern void tsc_init(void);
  52. extern void mark_tsc_unstable(char *reason);
  53. extern int unsynchronized_tsc(void);
  54. extern void init_tsc_clocksource(void);
  55. int check_tsc_unstable(void);
  56. /*
  57. * Boot-time check whether the TSCs are synchronized across
  58. * all CPUs/cores:
  59. */
  60. extern void check_tsc_sync_source(int cpu);
  61. extern void check_tsc_sync_target(void);
  62. #ifdef CONFIG_X86_64
  63. extern void tsc_calibrate(void);
  64. #endif
  65. #endif