HV: timer: keep TSC frequency in KHz

This patch represents TSC freqeuency in KHz using a 32-bit unsigned integer.

The conversion macros between ticks and us/ms are changed to inline functions to
enforce the types of the input parameters. Note that us_to_ticks accepts only
uint32_t (~4K us at most) and never overflows.

Results of some unit tests on the conversion functions:

    calibrate_tsc, tsc_khz=1881600
    64us -> ticks: 120422
    64us -> ticks -> us: 63
    511us -> ticks: 961497
    511us -> ticks -> us: 510
    1280000 ticks -> us: 680
    1280000 ticks -> us -> ticks: 1279488

Signed-off-by: Junjie Mao <junjie.mao@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Junjie Mao
2018-07-03 03:41:02 +08:00
committed by lijinxia
parent ffc0b27db4
commit e75cca64c8
7 changed files with 33 additions and 15 deletions

View File

@@ -44,12 +44,28 @@ int atoi(const char *str);
long strtol_deci(const char *nptr);
uint64_t strtoul_hex(const char *nptr);
extern uint64_t tsc_hz;
#define US_TO_TICKS(x) ((x) * tsc_hz / 1000000UL)
#define CYCLES_PER_MS US_TO_TICKS(1000UL)
/**
* Frequency of TSC in KHz (where 1KHz = 1000Hz). Only valid after
* calibrate_tsc() returns.
*/
extern uint32_t tsc_khz;
#define TICKS_TO_US(x) ((((x) * (1000000UL >> 8)) / tsc_hz) << 8)
#define TICKS_TO_MS(x) (((x) * 1000UL) / tsc_hz)
static inline uint64_t us_to_ticks(uint32_t us)
{
return ((uint64_t)us * (uint64_t)tsc_khz / 1000UL);
}
#define CYCLES_PER_MS us_to_ticks(1000U)
static inline uint64_t ticks_to_us(uint64_t ticks)
{
return (ticks * 1000UL) / (uint64_t)tsc_khz;
}
static inline uint64_t ticks_to_ms(uint64_t ticks)
{
return ticks / (uint64_t)tsc_khz;
}
static inline uint64_t rdtsc(void)
{