xref: /aosp_15_r20/external/ltp/include/tst_tsc.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright © International Business Machines  Corp., 2006-2008
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * AUTHOR
6*49cdfc7eSAndroid Build Coastguard Worker  *        Darren Hart <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker  *        Giuseppe Cavallaro <peppe.cavallarost.com>
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
10*49cdfc7eSAndroid Build Coastguard Worker  *      It directly comes from the librttest.h (see its HISTORY).
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #ifndef TST_TSC_H
14*49cdfc7eSAndroid Build Coastguard Worker #define TST_TSC_H
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #undef TSC_UNSUPPORTED
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker /* TSC macros */
19*49cdfc7eSAndroid Build Coastguard Worker #if defined(__i386__)
20*49cdfc7eSAndroid Build Coastguard Worker #define rdtscll(val) __asm__ __volatile__("rdtsc" : "=A" (val))
21*49cdfc7eSAndroid Build Coastguard Worker #elif defined(__x86_64__)
22*49cdfc7eSAndroid Build Coastguard Worker #define rdtscll(val)					\
23*49cdfc7eSAndroid Build Coastguard Worker 	do {						\
24*49cdfc7eSAndroid Build Coastguard Worker 		uint32_t low, high;			\
25*49cdfc7eSAndroid Build Coastguard Worker 		__asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high)); \
26*49cdfc7eSAndroid Build Coastguard Worker 		val = (uint64_t)high << 32 | low;	\
27*49cdfc7eSAndroid Build Coastguard Worker 	} while (0)
28*49cdfc7eSAndroid Build Coastguard Worker #elif defined(__powerpc__)
29*49cdfc7eSAndroid Build Coastguard Worker #if defined(__powerpc64__)	/* 64bit version */
30*49cdfc7eSAndroid Build Coastguard Worker #define rdtscll(val)					\
31*49cdfc7eSAndroid Build Coastguard Worker 	do {								\
32*49cdfc7eSAndroid Build Coastguard Worker 		__asm__ __volatile__ ("mfspr %0, 268" : "=r" (val));	\
33*49cdfc7eSAndroid Build Coastguard Worker 	} while (0)
34*49cdfc7eSAndroid Build Coastguard Worker #else	/*__powerpc__ 32bit version */
35*49cdfc7eSAndroid Build Coastguard Worker #define rdtscll(val)							\
36*49cdfc7eSAndroid Build Coastguard Worker 	 do {								\
37*49cdfc7eSAndroid Build Coastguard Worker 		uint32_t tbhi, tblo ;					\
38*49cdfc7eSAndroid Build Coastguard Worker 		__asm__ __volatile__ ("mftbu %0" : "=r" (tbhi));	\
39*49cdfc7eSAndroid Build Coastguard Worker 		__asm__ __volatile__ ("mftbl %0" : "=r" (tblo));	\
40*49cdfc7eSAndroid Build Coastguard Worker 		val = 1000 * ((uint64_t) tbhi << 32) | tblo;		\
41*49cdfc7eSAndroid Build Coastguard Worker 	} while (0)
42*49cdfc7eSAndroid Build Coastguard Worker #endif
43*49cdfc7eSAndroid Build Coastguard Worker #else
44*49cdfc7eSAndroid Build Coastguard Worker #warning TSC UNSUPPORTED
45*49cdfc7eSAndroid Build Coastguard Worker /* All tests will be compiled also for the
46*49cdfc7eSAndroid Build Coastguard Worker  * architecture without TSC support (e.g. SH).
47*49cdfc7eSAndroid Build Coastguard Worker  * At run-time these will fail with ENOTSUP.
48*49cdfc7eSAndroid Build Coastguard Worker  */
49*49cdfc7eSAndroid Build Coastguard Worker #define rdtscll(val)	do {  } while (0)
50*49cdfc7eSAndroid Build Coastguard Worker #define TSC_UNSUPPORTED
51*49cdfc7eSAndroid Build Coastguard Worker #endif
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker #endif
54