timecounter.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * linux/include/linux/timecounter.h
  3. *
  4. * based on code that migrated away from
  5. * linux/include/linux/clocksource.h
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #ifndef _LINUX_TIMECOUNTER_H
  18. #define _LINUX_TIMECOUNTER_H
  19. #include <linux/types.h>
  20. /* simplify initialization of mask field */
  21. #define CYCLECOUNTER_MASK(bits) (cycle_t)((bits) < 64 ? ((1ULL<<(bits))-1) : -1)
  22. /**
  23. * struct cyclecounter - hardware abstraction for a free running counter
  24. * Provides completely state-free accessors to the underlying hardware.
  25. * Depending on which hardware it reads, the cycle counter may wrap
  26. * around quickly. Locking rules (if necessary) have to be defined
  27. * by the implementor and user of specific instances of this API.
  28. *
  29. * @read: returns the current cycle value
  30. * @mask: bitmask for two's complement
  31. * subtraction of non 64 bit counters,
  32. * see CYCLECOUNTER_MASK() helper macro
  33. * @mult: cycle to nanosecond multiplier
  34. * @shift: cycle to nanosecond divisor (power of two)
  35. */
  36. struct cyclecounter {
  37. cycle_t (*read)(const struct cyclecounter *cc);
  38. cycle_t mask;
  39. u32 mult;
  40. u32 shift;
  41. };
  42. /**
  43. * struct timecounter - layer above a %struct cyclecounter which counts nanoseconds
  44. * Contains the state needed by timecounter_read() to detect
  45. * cycle counter wrap around. Initialize with
  46. * timecounter_init(). Also used to convert cycle counts into the
  47. * corresponding nanosecond counts with timecounter_cyc2time(). Users
  48. * of this code are responsible for initializing the underlying
  49. * cycle counter hardware, locking issues and reading the time
  50. * more often than the cycle counter wraps around. The nanosecond
  51. * counter will only wrap around after ~585 years.
  52. *
  53. * @cc: the cycle counter used by this instance
  54. * @cycle_last: most recent cycle counter value seen by
  55. * timecounter_read()
  56. * @nsec: continuously increasing count
  57. * @mask: bit mask for maintaining the 'frac' field
  58. * @frac: accumulated fractional nanoseconds
  59. */
  60. struct timecounter {
  61. const struct cyclecounter *cc;
  62. cycle_t cycle_last;
  63. u64 nsec;
  64. u64 mask;
  65. u64 frac;
  66. };
  67. /**
  68. * cyclecounter_cyc2ns - converts cycle counter cycles to nanoseconds
  69. * @cc: Pointer to cycle counter.
  70. * @cycles: Cycles
  71. * @mask: bit mask for maintaining the 'frac' field
  72. * @frac: pointer to storage for the fractional nanoseconds.
  73. */
  74. static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
  75. cycle_t cycles, u64 mask, u64 *frac)
  76. {
  77. u64 ns = (u64) cycles;
  78. ns = (ns * cc->mult) + *frac;
  79. *frac = ns & mask;
  80. return ns >> cc->shift;
  81. }
  82. /**
  83. * timecounter_adjtime - Shifts the time of the clock.
  84. * @delta: Desired change in nanoseconds.
  85. */
  86. static inline void timecounter_adjtime(struct timecounter *tc, s64 delta)
  87. {
  88. tc->nsec += delta;
  89. }
  90. /**
  91. * timecounter_init - initialize a time counter
  92. * @tc: Pointer to time counter which is to be initialized/reset
  93. * @cc: A cycle counter, ready to be used.
  94. * @start_tstamp: Arbitrary initial time stamp.
  95. *
  96. * After this call the current cycle register (roughly) corresponds to
  97. * the initial time stamp. Every call to timecounter_read() increments
  98. * the time stamp counter by the number of elapsed nanoseconds.
  99. */
  100. extern void timecounter_init(struct timecounter *tc,
  101. const struct cyclecounter *cc,
  102. u64 start_tstamp);
  103. /**
  104. * timecounter_read - return nanoseconds elapsed since timecounter_init()
  105. * plus the initial time stamp
  106. * @tc: Pointer to time counter.
  107. *
  108. * In other words, keeps track of time since the same epoch as
  109. * the function which generated the initial time stamp.
  110. */
  111. extern u64 timecounter_read(struct timecounter *tc);
  112. /**
  113. * timecounter_cyc2time - convert a cycle counter to same
  114. * time base as values returned by
  115. * timecounter_read()
  116. * @tc: Pointer to time counter.
  117. * @cycle_tstamp: a value returned by tc->cc->read()
  118. *
  119. * Cycle counts that are converted correctly as long as they
  120. * fall into the interval [-1/2 max cycle count, +1/2 max cycle count],
  121. * with "max cycle count" == cs->mask+1.
  122. *
  123. * This allows conversion of cycle counter values which were generated
  124. * in the past.
  125. */
  126. extern u64 timecounter_cyc2time(struct timecounter *tc,
  127. cycle_t cycle_tstamp);
  128. #endif