qdf_time.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright (c) 2014-2018 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. typedef __qdf_ktime_t qdf_ktime_t;
  35. /**
  36. * qdf_ns_to_ktime - Converts nanoseconds to a qdf_ktime_t object
  37. * @ns: time in nanoseconds
  38. *
  39. * Return: nanoseconds as qdf_ktime_t object
  40. */
  41. static inline qdf_ktime_t qdf_ns_to_ktime(uint64_t ns)
  42. {
  43. return __qdf_ns_to_ktime(ns);
  44. }
  45. /**
  46. * qdf_ktime_add - Adds two qdf_ktime_t objects and returns
  47. * a qdf_ktime_t object
  48. * @ktime1: time as qdf_ktime_t object
  49. * @ktime2: time as qdf_ktime_t object
  50. *
  51. * Return: sum of both qdf_ktime_t as qdf_ktime_t object
  52. */
  53. static inline qdf_ktime_t qdf_ktime_add(qdf_ktime_t ktime1, qdf_ktime_t ktime2)
  54. {
  55. return __qdf_ktime_add(ktime1, ktime2);
  56. }
  57. /**
  58. * qdf_ktime_get - Gets the current time as qdf_ktime_t object
  59. *
  60. * Return: current time as qdf_ktime_t object
  61. */
  62. static inline qdf_ktime_t qdf_ktime_get(void)
  63. {
  64. return __qdf_ktime_get();
  65. }
  66. /**
  67. * qdf_ktime_add_ns - Adds qdf_ktime_t object and nanoseconds value and
  68. * returns the qdf_ktime_t object
  69. * @ktime: time as qdf_ktime_t object
  70. * @ns: time in nanoseconds
  71. *
  72. * Return: qdf_ktime_t object
  73. */
  74. static inline qdf_ktime_t qdf_ktime_add_ns(qdf_ktime_t ktime, int64_t ns)
  75. {
  76. return __qdf_ktime_add_ns(ktime, ns);
  77. }
  78. /**
  79. * qdf_ktime_to_ms - Convert the qdf_ktime_t object into milliseconds
  80. * @ktime: time as qdf_ktime_t object
  81. *
  82. * Return: qdf_ktime_t in milliseconds
  83. */
  84. static inline int64_t qdf_ktime_to_ms(qdf_ktime_t ktime)
  85. {
  86. return __qdf_ktime_to_ms(ktime);
  87. }
  88. /**
  89. * qdf_ktime_to_ns - Convert the qdf_ktime_t object into nanoseconds
  90. * @ktime: time as qdf_ktime_t object
  91. *
  92. * Return: qdf_ktime_t in nanoseconds
  93. */
  94. static inline int64_t qdf_ktime_to_ns(qdf_ktime_t ktime)
  95. {
  96. return __qdf_ktime_to_ns(ktime);
  97. }
  98. /**
  99. * qdf_system_ticks - Count the number of ticks elapsed from the time when
  100. * the system booted
  101. *
  102. * Return: ticks
  103. */
  104. static inline qdf_time_t qdf_system_ticks(void)
  105. {
  106. return __qdf_system_ticks();
  107. }
  108. /**
  109. * qdf_system_ticks_to_msecs - convert ticks to milliseconds
  110. * @clock_ticks: Number of ticks
  111. *
  112. * Return: unsigned int Time in milliseconds
  113. */
  114. static inline uint32_t qdf_system_ticks_to_msecs(unsigned long clock_ticks)
  115. {
  116. return __qdf_system_ticks_to_msecs(clock_ticks);
  117. }
  118. /**
  119. * qdf_system_msecs_to_ticks - convert milliseconds to ticks
  120. * @msec: Time in milliseconds
  121. *
  122. * Return: unsigned long number of ticks
  123. */
  124. static inline qdf_time_t qdf_system_msecs_to_ticks(uint32_t msecs)
  125. {
  126. return __qdf_system_msecs_to_ticks(msecs);
  127. }
  128. /**
  129. * qdf_get_system_uptime - Return a monotonically increasing time
  130. * This increments once per HZ ticks
  131. *
  132. * Return: qdf_time_t system up time in ticks
  133. */
  134. static inline qdf_time_t qdf_get_system_uptime(void)
  135. {
  136. return __qdf_get_system_uptime();
  137. }
  138. /**
  139. * qdf_get_bootbased_boottime_ns() - Get the bootbased time in nanoseconds
  140. *
  141. * qdf_get_bootbased_boottime_ns() function returns the number of nanoseconds
  142. * that have elapsed since the system was booted. It also includes the time when
  143. * system was suspended.
  144. *
  145. * Return:
  146. * The time since system booted in nanoseconds
  147. */
  148. #if (LINUX_VERSION_CODE > KERNEL_VERSION(3, 10, 0))
  149. static inline s64 qdf_get_bootbased_boottime_ns(void)
  150. {
  151. return ktime_get_boot_ns();
  152. }
  153. #else
  154. static inline s64 qdf_get_bootbased_boottime_ns(void)
  155. {
  156. return ktime_to_ns(ktime_get_boottime());
  157. }
  158. #endif
  159. /**
  160. * qdf_get_system_timestamp - Return current timestamp
  161. *
  162. * Return: unsigned long timestamp in ms.
  163. */
  164. static inline unsigned long qdf_get_system_timestamp(void)
  165. {
  166. return __qdf_get_system_timestamp();
  167. }
  168. /**
  169. * qdf_udelay - delay in microseconds
  170. * @usecs: Number of microseconds to delay
  171. *
  172. * Return: none
  173. */
  174. static inline void qdf_udelay(int usecs)
  175. {
  176. __qdf_udelay(usecs);
  177. }
  178. /**
  179. * qdf_mdelay - Delay in milliseconds.
  180. * @msec: Number of milliseconds to delay
  181. *
  182. * Return: none
  183. */
  184. static inline void qdf_mdelay(int msecs)
  185. {
  186. __qdf_mdelay(msecs);
  187. }
  188. /**
  189. * qdf_system_time_after() - Check if a is later than b
  190. * @a: Time stamp value a
  191. * @b: Time stamp value b
  192. *
  193. * Return:
  194. * true if a < b else false
  195. */
  196. static inline bool qdf_system_time_after(qdf_time_t a, qdf_time_t b)
  197. {
  198. return __qdf_system_time_after(a, b);
  199. }
  200. /**
  201. * qdf_system_time_before() - Check if a is before b
  202. * @a: Time stamp value a
  203. * @b: Time stamp value b
  204. *
  205. * Return:
  206. * true if a is before b else false
  207. */
  208. static inline bool qdf_system_time_before(qdf_time_t a, qdf_time_t b)
  209. {
  210. return __qdf_system_time_before(a, b);
  211. }
  212. /**
  213. * qdf_system_time_after_eq() - Check if a atleast as recent as b, if not
  214. * later
  215. * @a: Time stamp value a
  216. * @b: Time stamp value b
  217. *
  218. * Return:
  219. * true if a >= b else false
  220. */
  221. static inline bool qdf_system_time_after_eq(qdf_time_t a, qdf_time_t b)
  222. {
  223. return __qdf_system_time_after_eq(a, b);
  224. }
  225. /**
  226. * enum qdf_timestamp_unit - what unit the qdf timestamp is in
  227. * @KERNEL_LOG: boottime time in uS (micro seconds)
  228. * @QTIMER: QTIME in (1/19200)S
  229. *
  230. * This enum is used to distinguish which timer source is used.
  231. */
  232. enum qdf_timestamp_unit {
  233. KERNEL_LOG,
  234. QTIMER,
  235. };
  236. #ifdef QCA_WIFI_3_0_ADRASTEA
  237. #define QDF_LOG_TIMESTAMP_UNIT QTIMER
  238. #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 192
  239. static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
  240. {
  241. /*
  242. * Try to preserve precision by multiplying by 10 first.
  243. * If that would cause a wrap around, divide first instead.
  244. */
  245. if (time * 10 < time) {
  246. do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
  247. return time * 10;
  248. }
  249. time = time * 10;
  250. do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
  251. return time;
  252. }
  253. #else
  254. #define QDF_LOG_TIMESTAMP_UNIT KERNEL_LOG
  255. #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 10
  256. static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
  257. {
  258. /* timestamps are already in micro seconds */
  259. return time;
  260. }
  261. #endif
  262. static inline void qdf_log_timestamp_to_secs(uint64_t time, uint64_t *secs,
  263. uint64_t *usecs)
  264. {
  265. *secs = qdf_log_timestamp_to_usecs(time);
  266. *usecs = do_div(*secs, 1000000ul);
  267. }
  268. static inline uint64_t qdf_usecs_to_log_timestamp(uint64_t usecs)
  269. {
  270. return (usecs * QDF_LOG_TIMESTAMP_CYCLES_PER_10_US) / 10;
  271. }
  272. /**
  273. * qdf_get_log_timestamp - get time stamp for logging
  274. * For adrastea this API returns QTIMER tick which is needed to synchronize
  275. * host and fw log timestamps
  276. * For ROME and other discrete solution this API returns system boot time stamp
  277. *
  278. * Return:
  279. * QTIMER ticks(19.2MHz) for adrastea
  280. * System tick for rome and other future discrete solutions
  281. */
  282. static inline uint64_t qdf_get_log_timestamp(void)
  283. {
  284. return __qdf_get_log_timestamp();
  285. }
  286. /**
  287. * qdf_get_monotonic_boottime - get monotonic kernel boot time
  288. * This API is similar to qdf_get_system_boottime but it includes
  289. * time spent in suspend.
  290. *
  291. * Return: Time in microseconds
  292. */
  293. static inline uint64_t qdf_get_monotonic_boottime(void)
  294. {
  295. return __qdf_get_monotonic_boottime();
  296. }
  297. #endif