qdf_time.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (c) 2014-2017 The Linux Foundation. All rights reserved.
  3. *
  4. * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
  5. *
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for
  8. * any purpose with or without fee is hereby granted, provided that the
  9. * above copyright notice and this permission notice appear in all
  10. * copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  13. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  15. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  16. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  17. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  18. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  19. * PERFORMANCE OF THIS SOFTWARE.
  20. */
  21. /*
  22. * This file was originally distributed by Qualcomm Atheros, Inc.
  23. * under proprietary terms before Copyright ownership was assigned
  24. * to the Linux Foundation.
  25. */
  26. /**
  27. * DOC: qdf_time
  28. * This file abstracts time related functionality.
  29. */
  30. #ifndef _QDF_OS_TIME_H
  31. #define _QDF_OS_TIME_H
  32. #include <i_qdf_time.h>
  33. typedef __qdf_time_t qdf_time_t;
  34. /**
  35. * qdf_system_ticks - Count the number of ticks elapsed from the time when
  36. * the system booted
  37. *
  38. * Return: ticks
  39. */
  40. static inline qdf_time_t qdf_system_ticks(void)
  41. {
  42. return __qdf_system_ticks();
  43. }
  44. /**
  45. * qdf_system_ticks_to_msecs - convert ticks to milliseconds
  46. * @clock_ticks: Number of ticks
  47. *
  48. * Return: unsigned int Time in milliseconds
  49. */
  50. static inline uint32_t qdf_system_ticks_to_msecs(unsigned long clock_ticks)
  51. {
  52. return __qdf_system_ticks_to_msecs(clock_ticks);
  53. }
  54. /**
  55. * qdf_system_msecs_to_ticks - convert milliseconds to ticks
  56. * @msec: Time in milliseconds
  57. *
  58. * Return: unsigned long number of ticks
  59. */
  60. static inline qdf_time_t qdf_system_msecs_to_ticks(uint32_t msecs)
  61. {
  62. return __qdf_system_msecs_to_ticks(msecs);
  63. }
  64. /**
  65. * qdf_get_system_uptime - Return a monotonically increasing time
  66. * This increments once per HZ ticks
  67. *
  68. * Return: qdf_time_t system up time in ticks
  69. */
  70. static inline qdf_time_t qdf_get_system_uptime(void)
  71. {
  72. return __qdf_get_system_uptime();
  73. }
  74. /**
  75. * qdf_get_bootbased_boottime_ns() - Get the bootbased time in nanoseconds
  76. *
  77. * qdf_get_bootbased_boottime_ns() function returns the number of nanoseconds
  78. * that have elapsed since the system was booted. It also includes the time when
  79. * system was suspended.
  80. *
  81. * Return:
  82. * The time since system booted in nanoseconds
  83. */
  84. static inline s64 qdf_get_bootbased_boottime_ns(void)
  85. {
  86. return ktime_get_boot_ns();
  87. }
  88. /**
  89. * qdf_get_system_timestamp - Return current timestamp
  90. *
  91. * Return: unsigned long timestamp in ms.
  92. */
  93. static inline unsigned long qdf_get_system_timestamp(void)
  94. {
  95. return __qdf_get_system_timestamp();
  96. }
  97. /**
  98. * qdf_udelay - delay in microseconds
  99. * @usecs: Number of microseconds to delay
  100. *
  101. * Return: none
  102. */
  103. static inline void qdf_udelay(int usecs)
  104. {
  105. __qdf_udelay(usecs);
  106. }
  107. /**
  108. * qdf_mdelay - Delay in milliseconds.
  109. * @msec: Number of milliseconds to delay
  110. *
  111. * Return: none
  112. */
  113. static inline void qdf_mdelay(int msecs)
  114. {
  115. __qdf_mdelay(msecs);
  116. }
  117. /**
  118. * qdf_system_time_after() - Check if a is later than b
  119. * @a: Time stamp value a
  120. * @b: Time stamp value b
  121. *
  122. * Return:
  123. * true if a < b else false
  124. */
  125. static inline bool qdf_system_time_after(qdf_time_t a, qdf_time_t b)
  126. {
  127. return __qdf_system_time_after(a, b);
  128. }
  129. /**
  130. * qdf_system_time_before() - Check if a is before b
  131. * @a: Time stamp value a
  132. * @b: Time stamp value b
  133. *
  134. * Return:
  135. * true if a is before b else false
  136. */
  137. static inline bool qdf_system_time_before(qdf_time_t a, qdf_time_t b)
  138. {
  139. return __qdf_system_time_before(a, b);
  140. }
  141. /**
  142. * qdf_system_time_after_eq() - Check if a atleast as recent as b, if not
  143. * later
  144. * @a: Time stamp value a
  145. * @b: Time stamp value b
  146. *
  147. * Return:
  148. * true if a >= b else false
  149. */
  150. static inline bool qdf_system_time_after_eq(qdf_time_t a, qdf_time_t b)
  151. {
  152. return __qdf_system_time_after_eq(a, b);
  153. }
  154. /**
  155. * enum qdf_timestamp_unit - what unit the qdf timestamp is in
  156. * @KERNEL_LOG: boottime time in uS (micro seconds)
  157. * @QTIMER: QTIME in (1/19200)S
  158. *
  159. * This enum is used to distinguish which timer source is used.
  160. */
  161. enum qdf_timestamp_unit {
  162. KERNEL_LOG,
  163. QTIMER,
  164. };
  165. #ifdef QCA_WIFI_3_0_ADRASTEA
  166. #define QDF_LOG_TIMESTAMP_UNIT QTIMER
  167. #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 192
  168. static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
  169. {
  170. /*
  171. * Try to preserve precision by multiplying by 10 first.
  172. * If that would cause a wrap around, divide first instead.
  173. */
  174. if (time * 10 < time) {
  175. do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
  176. return time * 10;
  177. }
  178. time = time * 10;
  179. do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
  180. return time;
  181. }
  182. #else
  183. #define QDF_LOG_TIMESTAMP_UNIT KERNEL_LOG
  184. #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 10
  185. static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
  186. {
  187. /* timestamps are already in micro seconds */
  188. return time;
  189. }
  190. #endif
  191. static inline void qdf_log_timestamp_to_secs(uint64_t time, uint64_t *secs,
  192. uint64_t *usecs)
  193. {
  194. *secs = qdf_log_timestamp_to_usecs(time);
  195. *usecs = do_div(*secs, 1000000ul);
  196. }
  197. static inline uint64_t qdf_usecs_to_log_timestamp(uint64_t usecs)
  198. {
  199. return (usecs * QDF_LOG_TIMESTAMP_CYCLES_PER_10_US) / 10;
  200. }
  201. /**
  202. * qdf_get_log_timestamp - get time stamp for logging
  203. * For adrastea this API returns QTIMER tick which is needed to synchronize
  204. * host and fw log timestamps
  205. * For ROME and other discrete solution this API returns system boot time stamp
  206. *
  207. * Return:
  208. * QTIMER ticks(19.2MHz) for adrastea
  209. * System tick for rome and other future discrete solutions
  210. */
  211. static inline uint64_t qdf_get_log_timestamp(void)
  212. {
  213. return __qdf_get_log_timestamp();
  214. }
  215. /**
  216. * qdf_get_monotonic_boottime - get monotonic kernel boot time
  217. * This API is similar to qdf_get_system_boottime but it includes
  218. * time spent in suspend.
  219. *
  220. * Return: Time in microseconds
  221. */
  222. static inline uint64_t qdf_get_monotonic_boottime(void)
  223. {
  224. return __qdf_get_monotonic_boottime();
  225. }
  226. #endif