1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_VDSO_GETTIMEOFDAY_H
3 #define __ASM_VDSO_GETTIMEOFDAY_H
4
5 #ifndef __ASSEMBLY__
6
7 #include <asm/barrier.h>
8 #include <asm/unistd.h>
9 #include <asm/csr.h>
10 #include <uapi/linux/time.h>
11
12 /*
13 * 32-bit land is lacking generic time vsyscalls as well as the legacy 32-bit
14 * time syscalls like gettimeofday. Skip these definitions since on 32-bit.
15 */
16 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
17
18 #define VDSO_HAS_CLOCK_GETRES 1
19
20 static __always_inline
gettimeofday_fallback(struct __kernel_old_timeval * _tv,struct timezone * _tz)21 int gettimeofday_fallback(struct __kernel_old_timeval *_tv,
22 struct timezone *_tz)
23 {
24 register struct __kernel_old_timeval *tv asm("a0") = _tv;
25 register struct timezone *tz asm("a1") = _tz;
26 register long ret asm("a0");
27 register long nr asm("a7") = __NR_gettimeofday;
28
29 asm volatile ("ecall\n"
30 : "=r" (ret)
31 : "r"(tv), "r"(tz), "r"(nr)
32 : "memory");
33
34 return ret;
35 }
36
37 static __always_inline
clock_gettime_fallback(clockid_t _clkid,struct __kernel_timespec * _ts)38 long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
39 {
40 register clockid_t clkid asm("a0") = _clkid;
41 register struct __kernel_timespec *ts asm("a1") = _ts;
42 register long ret asm("a0");
43 register long nr asm("a7") = __NR_clock_gettime;
44
45 asm volatile ("ecall\n"
46 : "=r" (ret)
47 : "r"(clkid), "r"(ts), "r"(nr)
48 : "memory");
49
50 return ret;
51 }
52
53 static __always_inline
clock_getres_fallback(clockid_t _clkid,struct __kernel_timespec * _ts)54 int clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
55 {
56 register clockid_t clkid asm("a0") = _clkid;
57 register struct __kernel_timespec *ts asm("a1") = _ts;
58 register long ret asm("a0");
59 register long nr asm("a7") = __NR_clock_getres;
60
61 asm volatile ("ecall\n"
62 : "=r" (ret)
63 : "r"(clkid), "r"(ts), "r"(nr)
64 : "memory");
65
66 return ret;
67 }
68
69 #endif /* CONFIG_GENERIC_TIME_VSYSCALL */
70
__arch_get_hw_counter(s32 clock_mode,const struct vdso_data * vd)71 static __always_inline u64 __arch_get_hw_counter(s32 clock_mode,
72 const struct vdso_data *vd)
73 {
74 /*
75 * The purpose of csr_read(CSR_TIME) is to trap the system into
76 * M-mode to obtain the value of CSR_TIME. Hence, unlike other
77 * architecture, no fence instructions surround the csr_read()
78 */
79 return csr_read(CSR_TIME);
80 }
81
__arch_get_vdso_data(void)82 static __always_inline const struct vdso_data *__arch_get_vdso_data(void)
83 {
84 return _vdso_data;
85 }
86
87 #ifdef CONFIG_TIME_NS
88 static __always_inline
__arch_get_timens_vdso_data(const struct vdso_data * vd)89 const struct vdso_data *__arch_get_timens_vdso_data(const struct vdso_data *vd)
90 {
91 return _timens_data;
92 }
93 #endif
94 #endif /* !__ASSEMBLY__ */
95
96 #endif /* __ASM_VDSO_GETTIMEOFDAY_H */
97