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 
12ad5d1122SVincent Chen #define VDSO_HAS_CLOCK_GETRES	1
13ad5d1122SVincent Chen 
14ad5d1122SVincent Chen static __always_inline
15ad5d1122SVincent Chen int gettimeofday_fallback(struct __kernel_old_timeval *_tv,
16ad5d1122SVincent Chen 			  struct timezone *_tz)
17ad5d1122SVincent Chen {
18ad5d1122SVincent Chen 	register struct __kernel_old_timeval *tv asm("a0") = _tv;
19ad5d1122SVincent Chen 	register struct timezone *tz asm("a1") = _tz;
20ad5d1122SVincent Chen 	register long ret asm("a0");
21ad5d1122SVincent Chen 	register long nr asm("a7") = __NR_gettimeofday;
22ad5d1122SVincent Chen 
23ad5d1122SVincent Chen 	asm volatile ("ecall\n"
24ad5d1122SVincent Chen 		      : "=r" (ret)
25ad5d1122SVincent Chen 		      : "r"(tv), "r"(tz), "r"(nr)
26ad5d1122SVincent Chen 		      : "memory");
27ad5d1122SVincent Chen 
28ad5d1122SVincent Chen 	return ret;
29ad5d1122SVincent Chen }
30ad5d1122SVincent Chen 
31ad5d1122SVincent Chen static __always_inline
32ad5d1122SVincent Chen long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
33ad5d1122SVincent Chen {
34ad5d1122SVincent Chen 	register clockid_t clkid asm("a0") = _clkid;
35ad5d1122SVincent Chen 	register struct __kernel_timespec *ts asm("a1") = _ts;
36ad5d1122SVincent Chen 	register long ret asm("a0");
37ad5d1122SVincent Chen 	register long nr asm("a7") = __NR_clock_gettime;
38ad5d1122SVincent Chen 
39ad5d1122SVincent Chen 	asm volatile ("ecall\n"
40ad5d1122SVincent Chen 		      : "=r" (ret)
41ad5d1122SVincent Chen 		      : "r"(clkid), "r"(ts), "r"(nr)
42ad5d1122SVincent Chen 		      : "memory");
43ad5d1122SVincent Chen 
44ad5d1122SVincent Chen 	return ret;
45ad5d1122SVincent Chen }
46ad5d1122SVincent Chen 
47ad5d1122SVincent Chen static __always_inline
48ad5d1122SVincent Chen int clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
49ad5d1122SVincent Chen {
50ad5d1122SVincent Chen 	register clockid_t clkid asm("a0") = _clkid;
51ad5d1122SVincent Chen 	register struct __kernel_timespec *ts asm("a1") = _ts;
52ad5d1122SVincent Chen 	register long ret asm("a0");
53ad5d1122SVincent Chen 	register long nr asm("a7") = __NR_clock_getres;
54ad5d1122SVincent Chen 
55ad5d1122SVincent Chen 	asm volatile ("ecall\n"
56ad5d1122SVincent Chen 		      : "=r" (ret)
57ad5d1122SVincent Chen 		      : "r"(clkid), "r"(ts), "r"(nr)
58ad5d1122SVincent Chen 		      : "memory");
59ad5d1122SVincent Chen 
60ad5d1122SVincent Chen 	return ret;
61ad5d1122SVincent Chen }
62ad5d1122SVincent Chen 
634c5a116aSThomas Gleixner static __always_inline u64 __arch_get_hw_counter(s32 clock_mode,
644c5a116aSThomas Gleixner 						 const struct vdso_data *vd)
65ad5d1122SVincent Chen {
66ad5d1122SVincent Chen 	/*
67ad5d1122SVincent Chen 	 * The purpose of csr_read(CSR_TIME) is to trap the system into
68ad5d1122SVincent Chen 	 * M-mode to obtain the value of CSR_TIME. Hence, unlike other
69ad5d1122SVincent Chen 	 * architecture, no fence instructions surround the csr_read()
70ad5d1122SVincent Chen 	 */
71ad5d1122SVincent Chen 	return csr_read(CSR_TIME);
72ad5d1122SVincent Chen }
73ad5d1122SVincent Chen 
74ad5d1122SVincent Chen static __always_inline const struct vdso_data *__arch_get_vdso_data(void)
75ad5d1122SVincent Chen {
76ad5d1122SVincent Chen 	return _vdso_data;
77ad5d1122SVincent Chen }
78ad5d1122SVincent Chen 
79ad5d1122SVincent Chen #endif /* !__ASSEMBLY__ */
80ad5d1122SVincent Chen 
81ad5d1122SVincent Chen #endif /* __ASM_VDSO_GETTIMEOFDAY_H */
82