qdf_time.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. static inline uint64_t qdf_get_bootbased_boottime_ns(void)
  149. {
  150. return __qdf_get_bootbased_boottime_ns();
  151. }
  152. /**
  153. * qdf_get_system_timestamp - Return current timestamp
  154. *
  155. * Return: unsigned long timestamp in ms.
  156. */
  157. static inline unsigned long qdf_get_system_timestamp(void)
  158. {
  159. return __qdf_get_system_timestamp();
  160. }
  161. /**
  162. * qdf_udelay - delay in microseconds
  163. * @usecs: Number of microseconds to delay
  164. *
  165. * Return: none
  166. */
  167. static inline void qdf_udelay(int usecs)
  168. {
  169. __qdf_udelay(usecs);
  170. }
  171. /**
  172. * qdf_mdelay - Delay in milliseconds.
  173. * @msec: Number of milliseconds to delay
  174. *
  175. * Return: none
  176. */
  177. static inline void qdf_mdelay(int msecs)
  178. {
  179. __qdf_mdelay(msecs);
  180. }
  181. /**
  182. * qdf_system_time_after() - Check if a is later than b
  183. * @a: Time stamp value a
  184. * @b: Time stamp value b
  185. *
  186. * Return:
  187. * true if a < b else false
  188. */
  189. static inline bool qdf_system_time_after(qdf_time_t a, qdf_time_t b)
  190. {
  191. return __qdf_system_time_after(a, b);
  192. }
  193. /**
  194. * qdf_system_time_before() - Check if a is before b
  195. * @a: Time stamp value a
  196. * @b: Time stamp value b
  197. *
  198. * Return:
  199. * true if a is before b else false
  200. */
  201. static inline bool qdf_system_time_before(qdf_time_t a, qdf_time_t b)
  202. {
  203. return __qdf_system_time_before(a, b);
  204. }
  205. /**
  206. * qdf_system_time_after_eq() - Check if a atleast as recent as b, if not
  207. * later
  208. * @a: Time stamp value a
  209. * @b: Time stamp value b
  210. *
  211. * Return:
  212. * true if a >= b else false
  213. */
  214. static inline bool qdf_system_time_after_eq(qdf_time_t a, qdf_time_t b)
  215. {
  216. return __qdf_system_time_after_eq(a, b);
  217. }
  218. /**
  219. * enum qdf_timestamp_unit - what unit the qdf timestamp is in
  220. * @KERNEL_LOG: boottime time in uS (micro seconds)
  221. * @QTIMER: QTIME in (1/19200)S
  222. *
  223. * This enum is used to distinguish which timer source is used.
  224. */
  225. enum qdf_timestamp_unit {
  226. KERNEL_LOG,
  227. QTIMER,
  228. };
  229. #ifdef QCA_WIFI_3_0_ADRASTEA
  230. #define QDF_LOG_TIMESTAMP_UNIT QTIMER
  231. #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 192
  232. static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
  233. {
  234. /*
  235. * Try to preserve precision by multiplying by 10 first.
  236. * If that would cause a wrap around, divide first instead.
  237. */
  238. if (time * 10 < time) {
  239. do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
  240. return time * 10;
  241. }
  242. time = time * 10;
  243. do_div(time, QDF_LOG_TIMESTAMP_CYCLES_PER_10_US);
  244. return time;
  245. }
  246. #else
  247. #define QDF_LOG_TIMESTAMP_UNIT KERNEL_LOG
  248. #define QDF_LOG_TIMESTAMP_CYCLES_PER_10_US 10
  249. static inline uint64_t qdf_log_timestamp_to_usecs(uint64_t time)
  250. {
  251. /* timestamps are already in micro seconds */
  252. return time;
  253. }
  254. #endif
  255. static inline void qdf_log_timestamp_to_secs(uint64_t time, uint64_t *secs,
  256. uint64_t *usecs)
  257. {
  258. *secs = qdf_log_timestamp_to_usecs(time);
  259. *usecs = do_div(*secs, 1000000ul);
  260. }
  261. static inline uint64_t qdf_usecs_to_log_timestamp(uint64_t usecs)
  262. {
  263. return (usecs * QDF_LOG_TIMESTAMP_CYCLES_PER_10_US) / 10;
  264. }
  265. /**
  266. * qdf_get_log_timestamp - get time stamp for logging
  267. * For adrastea this API returns QTIMER tick which is needed to synchronize
  268. * host and fw log timestamps
  269. * For ROME and other discrete solution this API returns system boot time stamp
  270. *
  271. * Return:
  272. * QTIMER ticks(19.2MHz) for adrastea
  273. * System tick for rome and other future discrete solutions
  274. */
  275. static inline uint64_t qdf_get_log_timestamp(void)
  276. {
  277. return __qdf_get_log_timestamp();
  278. }
  279. /**
  280. * qdf_get_monotonic_boottime - get monotonic kernel boot time
  281. * This API is similar to qdf_get_system_boottime but it includes
  282. * time spent in suspend.
  283. *
  284. * Return: Time in microseconds
  285. */
  286. static inline uint64_t qdf_get_monotonic_boottime(void)
  287. {
  288. return __qdf_get_monotonic_boottime();
  289. }
  290. #endif