Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_TIMEKEEPING_H
0003 #define _LINUX_TIMEKEEPING_H
0004 
0005 #include <linux/errno.h>
0006 #include <linux/clocksource_ids.h>
0007 
0008 /* Included from linux/ktime.h */
0009 
0010 void timekeeping_init(void);
0011 extern int timekeeping_suspended;
0012 
0013 /* Architecture timer tick functions: */
0014 extern void legacy_timer_tick(unsigned long ticks);
0015 
0016 /*
0017  * Get and set timeofday
0018  */
0019 extern int do_settimeofday64(const struct timespec64 *ts);
0020 extern int do_sys_settimeofday64(const struct timespec64 *tv,
0021                  const struct timezone *tz);
0022 
0023 /*
0024  * ktime_get() family: read the current time in a multitude of ways,
0025  *
0026  * The default time reference is CLOCK_MONOTONIC, starting at
0027  * boot time but not counting the time spent in suspend.
0028  * For other references, use the functions with "real", "clocktai",
0029  * "boottime" and "raw" suffixes.
0030  *
0031  * To get the time in a different format, use the ones wit
0032  * "ns", "ts64" and "seconds" suffix.
0033  *
0034  * See Documentation/core-api/timekeeping.rst for more details.
0035  */
0036 
0037 
0038 /*
0039  * timespec64 based interfaces
0040  */
0041 extern void ktime_get_raw_ts64(struct timespec64 *ts);
0042 extern void ktime_get_ts64(struct timespec64 *ts);
0043 extern void ktime_get_real_ts64(struct timespec64 *tv);
0044 extern void ktime_get_coarse_ts64(struct timespec64 *ts);
0045 extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
0046 
0047 void getboottime64(struct timespec64 *ts);
0048 
0049 /*
0050  * time64_t base interfaces
0051  */
0052 extern time64_t ktime_get_seconds(void);
0053 extern time64_t __ktime_get_real_seconds(void);
0054 extern time64_t ktime_get_real_seconds(void);
0055 
0056 /*
0057  * ktime_t based interfaces
0058  */
0059 
0060 enum tk_offsets {
0061     TK_OFFS_REAL,
0062     TK_OFFS_BOOT,
0063     TK_OFFS_TAI,
0064     TK_OFFS_MAX,
0065 };
0066 
0067 extern ktime_t ktime_get(void);
0068 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
0069 extern ktime_t ktime_get_coarse_with_offset(enum tk_offsets offs);
0070 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
0071 extern ktime_t ktime_get_raw(void);
0072 extern u32 ktime_get_resolution_ns(void);
0073 
0074 /**
0075  * ktime_get_real - get the real (wall-) time in ktime_t format
0076  */
0077 static inline ktime_t ktime_get_real(void)
0078 {
0079     return ktime_get_with_offset(TK_OFFS_REAL);
0080 }
0081 
0082 static inline ktime_t ktime_get_coarse_real(void)
0083 {
0084     return ktime_get_coarse_with_offset(TK_OFFS_REAL);
0085 }
0086 
0087 /**
0088  * ktime_get_boottime - Returns monotonic time since boot in ktime_t format
0089  *
0090  * This is similar to CLOCK_MONTONIC/ktime_get, but also includes the
0091  * time spent in suspend.
0092  */
0093 static inline ktime_t ktime_get_boottime(void)
0094 {
0095     return ktime_get_with_offset(TK_OFFS_BOOT);
0096 }
0097 
0098 static inline ktime_t ktime_get_coarse_boottime(void)
0099 {
0100     return ktime_get_coarse_with_offset(TK_OFFS_BOOT);
0101 }
0102 
0103 /**
0104  * ktime_get_clocktai - Returns the TAI time of day in ktime_t format
0105  */
0106 static inline ktime_t ktime_get_clocktai(void)
0107 {
0108     return ktime_get_with_offset(TK_OFFS_TAI);
0109 }
0110 
0111 static inline ktime_t ktime_get_coarse_clocktai(void)
0112 {
0113     return ktime_get_coarse_with_offset(TK_OFFS_TAI);
0114 }
0115 
0116 static inline ktime_t ktime_get_coarse(void)
0117 {
0118     struct timespec64 ts;
0119 
0120     ktime_get_coarse_ts64(&ts);
0121     return timespec64_to_ktime(ts);
0122 }
0123 
0124 static inline u64 ktime_get_coarse_ns(void)
0125 {
0126     return ktime_to_ns(ktime_get_coarse());
0127 }
0128 
0129 static inline u64 ktime_get_coarse_real_ns(void)
0130 {
0131     return ktime_to_ns(ktime_get_coarse_real());
0132 }
0133 
0134 static inline u64 ktime_get_coarse_boottime_ns(void)
0135 {
0136     return ktime_to_ns(ktime_get_coarse_boottime());
0137 }
0138 
0139 static inline u64 ktime_get_coarse_clocktai_ns(void)
0140 {
0141     return ktime_to_ns(ktime_get_coarse_clocktai());
0142 }
0143 
0144 /**
0145  * ktime_mono_to_real - Convert monotonic time to clock realtime
0146  */
0147 static inline ktime_t ktime_mono_to_real(ktime_t mono)
0148 {
0149     return ktime_mono_to_any(mono, TK_OFFS_REAL);
0150 }
0151 
0152 static inline u64 ktime_get_ns(void)
0153 {
0154     return ktime_to_ns(ktime_get());
0155 }
0156 
0157 static inline u64 ktime_get_real_ns(void)
0158 {
0159     return ktime_to_ns(ktime_get_real());
0160 }
0161 
0162 static inline u64 ktime_get_boottime_ns(void)
0163 {
0164     return ktime_to_ns(ktime_get_boottime());
0165 }
0166 
0167 static inline u64 ktime_get_clocktai_ns(void)
0168 {
0169     return ktime_to_ns(ktime_get_clocktai());
0170 }
0171 
0172 static inline u64 ktime_get_raw_ns(void)
0173 {
0174     return ktime_to_ns(ktime_get_raw());
0175 }
0176 
0177 extern u64 ktime_get_mono_fast_ns(void);
0178 extern u64 ktime_get_raw_fast_ns(void);
0179 extern u64 ktime_get_boot_fast_ns(void);
0180 extern u64 ktime_get_tai_fast_ns(void);
0181 extern u64 ktime_get_real_fast_ns(void);
0182 
0183 /*
0184  * timespec64/time64_t interfaces utilizing the ktime based ones
0185  * for API completeness, these could be implemented more efficiently
0186  * if needed.
0187  */
0188 static inline void ktime_get_boottime_ts64(struct timespec64 *ts)
0189 {
0190     *ts = ktime_to_timespec64(ktime_get_boottime());
0191 }
0192 
0193 static inline void ktime_get_coarse_boottime_ts64(struct timespec64 *ts)
0194 {
0195     *ts = ktime_to_timespec64(ktime_get_coarse_boottime());
0196 }
0197 
0198 static inline time64_t ktime_get_boottime_seconds(void)
0199 {
0200     return ktime_divns(ktime_get_coarse_boottime(), NSEC_PER_SEC);
0201 }
0202 
0203 static inline void ktime_get_clocktai_ts64(struct timespec64 *ts)
0204 {
0205     *ts = ktime_to_timespec64(ktime_get_clocktai());
0206 }
0207 
0208 static inline void ktime_get_coarse_clocktai_ts64(struct timespec64 *ts)
0209 {
0210     *ts = ktime_to_timespec64(ktime_get_coarse_clocktai());
0211 }
0212 
0213 static inline time64_t ktime_get_clocktai_seconds(void)
0214 {
0215     return ktime_divns(ktime_get_coarse_clocktai(), NSEC_PER_SEC);
0216 }
0217 
0218 /*
0219  * RTC specific
0220  */
0221 extern bool timekeeping_rtc_skipsuspend(void);
0222 extern bool timekeeping_rtc_skipresume(void);
0223 
0224 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
0225 
0226 /*
0227  * struct ktime_timestanps - Simultaneous mono/boot/real timestamps
0228  * @mono:   Monotonic timestamp
0229  * @boot:   Boottime timestamp
0230  * @real:   Realtime timestamp
0231  */
0232 struct ktime_timestamps {
0233     u64     mono;
0234     u64     boot;
0235     u64     real;
0236 };
0237 
0238 /**
0239  * struct system_time_snapshot - simultaneous raw/real time capture with
0240  *               counter value
0241  * @cycles: Clocksource counter value to produce the system times
0242  * @real:   Realtime system time
0243  * @raw:    Monotonic raw system time
0244  * @clock_was_set_seq:  The sequence number of clock was set events
0245  * @cs_was_changed_seq: The sequence number of clocksource change events
0246  */
0247 struct system_time_snapshot {
0248     u64         cycles;
0249     ktime_t         real;
0250     ktime_t         raw;
0251     enum clocksource_ids    cs_id;
0252     unsigned int        clock_was_set_seq;
0253     u8          cs_was_changed_seq;
0254 };
0255 
0256 /**
0257  * struct system_device_crosststamp - system/device cross-timestamp
0258  *                    (synchronized capture)
0259  * @device:     Device time
0260  * @sys_realtime:   Realtime simultaneous with device time
0261  * @sys_monoraw:    Monotonic raw simultaneous with device time
0262  */
0263 struct system_device_crosststamp {
0264     ktime_t device;
0265     ktime_t sys_realtime;
0266     ktime_t sys_monoraw;
0267 };
0268 
0269 /**
0270  * struct system_counterval_t - system counter value with the pointer to the
0271  *              corresponding clocksource
0272  * @cycles: System counter value
0273  * @cs:     Clocksource corresponding to system counter value. Used by
0274  *      timekeeping code to verify comparibility of two cycle values
0275  */
0276 struct system_counterval_t {
0277     u64         cycles;
0278     struct clocksource  *cs;
0279 };
0280 
0281 /*
0282  * Get cross timestamp between system clock and device clock
0283  */
0284 extern int get_device_system_crosststamp(
0285             int (*get_time_fn)(ktime_t *device_time,
0286                 struct system_counterval_t *system_counterval,
0287                 void *ctx),
0288             void *ctx,
0289             struct system_time_snapshot *history,
0290             struct system_device_crosststamp *xtstamp);
0291 
0292 /*
0293  * Simultaneously snapshot realtime and monotonic raw clocks
0294  */
0295 extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot);
0296 
0297 /* NMI safe mono/boot/realtime timestamps */
0298 extern void ktime_get_fast_timestamps(struct ktime_timestamps *snap);
0299 
0300 /*
0301  * Persistent clock related interfaces
0302  */
0303 extern int persistent_clock_is_local;
0304 
0305 extern void read_persistent_clock64(struct timespec64 *ts);
0306 void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock,
0307                       struct timespec64 *boot_offset);
0308 #ifdef CONFIG_GENERIC_CMOS_UPDATE
0309 extern int update_persistent_clock64(struct timespec64 now);
0310 #endif
0311 
0312 #endif