timecounter.h 4.1 KB

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