Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


updated to use pa_to_va address conversion
[palacios.releases.git] / kitten / kernel / time.c
1 #include <lwk/time.h>
2 #include <arch/div64.h>
3
4 static uint64_t shift;
5 static uint64_t mult;
6 static uint64_t offset;
7
8 /**
9  * Converts the input khz cycle counter frequency to a time source multiplier.
10  * The multiplier is used to convert cycle counts to nanoseconds.
11  */
12 void
13 init_cycles2ns(uint32_t khz)
14 {
15         /*
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.
19          */
20         shift = 22;
21
22         /*  
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
29          */
30         mult = ((u64)1000000) << shift;
31         mult += khz/2; /* round for do_div */
32         do_div(mult, khz);
33 }
34
35 /**
36  * Converts cycles to nanoseconds.
37  * init_cycles2ns() must be called before this will work properly.
38  */
39 uint64_t
40 cycles2ns(uint64_t cycles)
41 {
42         return (cycles * mult) >> shift;
43 }
44
45 /**
46  * Returns the current time in nanoseconds.
47  */
48 uint64_t
49 get_time(void)
50 {
51         return cycles2ns(get_cycles()) + offset;
52 }
53
54 /**
55  * Sets the current time in nanoseconds.
56  */
57 void
58 set_time(uint64_t ns)
59 {
60         offset = ns - cycles2ns(get_cycles());
61 }
62