xref: /openbmc/linux/tools/perf/util/tsc.c (revision b2441318)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
20b437860SAdrian Hunter #include <linux/compiler.h>
30b437860SAdrian Hunter #include <linux/types.h>
40b437860SAdrian Hunter 
50b437860SAdrian Hunter #include "tsc.h"
60b437860SAdrian Hunter 
70b437860SAdrian Hunter u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc)
80b437860SAdrian Hunter {
90b437860SAdrian Hunter 	u64 t, quot, rem;
100b437860SAdrian Hunter 
110b437860SAdrian Hunter 	t = ns - tc->time_zero;
120b437860SAdrian Hunter 	quot = t / tc->time_mult;
130b437860SAdrian Hunter 	rem  = t % tc->time_mult;
140b437860SAdrian Hunter 	return (quot << tc->time_shift) +
150b437860SAdrian Hunter 	       (rem << tc->time_shift) / tc->time_mult;
160b437860SAdrian Hunter }
170b437860SAdrian Hunter 
180b437860SAdrian Hunter u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc)
190b437860SAdrian Hunter {
200b437860SAdrian Hunter 	u64 quot, rem;
210b437860SAdrian Hunter 
220b437860SAdrian Hunter 	quot = cyc >> tc->time_shift;
23a23f96eeSAdrian Hunter 	rem  = cyc & (((u64)1 << tc->time_shift) - 1);
240b437860SAdrian Hunter 	return tc->time_zero + quot * tc->time_mult +
250b437860SAdrian Hunter 	       ((rem * tc->time_mult) >> tc->time_shift);
260b437860SAdrian Hunter }
27a6a69db4SAdrian Hunter 
28a6a69db4SAdrian Hunter u64 __weak rdtsc(void)
29a6a69db4SAdrian Hunter {
30a6a69db4SAdrian Hunter 	return 0;
31a6a69db4SAdrian Hunter }
32