coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
tsc.h
Go to the documentation of this file.
1 #ifndef CPU_X86_TSC_H
2 #define CPU_X86_TSC_H
3 
4 #include <stdint.h>
5 
6 #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 
14 struct tsc_struct {
15  unsigned int lo;
16  unsigned int hi;
17 };
18 typedef struct tsc_struct tsc_t;
19 
20 static inline tsc_t rdtsc(void)
21 {
22  tsc_t res;
23  asm volatile (
24  TSC_SYNC
25  "rdtsc"
26  : "=a" (res.lo), "=d"(res.hi) /* outputs */
27  );
28  return res;
29 }
30 
31 /* Simple 32- to 64-bit multiplication. Uses 16-bit words to avoid overflow.
32  * This code is used to prevent use of libgcc's umoddi3.
33  */
34 static inline void multiply_to_tsc(tsc_t *const tsc, const u32 a, const u32 b)
35 {
36  tsc->lo = (a & 0xffff) * (b & 0xffff);
37  tsc->hi = ((tsc->lo >> 16)
38  + ((a & 0xffff) * (b >> 16))
39  + ((b & 0xffff) * (a >> 16)));
40  tsc->lo = ((tsc->hi & 0xffff) << 16) | (tsc->lo & 0xffff);
41  tsc->hi = ((a >> 16) * (b >> 16)) + (tsc->hi >> 16);
42 }
43 
44 static inline uint64_t tsc_to_uint64(tsc_t tstamp)
45 {
46  return (((uint64_t)tstamp.hi) << 32) + tstamp.lo;
47 }
48 
49 static inline unsigned long long rdtscll(void)
50 {
51  return tsc_to_uint64(rdtsc());
52 }
53 
54 /* Provided by CPU/chipset code for the TSC rate in MHz. */
55 unsigned long tsc_freq_mhz(void);
56 
57 static inline int tsc_constant_rate(void)
58 {
59  return !CONFIG(UNKNOWN_TSC_RATE);
60 }
61 
62 #endif /* CPU_X86_TSC_H */
@ CONFIG
Definition: dsi_common.h:201
uint32_t u32
Definition: stdint.h:51
unsigned long long uint64_t
Definition: stdint.h:17
Definition: tsc.h:14
unsigned int hi
Definition: tsc.h:16
unsigned int lo
Definition: tsc.h:15
unsigned long tsc_freq_mhz(void)
Definition: fsb.c:118
#define TSC_SYNC
Definition: tsc.h:11
static tsc_t rdtsc(void)
Definition: tsc.h:20
static void multiply_to_tsc(tsc_t *const tsc, const u32 a, const u32 b)
Definition: tsc.h:34
static int tsc_constant_rate(void)
Definition: tsc.h:57
static uint64_t tsc_to_uint64(tsc_t tstamp)
Definition: tsc.h:44
static unsigned long long rdtscll(void)
Definition: tsc.h:49