time.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /**
  74. * usec2ticks() - Convert microseconds to internal ticks
  75. *
  76. * @usec: Value of microseconds to convert
  77. * @return Corresponding internal ticks value, calculated using get_tbclk()
  78. */
  79. ulong usec2ticks(unsigned long usec);
  80. /**
  81. * ticks2usec() - Convert internal ticks to microseconds
  82. *
  83. * @ticks: Value of ticks to convert
  84. * @return Corresponding microseconds value, calculated using get_tbclk()
  85. */
  86. ulong ticks2usec(unsigned long ticks);
  87. /**
  88. * wait_ticks() - waits a given number of ticks
  89. *
  90. * This is an internal function typically used to implement udelay() and
  91. * similar. Normally you should use udelay() or mdelay() instead.
  92. *
  93. * @ticks: Number of ticks to wait
  94. */
  95. void wait_ticks(unsigned long ticks);
  96. /**
  97. * timer_get_us() - Get monotonic microsecond timer
  98. *
  99. * @return value of monotonic microsecond timer
  100. */
  101. unsigned long timer_get_us(void);
  102. /**
  103. * get_ticks() - Get the current tick value
  104. *
  105. * This is an internal value used by the timer on the system. Ticks increase
  106. * monotonically at the rate given by get_tbclk().
  107. *
  108. * @return current tick value
  109. */
  110. uint64_t get_ticks(void);
  111. #endif /* _TIME_H */