xref: /openbmc/linux/tools/perf/util/tsc.c (revision 37b9c7bb)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 
4 #include <linux/compiler.h>
5 #include <linux/perf_event.h>
6 #include <linux/stddef.h>
7 #include <linux/types.h>
8 
9 #include <asm/barrier.h>
10 
11 #include "event.h"
12 #include "synthetic-events.h"
13 #include "debug.h"
14 #include "tsc.h"
15 
16 u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc)
17 {
18 	u64 t, quot, rem;
19 
20 	t = ns - tc->time_zero;
21 	quot = t / tc->time_mult;
22 	rem  = t % tc->time_mult;
23 	return (quot << tc->time_shift) +
24 	       (rem << tc->time_shift) / tc->time_mult;
25 }
26 
27 u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc)
28 {
29 	u64 quot, rem;
30 
31 	if (tc->cap_user_time_short)
32 		cyc = tc->time_cycles +
33 			((cyc - tc->time_cycles) & tc->time_mask);
34 
35 	quot = cyc >> tc->time_shift;
36 	rem  = cyc & (((u64)1 << tc->time_shift) - 1);
37 	return tc->time_zero + quot * tc->time_mult +
38 	       ((rem * tc->time_mult) >> tc->time_shift);
39 }
40 
41 int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,
42 			     struct perf_tsc_conversion *tc)
43 {
44 	u32 seq;
45 	int i = 0;
46 
47 	while (1) {
48 		seq = pc->lock;
49 		rmb();
50 		tc->time_mult = pc->time_mult;
51 		tc->time_shift = pc->time_shift;
52 		tc->time_zero = pc->time_zero;
53 		tc->time_cycles = pc->time_cycles;
54 		tc->time_mask = pc->time_mask;
55 		tc->cap_user_time_zero = pc->cap_user_time_zero;
56 		tc->cap_user_time_short	= pc->cap_user_time_short;
57 		rmb();
58 		if (pc->lock == seq && !(seq & 1))
59 			break;
60 		if (++i > 10000) {
61 			pr_debug("failed to get perf_event_mmap_page lock\n");
62 			return -EINVAL;
63 		}
64 	}
65 
66 	if (!tc->cap_user_time_zero)
67 		return -EOPNOTSUPP;
68 
69 	return 0;
70 }
71 
72 int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc,
73 				struct perf_tool *tool,
74 				perf_event__handler_t process,
75 				struct machine *machine)
76 {
77 	union perf_event event = {
78 		.time_conv = {
79 			.header = {
80 				.type = PERF_RECORD_TIME_CONV,
81 				.size = sizeof(struct perf_record_time_conv),
82 			},
83 		},
84 	};
85 	struct perf_tsc_conversion tc;
86 	int err;
87 
88 	if (!pc)
89 		return 0;
90 	err = perf_read_tsc_conversion(pc, &tc);
91 	if (err == -EOPNOTSUPP)
92 		return 0;
93 	if (err)
94 		return err;
95 
96 	pr_debug2("Synthesizing TSC conversion information\n");
97 
98 	event.time_conv.time_mult  = tc.time_mult;
99 	event.time_conv.time_shift = tc.time_shift;
100 	event.time_conv.time_zero  = tc.time_zero;
101 	event.time_conv.time_cycles = tc.time_cycles;
102 	event.time_conv.time_mask = tc.time_mask;
103 	event.time_conv.cap_user_time_zero = tc.cap_user_time_zero;
104 	event.time_conv.cap_user_time_short = tc.cap_user_time_short;
105 
106 	return process(tool, &event, NULL, machine);
107 }
108 
109 u64 __weak rdtsc(void)
110 {
111 	return 0;
112 }
113