Linux 4.19.133
[linux/fpc-iii.git] / tools / perf / arch / powerpc / util / kvm-stat.c
blob596ad6aedaace816c0f7b37db55ae8ec7b5eef0f
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include "util/kvm-stat.h"
4 #include "util/parse-events.h"
5 #include "util/debug.h"
7 #include "book3s_hv_exits.h"
8 #include "book3s_hcalls.h"
10 #define NR_TPS 4
12 const char *vcpu_id_str = "vcpu_id";
13 const int decode_str_len = 40;
14 const char *kvm_entry_trace = "kvm_hv:kvm_guest_enter";
15 const char *kvm_exit_trace = "kvm_hv:kvm_guest_exit";
17 define_exit_reasons_table(hv_exit_reasons, kvm_trace_symbol_exit);
18 define_exit_reasons_table(hcall_reasons, kvm_trace_symbol_hcall);
20 /* Tracepoints specific to ppc_book3s_hv */
21 const char *ppc_book3s_hv_kvm_tp[] = {
22 "kvm_hv:kvm_guest_enter",
23 "kvm_hv:kvm_guest_exit",
24 "kvm_hv:kvm_hcall_enter",
25 "kvm_hv:kvm_hcall_exit",
26 NULL,
29 /* 1 extra placeholder for NULL */
30 const char *kvm_events_tp[NR_TPS + 1];
31 const char *kvm_exit_reason;
33 static void hcall_event_get_key(struct perf_evsel *evsel,
34 struct perf_sample *sample,
35 struct event_key *key)
37 key->info = 0;
38 key->key = perf_evsel__intval(evsel, sample, "req");
41 static const char *get_hcall_exit_reason(u64 exit_code)
43 struct exit_reasons_table *tbl = hcall_reasons;
45 while (tbl->reason != NULL) {
46 if (tbl->exit_code == exit_code)
47 return tbl->reason;
48 tbl++;
51 pr_debug("Unknown hcall code: %lld\n",
52 (unsigned long long)exit_code);
53 return "UNKNOWN";
56 static bool hcall_event_end(struct perf_evsel *evsel,
57 struct perf_sample *sample __maybe_unused,
58 struct event_key *key __maybe_unused)
60 return (!strcmp(evsel->name, kvm_events_tp[3]));
63 static bool hcall_event_begin(struct perf_evsel *evsel,
64 struct perf_sample *sample, struct event_key *key)
66 if (!strcmp(evsel->name, kvm_events_tp[2])) {
67 hcall_event_get_key(evsel, sample, key);
68 return true;
71 return false;
73 static void hcall_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
74 struct event_key *key,
75 char *decode)
77 const char *hcall_reason = get_hcall_exit_reason(key->key);
79 scnprintf(decode, decode_str_len, "%s", hcall_reason);
82 static struct kvm_events_ops hcall_events = {
83 .is_begin_event = hcall_event_begin,
84 .is_end_event = hcall_event_end,
85 .decode_key = hcall_event_decode_key,
86 .name = "HCALL-EVENT",
89 static struct kvm_events_ops exit_events = {
90 .is_begin_event = exit_event_begin,
91 .is_end_event = exit_event_end,
92 .decode_key = exit_event_decode_key,
93 .name = "VM-EXIT"
96 struct kvm_reg_events_ops kvm_reg_events_ops[] = {
97 { .name = "vmexit", .ops = &exit_events },
98 { .name = "hcall", .ops = &hcall_events },
99 { NULL, NULL },
102 const char * const kvm_skip_events[] = {
103 NULL,
107 static int is_tracepoint_available(const char *str, struct perf_evlist *evlist)
109 struct parse_events_error err;
110 int ret;
112 err.str = NULL;
113 ret = parse_events(evlist, str, &err);
114 if (err.str)
115 pr_err("%s : %s\n", str, err.str);
116 return ret;
119 static int ppc__setup_book3s_hv(struct perf_kvm_stat *kvm,
120 struct perf_evlist *evlist)
122 const char **events_ptr;
123 int i, nr_tp = 0, err = -1;
125 /* Check for book3s_hv tracepoints */
126 for (events_ptr = ppc_book3s_hv_kvm_tp; *events_ptr; events_ptr++) {
127 err = is_tracepoint_available(*events_ptr, evlist);
128 if (err)
129 return -1;
130 nr_tp++;
133 for (i = 0; i < nr_tp; i++)
134 kvm_events_tp[i] = ppc_book3s_hv_kvm_tp[i];
136 kvm_events_tp[i] = NULL;
137 kvm_exit_reason = "trap";
138 kvm->exit_reasons = hv_exit_reasons;
139 kvm->exit_reasons_isa = "HV";
141 return 0;
144 /* Wrapper to setup kvm tracepoints */
145 static int ppc__setup_kvm_tp(struct perf_kvm_stat *kvm)
147 struct perf_evlist *evlist = perf_evlist__new();
149 if (evlist == NULL)
150 return -ENOMEM;
152 /* Right now, only supported on book3s_hv */
153 return ppc__setup_book3s_hv(kvm, evlist);
156 int setup_kvm_events_tp(struct perf_kvm_stat *kvm)
158 return ppc__setup_kvm_tp(kvm);
161 int cpu_isa_init(struct perf_kvm_stat *kvm, const char *cpuid __maybe_unused)
163 int ret;
165 ret = ppc__setup_kvm_tp(kvm);
166 if (ret) {
167 kvm->exit_reasons = NULL;
168 kvm->exit_reasons_isa = NULL;
171 return ret;