xref: /openbmc/linux/arch/sparc/vdso/vclock_gettime.c (revision 19832d24)
19a08862aSNagarathnam Muthusamy /*
29a08862aSNagarathnam Muthusamy  * Copyright 2006 Andi Kleen, SUSE Labs.
39a08862aSNagarathnam Muthusamy  * Subject to the GNU Public License, v.2
49a08862aSNagarathnam Muthusamy  *
59a08862aSNagarathnam Muthusamy  * Fast user context implementation of clock_gettime, gettimeofday, and time.
69a08862aSNagarathnam Muthusamy  *
79a08862aSNagarathnam Muthusamy  * The code should have no internal unresolved relocations.
89a08862aSNagarathnam Muthusamy  * Check with readelf after changing.
99a08862aSNagarathnam Muthusamy  * Also alternative() doesn't work.
109a08862aSNagarathnam Muthusamy  */
119a08862aSNagarathnam Muthusamy /*
129a08862aSNagarathnam Muthusamy  * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
139a08862aSNagarathnam Muthusamy  */
149a08862aSNagarathnam Muthusamy 
159a08862aSNagarathnam Muthusamy #include <linux/kernel.h>
169a08862aSNagarathnam Muthusamy #include <linux/time.h>
179a08862aSNagarathnam Muthusamy #include <linux/string.h>
189a08862aSNagarathnam Muthusamy #include <asm/io.h>
199a08862aSNagarathnam Muthusamy #include <asm/unistd.h>
209a08862aSNagarathnam Muthusamy #include <asm/timex.h>
219a08862aSNagarathnam Muthusamy #include <asm/clocksource.h>
229a08862aSNagarathnam Muthusamy #include <asm/vvar.h>
239a08862aSNagarathnam Muthusamy 
24776ca154SDavid S. Miller #ifdef	CONFIG_SPARC64
259a08862aSNagarathnam Muthusamy #define SYSCALL_STRING							\
269a08862aSNagarathnam Muthusamy 	"ta	0x6d;"							\
27776ca154SDavid S. Miller 	"bcs,a	1f;"							\
289a08862aSNagarathnam Muthusamy 	" sub	%%g0, %%o0, %%o0;"					\
29776ca154SDavid S. Miller 	"1:"
30776ca154SDavid S. Miller #else
31776ca154SDavid S. Miller #define SYSCALL_STRING							\
32776ca154SDavid S. Miller 	"ta	0x10;"							\
33776ca154SDavid S. Miller 	"bcs,a	1f;"							\
34776ca154SDavid S. Miller 	" sub	%%g0, %%o0, %%o0;"					\
35776ca154SDavid S. Miller 	"1:"
36776ca154SDavid S. Miller #endif
379a08862aSNagarathnam Muthusamy 
389a08862aSNagarathnam Muthusamy #define SYSCALL_CLOBBERS						\
399a08862aSNagarathnam Muthusamy 	"f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",			\
409a08862aSNagarathnam Muthusamy 	"f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",		\
419a08862aSNagarathnam Muthusamy 	"f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",		\
429a08862aSNagarathnam Muthusamy 	"f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",		\
439a08862aSNagarathnam Muthusamy 	"f32", "f34", "f36", "f38", "f40", "f42", "f44", "f46",		\
449a08862aSNagarathnam Muthusamy 	"f48", "f50", "f52", "f54", "f56", "f58", "f60", "f62",		\
459a08862aSNagarathnam Muthusamy 	"cc", "memory"
469a08862aSNagarathnam Muthusamy 
479a08862aSNagarathnam Muthusamy /*
489a08862aSNagarathnam Muthusamy  * Compute the vvar page's address in the process address space, and return it
499a08862aSNagarathnam Muthusamy  * as a pointer to the vvar_data.
509a08862aSNagarathnam Muthusamy  */
51794b88e0SDavid S. Miller notrace static __always_inline struct vvar_data *get_vvar_data(void)
529a08862aSNagarathnam Muthusamy {
539a08862aSNagarathnam Muthusamy 	unsigned long ret;
549a08862aSNagarathnam Muthusamy 
559a08862aSNagarathnam Muthusamy 	/*
56794b88e0SDavid S. Miller 	 * vdso data page is the first vDSO page so grab the PC
579a08862aSNagarathnam Muthusamy 	 * and move up a page to get to the data page.
589a08862aSNagarathnam Muthusamy 	 */
59794b88e0SDavid S. Miller 	__asm__("rd %%pc, %0" : "=r" (ret));
609a08862aSNagarathnam Muthusamy 	ret &= ~(8192 - 1);
619a08862aSNagarathnam Muthusamy 	ret -= 8192;
629a08862aSNagarathnam Muthusamy 
639a08862aSNagarathnam Muthusamy 	return (struct vvar_data *) ret;
649a08862aSNagarathnam Muthusamy }
659a08862aSNagarathnam Muthusamy 
66794b88e0SDavid S. Miller notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
679a08862aSNagarathnam Muthusamy {
689a08862aSNagarathnam Muthusamy 	register long num __asm__("g1") = __NR_clock_gettime;
699a08862aSNagarathnam Muthusamy 	register long o0 __asm__("o0") = clock;
709a08862aSNagarathnam Muthusamy 	register long o1 __asm__("o1") = (long) ts;
719a08862aSNagarathnam Muthusamy 
729a08862aSNagarathnam Muthusamy 	__asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num),
739a08862aSNagarathnam Muthusamy 			     "0" (o0), "r" (o1) : SYSCALL_CLOBBERS);
749a08862aSNagarathnam Muthusamy 	return o0;
759a08862aSNagarathnam Muthusamy }
769a08862aSNagarathnam Muthusamy 
77794b88e0SDavid S. Miller notrace static long vdso_fallback_gettimeofday(struct timeval *tv, struct timezone *tz)
789a08862aSNagarathnam Muthusamy {
799a08862aSNagarathnam Muthusamy 	register long num __asm__("g1") = __NR_gettimeofday;
809a08862aSNagarathnam Muthusamy 	register long o0 __asm__("o0") = (long) tv;
819a08862aSNagarathnam Muthusamy 	register long o1 __asm__("o1") = (long) tz;
829a08862aSNagarathnam Muthusamy 
839a08862aSNagarathnam Muthusamy 	__asm__ __volatile__(SYSCALL_STRING : "=r" (o0) : "r" (num),
849a08862aSNagarathnam Muthusamy 			     "0" (o0), "r" (o1) : SYSCALL_CLOBBERS);
859a08862aSNagarathnam Muthusamy 	return o0;
869a08862aSNagarathnam Muthusamy }
879a08862aSNagarathnam Muthusamy 
889a08862aSNagarathnam Muthusamy #ifdef	CONFIG_SPARC64
89794b88e0SDavid S. Miller notrace static __always_inline u64 vread_tick(void)
90794b88e0SDavid S. Miller {
919a08862aSNagarathnam Muthusamy 	u64	ret;
929a08862aSNagarathnam Muthusamy 
932f6c9bf3SDavid S. Miller 	__asm__ __volatile__("1:\n\t"
942f6c9bf3SDavid S. Miller 			     "rd		%%tick, %0\n\t"
952f6c9bf3SDavid S. Miller 			     ".pushsection	.tick_patch, \"a\"\n\t"
962f6c9bf3SDavid S. Miller 			     ".word		1b - ., 1f - .\n\t"
972f6c9bf3SDavid S. Miller 			     ".popsection\n\t"
982f6c9bf3SDavid S. Miller 			     ".pushsection	.tick_patch_replacement, \"ax\"\n\t"
992f6c9bf3SDavid S. Miller 			     "1:\n\t"
1002f6c9bf3SDavid S. Miller 			     "rd		%%asr24, %0\n\t"
1012f6c9bf3SDavid S. Miller 			     ".popsection\n"
1022f6c9bf3SDavid S. Miller 			     : "=r" (ret));
1033fe5d7e8SDavid S. Miller 	return ret;
1049a08862aSNagarathnam Muthusamy }
1059a08862aSNagarathnam Muthusamy #else
106794b88e0SDavid S. Miller notrace static __always_inline u64 vread_tick(void)
1079a08862aSNagarathnam Muthusamy {
1082f6c9bf3SDavid S. Miller 	register unsigned long long ret asm("o4");
1099a08862aSNagarathnam Muthusamy 
1102f6c9bf3SDavid S. Miller 	__asm__ __volatile__("1:\n\t"
1112f6c9bf3SDavid S. Miller 			     "rd		%%tick, %L0\n\t"
1122f6c9bf3SDavid S. Miller 			     "srlx		%L0, 32, %H0\n\t"
1132f6c9bf3SDavid S. Miller 			     ".pushsection	.tick_patch, \"a\"\n\t"
1142f6c9bf3SDavid S. Miller 			     ".word		1b - ., 1f - .\n\t"
1152f6c9bf3SDavid S. Miller 			     ".popsection\n\t"
1162f6c9bf3SDavid S. Miller 			     ".pushsection	.tick_patch_replacement, \"ax\"\n\t"
1172f6c9bf3SDavid S. Miller 			     "1:\n\t"
1182f6c9bf3SDavid S. Miller 			     "rd		%%asr24, %L0\n\t"
1192f6c9bf3SDavid S. Miller 			     ".popsection\n"
1202f6c9bf3SDavid S. Miller 			     : "=r" (ret));
1212f6c9bf3SDavid S. Miller 	return ret;
1229a08862aSNagarathnam Muthusamy }
1239a08862aSNagarathnam Muthusamy #endif
1249a08862aSNagarathnam Muthusamy 
125794b88e0SDavid S. Miller notrace static __always_inline u64 vgetsns(struct vvar_data *vvar)
1269a08862aSNagarathnam Muthusamy {
1279a08862aSNagarathnam Muthusamy 	u64 v;
1289a08862aSNagarathnam Muthusamy 	u64 cycles;
1299a08862aSNagarathnam Muthusamy 
1309a08862aSNagarathnam Muthusamy 	cycles = vread_tick();
1319a08862aSNagarathnam Muthusamy 	v = (cycles - vvar->clock.cycle_last) & vvar->clock.mask;
1329a08862aSNagarathnam Muthusamy 	return v * vvar->clock.mult;
1339a08862aSNagarathnam Muthusamy }
1349a08862aSNagarathnam Muthusamy 
135794b88e0SDavid S. Miller notrace static __always_inline int do_realtime(struct vvar_data *vvar,
136794b88e0SDavid S. Miller 					       struct timespec *ts)
1379a08862aSNagarathnam Muthusamy {
1389a08862aSNagarathnam Muthusamy 	unsigned long seq;
1399a08862aSNagarathnam Muthusamy 	u64 ns;
1409a08862aSNagarathnam Muthusamy 
1419a08862aSNagarathnam Muthusamy 	do {
1429a08862aSNagarathnam Muthusamy 		seq = vvar_read_begin(vvar);
1439a08862aSNagarathnam Muthusamy 		ts->tv_sec = vvar->wall_time_sec;
1449a08862aSNagarathnam Muthusamy 		ns = vvar->wall_time_snsec;
1459a08862aSNagarathnam Muthusamy 		ns += vgetsns(vvar);
1469a08862aSNagarathnam Muthusamy 		ns >>= vvar->clock.shift;
1479a08862aSNagarathnam Muthusamy 	} while (unlikely(vvar_read_retry(vvar, seq)));
1489a08862aSNagarathnam Muthusamy 
14919832d24SDavid S. Miller 	ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
15019832d24SDavid S. Miller 	ts->tv_nsec = ns;
1519a08862aSNagarathnam Muthusamy 
1529a08862aSNagarathnam Muthusamy 	return 0;
1539a08862aSNagarathnam Muthusamy }
1549a08862aSNagarathnam Muthusamy 
155794b88e0SDavid S. Miller notrace static __always_inline int do_monotonic(struct vvar_data *vvar,
156794b88e0SDavid S. Miller 						struct timespec *ts)
1579a08862aSNagarathnam Muthusamy {
1589a08862aSNagarathnam Muthusamy 	unsigned long seq;
1599a08862aSNagarathnam Muthusamy 	u64 ns;
1609a08862aSNagarathnam Muthusamy 
1619a08862aSNagarathnam Muthusamy 	do {
1629a08862aSNagarathnam Muthusamy 		seq = vvar_read_begin(vvar);
1639a08862aSNagarathnam Muthusamy 		ts->tv_sec = vvar->monotonic_time_sec;
1649a08862aSNagarathnam Muthusamy 		ns = vvar->monotonic_time_snsec;
1659a08862aSNagarathnam Muthusamy 		ns += vgetsns(vvar);
1669a08862aSNagarathnam Muthusamy 		ns >>= vvar->clock.shift;
1679a08862aSNagarathnam Muthusamy 	} while (unlikely(vvar_read_retry(vvar, seq)));
1689a08862aSNagarathnam Muthusamy 
16919832d24SDavid S. Miller 	ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
17019832d24SDavid S. Miller 	ts->tv_nsec = ns;
1719a08862aSNagarathnam Muthusamy 
1729a08862aSNagarathnam Muthusamy 	return 0;
1739a08862aSNagarathnam Muthusamy }
1749a08862aSNagarathnam Muthusamy 
175794b88e0SDavid S. Miller notrace static int do_realtime_coarse(struct vvar_data *vvar,
176794b88e0SDavid S. Miller 				      struct timespec *ts)
1779a08862aSNagarathnam Muthusamy {
1789a08862aSNagarathnam Muthusamy 	unsigned long seq;
1799a08862aSNagarathnam Muthusamy 
1809a08862aSNagarathnam Muthusamy 	do {
1819a08862aSNagarathnam Muthusamy 		seq = vvar_read_begin(vvar);
1829a08862aSNagarathnam Muthusamy 		ts->tv_sec = vvar->wall_time_coarse_sec;
1839a08862aSNagarathnam Muthusamy 		ts->tv_nsec = vvar->wall_time_coarse_nsec;
1849a08862aSNagarathnam Muthusamy 	} while (unlikely(vvar_read_retry(vvar, seq)));
1859a08862aSNagarathnam Muthusamy 	return 0;
1869a08862aSNagarathnam Muthusamy }
1879a08862aSNagarathnam Muthusamy 
188794b88e0SDavid S. Miller notrace static int do_monotonic_coarse(struct vvar_data *vvar,
189794b88e0SDavid S. Miller 				       struct timespec *ts)
1909a08862aSNagarathnam Muthusamy {
1919a08862aSNagarathnam Muthusamy 	unsigned long seq;
1929a08862aSNagarathnam Muthusamy 
1939a08862aSNagarathnam Muthusamy 	do {
1949a08862aSNagarathnam Muthusamy 		seq = vvar_read_begin(vvar);
1959a08862aSNagarathnam Muthusamy 		ts->tv_sec = vvar->monotonic_time_coarse_sec;
1969a08862aSNagarathnam Muthusamy 		ts->tv_nsec = vvar->monotonic_time_coarse_nsec;
1979a08862aSNagarathnam Muthusamy 	} while (unlikely(vvar_read_retry(vvar, seq)));
1989a08862aSNagarathnam Muthusamy 
1999a08862aSNagarathnam Muthusamy 	return 0;
2009a08862aSNagarathnam Muthusamy }
2019a08862aSNagarathnam Muthusamy 
2029a08862aSNagarathnam Muthusamy notrace int
2039a08862aSNagarathnam Muthusamy __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
2049a08862aSNagarathnam Muthusamy {
2059a08862aSNagarathnam Muthusamy 	struct vvar_data *vvd = get_vvar_data();
2069a08862aSNagarathnam Muthusamy 
2079a08862aSNagarathnam Muthusamy 	switch (clock) {
2089a08862aSNagarathnam Muthusamy 	case CLOCK_REALTIME:
2099a08862aSNagarathnam Muthusamy 		if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
2109a08862aSNagarathnam Muthusamy 			break;
2119a08862aSNagarathnam Muthusamy 		return do_realtime(vvd, ts);
2129a08862aSNagarathnam Muthusamy 	case CLOCK_MONOTONIC:
2139a08862aSNagarathnam Muthusamy 		if (unlikely(vvd->vclock_mode == VCLOCK_NONE))
2149a08862aSNagarathnam Muthusamy 			break;
2159a08862aSNagarathnam Muthusamy 		return do_monotonic(vvd, ts);
2169a08862aSNagarathnam Muthusamy 	case CLOCK_REALTIME_COARSE:
2179a08862aSNagarathnam Muthusamy 		return do_realtime_coarse(vvd, ts);
2189a08862aSNagarathnam Muthusamy 	case CLOCK_MONOTONIC_COARSE:
2199a08862aSNagarathnam Muthusamy 		return do_monotonic_coarse(vvd, ts);
2209a08862aSNagarathnam Muthusamy 	}
2219a08862aSNagarathnam Muthusamy 	/*
2229a08862aSNagarathnam Muthusamy 	 * Unknown clock ID ? Fall back to the syscall.
2239a08862aSNagarathnam Muthusamy 	 */
2249a08862aSNagarathnam Muthusamy 	return vdso_fallback_gettime(clock, ts);
2259a08862aSNagarathnam Muthusamy }
2269a08862aSNagarathnam Muthusamy int
2279a08862aSNagarathnam Muthusamy clock_gettime(clockid_t, struct timespec *)
2289a08862aSNagarathnam Muthusamy 	__attribute__((weak, alias("__vdso_clock_gettime")));
2299a08862aSNagarathnam Muthusamy 
2309a08862aSNagarathnam Muthusamy notrace int
2319a08862aSNagarathnam Muthusamy __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
2329a08862aSNagarathnam Muthusamy {
2339a08862aSNagarathnam Muthusamy 	struct vvar_data *vvd = get_vvar_data();
2349a08862aSNagarathnam Muthusamy 
2359a08862aSNagarathnam Muthusamy 	if (likely(vvd->vclock_mode != VCLOCK_NONE)) {
2369a08862aSNagarathnam Muthusamy 		if (likely(tv != NULL)) {
2379a08862aSNagarathnam Muthusamy 			union tstv_t {
2389a08862aSNagarathnam Muthusamy 				struct timespec ts;
2399a08862aSNagarathnam Muthusamy 				struct timeval tv;
2409a08862aSNagarathnam Muthusamy 			} *tstv = (union tstv_t *) tv;
2419a08862aSNagarathnam Muthusamy 			do_realtime(vvd, &tstv->ts);
2429a08862aSNagarathnam Muthusamy 			/*
2439a08862aSNagarathnam Muthusamy 			 * Assign before dividing to ensure that the division is
2449a08862aSNagarathnam Muthusamy 			 * done in the type of tv_usec, not tv_nsec.
2459a08862aSNagarathnam Muthusamy 			 *
2469a08862aSNagarathnam Muthusamy 			 * There cannot be > 1 billion usec in a second:
2479a08862aSNagarathnam Muthusamy 			 * do_realtime() has already distributed such overflow
2489a08862aSNagarathnam Muthusamy 			 * into tv_sec.  So we can assign it to an int safely.
2499a08862aSNagarathnam Muthusamy 			 */
2509a08862aSNagarathnam Muthusamy 			tstv->tv.tv_usec = tstv->ts.tv_nsec;
2519a08862aSNagarathnam Muthusamy 			tstv->tv.tv_usec /= 1000;
2529a08862aSNagarathnam Muthusamy 		}
2539a08862aSNagarathnam Muthusamy 		if (unlikely(tz != NULL)) {
2549a08862aSNagarathnam Muthusamy 			/* Avoid memcpy. Some old compilers fail to inline it */
2559a08862aSNagarathnam Muthusamy 			tz->tz_minuteswest = vvd->tz_minuteswest;
2569a08862aSNagarathnam Muthusamy 			tz->tz_dsttime = vvd->tz_dsttime;
2579a08862aSNagarathnam Muthusamy 		}
2589a08862aSNagarathnam Muthusamy 		return 0;
2599a08862aSNagarathnam Muthusamy 	}
2609a08862aSNagarathnam Muthusamy 	return vdso_fallback_gettimeofday(tv, tz);
2619a08862aSNagarathnam Muthusamy }
2629a08862aSNagarathnam Muthusamy int
2639a08862aSNagarathnam Muthusamy gettimeofday(struct timeval *, struct timezone *)
2649a08862aSNagarathnam Muthusamy 	__attribute__((weak, alias("__vdso_gettimeofday")));
265