2 * x86 TSC related functions
7 #include <asm/processor.h>
9 #define NS_SCALE 10 /* 2^10, carefully chosen */
10 #define US_SCALE 32 /* 2^32, arbitralrily chosen */
13 * Standard way to access the cycle counter.
15 typedef unsigned long long cycles_t;
17 extern unsigned int cpu_khz;
18 extern unsigned int tsc_khz;
19 /* flag for disabling the tsc */
20 extern int tsc_disable;
22 extern void disable_TSC(void);
24 static inline cycles_t get_cycles(void)
26 unsigned long long ret = 0;
28 #ifndef CONFIG_X86_TSC
33 #if defined(CONFIG_X86_GENERIC) || defined(CONFIG_X86_TSC)
39 /* Like get_cycles, but make sure the CPU is synchronized. */
40 static __always_inline cycles_t __get_cycles_sync(void)
42 unsigned long long ret;
46 * Use RDTSCP if possible; it is guaranteed to be synchronous
47 * and doesn't cause a VMEXIT on Hypervisors
49 alternative_io(ASM_NOP3, ".byte 0x0f,0x01,0xf9", X86_FEATURE_RDTSCP,
50 ASM_OUTPUT2("=a" (eax), "=d" (edx)),
51 "a" (0U), "d" (0U) : "ecx", "memory");
52 ret = (((unsigned long long)edx) << 32) | ((unsigned long long)eax);
57 * Don't do an additional sync on CPUs where we know
58 * RDTSC is already synchronous:
60 alternative_io("cpuid", ASM_NOP2, X86_FEATURE_SYNC_RDTSC,
61 "=a" (eax), "0" (1) : "ebx","ecx","edx","memory");
66 static __always_inline cycles_t get_cycles_sync(void)
68 unsigned long long ret;
69 ret = __get_cycles_sync();
75 #ifdef CONFIG_PARAVIRT
77 * For paravirt guests, some functionalities are executed through function
78 * pointers in the various pvops structures.
79 * These function pointers exist inside the kernel and can not
80 * be accessed by user space. To avoid this, we make a copy of the
81 * get_cycles_sync (called in kernel) but force the use of native_read_tsc.
82 * Ideally, the guest should set up it's own clock and vread
84 static __always_inline long long vget_cycles_sync(void)
86 unsigned long long ret;
87 ret = __get_cycles_sync();
89 ret = native_read_tsc();
93 # define vget_cycles_sync() get_cycles_sync()
96 extern void tsc_init(void);
97 extern void mark_tsc_unstable(char *reason);
98 extern int unsynchronized_tsc(void);
99 extern void init_tsc_clocksource(void);
100 int check_tsc_unstable(void);
103 * Boot-time check whether the TSCs are synchronized across
106 extern void check_tsc_sync_source(int cpu);
107 extern void check_tsc_sync_target(void);
109 extern void tsc_calibrate(void);
110 extern int notsc_setup(char *);