qdf_time.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #if (LINUX_VERSION_CODE > KERNEL_VERSION(3, 10, 0))
  85. static inline s64 qdf_get_bootbased_boottime_ns(void)
  86. {
  87. return ktime_get_boot_ns();
  88. }
  89. #else
  90. static inline s64 qdf_get_bootbased_boottime_ns(void)
  91. {
  92. return ktime_to_ns(ktime_get_boottime());
  93. }
  94. #endif
  95. /**
  96. * qdf_get_system_timestamp - Return current timestamp
  97. *
  98. * Return: unsigned long timestamp in ms.
  99. */
  100. static inline unsigned long qdf_get_system_timestamp(void)
  101. {
  102. return __qdf_get_system_timestamp();
  103. }
  104. /**
  105. * qdf_udelay - delay in microseconds
  106. * @usecs: Number of microseconds to delay
  107. *
  108. * Return: none
  109. */
  110. static inline void qdf_udelay(int usecs)
  111. {
  112. __qdf_udelay(usecs);
  113. }
  114. /**
  115. * qdf_mdelay - Delay in milliseconds.
  116. * @msec: Number of milliseconds to delay
  117. *
  118. * Return: none
  119. */
  120. static inline void qdf_mdelay(int msecs)
  121. {
  122. __qdf_mdelay(msecs);
  123. }
  124. /**
  125. * qdf_system_time_after() - Check if a is later than b
  126. * @a: Time stamp value a
  127. * @b: Time stamp value b
  128. *
  129. * Return:
  130. * true if a < b else false
  131. */
  132. static inline bool qdf_system_time_after(qdf_time_t a, qdf_time_t b)
  133. {
  134. return __qdf_system_time_after(a, b);
  135. }
  136. /**
  137. * qdf_system_time_before() - Check if a is before b
  138. * @a: Time stamp value a
  139. * @b: Time stamp value b
  140. *
  141. * Return:
  142. * true if a is before b else false
  143. */
  144. static inline bool qdf_system_time_before(qdf_time_t a, qdf_time_t b)
  145. {
  146. return __qdf_system_time_before(a, b);
  147. }
  148. /**
  149. * qdf_system_time_after_eq() - Check if a atleast as recent as b, if not
  150. * later
  151. * @a: Time stamp value a
  152. * @b: Time stamp value b
  153. *
  154. * Return:
  155. * true if a >= b else false
  156. */
  157. static inline bool qdf_system_time_after_eq(qdf_time_t a, qdf_time_t b)
  158. {
  159. return __qdf_system_time_after_eq(a, b);
  160. }
  161. /**
  162. * enum qdf_timestamp_unit - what unit the qdf timestamp is in
  163. * @KERNEL_LOG: boottime time in uS (micro seconds)
  164. * @QTIMER: QTIME in (1/19200)S
  165. *
  166. * This enum is used to distinguish which timer source is used.
  167. */
  168. enum qdf_timestamp_unit {
  169. KERNEL_LOG,
  170. QTIMER,
  171. };
  172. #ifdef QCA_WIFI_3_0_ADRASTEA
  173. #define QDF_LOG_TIMESTAMP_UNIT QTIMER
  174. #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 192
  175. static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
  176. {
  177. /*
  178. * Try to preserve precision by multiplying by 10 first.
  179. * If that would cause a wrap around, divide first instead.
  180. */
  181. if (time * 10 < time) {
  182. do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
  183. return time * 10;
  184. }
  185. time = time * 10;
  186. do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
  187. return time;
  188. }
  189. #else
  190. #define QDF_LOG_TIMESTAMP_UNIT KERNEL_LOG
  191. #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 10
  192. static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
  193. {
  194. /* timestamps are already in micro seconds */
  195. return time;
  196. }
  197. #endif
  198. static inline void qdf_log_timestamp_to_secs(uint64_t time, uint64_t *secs,
  199. uint64_t *usecs)
  200. {
  201. *secs = qdf_log_timestamp_to_usecs(time);
  202. *usecs = do_div(*secs, 1000000ul);
  203. }
  204. static inline uint64_t qdf_usecs_to_log_timestamp(uint64_t usecs)
  205. {
  206. return (usecs * QDF_LOG_TIMESTAMP_CYCLES_PER_10_US) / 10;
  207. }
  208. /**
  209. * qdf_get_log_timestamp - get time stamp for logging
  210. * For adrastea this API returns QTIMER tick which is needed to synchronize
  211. * host and fw log timestamps
  212. * For ROME and other discrete solution this API returns system boot time stamp
  213. *
  214. * Return:
  215. * QTIMER ticks(19.2MHz) for adrastea
  216. * System tick for rome and other future discrete solutions
  217. */
  218. static inline uint64_t qdf_get_log_timestamp(void)
  219. {
  220. return __qdf_get_log_timestamp();
  221. }
  222. /**
  223. * qdf_get_monotonic_boottime - get monotonic kernel boot time
  224. * This API is similar to qdf_get_system_boottime but it includes
  225. * time spent in suspend.
  226. *
  227. * Return: Time in microseconds
  228. */
  229. static inline uint64_t qdf_get_monotonic_boottime(void)
  230. {
  231. return __qdf_get_monotonic_boottime();
  232. }
  233. #endif