blob: 401303724130b047b861ae2b7f3b1eff3e17108b [file] [log] [blame]
Andres Salomon2272b0e2007-03-06 01:42:05 -08001/*
Thomas Gleixner2f0798a2007-10-12 23:04:23 +02002 * x86 TSC related functions
Andres Salomon2272b0e2007-03-06 01:42:05 -08003 */
Thomas Gleixner2f0798a2007-10-12 23:04:23 +02004#ifndef _ASM_X86_TSC_H
5#define _ASM_X86_TSC_H
Andres Salomon2272b0e2007-03-06 01:42:05 -08006
7#include <asm/processor.h>
8
Thomas Gleixner2f0798a2007-10-12 23:04:23 +02009#define NS_SCALE 10 /* 2^10, carefully chosen */
10#define US_SCALE 32 /* 2^32, arbitralrily chosen */
11
Andres Salomon2272b0e2007-03-06 01:42:05 -080012/*
13 * Standard way to access the cycle counter.
14 */
15typedef unsigned long long cycles_t;
16
17extern unsigned int cpu_khz;
18extern unsigned int tsc_khz;
Glauber de Oliveira Costa73018a62008-01-30 13:31:26 +010019/* flag for disabling the tsc */
20extern int tsc_disable;
21
22extern void disable_TSC(void);
Andres Salomon2272b0e2007-03-06 01:42:05 -080023
24static inline cycles_t get_cycles(void)
25{
26 unsigned long long ret = 0;
27
28#ifndef CONFIG_X86_TSC
29 if (!cpu_has_tsc)
30 return 0;
31#endif
32
33#if defined(CONFIG_X86_GENERIC) || defined(CONFIG_X86_TSC)
34 rdtscll(ret);
35#endif
36 return ret;
37}
38
39/* Like get_cycles, but make sure the CPU is synchronized. */
Glauber de Oliveira Costa4e871732008-01-30 13:31:03 +010040static __always_inline cycles_t __get_cycles_sync(void)
Andres Salomon2272b0e2007-03-06 01:42:05 -080041{
42 unsigned long long ret;
Joerg Roedel6041b572007-05-10 22:22:14 -070043 unsigned eax, edx;
Andres Salomon2272b0e2007-03-06 01:42:05 -080044
45 /*
Glauber de Oliveira Costa4e871732008-01-30 13:31:03 +010046 * Use RDTSCP if possible; it is guaranteed to be synchronous
47 * and doesn't cause a VMEXIT on Hypervisors
Andi Kleenc5bcb562007-05-02 19:27:21 +020048 */
49 alternative_io(ASM_NOP3, ".byte 0x0f,0x01,0xf9", X86_FEATURE_RDTSCP,
Joerg Roedel6041b572007-05-10 22:22:14 -070050 ASM_OUTPUT2("=a" (eax), "=d" (edx)),
51 "a" (0U), "d" (0U) : "ecx", "memory");
52 ret = (((unsigned long long)edx) << 32) | ((unsigned long long)eax);
Andi Kleenc5bcb562007-05-02 19:27:21 +020053 if (ret)
54 return ret;
55
56 /*
Andres Salomon2272b0e2007-03-06 01:42:05 -080057 * Don't do an additional sync on CPUs where we know
58 * RDTSC is already synchronous:
59 */
60 alternative_io("cpuid", ASM_NOP2, X86_FEATURE_SYNC_RDTSC,
61 "=a" (eax), "0" (1) : "ebx","ecx","edx","memory");
Andres Salomon2272b0e2007-03-06 01:42:05 -080062
Glauber de Oliveira Costa4e871732008-01-30 13:31:03 +010063 return 0;
64}
65
66static __always_inline cycles_t get_cycles_sync(void)
67{
68 unsigned long long ret;
69 ret = __get_cycles_sync();
70 if (!ret)
71 rdtscll(ret);
Andres Salomon2272b0e2007-03-06 01:42:05 -080072 return ret;
73}
74
Glauber de Oliveira Costa4e871732008-01-30 13:31:03 +010075#ifdef CONFIG_PARAVIRT
76/*
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
83 */
84static __always_inline long long vget_cycles_sync(void)
85{
86 unsigned long long ret;
87 ret = __get_cycles_sync();
88 if (!ret)
89 ret = native_read_tsc();
90 return ret;
91}
92#else
93# define vget_cycles_sync() get_cycles_sync()
94#endif
95
Andres Salomon2272b0e2007-03-06 01:42:05 -080096extern void tsc_init(void);
john stultz5a90cf22007-05-02 19:27:08 +020097extern void mark_tsc_unstable(char *reason);
Andres Salomon2272b0e2007-03-06 01:42:05 -080098extern int unsynchronized_tsc(void);
99extern void init_tsc_clocksource(void);
Rusty Russelld7e28ff2007-07-19 01:49:23 -0700100int check_tsc_unstable(void);
Andres Salomon2272b0e2007-03-06 01:42:05 -0800101
102/*
103 * Boot-time check whether the TSCs are synchronized across
104 * all CPUs/cores:
105 */
106extern void check_tsc_sync_source(int cpu);
107extern void check_tsc_sync_target(void);
108
Thomas Gleixnerd3716982007-10-12 23:04:06 +0200109extern void tsc_calibrate(void);
Thomas Gleixner80ca9c92008-01-30 13:30:18 +0100110extern int notsc_setup(char *);
Thomas Gleixnerd3716982007-10-12 23:04:06 +0200111
Andres Salomon2272b0e2007-03-06 01:42:05 -0800112#endif