blob: 5a7fbc2007b35c3a6ebe83f33d9ffa57d4da1c72 [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
Stefan Reinauer0db68202012-08-07 14:44:51 -07006#if CONFIG_TSC_SYNC_MFENCE
7#define TSC_SYNC "mfence\n"
8#elif CONFIG_TSC_SYNC_LFENCE
9#define TSC_SYNC "lfence\n"
10#else
11#define TSC_SYNC
12#endif
13
Eric Biedermanc84c1902004-10-14 20:13:01 +000014struct tsc_struct {
Lee Leahy0ca2a062017-03-06 18:01:04 -080015 unsigned int lo;
16 unsigned int hi;
Eric Biedermanc84c1902004-10-14 20:13:01 +000017};
18typedef struct tsc_struct tsc_t;
19
Stefan Reinauer348a1ba2010-03-17 01:51:11 +000020static inline tsc_t rdtsc(void)
Eric Biedermanc84c1902004-10-14 20:13:01 +000021{
22 tsc_t res;
Stefan Reinauer0db68202012-08-07 14:44:51 -070023 asm volatile (
24 TSC_SYNC
Eric Biedermanc84c1902004-10-14 20:13:01 +000025 "rdtsc"
26 : "=a" (res.lo), "=d"(res.hi) /* outputs */
Stefan Reinauer0db68202012-08-07 14:44:51 -070027 );
Eric Biedermanc84c1902004-10-14 20:13:01 +000028 return res;
29}
30
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000031#if !defined(__ROMCC__)
Ronald G. Minnich5750fdd2013-05-08 17:08:55 +020032/* Simple 32- to 64-bit multiplication. Uses 16-bit words to avoid overflow.
33 * This code is used to prevent use of libgcc's umoddi3.
34 */
35static inline void multiply_to_tsc(tsc_t *const tsc, const u32 a, const u32 b)
36{
37 tsc->lo = (a & 0xffff) * (b & 0xffff);
38 tsc->hi = ((tsc->lo >> 16)
39 + ((a & 0xffff) * (b >> 16))
40 + ((b & 0xffff) * (a >> 16)));
41 tsc->lo = ((tsc->hi & 0xffff) << 16) | (tsc->lo & 0xffff);
42 tsc->hi = ((a >> 16) * (b >> 16)) + (tsc->hi >> 16);
43}
44
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000045/* Too many registers for ROMCC */
Eric Biedermanc84c1902004-10-14 20:13:01 +000046static inline unsigned long long rdtscll(void)
47{
48 unsigned long long val;
Stefan Reinauer0db68202012-08-07 14:44:51 -070049 asm volatile (
50 TSC_SYNC
51 "rdtsc"
52 : "=A" (val)
53 );
Eric Biedermanc84c1902004-10-14 20:13:01 +000054 return val;
55}
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070056
57static inline uint64_t tsc_to_uint64(tsc_t tstamp)
58{
Elyes HAOUAS8ffd0502016-09-01 19:01:41 +020059 return (((uint64_t)tstamp.hi) << 32) + tstamp.lo;
Stefan Reinauer3a6550d2013-08-01 13:31:44 -070060}
Eric Biedermanc84c1902004-10-14 20:13:01 +000061#endif
62
Aaron Durbinc49014e2015-08-30 21:19:55 -050063/* Provided by CPU/chipset code for the TSC rate in MHz. */
Aaron Durbin8e73b5d2013-05-01 15:27:09 -050064unsigned long tsc_freq_mhz(void);
Aaron Durbin8e73b5d2013-05-01 15:27:09 -050065
Eric Biedermanc84c1902004-10-14 20:13:01 +000066#endif /* CPU_X86_TSC_H */