blob: 35c8a820d59c85980e7839bda0939f17b01909b0 [file] [log] [blame]
Eric Biedermanc84c1902004-10-14 20:13:01 +00001#ifndef CPU_X86_TSC_H
2#define CPU_X86_TSC_H
3
Aaron Durbina86e81d2014-01-27 15:07:26 -06004#include <stdint.h>
5
Martin Roth96345472017-06-24 14:13:53 -06006#if IS_ENABLED(CONFIG_TSC_SYNC_MFENCE)
Stefan Reinauer0db68202012-08-07 14:44:51 -07007#define TSC_SYNC "mfence\n"
Martin Roth96345472017-06-24 14:13:53 -06008#elif IS_ENABLED(CONFIG_TSC_SYNC_LFENCE)
Stefan Reinauer0db68202012-08-07 14:44:51 -07009#define TSC_SYNC "lfence\n"
10#else
11#define TSC_SYNC
12#endif
13
Elyes HAOUAS5e2ac2c2017-12-20 21:25:12 +010014#define MSR_PLATFORM_INFO 0xce
15
Eric Biedermanc84c1902004-10-14 20:13:01 +000016struct tsc_struct {
Lee Leahy0ca2a062017-03-06 18:01:04 -080017 unsigned int lo;
18 unsigned int hi;
Eric Biedermanc84c1902004-10-14 20:13:01 +000019};
20typedef struct tsc_struct tsc_t;
21
Stefan Reinauer348a1ba2010-03-17 01:51:11 +000022static inline tsc_t rdtsc(void)
Eric Biedermanc84c1902004-10-14 20:13:01 +000023{
24 tsc_t res;
Stefan Reinauer0db68202012-08-07 14:44:51 -070025 asm volatile (
26 TSC_SYNC
Eric Biedermanc84c1902004-10-14 20:13:01 +000027 "rdtsc"
28 : "=a" (res.lo), "=d"(res.hi) /* outputs */
Stefan Reinauer0db68202012-08-07 14:44:51 -070029 );
Eric Biedermanc84c1902004-10-14 20:13:01 +000030 return res;
31}
32
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000033#if !defined(__ROMCC__)
Ronald G. Minnich5750fdd2013-05-08 17:08:55 +020034/* Simple 32- to 64-bit multiplication. Uses 16-bit words to avoid overflow.
35 * This code is used to prevent use of libgcc's umoddi3.
36 */
37static inline void multiply_to_tsc(tsc_t *const tsc, const u32 a, const u32 b)
38{
39 tsc->lo = (a & 0xffff) * (b & 0xffff);
40 tsc->hi = ((tsc->lo >> 16)
41 + ((a & 0xffff) * (b >> 16))
42 + ((b & 0xffff) * (a >> 16)));
43 tsc->lo = ((tsc->hi & 0xffff) << 16) | (tsc->lo & 0xffff);
44 tsc->hi = ((a >> 16) * (b >> 16)) + (tsc->hi >> 16);
45}
46
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000047/* Too many registers for ROMCC */
Eric Biedermanc84c1902004-10-14 20:13:01 +000048static inline unsigned long long rdtscll(void)
49{
50 unsigned long long val;
Stefan Reinauer0db68202012-08-07 14:44:51 -070051 asm volatile (
52 TSC_SYNC
53 "rdtsc"
54 : "=A" (val)
55 );
Eric Biedermanc84c1902004-10-14 20:13:01 +000056 return val;
57}
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070058
59static inline uint64_t tsc_to_uint64(tsc_t tstamp)
60{
Elyes HAOUAS8ffd0502016-09-01 19:01:41 +020061 return (((uint64_t)tstamp.hi) << 32) + tstamp.lo;
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070062}
Eric Biedermanc84c1902004-10-14 20:13:01 +000063#endif
64
Aaron Durbinc49014e2015-08-30 21:19:55 -050065/* Provided by CPU/chipset code for the TSC rate in MHz. */
Aaron Durbin8e73b5d2013-05-01 15:27:09 -050066unsigned long tsc_freq_mhz(void);
Aaron Durbin8e73b5d2013-05-01 15:27:09 -050067
Eric Biedermanc84c1902004-10-14 20:13:01 +000068#endif /* CPU_X86_TSC_H */