124640f23SVincenzo Frascino /*
224640f23SVincenzo Frascino * Copyright (C) 2018 ARM Limited
324640f23SVincenzo Frascino * Copyright (C) 2015 Imagination Technologies
424640f23SVincenzo Frascino * Author: Alex Smith <alex.smith@imgtec.com>
524640f23SVincenzo Frascino *
624640f23SVincenzo Frascino * This program is free software; you can redistribute it and/or modify it
724640f23SVincenzo Frascino * under the terms of the GNU General Public License as published by the
824640f23SVincenzo Frascino * Free Software Foundation; either version 2 of the License, or (at your
924640f23SVincenzo Frascino * option) any later version.
1024640f23SVincenzo Frascino */
1124640f23SVincenzo Frascino #ifndef __ASM_VDSO_GETTIMEOFDAY_H
1224640f23SVincenzo Frascino #define __ASM_VDSO_GETTIMEOFDAY_H
1324640f23SVincenzo Frascino
1424640f23SVincenzo Frascino #ifndef __ASSEMBLY__
1524640f23SVincenzo Frascino
1624640f23SVincenzo Frascino #include <asm/vdso/vdso.h>
1724640f23SVincenzo Frascino #include <asm/clocksource.h>
1824640f23SVincenzo Frascino #include <asm/unistd.h>
1924640f23SVincenzo Frascino #include <asm/vdso.h>
2024640f23SVincenzo Frascino
21abed3d82SVincenzo Frascino #define VDSO_HAS_CLOCK_GETRES 1
22abed3d82SVincenzo Frascino
23*1d7ba016SRomain Naour #if MIPS_ISA_REV < 6
24*1d7ba016SRomain Naour #define VDSO_SYSCALL_CLOBBERS "hi", "lo",
25*1d7ba016SRomain Naour #else
26*1d7ba016SRomain Naour #define VDSO_SYSCALL_CLOBBERS
27*1d7ba016SRomain Naour #endif
28*1d7ba016SRomain Naour
gettimeofday_fallback(struct __kernel_old_timeval * _tv,struct timezone * _tz)2924640f23SVincenzo Frascino static __always_inline long gettimeofday_fallback(
3024640f23SVincenzo Frascino struct __kernel_old_timeval *_tv,
3124640f23SVincenzo Frascino struct timezone *_tz)
3224640f23SVincenzo Frascino {
3324640f23SVincenzo Frascino register struct timezone *tz asm("a1") = _tz;
3424640f23SVincenzo Frascino register struct __kernel_old_timeval *tv asm("a0") = _tv;
3524640f23SVincenzo Frascino register long ret asm("v0");
3624640f23SVincenzo Frascino register long nr asm("v0") = __NR_gettimeofday;
3724640f23SVincenzo Frascino register long error asm("a3");
3824640f23SVincenzo Frascino
3924640f23SVincenzo Frascino asm volatile(
4024640f23SVincenzo Frascino " syscall\n"
4124640f23SVincenzo Frascino : "=r" (ret), "=r" (error)
4224640f23SVincenzo Frascino : "r" (tv), "r" (tz), "r" (nr)
4324640f23SVincenzo Frascino : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
44*1d7ba016SRomain Naour "$14", "$15", "$24", "$25",
45*1d7ba016SRomain Naour VDSO_SYSCALL_CLOBBERS
46*1d7ba016SRomain Naour "memory");
4724640f23SVincenzo Frascino
4824640f23SVincenzo Frascino return error ? -ret : ret;
4924640f23SVincenzo Frascino }
5024640f23SVincenzo Frascino
clock_gettime_fallback(clockid_t _clkid,struct __kernel_timespec * _ts)5124640f23SVincenzo Frascino static __always_inline long clock_gettime_fallback(
5224640f23SVincenzo Frascino clockid_t _clkid,
5324640f23SVincenzo Frascino struct __kernel_timespec *_ts)
5424640f23SVincenzo Frascino {
5524640f23SVincenzo Frascino register struct __kernel_timespec *ts asm("a1") = _ts;
5624640f23SVincenzo Frascino register clockid_t clkid asm("a0") = _clkid;
5724640f23SVincenzo Frascino register long ret asm("v0");
5824640f23SVincenzo Frascino #if _MIPS_SIM == _MIPS_SIM_ABI64
5924640f23SVincenzo Frascino register long nr asm("v0") = __NR_clock_gettime;
6024640f23SVincenzo Frascino #else
6124640f23SVincenzo Frascino register long nr asm("v0") = __NR_clock_gettime64;
6224640f23SVincenzo Frascino #endif
6324640f23SVincenzo Frascino register long error asm("a3");
6424640f23SVincenzo Frascino
6524640f23SVincenzo Frascino asm volatile(
6624640f23SVincenzo Frascino " syscall\n"
6724640f23SVincenzo Frascino : "=r" (ret), "=r" (error)
6824640f23SVincenzo Frascino : "r" (clkid), "r" (ts), "r" (nr)
6924640f23SVincenzo Frascino : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
70*1d7ba016SRomain Naour "$14", "$15", "$24", "$25",
71*1d7ba016SRomain Naour VDSO_SYSCALL_CLOBBERS
72*1d7ba016SRomain Naour "memory");
7324640f23SVincenzo Frascino
7424640f23SVincenzo Frascino return error ? -ret : ret;
7524640f23SVincenzo Frascino }
7624640f23SVincenzo Frascino
clock_getres_fallback(clockid_t _clkid,struct __kernel_timespec * _ts)77abed3d82SVincenzo Frascino static __always_inline int clock_getres_fallback(
78abed3d82SVincenzo Frascino clockid_t _clkid,
79abed3d82SVincenzo Frascino struct __kernel_timespec *_ts)
80abed3d82SVincenzo Frascino {
81abed3d82SVincenzo Frascino register struct __kernel_timespec *ts asm("a1") = _ts;
82abed3d82SVincenzo Frascino register clockid_t clkid asm("a0") = _clkid;
83abed3d82SVincenzo Frascino register long ret asm("v0");
84abed3d82SVincenzo Frascino #if _MIPS_SIM == _MIPS_SIM_ABI64
85abed3d82SVincenzo Frascino register long nr asm("v0") = __NR_clock_getres;
86abed3d82SVincenzo Frascino #else
87abed3d82SVincenzo Frascino register long nr asm("v0") = __NR_clock_getres_time64;
88abed3d82SVincenzo Frascino #endif
89abed3d82SVincenzo Frascino register long error asm("a3");
90abed3d82SVincenzo Frascino
91abed3d82SVincenzo Frascino asm volatile(
92abed3d82SVincenzo Frascino " syscall\n"
93abed3d82SVincenzo Frascino : "=r" (ret), "=r" (error)
94abed3d82SVincenzo Frascino : "r" (clkid), "r" (ts), "r" (nr)
95abed3d82SVincenzo Frascino : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
96*1d7ba016SRomain Naour "$14", "$15", "$24", "$25",
97*1d7ba016SRomain Naour VDSO_SYSCALL_CLOBBERS
98*1d7ba016SRomain Naour "memory");
99abed3d82SVincenzo Frascino
100abed3d82SVincenzo Frascino return error ? -ret : ret;
101abed3d82SVincenzo Frascino }
102abed3d82SVincenzo Frascino
103932bb934SVincenzo Frascino #if _MIPS_SIM != _MIPS_SIM_ABI64
104932bb934SVincenzo Frascino
clock_gettime32_fallback(clockid_t _clkid,struct old_timespec32 * _ts)105932bb934SVincenzo Frascino static __always_inline long clock_gettime32_fallback(
106932bb934SVincenzo Frascino clockid_t _clkid,
107932bb934SVincenzo Frascino struct old_timespec32 *_ts)
108932bb934SVincenzo Frascino {
109932bb934SVincenzo Frascino register struct old_timespec32 *ts asm("a1") = _ts;
110932bb934SVincenzo Frascino register clockid_t clkid asm("a0") = _clkid;
111932bb934SVincenzo Frascino register long ret asm("v0");
112932bb934SVincenzo Frascino register long nr asm("v0") = __NR_clock_gettime;
113932bb934SVincenzo Frascino register long error asm("a3");
114932bb934SVincenzo Frascino
115932bb934SVincenzo Frascino asm volatile(
116932bb934SVincenzo Frascino " syscall\n"
117932bb934SVincenzo Frascino : "=r" (ret), "=r" (error)
118932bb934SVincenzo Frascino : "r" (clkid), "r" (ts), "r" (nr)
119932bb934SVincenzo Frascino : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
120*1d7ba016SRomain Naour "$14", "$15", "$24", "$25",
121*1d7ba016SRomain Naour VDSO_SYSCALL_CLOBBERS
122*1d7ba016SRomain Naour "memory");
123932bb934SVincenzo Frascino
124932bb934SVincenzo Frascino return error ? -ret : ret;
125932bb934SVincenzo Frascino }
126932bb934SVincenzo Frascino
clock_getres32_fallback(clockid_t _clkid,struct old_timespec32 * _ts)127932bb934SVincenzo Frascino static __always_inline int clock_getres32_fallback(
128932bb934SVincenzo Frascino clockid_t _clkid,
129932bb934SVincenzo Frascino struct old_timespec32 *_ts)
130932bb934SVincenzo Frascino {
131932bb934SVincenzo Frascino register struct old_timespec32 *ts asm("a1") = _ts;
132932bb934SVincenzo Frascino register clockid_t clkid asm("a0") = _clkid;
133932bb934SVincenzo Frascino register long ret asm("v0");
134932bb934SVincenzo Frascino register long nr asm("v0") = __NR_clock_getres;
135932bb934SVincenzo Frascino register long error asm("a3");
136932bb934SVincenzo Frascino
137932bb934SVincenzo Frascino asm volatile(
138932bb934SVincenzo Frascino " syscall\n"
139932bb934SVincenzo Frascino : "=r" (ret), "=r" (error)
140932bb934SVincenzo Frascino : "r" (clkid), "r" (ts), "r" (nr)
141932bb934SVincenzo Frascino : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
142*1d7ba016SRomain Naour "$14", "$15", "$24", "$25",
143*1d7ba016SRomain Naour VDSO_SYSCALL_CLOBBERS
144*1d7ba016SRomain Naour "memory");
145932bb934SVincenzo Frascino
146932bb934SVincenzo Frascino return error ? -ret : ret;
147932bb934SVincenzo Frascino }
148932bb934SVincenzo Frascino #endif
149932bb934SVincenzo Frascino
15024640f23SVincenzo Frascino #ifdef CONFIG_CSRC_R4K
15124640f23SVincenzo Frascino
read_r4k_count(void)15224640f23SVincenzo Frascino static __always_inline u64 read_r4k_count(void)
15324640f23SVincenzo Frascino {
15424640f23SVincenzo Frascino unsigned int count;
15524640f23SVincenzo Frascino
15624640f23SVincenzo Frascino __asm__ __volatile__(
15724640f23SVincenzo Frascino " .set push\n"
15824640f23SVincenzo Frascino " .set mips32r2\n"
15924640f23SVincenzo Frascino " rdhwr %0, $2\n"
16024640f23SVincenzo Frascino " .set pop\n"
16124640f23SVincenzo Frascino : "=r" (count));
16224640f23SVincenzo Frascino
16324640f23SVincenzo Frascino return count;
16424640f23SVincenzo Frascino }
16524640f23SVincenzo Frascino
16624640f23SVincenzo Frascino #endif
16724640f23SVincenzo Frascino
16824640f23SVincenzo Frascino #ifdef CONFIG_CLKSRC_MIPS_GIC
16924640f23SVincenzo Frascino
read_gic_count(const struct vdso_data * data)17024640f23SVincenzo Frascino static __always_inline u64 read_gic_count(const struct vdso_data *data)
17124640f23SVincenzo Frascino {
17224640f23SVincenzo Frascino void __iomem *gic = get_gic(data);
17324640f23SVincenzo Frascino u32 hi, hi2, lo;
17424640f23SVincenzo Frascino
17524640f23SVincenzo Frascino do {
17624640f23SVincenzo Frascino hi = __raw_readl(gic + sizeof(lo));
17724640f23SVincenzo Frascino lo = __raw_readl(gic);
17824640f23SVincenzo Frascino hi2 = __raw_readl(gic + sizeof(lo));
17924640f23SVincenzo Frascino } while (hi2 != hi);
18024640f23SVincenzo Frascino
18124640f23SVincenzo Frascino return (((u64)hi) << 32) + lo;
18224640f23SVincenzo Frascino }
18324640f23SVincenzo Frascino
18424640f23SVincenzo Frascino #endif
18524640f23SVincenzo Frascino
__arch_get_hw_counter(s32 clock_mode,const struct vdso_data * vd)1864c5a116aSThomas Gleixner static __always_inline u64 __arch_get_hw_counter(s32 clock_mode,
1874c5a116aSThomas Gleixner const struct vdso_data *vd)
18824640f23SVincenzo Frascino {
18924640f23SVincenzo Frascino #ifdef CONFIG_CSRC_R4K
190e1bdb22eSThomas Gleixner if (clock_mode == VDSO_CLOCKMODE_R4K)
191e1bdb22eSThomas Gleixner return read_r4k_count();
19224640f23SVincenzo Frascino #endif
19324640f23SVincenzo Frascino #ifdef CONFIG_CLKSRC_MIPS_GIC
194e1bdb22eSThomas Gleixner if (clock_mode == VDSO_CLOCKMODE_GIC)
1954c5a116aSThomas Gleixner return read_gic_count(vd);
19624640f23SVincenzo Frascino #endif
197e1bdb22eSThomas Gleixner /*
198e1bdb22eSThomas Gleixner * Core checks mode already. So this raced against a concurrent
199e1bdb22eSThomas Gleixner * update. Return something. Core will do another round see the
200e1bdb22eSThomas Gleixner * change and fallback to syscall.
201e1bdb22eSThomas Gleixner */
202e1bdb22eSThomas Gleixner return 0;
20324640f23SVincenzo Frascino }
20424640f23SVincenzo Frascino
mips_vdso_hres_capable(void)20525a2a656SThomas Gleixner static inline bool mips_vdso_hres_capable(void)
20625a2a656SThomas Gleixner {
20725a2a656SThomas Gleixner return IS_ENABLED(CONFIG_CSRC_R4K) ||
20825a2a656SThomas Gleixner IS_ENABLED(CONFIG_CLKSRC_MIPS_GIC);
20925a2a656SThomas Gleixner }
21025a2a656SThomas Gleixner #define __arch_vdso_hres_capable mips_vdso_hres_capable
21125a2a656SThomas Gleixner
__arch_get_vdso_data(void)21224640f23SVincenzo Frascino static __always_inline const struct vdso_data *__arch_get_vdso_data(void)
21324640f23SVincenzo Frascino {
21424640f23SVincenzo Frascino return get_vdso_data();
21524640f23SVincenzo Frascino }
21624640f23SVincenzo Frascino
21724640f23SVincenzo Frascino #endif /* !__ASSEMBLY__ */
21824640f23SVincenzo Frascino
21924640f23SVincenzo Frascino #endif /* __ASM_VDSO_GETTIMEOFDAY_H */
220