xref: /OK3568_Linux_fs/kernel/arch/mips/include/asm/vdso/gettimeofday.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2018 ARM Limited
3*4882a593Smuzhiyun  * Copyright (C) 2015 Imagination Technologies
4*4882a593Smuzhiyun  * Author: Alex Smith <alex.smith@imgtec.com>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify it
7*4882a593Smuzhiyun  * under the terms of the GNU General Public License as published by the
8*4882a593Smuzhiyun  * Free Software Foundation;  either version 2 of the  License, or (at your
9*4882a593Smuzhiyun  * option) any later version.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun #ifndef __ASM_VDSO_GETTIMEOFDAY_H
12*4882a593Smuzhiyun #define __ASM_VDSO_GETTIMEOFDAY_H
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #ifndef __ASSEMBLY__
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <asm/vdso/vdso.h>
17*4882a593Smuzhiyun #include <asm/clocksource.h>
18*4882a593Smuzhiyun #include <asm/unistd.h>
19*4882a593Smuzhiyun #include <asm/vdso.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define VDSO_HAS_CLOCK_GETRES		1
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #if MIPS_ISA_REV < 6
24*4882a593Smuzhiyun #define VDSO_SYSCALL_CLOBBERS "hi", "lo",
25*4882a593Smuzhiyun #else
26*4882a593Smuzhiyun #define VDSO_SYSCALL_CLOBBERS
27*4882a593Smuzhiyun #endif
28*4882a593Smuzhiyun 
gettimeofday_fallback(struct __kernel_old_timeval * _tv,struct timezone * _tz)29*4882a593Smuzhiyun static __always_inline long gettimeofday_fallback(
30*4882a593Smuzhiyun 				struct __kernel_old_timeval *_tv,
31*4882a593Smuzhiyun 				struct timezone *_tz)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	register struct timezone *tz asm("a1") = _tz;
34*4882a593Smuzhiyun 	register struct __kernel_old_timeval *tv asm("a0") = _tv;
35*4882a593Smuzhiyun 	register long ret asm("v0");
36*4882a593Smuzhiyun 	register long nr asm("v0") = __NR_gettimeofday;
37*4882a593Smuzhiyun 	register long error asm("a3");
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	asm volatile(
40*4882a593Smuzhiyun 	"       syscall\n"
41*4882a593Smuzhiyun 	: "=r" (ret), "=r" (error)
42*4882a593Smuzhiyun 	: "r" (tv), "r" (tz), "r" (nr)
43*4882a593Smuzhiyun 	: "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
44*4882a593Smuzhiyun 	  "$14", "$15", "$24", "$25",
45*4882a593Smuzhiyun 	  VDSO_SYSCALL_CLOBBERS
46*4882a593Smuzhiyun 	  "memory");
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	return error ? -ret : ret;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
clock_gettime_fallback(clockid_t _clkid,struct __kernel_timespec * _ts)51*4882a593Smuzhiyun static __always_inline long clock_gettime_fallback(
52*4882a593Smuzhiyun 					clockid_t _clkid,
53*4882a593Smuzhiyun 					struct __kernel_timespec *_ts)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	register struct __kernel_timespec *ts asm("a1") = _ts;
56*4882a593Smuzhiyun 	register clockid_t clkid asm("a0") = _clkid;
57*4882a593Smuzhiyun 	register long ret asm("v0");
58*4882a593Smuzhiyun #if _MIPS_SIM == _MIPS_SIM_ABI64
59*4882a593Smuzhiyun 	register long nr asm("v0") = __NR_clock_gettime;
60*4882a593Smuzhiyun #else
61*4882a593Smuzhiyun 	register long nr asm("v0") = __NR_clock_gettime64;
62*4882a593Smuzhiyun #endif
63*4882a593Smuzhiyun 	register long error asm("a3");
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	asm volatile(
66*4882a593Smuzhiyun 	"       syscall\n"
67*4882a593Smuzhiyun 	: "=r" (ret), "=r" (error)
68*4882a593Smuzhiyun 	: "r" (clkid), "r" (ts), "r" (nr)
69*4882a593Smuzhiyun 	: "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
70*4882a593Smuzhiyun 	  "$14", "$15", "$24", "$25",
71*4882a593Smuzhiyun 	  VDSO_SYSCALL_CLOBBERS
72*4882a593Smuzhiyun 	  "memory");
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	return error ? -ret : ret;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
clock_getres_fallback(clockid_t _clkid,struct __kernel_timespec * _ts)77*4882a593Smuzhiyun static __always_inline int clock_getres_fallback(
78*4882a593Smuzhiyun 					clockid_t _clkid,
79*4882a593Smuzhiyun 					struct __kernel_timespec *_ts)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	register struct __kernel_timespec *ts asm("a1") = _ts;
82*4882a593Smuzhiyun 	register clockid_t clkid asm("a0") = _clkid;
83*4882a593Smuzhiyun 	register long ret asm("v0");
84*4882a593Smuzhiyun #if _MIPS_SIM == _MIPS_SIM_ABI64
85*4882a593Smuzhiyun 	register long nr asm("v0") = __NR_clock_getres;
86*4882a593Smuzhiyun #else
87*4882a593Smuzhiyun 	register long nr asm("v0") = __NR_clock_getres_time64;
88*4882a593Smuzhiyun #endif
89*4882a593Smuzhiyun 	register long error asm("a3");
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	asm volatile(
92*4882a593Smuzhiyun 	"       syscall\n"
93*4882a593Smuzhiyun 	: "=r" (ret), "=r" (error)
94*4882a593Smuzhiyun 	: "r" (clkid), "r" (ts), "r" (nr)
95*4882a593Smuzhiyun 	: "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
96*4882a593Smuzhiyun 	  "$14", "$15", "$24", "$25",
97*4882a593Smuzhiyun 	  VDSO_SYSCALL_CLOBBERS
98*4882a593Smuzhiyun 	  "memory");
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	return error ? -ret : ret;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun #if _MIPS_SIM != _MIPS_SIM_ABI64
104*4882a593Smuzhiyun 
clock_gettime32_fallback(clockid_t _clkid,struct old_timespec32 * _ts)105*4882a593Smuzhiyun static __always_inline long clock_gettime32_fallback(
106*4882a593Smuzhiyun 					clockid_t _clkid,
107*4882a593Smuzhiyun 					struct old_timespec32 *_ts)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	register struct old_timespec32 *ts asm("a1") = _ts;
110*4882a593Smuzhiyun 	register clockid_t clkid asm("a0") = _clkid;
111*4882a593Smuzhiyun 	register long ret asm("v0");
112*4882a593Smuzhiyun 	register long nr asm("v0") = __NR_clock_gettime;
113*4882a593Smuzhiyun 	register long error asm("a3");
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	asm volatile(
116*4882a593Smuzhiyun 	"       syscall\n"
117*4882a593Smuzhiyun 	: "=r" (ret), "=r" (error)
118*4882a593Smuzhiyun 	: "r" (clkid), "r" (ts), "r" (nr)
119*4882a593Smuzhiyun 	: "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
120*4882a593Smuzhiyun 	  "$14", "$15", "$24", "$25",
121*4882a593Smuzhiyun 	  VDSO_SYSCALL_CLOBBERS
122*4882a593Smuzhiyun 	  "memory");
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	return error ? -ret : ret;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
clock_getres32_fallback(clockid_t _clkid,struct old_timespec32 * _ts)127*4882a593Smuzhiyun static __always_inline int clock_getres32_fallback(
128*4882a593Smuzhiyun 					clockid_t _clkid,
129*4882a593Smuzhiyun 					struct old_timespec32 *_ts)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	register struct old_timespec32 *ts asm("a1") = _ts;
132*4882a593Smuzhiyun 	register clockid_t clkid asm("a0") = _clkid;
133*4882a593Smuzhiyun 	register long ret asm("v0");
134*4882a593Smuzhiyun 	register long nr asm("v0") = __NR_clock_getres;
135*4882a593Smuzhiyun 	register long error asm("a3");
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	asm volatile(
138*4882a593Smuzhiyun 	"       syscall\n"
139*4882a593Smuzhiyun 	: "=r" (ret), "=r" (error)
140*4882a593Smuzhiyun 	: "r" (clkid), "r" (ts), "r" (nr)
141*4882a593Smuzhiyun 	: "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
142*4882a593Smuzhiyun 	  "$14", "$15", "$24", "$25",
143*4882a593Smuzhiyun 	  VDSO_SYSCALL_CLOBBERS
144*4882a593Smuzhiyun 	  "memory");
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	return error ? -ret : ret;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun #endif
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun #ifdef CONFIG_CSRC_R4K
151*4882a593Smuzhiyun 
read_r4k_count(void)152*4882a593Smuzhiyun static __always_inline u64 read_r4k_count(void)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	unsigned int count;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	__asm__ __volatile__(
157*4882a593Smuzhiyun 	"	.set push\n"
158*4882a593Smuzhiyun 	"	.set mips32r2\n"
159*4882a593Smuzhiyun 	"	rdhwr	%0, $2\n"
160*4882a593Smuzhiyun 	"	.set pop\n"
161*4882a593Smuzhiyun 	: "=r" (count));
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	return count;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun #endif
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun #ifdef CONFIG_CLKSRC_MIPS_GIC
169*4882a593Smuzhiyun 
read_gic_count(const struct vdso_data * data)170*4882a593Smuzhiyun static __always_inline u64 read_gic_count(const struct vdso_data *data)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	void __iomem *gic = get_gic(data);
173*4882a593Smuzhiyun 	u32 hi, hi2, lo;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	do {
176*4882a593Smuzhiyun 		hi = __raw_readl(gic + sizeof(lo));
177*4882a593Smuzhiyun 		lo = __raw_readl(gic);
178*4882a593Smuzhiyun 		hi2 = __raw_readl(gic + sizeof(lo));
179*4882a593Smuzhiyun 	} while (hi2 != hi);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	return (((u64)hi) << 32) + lo;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun #endif
185*4882a593Smuzhiyun 
__arch_get_hw_counter(s32 clock_mode,const struct vdso_data * vd)186*4882a593Smuzhiyun static __always_inline u64 __arch_get_hw_counter(s32 clock_mode,
187*4882a593Smuzhiyun 						 const struct vdso_data *vd)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun #ifdef CONFIG_CSRC_R4K
190*4882a593Smuzhiyun 	if (clock_mode == VDSO_CLOCKMODE_R4K)
191*4882a593Smuzhiyun 		return read_r4k_count();
192*4882a593Smuzhiyun #endif
193*4882a593Smuzhiyun #ifdef CONFIG_CLKSRC_MIPS_GIC
194*4882a593Smuzhiyun 	if (clock_mode == VDSO_CLOCKMODE_GIC)
195*4882a593Smuzhiyun 		return read_gic_count(vd);
196*4882a593Smuzhiyun #endif
197*4882a593Smuzhiyun 	/*
198*4882a593Smuzhiyun 	 * Core checks mode already. So this raced against a concurrent
199*4882a593Smuzhiyun 	 * update. Return something. Core will do another round see the
200*4882a593Smuzhiyun 	 * change and fallback to syscall.
201*4882a593Smuzhiyun 	 */
202*4882a593Smuzhiyun 	return 0;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun 
mips_vdso_hres_capable(void)205*4882a593Smuzhiyun static inline bool mips_vdso_hres_capable(void)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_CSRC_R4K) ||
208*4882a593Smuzhiyun 	       IS_ENABLED(CONFIG_CLKSRC_MIPS_GIC);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun #define __arch_vdso_hres_capable mips_vdso_hres_capable
211*4882a593Smuzhiyun 
__arch_get_vdso_data(void)212*4882a593Smuzhiyun static __always_inline const struct vdso_data *__arch_get_vdso_data(void)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	return get_vdso_data();
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun #endif /* !__ASSEMBLY__ */
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun #endif /* __ASM_VDSO_GETTIMEOFDAY_H */
220