| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * You SHOULD NOT be including this unless you're vsyscall |
| 4 | * handling code or timekeeping internal code! |
| 5 | */ |
| 6 | |
| 7 | #ifndef _LINUX_TIMEKEEPER_INTERNAL_H |
| 8 | #define _LINUX_TIMEKEEPER_INTERNAL_H |
| 9 | |
| 10 | #include <linux/clocksource.h> |
| 11 | #include <linux/jiffies.h> |
| 12 | #include <linux/time.h> |
| 13 | |
| 14 | /** |
| 15 | * timekeeper_ids - IDs for various time keepers in the kernel |
| 16 | * @TIMEKEEPER_CORE: The central core timekeeper managing system time |
| 17 | * @TIMEKEEPER_AUX_FIRST: The first AUX timekeeper |
| 18 | * @TIMEKEEPER_AUX_LAST: The last AUX timekeeper |
| 19 | * @TIMEKEEPERS_MAX: The maximum number of timekeepers managed |
| 20 | */ |
| 21 | enum timekeeper_ids { |
| 22 | TIMEKEEPER_CORE, |
| 23 | #ifdef CONFIG_POSIX_AUX_CLOCKS |
| 24 | TIMEKEEPER_AUX_FIRST, |
| 25 | TIMEKEEPER_AUX_LAST = TIMEKEEPER_AUX_FIRST + MAX_AUX_CLOCKS - 1, |
| 26 | #endif |
| 27 | TIMEKEEPERS_MAX, |
| 28 | }; |
| 29 | |
| 30 | /** |
| 31 | * struct tk_read_base - base structure for timekeeping readout |
| 32 | * @clock: Current clocksource used for timekeeping. |
| 33 | * @mask: Bitmask for two's complement subtraction of non 64bit clocks |
| 34 | * @cycle_last: @clock cycle value at last update |
| 35 | * @mult: (NTP adjusted) multiplier for scaled math conversion |
| 36 | * @shift: Shift value for scaled math conversion |
| 37 | * @xtime_nsec: Shifted (fractional) nano seconds offset for readout |
| 38 | * @base: ktime_t (nanoseconds) base time for readout |
| 39 | * @base_real: Nanoseconds base value for clock REALTIME readout |
| 40 | * |
| 41 | * This struct has size 56 byte on 64 bit. Together with a seqcount it |
| 42 | * occupies a single 64byte cache line. |
| 43 | * |
| 44 | * The struct is separate from struct timekeeper as it is also used |
| 45 | * for the fast NMI safe accessors. |
| 46 | * |
| 47 | * @base_real is for the fast NMI safe accessor to allow reading clock |
| 48 | * realtime from any context. |
| 49 | */ |
| 50 | struct tk_read_base { |
| 51 | struct clocksource *clock; |
| 52 | u64 mask; |
| 53 | u64 cycle_last; |
| 54 | u32 mult; |
| 55 | u32 shift; |
| 56 | u64 xtime_nsec; |
| 57 | ktime_t base; |
| 58 | u64 base_real; |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * struct timekeeper - Structure holding internal timekeeping values. |
| 63 | * @tkr_mono: The readout base structure for CLOCK_MONOTONIC |
| 64 | * @xtime_sec: Current CLOCK_REALTIME time in seconds |
| 65 | * @ktime_sec: Current CLOCK_MONOTONIC time in seconds |
| 66 | * @wall_to_monotonic: CLOCK_REALTIME to CLOCK_MONOTONIC offset |
| 67 | * @offs_real: Offset clock monotonic -> clock realtime |
| 68 | * @offs_boot: Offset clock monotonic -> clock boottime |
| 69 | * @offs_tai: Offset clock monotonic -> clock tai |
| 70 | * @offs_aux: Offset clock monotonic -> clock AUX |
| 71 | * @coarse_nsec: The nanoseconds part for coarse time getters |
| 72 | * @id: The timekeeper ID |
| 73 | * @tkr_raw: The readout base structure for CLOCK_MONOTONIC_RAW |
| 74 | * @raw_sec: CLOCK_MONOTONIC_RAW time in seconds |
| 75 | * @clock_was_set_seq: The sequence number of clock was set events |
| 76 | * @cs_was_changed_seq: The sequence number of clocksource change events |
| 77 | * @clock_valid: Indicator for valid clock |
| 78 | * @monotonic_to_boot: CLOCK_MONOTONIC to CLOCK_BOOTTIME offset |
| 79 | * @monotonic_to_aux: CLOCK_MONOTONIC to CLOCK_AUX offset |
| 80 | * @cycle_interval: Number of clock cycles in one NTP interval |
| 81 | * @xtime_interval: Number of clock shifted nano seconds in one NTP |
| 82 | * interval. |
| 83 | * @xtime_remainder: Shifted nano seconds left over when rounding |
| 84 | * @cycle_interval |
| 85 | * @raw_interval: Shifted raw nano seconds accumulated per NTP interval. |
| 86 | * @next_leap_ktime: CLOCK_MONOTONIC time value of a pending leap-second |
| 87 | * @ntp_tick: The ntp_tick_length() value currently being |
| 88 | * used. This cached copy ensures we consistently |
| 89 | * apply the tick length for an entire tick, as |
| 90 | * ntp_tick_length may change mid-tick, and we don't |
| 91 | * want to apply that new value to the tick in |
| 92 | * progress. |
| 93 | * @ntp_error: Difference between accumulated time and NTP time in ntp |
| 94 | * shifted nano seconds. |
| 95 | * @ntp_error_shift: Shift conversion between clock shifted nano seconds and |
| 96 | * ntp shifted nano seconds. |
| 97 | * @ntp_err_mult: Multiplication factor for scaled math conversion |
| 98 | * @skip_second_overflow: Flag used to avoid updating NTP twice with same second |
| 99 | * @tai_offset: The current UTC to TAI offset in seconds |
| 100 | * |
| 101 | * Note: For timespec(64) based interfaces wall_to_monotonic is what |
| 102 | * we need to add to xtime (or xtime corrected for sub jiffy times) |
| 103 | * to get to monotonic time. Monotonic is pegged at zero at system |
| 104 | * boot time, so wall_to_monotonic will be negative, however, we will |
| 105 | * ALWAYS keep the tv_nsec part positive so we can use the usual |
| 106 | * normalization. |
| 107 | * |
| 108 | * wall_to_monotonic is moved after resume from suspend for the |
| 109 | * monotonic time not to jump. We need to add total_sleep_time to |
| 110 | * wall_to_monotonic to get the real boot based time offset. |
| 111 | * |
| 112 | * wall_to_monotonic is no longer the boot time, getboottime must be |
| 113 | * used instead. |
| 114 | * |
| 115 | * @monotonic_to_boottime is a timespec64 representation of @offs_boot to |
| 116 | * accelerate the VDSO update for CLOCK_BOOTTIME. |
| 117 | * |
| 118 | * @offs_aux is used by the auxiliary timekeepers which do not utilize any |
| 119 | * of the regular timekeeper offset fields. |
| 120 | * |
| 121 | * @monotonic_to_aux is a timespec64 representation of @offs_aux to |
| 122 | * accelerate the VDSO update for CLOCK_AUX. |
| 123 | * |
| 124 | * The cacheline ordering of the structure is optimized for in kernel usage of |
| 125 | * the ktime_get() and ktime_get_ts64() family of time accessors. Struct |
| 126 | * timekeeper is prepended in the core timekeeping code with a sequence count, |
| 127 | * which results in the following cacheline layout: |
| 128 | * |
| 129 | * 0: seqcount, tkr_mono |
| 130 | * 1: xtime_sec ... id |
| 131 | * 2: tkr_raw, raw_sec |
| 132 | * 3,4: Internal variables |
| 133 | * |
| 134 | * Cacheline 0,1 contain the data which is used for accessing |
| 135 | * CLOCK_MONOTONIC/REALTIME/BOOTTIME/TAI, while cacheline 2 contains the |
| 136 | * data for accessing CLOCK_MONOTONIC_RAW. Cacheline 3,4 are internal |
| 137 | * variables which are only accessed during timekeeper updates once per |
| 138 | * tick. |
| 139 | */ |
| 140 | struct timekeeper { |
| 141 | /* Cacheline 0 (together with prepended seqcount of timekeeper core): */ |
| 142 | struct tk_read_base tkr_mono; |
| 143 | |
| 144 | /* Cacheline 1: */ |
| 145 | u64 xtime_sec; |
| 146 | unsigned long ktime_sec; |
| 147 | struct timespec64 wall_to_monotonic; |
| 148 | ktime_t offs_real; |
| 149 | ktime_t offs_boot; |
| 150 | union { |
| 151 | ktime_t offs_tai; |
| 152 | ktime_t offs_aux; |
| 153 | }; |
| 154 | u32 coarse_nsec; |
| 155 | enum timekeeper_ids id; |
| 156 | |
| 157 | /* Cacheline 2: */ |
| 158 | struct tk_read_base tkr_raw; |
| 159 | u64 raw_sec; |
| 160 | |
| 161 | /* Cachline 3 and 4 (timekeeping internal variables): */ |
| 162 | unsigned int clock_was_set_seq; |
| 163 | u8 cs_was_changed_seq; |
| 164 | u8 clock_valid; |
| 165 | |
| 166 | union { |
| 167 | struct timespec64 monotonic_to_boot; |
| 168 | struct timespec64 monotonic_to_aux; |
| 169 | }; |
| 170 | |
| 171 | u64 cycle_interval; |
| 172 | u64 xtime_interval; |
| 173 | s64 xtime_remainder; |
| 174 | u64 raw_interval; |
| 175 | |
| 176 | ktime_t next_leap_ktime; |
| 177 | u64 ntp_tick; |
| 178 | s64 ntp_error; |
| 179 | u32 ntp_error_shift; |
| 180 | u32 ntp_err_mult; |
| 181 | u32 skip_second_overflow; |
| 182 | s32 tai_offset; |
| 183 | }; |
| 184 | |
| 185 | #ifdef CONFIG_GENERIC_TIME_VSYSCALL |
| 186 | |
| 187 | extern void update_vsyscall(struct timekeeper *tk); |
| 188 | extern void update_vsyscall_tz(void); |
| 189 | |
| 190 | #else |
| 191 | |
| 192 | static inline void update_vsyscall(struct timekeeper *tk) |
| 193 | { |
| 194 | } |
| 195 | static inline void update_vsyscall_tz(void) |
| 196 | { |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | #if defined(CONFIG_GENERIC_GETTIMEOFDAY) && defined(CONFIG_POSIX_AUX_CLOCKS) |
| 201 | extern void vdso_time_update_aux(struct timekeeper *tk); |
| 202 | #else |
| 203 | static inline void vdso_time_update_aux(struct timekeeper *tk) { } |
| 204 | #endif |
| 205 | |
| 206 | #endif /* _LINUX_TIMEKEEPER_INTERNAL_H */ |
| 207 | |