qdf_time.h 8.0 KB

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