mb/asrock/z87_extreme4: Select correct Kconfig for ASM1061
[coreboot2.git] / src / include / cpu / x86 / tsc.h
blob5d173da66e301444b838e01e1f94ebb9145864ad
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef CPU_X86_TSC_H
4 #define CPU_X86_TSC_H
6 #include <stdint.h>
8 #if CONFIG(TSC_SYNC_MFENCE)
9 #define TSC_SYNC "mfence\n"
10 #elif CONFIG(TSC_SYNC_LFENCE)
11 #define TSC_SYNC "lfence\n"
12 #else
13 #define TSC_SYNC
14 #endif
16 struct tsc_struct {
17 unsigned int lo;
18 unsigned int hi;
20 typedef struct tsc_struct tsc_t;
22 static inline tsc_t rdtsc(void)
24 tsc_t res;
25 asm volatile (
26 TSC_SYNC
27 "rdtsc"
28 : "=a" (res.lo), "=d"(res.hi) /* outputs */
30 return res;
33 /* Simple 32- to 64-bit multiplication. Uses 16-bit words to avoid overflow.
34 * This code is used to prevent use of libgcc's umoddi3.
36 static inline void multiply_to_tsc(tsc_t *const tsc, const u32 a, const u32 b)
38 tsc->lo = (a & 0xffff) * (b & 0xffff);
39 tsc->hi = ((tsc->lo >> 16)
40 + ((a & 0xffff) * (b >> 16))
41 + ((b & 0xffff) * (a >> 16)));
42 tsc->lo = ((tsc->hi & 0xffff) << 16) | (tsc->lo & 0xffff);
43 tsc->hi = ((a >> 16) * (b >> 16)) + (tsc->hi >> 16);
46 static inline uint64_t tsc_to_uint64(tsc_t tstamp)
48 return (((uint64_t)tstamp.hi) << 32) + tstamp.lo;
51 static inline unsigned long long rdtscll(void)
53 return tsc_to_uint64(rdtsc());
56 /* Provided by CPU/chipset code for the TSC rate in MHz. */
57 unsigned long tsc_freq_mhz(void);
59 static inline int tsc_constant_rate(void)
61 return !CONFIG(UNKNOWN_TSC_RATE);
64 #endif /* CPU_X86_TSC_H */