1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2015 Mentor Graphics Corporation.
6 #include <linux/compiler.h>
7 #include <linux/hrtimer.h>
8 #include <linux/time.h>
9 #include <asm/barrier.h>
13 #include <asm/unistd.h>
14 #include <asm/vdso_datapage.h>
17 #error This code depends on AEABI system call conventions
20 extern struct vdso_data *__get_datapage(void);
22 static notrace u32 __vdso_read_begin(const struct vdso_data *vdata)
26 seq = READ_ONCE(vdata->seq_count);
34 static notrace u32 vdso_read_begin(const struct vdso_data *vdata)
38 seq = __vdso_read_begin(vdata);
40 smp_rmb(); /* Pairs with smp_wmb in vdso_write_end */
44 static notrace int vdso_read_retry(const struct vdso_data *vdata, u32 start)
46 smp_rmb(); /* Pairs with smp_wmb in vdso_write_begin */
47 return vdata->seq_count != start;
50 static notrace long clock_gettime_fallback(clockid_t _clkid,
53 register struct timespec *ts asm("r1") = _ts;
54 register clockid_t clkid asm("r0") = _clkid;
55 register long ret asm ("r0");
56 register long nr asm("r7") = __NR_clock_gettime;
61 : "r" (clkid), "r" (ts), "r" (nr)
67 static notrace int do_realtime_coarse(struct timespec *ts,
68 struct vdso_data *vdata)
73 seq = vdso_read_begin(vdata);
75 ts->tv_sec = vdata->xtime_coarse_sec;
76 ts->tv_nsec = vdata->xtime_coarse_nsec;
78 } while (vdso_read_retry(vdata, seq));
83 static notrace int do_monotonic_coarse(struct timespec *ts,
84 struct vdso_data *vdata)
86 struct timespec tomono;
90 seq = vdso_read_begin(vdata);
92 ts->tv_sec = vdata->xtime_coarse_sec;
93 ts->tv_nsec = vdata->xtime_coarse_nsec;
95 tomono.tv_sec = vdata->wtm_clock_sec;
96 tomono.tv_nsec = vdata->wtm_clock_nsec;
98 } while (vdso_read_retry(vdata, seq));
100 ts->tv_sec += tomono.tv_sec;
101 timespec_add_ns(ts, tomono.tv_nsec);
106 #ifdef CONFIG_ARM_ARCH_TIMER
108 static notrace u64 get_ns(struct vdso_data *vdata)
115 cycle_now = read_sysreg(CNTVCT);
117 cycle_delta = (cycle_now - vdata->cs_cycle_last) & vdata->cs_mask;
119 nsec = (cycle_delta * vdata->cs_mult) + vdata->xtime_clock_snsec;
120 nsec >>= vdata->cs_shift;
125 static notrace int do_realtime(struct timespec *ts, struct vdso_data *vdata)
131 seq = vdso_read_begin(vdata);
133 if (!vdata->tk_is_cntvct)
136 ts->tv_sec = vdata->xtime_clock_sec;
137 nsecs = get_ns(vdata);
139 } while (vdso_read_retry(vdata, seq));
142 timespec_add_ns(ts, nsecs);
147 static notrace int do_monotonic(struct timespec *ts, struct vdso_data *vdata)
149 struct timespec tomono;
154 seq = vdso_read_begin(vdata);
156 if (!vdata->tk_is_cntvct)
159 ts->tv_sec = vdata->xtime_clock_sec;
160 nsecs = get_ns(vdata);
162 tomono.tv_sec = vdata->wtm_clock_sec;
163 tomono.tv_nsec = vdata->wtm_clock_nsec;
165 } while (vdso_read_retry(vdata, seq));
167 ts->tv_sec += tomono.tv_sec;
169 timespec_add_ns(ts, nsecs + tomono.tv_nsec);
174 #else /* CONFIG_ARM_ARCH_TIMER */
176 static notrace int do_realtime(struct timespec *ts, struct vdso_data *vdata)
181 static notrace int do_monotonic(struct timespec *ts, struct vdso_data *vdata)
186 #endif /* CONFIG_ARM_ARCH_TIMER */
188 notrace int __vdso_clock_gettime(clockid_t clkid, struct timespec *ts)
190 struct vdso_data *vdata;
193 vdata = __get_datapage();
196 case CLOCK_REALTIME_COARSE:
197 ret = do_realtime_coarse(ts, vdata);
199 case CLOCK_MONOTONIC_COARSE:
200 ret = do_monotonic_coarse(ts, vdata);
203 ret = do_realtime(ts, vdata);
205 case CLOCK_MONOTONIC:
206 ret = do_monotonic(ts, vdata);
213 ret = clock_gettime_fallback(clkid, ts);
218 static notrace long gettimeofday_fallback(struct timeval *_tv,
219 struct timezone *_tz)
221 register struct timezone *tz asm("r1") = _tz;
222 register struct timeval *tv asm("r0") = _tv;
223 register long ret asm ("r0");
224 register long nr asm("r7") = __NR_gettimeofday;
229 : "r" (tv), "r" (tz), "r" (nr)
235 notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
238 struct vdso_data *vdata;
241 vdata = __get_datapage();
243 ret = do_realtime(&ts, vdata);
245 return gettimeofday_fallback(tv, tz);
248 tv->tv_sec = ts.tv_sec;
249 tv->tv_usec = ts.tv_nsec / 1000;
252 tz->tz_minuteswest = vdata->tz_minuteswest;
253 tz->tz_dsttime = vdata->tz_dsttime;
259 /* Avoid unresolved references emitted by GCC */
261 void __aeabi_unwind_cpp_pr0(void)
265 void __aeabi_unwind_cpp_pr1(void)
269 void __aeabi_unwind_cpp_pr2(void)