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