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