timecounter.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Based on clocksource code. See commit 74d23cc704d1
  4. */
  5. #include <linux/export.h>
  6. #include <linux/timecounter.h>
  7. void timecounter_init(struct timecounter *tc,
  8. const struct cyclecounter *cc,
  9. u64 start_tstamp)
  10. {
  11. tc->cc = cc;
  12. tc->cycle_last = cc->read(cc);
  13. tc->nsec = start_tstamp;
  14. tc->mask = (1ULL << cc->shift) - 1;
  15. tc->frac = 0;
  16. }
  17. EXPORT_SYMBOL_GPL(timecounter_init);
  18. /**
  19. * timecounter_read_delta - get nanoseconds since last call of this function
  20. * @tc: Pointer to time counter
  21. *
  22. * When the underlying cycle counter runs over, this will be handled
  23. * correctly as long as it does not run over more than once between
  24. * calls.
  25. *
  26. * The first call to this function for a new time counter initializes
  27. * the time tracking and returns an undefined result.
  28. */
  29. static u64 timecounter_read_delta(struct timecounter *tc)
  30. {
  31. u64 cycle_now, cycle_delta;
  32. u64 ns_offset;
  33. /* read cycle counter: */
  34. cycle_now = tc->cc->read(tc->cc);
  35. /* calculate the delta since the last timecounter_read_delta(): */
  36. cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
  37. /* convert to nanoseconds: */
  38. ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta,
  39. tc->mask, &tc->frac);
  40. /* update time stamp of timecounter_read_delta() call: */
  41. tc->cycle_last = cycle_now;
  42. return ns_offset;
  43. }
  44. u64 timecounter_read(struct timecounter *tc)
  45. {
  46. u64 nsec;
  47. /* increment time by nanoseconds since last call */
  48. nsec = timecounter_read_delta(tc);
  49. nsec += tc->nsec;
  50. tc->nsec = nsec;
  51. return nsec;
  52. }
  53. EXPORT_SYMBOL_GPL(timecounter_read);
  54. /*
  55. * This is like cyclecounter_cyc2ns(), but it is used for computing a
  56. * time previous to the time stored in the cycle counter.
  57. */
  58. static u64 cc_cyc2ns_backwards(const struct cyclecounter *cc,
  59. u64 cycles, u64 mask, u64 frac)
  60. {
  61. u64 ns = (u64) cycles;
  62. ns = ((ns * cc->mult) - frac) >> cc->shift;
  63. return ns;
  64. }
  65. u64 timecounter_cyc2time(const struct timecounter *tc,
  66. u64 cycle_tstamp)
  67. {
  68. u64 delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
  69. u64 nsec = tc->nsec, frac = tc->frac;
  70. /*
  71. * Instead of always treating cycle_tstamp as more recent
  72. * than tc->cycle_last, detect when it is too far in the
  73. * future and treat it as old time stamp instead.
  74. */
  75. if (delta > tc->cc->mask / 2) {
  76. delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
  77. nsec -= cc_cyc2ns_backwards(tc->cc, delta, tc->mask, frac);
  78. } else {
  79. nsec += cyclecounter_cyc2ns(tc->cc, delta, tc->mask, &frac);
  80. }
  81. return nsec;
  82. }
  83. EXPORT_SYMBOL_GPL(timecounter_cyc2time);