time.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. #ifndef _TIME_H
  3. #define _TIME_H
  4. #include <linux/typecheck.h>
  5. #include <linux/types.h>
  6. ulong get_tbclk(void);
  7. unsigned long get_timer(unsigned long base);
  8. /*
  9. * Return the current value of a monotonically increasing microsecond timer.
  10. * Granularity may be larger than 1us if hardware does not support this.
  11. */
  12. unsigned long timer_get_us(void);
  13. uint64_t get_timer_us(uint64_t base);
  14. /**
  15. * get_timer_us_long() - Get the number of elapsed microseconds
  16. *
  17. * This uses 32-bit arithmetic on 32-bit machines, which is enough to handle
  18. * delays of over an hour. For 64-bit machines it uses a 64-bit value.
  19. *
  20. *@base: Base time to consider
  21. *Return: elapsed time since @base
  22. */
  23. unsigned long get_timer_us_long(unsigned long base);
  24. /*
  25. * timer_test_add_offset()
  26. *
  27. * Allow tests to add to the time reported through lib/time.c functions
  28. * offset: number of milliseconds to advance the system time
  29. */
  30. void timer_test_add_offset(unsigned long offset);
  31. /**
  32. * usec_to_tick() - convert microseconds to clock ticks
  33. *
  34. * @usec: duration in microseconds
  35. * Return: duration in clock ticks
  36. */
  37. uint64_t usec_to_tick(unsigned long usec);
  38. /*
  39. * These inlines deal with timer wrapping correctly. You are
  40. * strongly encouraged to use them
  41. * 1. Because people otherwise forget
  42. * 2. Because if the timer wrap changes in future you won't have to
  43. * alter your driver code.
  44. *
  45. * time_after(a,b) returns true if the time a is after time b.
  46. *
  47. * Do this with "<0" and ">=0" to only test the sign of the result. A
  48. * good compiler would generate better code (and a really good compiler
  49. * wouldn't care). Gcc is currently neither.
  50. */
  51. #define time_after(a,b) \
  52. (typecheck(unsigned long, a) && \
  53. typecheck(unsigned long, b) && \
  54. ((long)((b) - (a)) < 0))
  55. #define time_before(a,b) time_after(b,a)
  56. #define time_after_eq(a,b) \
  57. (typecheck(unsigned long, a) && \
  58. typecheck(unsigned long, b) && \
  59. ((long)((a) - (b)) >= 0))
  60. #define time_before_eq(a,b) time_after_eq(b,a)
  61. /*
  62. * Calculate whether a is in the range of [b, c].
  63. */
  64. #define time_in_range(a,b,c) \
  65. (time_after_eq(a,b) && \
  66. time_before_eq(a,c))
  67. /*
  68. * Calculate whether a is in the range of [b, c).
  69. */
  70. #define time_in_range_open(a,b,c) \
  71. (time_after_eq(a,b) && \
  72. time_before(a,c))
  73. /* Same as above, but does so with platform independent 64bit types.
  74. * These must be used when utilizing jiffies_64 (i.e. return value of
  75. * get_jiffies_64() */
  76. #define time_after64(a,b) \
  77. (typecheck(__u64, a) && \
  78. typecheck(__u64, b) && \
  79. ((__s64)((b) - (a)) < 0))
  80. #define time_before64(a,b) time_after64(b,a)
  81. #define time_after_eq64(a,b) \
  82. (typecheck(__u64, a) && \
  83. typecheck(__u64, b) && \
  84. ((__s64)((a) - (b)) >= 0))
  85. #define time_before_eq64(a,b) time_after_eq64(b,a)
  86. #define time_in_range64(a, b, c) \
  87. (time_after_eq64(a, b) && \
  88. time_before_eq64(a, c))
  89. /**
  90. * usec2ticks() - Convert microseconds to internal ticks
  91. *
  92. * @usec: Value of microseconds to convert
  93. * Return: Corresponding internal ticks value, calculated using get_tbclk()
  94. */
  95. ulong usec2ticks(unsigned long usec);
  96. /**
  97. * ticks2usec() - Convert internal ticks to microseconds
  98. *
  99. * @ticks: Value of ticks to convert
  100. * Return: Corresponding microseconds value, calculated using get_tbclk()
  101. */
  102. ulong ticks2usec(unsigned long ticks);
  103. /**
  104. * wait_ticks() - waits a given number of ticks
  105. *
  106. * This is an internal function typically used to implement udelay() and
  107. * similar. Normally you should use udelay() or mdelay() instead.
  108. *
  109. * @ticks: Number of ticks to wait
  110. */
  111. void wait_ticks(unsigned long ticks);
  112. /**
  113. * timer_get_us() - Get monotonic microsecond timer
  114. *
  115. * Return: value of monotonic microsecond timer
  116. */
  117. unsigned long timer_get_us(void);
  118. /**
  119. * get_ticks() - Get the current tick value
  120. *
  121. * This is an internal value used by the timer on the system. Ticks increase
  122. * monotonically at the rate given by get_tbclk().
  123. *
  124. * Return: current tick value
  125. */
  126. uint64_t get_ticks(void);
  127. #endif /* _TIME_H */