qdf_time.h 7.4 KB

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