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/irqchip/mips-gic.h>
15 #include <linux/time.h>
17 #include <asm/clocksource.h>
19 #include <asm/mips-cm.h>
20 #include <asm/unistd.h>
23 #ifdef CONFIG_MIPS_CLOCK_VSYSCALL
25 static __always_inline
long gettimeofday_fallback(struct timeval
*_tv
,
28 register struct timezone
*tz
asm("a1") = _tz
;
29 register struct timeval
*tv
asm("a0") = _tv
;
30 register long ret
asm("v0");
31 register long nr
asm("v0") = __NR_gettimeofday
;
32 register long error
asm("a3");
36 : "=r" (ret
), "=r" (error
)
37 : "r" (tv
), "r" (tz
), "r" (nr
)
38 : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
39 "$14", "$15", "$24", "$25", "hi", "lo", "memory");
41 return error
? -ret
: ret
;
46 static __always_inline
long clock_gettime_fallback(clockid_t _clkid
,
49 register struct timespec
*ts
asm("a1") = _ts
;
50 register clockid_t clkid
asm("a0") = _clkid
;
51 register long ret
asm("v0");
52 register long nr
asm("v0") = __NR_clock_gettime
;
53 register long error
asm("a3");
57 : "=r" (ret
), "=r" (error
)
58 : "r" (clkid
), "r" (ts
), "r" (nr
)
59 : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
60 "$14", "$15", "$24", "$25", "hi", "lo", "memory");
62 return error
? -ret
: ret
;
65 static __always_inline
int do_realtime_coarse(struct timespec
*ts
,
66 const union mips_vdso_data
*data
)
71 start_seq
= vdso_data_read_begin(data
);
73 ts
->tv_sec
= data
->xtime_sec
;
74 ts
->tv_nsec
= data
->xtime_nsec
>> data
->cs_shift
;
75 } while (vdso_data_read_retry(data
, start_seq
));
80 static __always_inline
int do_monotonic_coarse(struct timespec
*ts
,
81 const union mips_vdso_data
*data
)
88 start_seq
= vdso_data_read_begin(data
);
90 ts
->tv_sec
= data
->xtime_sec
;
91 ts
->tv_nsec
= data
->xtime_nsec
>> data
->cs_shift
;
93 to_mono_sec
= data
->wall_to_mono_sec
;
94 to_mono_nsec
= data
->wall_to_mono_nsec
;
95 } while (vdso_data_read_retry(data
, start_seq
));
97 ts
->tv_sec
+= to_mono_sec
;
98 timespec_add_ns(ts
, to_mono_nsec
);
103 #ifdef CONFIG_CSRC_R4K
105 static __always_inline u64
read_r4k_count(void)
109 __asm__
__volatile__(
121 #ifdef CONFIG_CLKSRC_MIPS_GIC
123 static __always_inline u64
read_gic_count(const union mips_vdso_data
*data
)
125 void __iomem
*gic
= get_gic(data
);
129 hi
= __raw_readl(gic
+ GIC_UMV_SH_COUNTER_63_32_OFS
);
130 lo
= __raw_readl(gic
+ GIC_UMV_SH_COUNTER_31_00_OFS
);
131 hi2
= __raw_readl(gic
+ GIC_UMV_SH_COUNTER_63_32_OFS
);
134 return (((u64
)hi
) << 32) + lo
;
139 static __always_inline u64
get_ns(const union mips_vdso_data
*data
)
141 u64 cycle_now
, delta
, nsec
;
143 switch (data
->clock_mode
) {
144 #ifdef CONFIG_CSRC_R4K
146 cycle_now
= read_r4k_count();
149 #ifdef CONFIG_CLKSRC_MIPS_GIC
151 cycle_now
= read_gic_count(data
);
158 delta
= (cycle_now
- data
->cs_cycle_last
) & data
->cs_mask
;
160 nsec
= (delta
* data
->cs_mult
) + data
->xtime_nsec
;
161 nsec
>>= data
->cs_shift
;
166 static __always_inline
int do_realtime(struct timespec
*ts
,
167 const union mips_vdso_data
*data
)
173 start_seq
= vdso_data_read_begin(data
);
175 if (data
->clock_mode
== VDSO_CLOCK_NONE
)
178 ts
->tv_sec
= data
->xtime_sec
;
180 } while (vdso_data_read_retry(data
, start_seq
));
183 timespec_add_ns(ts
, ns
);
188 static __always_inline
int do_monotonic(struct timespec
*ts
,
189 const union mips_vdso_data
*data
)
197 start_seq
= vdso_data_read_begin(data
);
199 if (data
->clock_mode
== VDSO_CLOCK_NONE
)
202 ts
->tv_sec
= data
->xtime_sec
;
205 to_mono_sec
= data
->wall_to_mono_sec
;
206 to_mono_nsec
= data
->wall_to_mono_nsec
;
207 } while (vdso_data_read_retry(data
, start_seq
));
209 ts
->tv_sec
+= to_mono_sec
;
211 timespec_add_ns(ts
, ns
+ to_mono_nsec
);
216 #ifdef CONFIG_MIPS_CLOCK_VSYSCALL
219 * This is behind the ifdef so that we don't provide the symbol when there's no
220 * possibility of there being a usable clocksource, because there's nothing we
221 * can do without it. When libc fails the symbol lookup it should fall back on
222 * the standard syscall path.
224 int __vdso_gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
226 const union mips_vdso_data
*data
= get_vdso_data();
230 ret
= do_realtime(&ts
, data
);
232 return gettimeofday_fallback(tv
, tz
);
235 tv
->tv_sec
= ts
.tv_sec
;
236 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
240 tz
->tz_minuteswest
= data
->tz_minuteswest
;
241 tz
->tz_dsttime
= data
->tz_dsttime
;
247 #endif /* CONFIG_MIPS_CLOCK_VSYSCALL */
249 int __vdso_clock_gettime(clockid_t clkid
, struct timespec
*ts
)
251 const union mips_vdso_data
*data
= get_vdso_data();
255 case CLOCK_REALTIME_COARSE
:
256 ret
= do_realtime_coarse(ts
, data
);
258 case CLOCK_MONOTONIC_COARSE
:
259 ret
= do_monotonic_coarse(ts
, data
);
262 ret
= do_realtime(ts
, data
);
264 case CLOCK_MONOTONIC
:
265 ret
= do_monotonic(ts
, data
);
272 ret
= clock_gettime_fallback(clkid
, ts
);