2 * Copyright (C) 2015 Imagination Technologies
3 * Author: Alex Smith <alex.smith@imgtec.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
13 #include <linux/compiler.h>
14 #include <linux/time.h>
16 #include <asm/clocksource.h>
18 #include <asm/unistd.h>
22 #define VDSO_SYSCALL_CLOBBERS "hi", "lo",
24 #define VDSO_SYSCALL_CLOBBERS
27 #ifdef CONFIG_MIPS_CLOCK_VSYSCALL
29 static __always_inline long gettimeofday_fallback(struct timeval *_tv,
32 register struct timezone *tz asm("a1") = _tz;
33 register struct timeval *tv asm("a0") = _tv;
34 register long ret asm("v0");
35 register long nr asm("v0") = __NR_gettimeofday;
36 register long error asm("a3");
40 : "=r" (ret), "=r" (error)
41 : "r" (tv), "r" (tz), "r" (nr)
42 : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
43 "$14", "$15", "$24", "$25",
47 return error ? -ret : ret;
52 static __always_inline long clock_gettime_fallback(clockid_t _clkid,
55 register struct timespec *ts asm("a1") = _ts;
56 register clockid_t clkid asm("a0") = _clkid;
57 register long ret asm("v0");
58 register long nr asm("v0") = __NR_clock_gettime;
59 register long error asm("a3");
63 : "=r" (ret), "=r" (error)
64 : "r" (clkid), "r" (ts), "r" (nr)
65 : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
66 "$14", "$15", "$24", "$25",
70 return error ? -ret : ret;
73 static __always_inline int do_realtime_coarse(struct timespec *ts,
74 const union mips_vdso_data *data)
79 start_seq = vdso_data_read_begin(data);
81 ts->tv_sec = data->xtime_sec;
82 ts->tv_nsec = data->xtime_nsec >> data->cs_shift;
83 } while (vdso_data_read_retry(data, start_seq));
88 static __always_inline int do_monotonic_coarse(struct timespec *ts,
89 const union mips_vdso_data *data)
96 start_seq = vdso_data_read_begin(data);
98 ts->tv_sec = data->xtime_sec;
99 ts->tv_nsec = data->xtime_nsec >> data->cs_shift;
101 to_mono_sec = data->wall_to_mono_sec;
102 to_mono_nsec = data->wall_to_mono_nsec;
103 } while (vdso_data_read_retry(data, start_seq));
105 ts->tv_sec += to_mono_sec;
106 timespec_add_ns(ts, to_mono_nsec);
111 #ifdef CONFIG_CSRC_R4K
113 static __always_inline u64 read_r4k_count(void)
117 __asm__ __volatile__(
129 #ifdef CONFIG_CLKSRC_MIPS_GIC
131 static __always_inline u64 read_gic_count(const union mips_vdso_data *data)
133 void __iomem *gic = get_gic(data);
137 hi = __raw_readl(gic + sizeof(lo));
138 lo = __raw_readl(gic);
139 hi2 = __raw_readl(gic + sizeof(lo));
142 return (((u64)hi) << 32) + lo;
147 static __always_inline u64 get_ns(const union mips_vdso_data *data)
149 u64 cycle_now, delta, nsec;
151 switch (data->clock_mode) {
152 #ifdef CONFIG_CSRC_R4K
154 cycle_now = read_r4k_count();
157 #ifdef CONFIG_CLKSRC_MIPS_GIC
159 cycle_now = read_gic_count(data);
166 delta = (cycle_now - data->cs_cycle_last) & data->cs_mask;
168 nsec = (delta * data->cs_mult) + data->xtime_nsec;
169 nsec >>= data->cs_shift;
174 static __always_inline int do_realtime(struct timespec *ts,
175 const union mips_vdso_data *data)
181 start_seq = vdso_data_read_begin(data);
183 if (data->clock_mode == VDSO_CLOCK_NONE)
186 ts->tv_sec = data->xtime_sec;
188 } while (vdso_data_read_retry(data, start_seq));
191 timespec_add_ns(ts, ns);
196 static __always_inline int do_monotonic(struct timespec *ts,
197 const union mips_vdso_data *data)
205 start_seq = vdso_data_read_begin(data);
207 if (data->clock_mode == VDSO_CLOCK_NONE)
210 ts->tv_sec = data->xtime_sec;
213 to_mono_sec = data->wall_to_mono_sec;
214 to_mono_nsec = data->wall_to_mono_nsec;
215 } while (vdso_data_read_retry(data, start_seq));
217 ts->tv_sec += to_mono_sec;
219 timespec_add_ns(ts, ns + to_mono_nsec);
224 #ifdef CONFIG_MIPS_CLOCK_VSYSCALL
227 * This is behind the ifdef so that we don't provide the symbol when there's no
228 * possibility of there being a usable clocksource, because there's nothing we
229 * can do without it. When libc fails the symbol lookup it should fall back on
230 * the standard syscall path.
232 int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
234 const union mips_vdso_data *data = get_vdso_data();
238 ret = do_realtime(&ts, data);
240 return gettimeofday_fallback(tv, tz);
243 tv->tv_sec = ts.tv_sec;
244 tv->tv_usec = ts.tv_nsec / 1000;
248 tz->tz_minuteswest = data->tz_minuteswest;
249 tz->tz_dsttime = data->tz_dsttime;
255 #endif /* CONFIG_MIPS_CLOCK_VSYSCALL */
257 int __vdso_clock_gettime(clockid_t clkid, struct timespec *ts)
259 const union mips_vdso_data *data = get_vdso_data();
263 case CLOCK_REALTIME_COARSE:
264 ret = do_realtime_coarse(ts, data);
266 case CLOCK_MONOTONIC_COARSE:
267 ret = do_monotonic_coarse(ts, data);
270 ret = do_realtime(ts, data);
272 case CLOCK_MONOTONIC:
273 ret = do_monotonic(ts, data);
280 ret = clock_gettime_fallback(clkid, ts);