Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * You SHOULD NOT be including this unless you're vsyscall
0004  * handling code or timekeeping internal code!
0005  */
0006 
0007 #ifndef _LINUX_TIMEKEEPER_INTERNAL_H
0008 #define _LINUX_TIMEKEEPER_INTERNAL_H
0009 
0010 #include <linux/clocksource.h>
0011 #include <linux/jiffies.h>
0012 #include <linux/time.h>
0013 
0014 /**
0015  * struct tk_read_base - base structure for timekeeping readout
0016  * @clock:  Current clocksource used for timekeeping.
0017  * @mask:   Bitmask for two's complement subtraction of non 64bit clocks
0018  * @cycle_last: @clock cycle value at last update
0019  * @mult:   (NTP adjusted) multiplier for scaled math conversion
0020  * @shift:  Shift value for scaled math conversion
0021  * @xtime_nsec: Shifted (fractional) nano seconds offset for readout
0022  * @base:   ktime_t (nanoseconds) base time for readout
0023  * @base_real:  Nanoseconds base value for clock REALTIME readout
0024  *
0025  * This struct has size 56 byte on 64 bit. Together with a seqcount it
0026  * occupies a single 64byte cache line.
0027  *
0028  * The struct is separate from struct timekeeper as it is also used
0029  * for a fast NMI safe accessors.
0030  *
0031  * @base_real is for the fast NMI safe accessor to allow reading clock
0032  * realtime from any context.
0033  */
0034 struct tk_read_base {
0035     struct clocksource  *clock;
0036     u64         mask;
0037     u64         cycle_last;
0038     u32         mult;
0039     u32         shift;
0040     u64         xtime_nsec;
0041     ktime_t         base;
0042     u64         base_real;
0043 };
0044 
0045 /**
0046  * struct timekeeper - Structure holding internal timekeeping values.
0047  * @tkr_mono:       The readout base structure for CLOCK_MONOTONIC
0048  * @tkr_raw:        The readout base structure for CLOCK_MONOTONIC_RAW
0049  * @xtime_sec:      Current CLOCK_REALTIME time in seconds
0050  * @ktime_sec:      Current CLOCK_MONOTONIC time in seconds
0051  * @wall_to_monotonic:  CLOCK_REALTIME to CLOCK_MONOTONIC offset
0052  * @offs_real:      Offset clock monotonic -> clock realtime
0053  * @offs_boot:      Offset clock monotonic -> clock boottime
0054  * @offs_tai:       Offset clock monotonic -> clock tai
0055  * @tai_offset:     The current UTC to TAI offset in seconds
0056  * @clock_was_set_seq:  The sequence number of clock was set events
0057  * @cs_was_changed_seq: The sequence number of clocksource change events
0058  * @next_leap_ktime:    CLOCK_MONOTONIC time value of a pending leap-second
0059  * @raw_sec:        CLOCK_MONOTONIC_RAW  time in seconds
0060  * @monotonic_to_boot:  CLOCK_MONOTONIC to CLOCK_BOOTTIME offset
0061  * @cycle_interval: Number of clock cycles in one NTP interval
0062  * @xtime_interval: Number of clock shifted nano seconds in one NTP
0063  *          interval.
0064  * @xtime_remainder:    Shifted nano seconds left over when rounding
0065  *          @cycle_interval
0066  * @raw_interval:   Shifted raw nano seconds accumulated per NTP interval.
0067  * @ntp_error:      Difference between accumulated time and NTP time in ntp
0068  *          shifted nano seconds.
0069  * @ntp_error_shift:    Shift conversion between clock shifted nano seconds and
0070  *          ntp shifted nano seconds.
0071  * @last_warning:   Warning ratelimiter (DEBUG_TIMEKEEPING)
0072  * @underflow_seen: Underflow warning flag (DEBUG_TIMEKEEPING)
0073  * @overflow_seen:  Overflow warning flag (DEBUG_TIMEKEEPING)
0074  *
0075  * Note: For timespec(64) based interfaces wall_to_monotonic is what
0076  * we need to add to xtime (or xtime corrected for sub jiffie times)
0077  * to get to monotonic time.  Monotonic is pegged at zero at system
0078  * boot time, so wall_to_monotonic will be negative, however, we will
0079  * ALWAYS keep the tv_nsec part positive so we can use the usual
0080  * normalization.
0081  *
0082  * wall_to_monotonic is moved after resume from suspend for the
0083  * monotonic time not to jump. We need to add total_sleep_time to
0084  * wall_to_monotonic to get the real boot based time offset.
0085  *
0086  * wall_to_monotonic is no longer the boot time, getboottime must be
0087  * used instead.
0088  *
0089  * @monotonic_to_boottime is a timespec64 representation of @offs_boot to
0090  * accelerate the VDSO update for CLOCK_BOOTTIME.
0091  */
0092 struct timekeeper {
0093     struct tk_read_base tkr_mono;
0094     struct tk_read_base tkr_raw;
0095     u64         xtime_sec;
0096     unsigned long       ktime_sec;
0097     struct timespec64   wall_to_monotonic;
0098     ktime_t         offs_real;
0099     ktime_t         offs_boot;
0100     ktime_t         offs_tai;
0101     s32         tai_offset;
0102     unsigned int        clock_was_set_seq;
0103     u8          cs_was_changed_seq;
0104     ktime_t         next_leap_ktime;
0105     u64         raw_sec;
0106     struct timespec64   monotonic_to_boot;
0107 
0108     /* The following members are for timekeeping internal use */
0109     u64         cycle_interval;
0110     u64         xtime_interval;
0111     s64         xtime_remainder;
0112     u64         raw_interval;
0113     /* The ntp_tick_length() value currently being used.
0114      * This cached copy ensures we consistently apply the tick
0115      * length for an entire tick, as ntp_tick_length may change
0116      * mid-tick, and we don't want to apply that new value to
0117      * the tick in progress.
0118      */
0119     u64         ntp_tick;
0120     /* Difference between accumulated time and NTP time in ntp
0121      * shifted nano seconds. */
0122     s64         ntp_error;
0123     u32         ntp_error_shift;
0124     u32         ntp_err_mult;
0125     /* Flag used to avoid updating NTP twice with same second */
0126     u32         skip_second_overflow;
0127 #ifdef CONFIG_DEBUG_TIMEKEEPING
0128     long            last_warning;
0129     /*
0130      * These simple flag variables are managed
0131      * without locks, which is racy, but they are
0132      * ok since we don't really care about being
0133      * super precise about how many events were
0134      * seen, just that a problem was observed.
0135      */
0136     int         underflow_seen;
0137     int         overflow_seen;
0138 #endif
0139 };
0140 
0141 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
0142 
0143 extern void update_vsyscall(struct timekeeper *tk);
0144 extern void update_vsyscall_tz(void);
0145 
0146 #else
0147 
0148 static inline void update_vsyscall(struct timekeeper *tk)
0149 {
0150 }
0151 static inline void update_vsyscall_tz(void)
0152 {
0153 }
0154 #endif
0155 
0156 #endif /* _LINUX_TIMEKEEPER_INTERNAL_H */