2 #include <arch/div64.h>
6 static uint64_t offset;
9 * Converts the input khz cycle counter frequency to a time source multiplier.
10 * The multiplier is used to convert cycle counts to nanoseconds.
13 init_cycles2ns(uint32_t khz)
16 * Shift is used to obtain greater precision.
17 * Linux uses 22 for the x86 time stamp counter.
18 * For now we assume this will work for most cases.
23 * khz = cyc/(Million ns)
24 * mult/2^shift = ns/cyc
25 * mult = ns/cyc * 2^shift
26 * mult = 1Million/khz * 2^shift
27 * mult = 1000000 * 2^shift / khz
28 * mult = (1000000<<shift) / khz
30 mult = ((u64)1000000) << shift;
31 mult += khz/2; /* round for do_div */
36 * Converts cycles to nanoseconds.
37 * init_cycles2ns() must be called before this will work properly.
40 cycles2ns(uint64_t cycles)
42 return (cycles * mult) >> shift;
46 * Returns the current time in nanoseconds.
51 return cycles2ns(get_cycles()) + offset;
55 * Sets the current time in nanoseconds.
60 offset = ns - cycles2ns(get_cycles());