xref: /linux/tools/perf/util/tsc.c (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/types.h>
4 
5 #include "tsc.h"
6 
7 u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc)
8 {
9 	u64 t, quot, rem;
10 
11 	t = ns - tc->time_zero;
12 	quot = t / tc->time_mult;
13 	rem  = t % tc->time_mult;
14 	return (quot << tc->time_shift) +
15 	       (rem << tc->time_shift) / tc->time_mult;
16 }
17 
18 u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc)
19 {
20 	u64 quot, rem;
21 
22 	quot = cyc >> tc->time_shift;
23 	rem  = cyc & (((u64)1 << tc->time_shift) - 1);
24 	return tc->time_zero + quot * tc->time_mult +
25 	       ((rem * tc->time_mult) >> tc->time_shift);
26 }
27 
28 u64 __weak rdtsc(void)
29 {
30 	return 0;
31 }
32