Commit | Line | Data |
---|---|---|
8512287a | 1 | /* |
ba88f57f | 2 | * Userspace implementations of gettimeofday() and friends. |
8512287a | 3 | * |
ba88f57f MS |
4 | * Copyright (C) 2017 Cavium, Inc. |
5 | * Copyright (C) 2015 Mentor Graphics Corporation | |
6 | * Copyright (C) 2012 ARM Limited | |
8512287a | 7 | * |
ba88f57f MS |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
8512287a NL |
16 | * |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
ba88f57f MS |
19 | * |
20 | * Author: Will Deacon <will.deacon@arm.com> | |
21 | * Rewriten from arch64 version into C by: Andrew Pinski <apinski@cavium.com> | |
22 | * Reworked and rebased over arm version by: Mark Salyzyn <salyzyn@android.com> | |
8512287a NL |
23 | */ |
24 | ||
8512287a | 25 | #include <asm/barrier.h> |
a866f568 | 26 | #include <linux/compiler.h> /* for notrace */ |
f21c33d6 MS |
27 | #include <linux/math64.h> /* for __iter_div_u64_rem() */ |
28 | #include <uapi/linux/time.h> /* for struct timespec */ | |
8512287a | 29 | |
a866f568 | 30 | #include "compiler.h" |
5441a8aa | 31 | #include "datapage.h" |
8512287a | 32 | |
a866f568 MS |
33 | DEFINE_FALLBACK(gettimeofday, struct timeval *, tv, struct timezone *, tz) |
34 | DEFINE_FALLBACK(clock_gettime, clockid_t, clock, struct timespec *, ts) | |
35 | ||
5441a8aa | 36 | static notrace u32 vdso_read_begin(const struct vdso_data *vd) |
8512287a NL |
37 | { |
38 | u32 seq; | |
39 | ||
ba88f57f | 40 | do { |
5441a8aa | 41 | seq = READ_ONCE(vd->tb_seq_count); |
ba88f57f MS |
42 | |
43 | if ((seq & 1) == 0) | |
44 | break; | |
8512287a | 45 | |
ba88f57f MS |
46 | cpu_relax(); |
47 | } while (true); | |
48 | ||
49 | smp_rmb(); /* Pairs with second smp_wmb in update_vsyscall */ | |
8512287a NL |
50 | return seq; |
51 | } | |
52 | ||
5441a8aa | 53 | static notrace int vdso_read_retry(const struct vdso_data *vd, u32 start) |
8512287a | 54 | { |
ba88f57f MS |
55 | u32 seq; |
56 | ||
57 | smp_rmb(); /* Pairs with first smp_wmb in update_vsyscall */ | |
5441a8aa | 58 | seq = READ_ONCE(vd->tb_seq_count); |
ba88f57f | 59 | return seq != start; |
8512287a NL |
60 | } |
61 | ||
5441a8aa MS |
62 | static notrace int do_realtime_coarse(const struct vdso_data *vd, |
63 | struct timespec *ts) | |
8512287a NL |
64 | { |
65 | u32 seq; | |
66 | ||
67 | do { | |
5441a8aa | 68 | seq = vdso_read_begin(vd); |
8512287a | 69 | |
5441a8aa MS |
70 | ts->tv_sec = vd->xtime_coarse_sec; |
71 | ts->tv_nsec = vd->xtime_coarse_nsec; | |
8512287a | 72 | |
5441a8aa | 73 | } while (vdso_read_retry(vd, seq)); |
8512287a NL |
74 | |
75 | return 0; | |
76 | } | |
77 | ||
5441a8aa MS |
78 | static notrace int do_monotonic_coarse(const struct vdso_data *vd, |
79 | struct timespec *ts) | |
8512287a NL |
80 | { |
81 | struct timespec tomono; | |
82 | u32 seq; | |
f21c33d6 | 83 | u64 nsec; |
8512287a NL |
84 | |
85 | do { | |
5441a8aa | 86 | seq = vdso_read_begin(vd); |
8512287a | 87 | |
5441a8aa MS |
88 | ts->tv_sec = vd->xtime_coarse_sec; |
89 | ts->tv_nsec = vd->xtime_coarse_nsec; | |
8512287a | 90 | |
5441a8aa MS |
91 | tomono.tv_sec = vd->wtm_clock_sec; |
92 | tomono.tv_nsec = vd->wtm_clock_nsec; | |
8512287a | 93 | |
5441a8aa | 94 | } while (vdso_read_retry(vd, seq)); |
8512287a NL |
95 | |
96 | ts->tv_sec += tomono.tv_sec; | |
f21c33d6 MS |
97 | /* open coding timespec_add_ns */ |
98 | ts->tv_sec += __iter_div_u64_rem(ts->tv_nsec + tomono.tv_nsec, | |
99 | NSEC_PER_SEC, &nsec); | |
100 | ts->tv_nsec = nsec; | |
8512287a NL |
101 | |
102 | return 0; | |
103 | } | |
104 | ||
105 | #ifdef CONFIG_ARM_ARCH_TIMER | |
106 | ||
f21c33d6 MS |
107 | /* |
108 | * Returns the clock delta, in nanoseconds left-shifted by the clock | |
109 | * shift. | |
110 | */ | |
111 | static notrace u64 get_clock_shifted_nsec(const u64 cycle_last, | |
112 | const u32 mult, | |
113 | const u64 mask) | |
8512287a | 114 | { |
f21c33d6 | 115 | u64 res; |
8512287a | 116 | |
f21c33d6 MS |
117 | /* Read the virtual counter. */ |
118 | res = arch_vdso_read_counter(); | |
8512287a | 119 | |
f21c33d6 | 120 | res = res - cycle_last; |
8512287a | 121 | |
f21c33d6 MS |
122 | res &= mask; |
123 | return res * mult; | |
8512287a NL |
124 | } |
125 | ||
5441a8aa | 126 | static notrace int do_realtime(const struct vdso_data *vd, struct timespec *ts) |
8512287a | 127 | { |
f21c33d6 MS |
128 | u32 seq, mult, shift; |
129 | u64 nsec, cycle_last; | |
130 | u64 mask; | |
131 | vdso_xtime_clock_sec_t sec; | |
8512287a NL |
132 | |
133 | do { | |
5441a8aa | 134 | seq = vdso_read_begin(vd); |
8512287a | 135 | |
5441a8aa | 136 | if (vd->use_syscall) |
8512287a NL |
137 | return -1; |
138 | ||
f21c33d6 | 139 | cycle_last = vd->cs_cycle_last; |
8512287a | 140 | |
f21c33d6 MS |
141 | mult = vd->cs_mono_mult; |
142 | shift = vd->cs_shift; | |
143 | mask = vd->cs_mask; | |
144 | ||
145 | sec = vd->xtime_clock_sec; | |
146 | nsec = vd->xtime_clock_snsec; | |
147 | ||
148 | } while (unlikely(vdso_read_retry(vd, seq))); | |
8512287a | 149 | |
f21c33d6 MS |
150 | nsec += get_clock_shifted_nsec(cycle_last, mult, mask); |
151 | nsec >>= shift; | |
152 | /* open coding timespec_add_ns to save a ts->tv_nsec = 0 */ | |
153 | ts->tv_sec = sec + __iter_div_u64_rem(nsec, NSEC_PER_SEC, &nsec); | |
154 | ts->tv_nsec = nsec; | |
8512287a NL |
155 | |
156 | return 0; | |
157 | } | |
158 | ||
5441a8aa | 159 | static notrace int do_monotonic(const struct vdso_data *vd, struct timespec *ts) |
8512287a | 160 | { |
f21c33d6 MS |
161 | u32 seq, mult, shift; |
162 | u64 nsec, cycle_last; | |
163 | u64 mask; | |
164 | vdso_wtm_clock_nsec_t wtm_nsec; | |
165 | __kernel_time_t sec; | |
8512287a NL |
166 | |
167 | do { | |
5441a8aa | 168 | seq = vdso_read_begin(vd); |
8512287a | 169 | |
5441a8aa | 170 | if (vd->use_syscall) |
8512287a NL |
171 | return -1; |
172 | ||
f21c33d6 | 173 | cycle_last = vd->cs_cycle_last; |
8512287a | 174 | |
f21c33d6 MS |
175 | mult = vd->cs_mono_mult; |
176 | shift = vd->cs_shift; | |
177 | mask = vd->cs_mask; | |
8512287a | 178 | |
f21c33d6 MS |
179 | sec = vd->xtime_clock_sec; |
180 | nsec = vd->xtime_clock_snsec; | |
8512287a | 181 | |
f21c33d6 MS |
182 | sec += vd->wtm_clock_sec; |
183 | wtm_nsec = vd->wtm_clock_nsec; | |
184 | ||
185 | } while (unlikely(vdso_read_retry(vd, seq))); | |
186 | ||
187 | nsec += get_clock_shifted_nsec(cycle_last, mult, mask); | |
188 | nsec >>= shift; | |
189 | nsec += wtm_nsec; | |
190 | /* open coding timespec_add_ns to save a ts->tv_nsec = 0 */ | |
191 | ts->tv_sec = sec + __iter_div_u64_rem(nsec, NSEC_PER_SEC, &nsec); | |
192 | ts->tv_nsec = nsec; | |
8512287a NL |
193 | |
194 | return 0; | |
195 | } | |
196 | ||
197 | #else /* CONFIG_ARM_ARCH_TIMER */ | |
198 | ||
5441a8aa | 199 | static notrace int do_realtime(const struct vdso_data *vd, struct timespec *ts) |
8512287a NL |
200 | { |
201 | return -1; | |
202 | } | |
203 | ||
5441a8aa | 204 | static notrace int do_monotonic(const struct vdso_data *vd, struct timespec *ts) |
8512287a NL |
205 | { |
206 | return -1; | |
207 | } | |
208 | ||
209 | #endif /* CONFIG_ARM_ARCH_TIMER */ | |
210 | ||
a866f568 | 211 | notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) |
8512287a | 212 | { |
5441a8aa | 213 | const struct vdso_data *vd = __get_datapage(); |
8512287a | 214 | |
a866f568 | 215 | switch (clock) { |
8512287a | 216 | case CLOCK_REALTIME_COARSE: |
a866f568 | 217 | do_realtime_coarse(vd, ts); |
8512287a NL |
218 | break; |
219 | case CLOCK_MONOTONIC_COARSE: | |
a866f568 | 220 | do_monotonic_coarse(vd, ts); |
8512287a NL |
221 | break; |
222 | case CLOCK_REALTIME: | |
a866f568 MS |
223 | if (do_realtime(vd, ts)) |
224 | goto fallback; | |
8512287a NL |
225 | break; |
226 | case CLOCK_MONOTONIC: | |
a866f568 MS |
227 | if (do_monotonic(vd, ts)) |
228 | goto fallback; | |
8512287a NL |
229 | break; |
230 | default: | |
a866f568 | 231 | goto fallback; |
8512287a NL |
232 | } |
233 | ||
a866f568 MS |
234 | return 0; |
235 | fallback: | |
236 | return clock_gettime_fallback(clock, ts); | |
8512287a NL |
237 | } |
238 | ||
239 | notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz) | |
240 | { | |
5441a8aa | 241 | const struct vdso_data *vd = __get_datapage(); |
8512287a | 242 | |
a866f568 MS |
243 | if (likely(tv != NULL)) { |
244 | struct timespec ts; | |
245 | ||
246 | if (do_realtime(vd, &ts)) | |
247 | return gettimeofday_fallback(tv, tz); | |
8512287a | 248 | |
8512287a NL |
249 | tv->tv_sec = ts.tv_sec; |
250 | tv->tv_usec = ts.tv_nsec / 1000; | |
251 | } | |
a866f568 MS |
252 | |
253 | if (unlikely(tz != NULL)) { | |
5441a8aa MS |
254 | tz->tz_minuteswest = vd->tz_minuteswest; |
255 | tz->tz_dsttime = vd->tz_dsttime; | |
8512287a NL |
256 | } |
257 | ||
a866f568 | 258 | return 0; |
8512287a | 259 | } |