GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / x86 / kvm / pmu_intel.c
1 /*
2  * KVM PMU support for Intel CPUs
3  *
4  * Copyright 2011 Red Hat, Inc. and/or its affiliates.
5  *
6  * Authors:
7  *   Avi Kivity   <avi@redhat.com>
8  *   Gleb Natapov <gleb@redhat.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2.  See
11  * the COPYING file in the top-level directory.
12  *
13  */
14 #include <linux/types.h>
15 #include <linux/kvm_host.h>
16 #include <linux/perf_event.h>
17 #include <asm/perf_event.h>
18 #include "x86.h"
19 #include "cpuid.h"
20 #include "lapic.h"
21 #include "pmu.h"
22
23 static struct kvm_event_hw_type_mapping intel_arch_events[] = {
24         /* Index must match CPUID 0x0A.EBX bit vector */
25         [0] = { 0x3c, 0x00, PERF_COUNT_HW_CPU_CYCLES },
26         [1] = { 0xc0, 0x00, PERF_COUNT_HW_INSTRUCTIONS },
27         [2] = { 0x3c, 0x01, PERF_COUNT_HW_BUS_CYCLES  },
28         [3] = { 0x2e, 0x4f, PERF_COUNT_HW_CACHE_REFERENCES },
29         [4] = { 0x2e, 0x41, PERF_COUNT_HW_CACHE_MISSES },
30         [5] = { 0xc4, 0x00, PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
31         [6] = { 0xc5, 0x00, PERF_COUNT_HW_BRANCH_MISSES },
32         [7] = { 0x00, 0x03, PERF_COUNT_HW_REF_CPU_CYCLES },
33 };
34
35 /* mapping between fixed pmc index and intel_arch_events array */
36 static int fixed_pmc_events[] = {1, 0, 7};
37
38 static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
39 {
40         int i;
41
42         for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
43                 u8 new_ctrl = fixed_ctrl_field(data, i);
44                 u8 old_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i);
45                 struct kvm_pmc *pmc;
46
47                 pmc = get_fixed_pmc(pmu, MSR_CORE_PERF_FIXED_CTR0 + i);
48
49                 if (old_ctrl == new_ctrl)
50                         continue;
51
52                 reprogram_fixed_counter(pmc, new_ctrl, i);
53         }
54
55         pmu->fixed_ctr_ctrl = data;
56 }
57
58 /* function is called when global control register has been updated. */
59 static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data)
60 {
61         int bit;
62         u64 diff = pmu->global_ctrl ^ data;
63
64         pmu->global_ctrl = data;
65
66         for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX)
67                 reprogram_counter(pmu, bit);
68 }
69
70 static unsigned intel_find_arch_event(struct kvm_pmu *pmu,
71                                       u8 event_select,
72                                       u8 unit_mask)
73 {
74         int i;
75
76         for (i = 0; i < ARRAY_SIZE(intel_arch_events); i++)
77                 if (intel_arch_events[i].eventsel == event_select
78                     && intel_arch_events[i].unit_mask == unit_mask
79                     && (pmu->available_event_types & (1 << i)))
80                         break;
81
82         if (i == ARRAY_SIZE(intel_arch_events))
83                 return PERF_COUNT_HW_MAX;
84
85         return intel_arch_events[i].event_type;
86 }
87
88 static unsigned intel_find_fixed_event(int idx)
89 {
90         u32 event;
91         size_t size = ARRAY_SIZE(fixed_pmc_events);
92
93         if (idx >= size)
94                 return PERF_COUNT_HW_MAX;
95
96         event = fixed_pmc_events[array_index_nospec(idx, size)];
97         return intel_arch_events[event].event_type;
98 }
99
100 /* check if a PMC is enabled by comparing it with globl_ctrl bits. */
101 static bool intel_pmc_is_enabled(struct kvm_pmc *pmc)
102 {
103         struct kvm_pmu *pmu = pmc_to_pmu(pmc);
104
105         return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl);
106 }
107
108 static struct kvm_pmc *intel_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx)
109 {
110         if (pmc_idx < INTEL_PMC_IDX_FIXED)
111                 return get_gp_pmc(pmu, MSR_P6_EVNTSEL0 + pmc_idx,
112                                   MSR_P6_EVNTSEL0);
113         else {
114                 u32 idx = pmc_idx - INTEL_PMC_IDX_FIXED;
115
116                 return get_fixed_pmc(pmu, idx + MSR_CORE_PERF_FIXED_CTR0);
117         }
118 }
119
120 /* returns 0 if idx's corresponding MSR exists; otherwise returns 1. */
121 static int intel_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx)
122 {
123         struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
124         bool fixed = idx & (1u << 30);
125
126         idx &= ~(3u << 30);
127
128         return (!fixed && idx >= pmu->nr_arch_gp_counters) ||
129                 (fixed && idx >= pmu->nr_arch_fixed_counters);
130 }
131
132 static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu,
133                                             unsigned idx, u64 *mask)
134 {
135         struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
136         bool fixed = idx & (1u << 30);
137         struct kvm_pmc *counters;
138         unsigned int num_counters;
139
140         idx &= ~(3u << 30);
141         if (fixed) {
142                 counters = pmu->fixed_counters;
143                 num_counters = pmu->nr_arch_fixed_counters;
144         } else {
145                 counters = pmu->gp_counters;
146                 num_counters = pmu->nr_arch_gp_counters;
147         }
148         if (idx >= num_counters)
149                 return NULL;
150         *mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
151         return &counters[array_index_nospec(idx, num_counters)];
152 }
153
154 static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
155 {
156         struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
157         int ret;
158
159         switch (msr) {
160         case MSR_CORE_PERF_FIXED_CTR_CTRL:
161         case MSR_CORE_PERF_GLOBAL_STATUS:
162         case MSR_CORE_PERF_GLOBAL_CTRL:
163         case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
164                 ret = pmu->version > 1;
165                 break;
166         default:
167                 ret = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0) ||
168                         get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0) ||
169                         get_fixed_pmc(pmu, msr);
170                 break;
171         }
172
173         return ret;
174 }
175
176 static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
177 {
178         struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
179         struct kvm_pmc *pmc;
180
181         switch (msr) {
182         case MSR_CORE_PERF_FIXED_CTR_CTRL:
183                 *data = pmu->fixed_ctr_ctrl;
184                 return 0;
185         case MSR_CORE_PERF_GLOBAL_STATUS:
186                 *data = pmu->global_status;
187                 return 0;
188         case MSR_CORE_PERF_GLOBAL_CTRL:
189                 *data = pmu->global_ctrl;
190                 return 0;
191         case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
192                 *data = pmu->global_ovf_ctrl;
193                 return 0;
194         default:
195                 if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0))) {
196                         u64 val = pmc_read_counter(pmc);
197                         *data = val & pmu->counter_bitmask[KVM_PMC_GP];
198                         return 0;
199                 } else if ((pmc = get_fixed_pmc(pmu, msr))) {
200                         u64 val = pmc_read_counter(pmc);
201                         *data = val & pmu->counter_bitmask[KVM_PMC_FIXED];
202                         return 0;
203                 } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
204                         *data = pmc->eventsel;
205                         return 0;
206                 }
207         }
208
209         return 1;
210 }
211
212 static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
213 {
214         struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
215         struct kvm_pmc *pmc;
216         u32 msr = msr_info->index;
217         u64 data = msr_info->data;
218
219         switch (msr) {
220         case MSR_CORE_PERF_FIXED_CTR_CTRL:
221                 if (pmu->fixed_ctr_ctrl == data)
222                         return 0;
223                 if (!(data & 0xfffffffffffff444ull)) {
224                         reprogram_fixed_counters(pmu, data);
225                         return 0;
226                 }
227                 break;
228         case MSR_CORE_PERF_GLOBAL_STATUS:
229                 if (msr_info->host_initiated) {
230                         pmu->global_status = data;
231                         return 0;
232                 }
233                 break; /* RO MSR */
234         case MSR_CORE_PERF_GLOBAL_CTRL:
235                 if (pmu->global_ctrl == data)
236                         return 0;
237                 if (!(data & pmu->global_ctrl_mask)) {
238                         global_ctrl_changed(pmu, data);
239                         return 0;
240                 }
241                 break;
242         case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
243                 if (!(data & (pmu->global_ctrl_mask & ~(3ull<<62)))) {
244                         if (!msr_info->host_initiated)
245                                 pmu->global_status &= ~data;
246                         pmu->global_ovf_ctrl = data;
247                         return 0;
248                 }
249                 break;
250         default:
251                 if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0))) {
252                         if (msr_info->host_initiated)
253                                 pmc->counter = data;
254                         else
255                                 pmc->counter = (s32)data;
256                         return 0;
257                 } else if ((pmc = get_fixed_pmc(pmu, msr))) {
258                         pmc->counter = data;
259                         return 0;
260                 } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
261                         if (data == pmc->eventsel)
262                                 return 0;
263                         if (!(data & pmu->reserved_bits)) {
264                                 reprogram_gp_counter(pmc, data);
265                                 return 0;
266                         }
267                 }
268         }
269
270         return 1;
271 }
272
273 static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
274 {
275         struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
276         struct kvm_cpuid_entry2 *entry;
277         union cpuid10_eax eax;
278         union cpuid10_edx edx;
279
280         pmu->nr_arch_gp_counters = 0;
281         pmu->nr_arch_fixed_counters = 0;
282         pmu->counter_bitmask[KVM_PMC_GP] = 0;
283         pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
284         pmu->version = 0;
285         pmu->reserved_bits = 0xffffffff00200000ull;
286
287         entry = kvm_find_cpuid_entry(vcpu, 0xa, 0);
288         if (!entry)
289                 return;
290         eax.full = entry->eax;
291         edx.full = entry->edx;
292
293         pmu->version = eax.split.version_id;
294         if (!pmu->version)
295                 return;
296
297         pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters,
298                                         INTEL_PMC_MAX_GENERIC);
299         pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << eax.split.bit_width) - 1;
300         pmu->available_event_types = ~entry->ebx &
301                                         ((1ull << eax.split.mask_length) - 1);
302
303         if (pmu->version == 1) {
304                 pmu->nr_arch_fixed_counters = 0;
305         } else {
306                 pmu->nr_arch_fixed_counters =
307                         min_t(int, edx.split.num_counters_fixed,
308                                 INTEL_PMC_MAX_FIXED);
309                 pmu->counter_bitmask[KVM_PMC_FIXED] =
310                         ((u64)1 << edx.split.bit_width_fixed) - 1;
311         }
312
313         pmu->global_ctrl = ((1ull << pmu->nr_arch_gp_counters) - 1) |
314                 (((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED);
315         pmu->global_ctrl_mask = ~pmu->global_ctrl;
316
317         entry = kvm_find_cpuid_entry(vcpu, 7, 0);
318         if (entry &&
319             (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
320             (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM)))
321                 pmu->reserved_bits ^= HSW_IN_TX|HSW_IN_TX_CHECKPOINTED;
322 }
323
324 static void intel_pmu_init(struct kvm_vcpu *vcpu)
325 {
326         int i;
327         struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
328
329         for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) {
330                 pmu->gp_counters[i].type = KVM_PMC_GP;
331                 pmu->gp_counters[i].vcpu = vcpu;
332                 pmu->gp_counters[i].idx = i;
333         }
334
335         for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) {
336                 pmu->fixed_counters[i].type = KVM_PMC_FIXED;
337                 pmu->fixed_counters[i].vcpu = vcpu;
338                 pmu->fixed_counters[i].idx = i + INTEL_PMC_IDX_FIXED;
339         }
340 }
341
342 static void intel_pmu_reset(struct kvm_vcpu *vcpu)
343 {
344         struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
345         int i;
346
347         for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) {
348                 struct kvm_pmc *pmc = &pmu->gp_counters[i];
349
350                 pmc_stop_counter(pmc);
351                 pmc->counter = pmc->eventsel = 0;
352         }
353
354         for (i = 0; i < INTEL_PMC_MAX_FIXED; i++)
355                 pmc_stop_counter(&pmu->fixed_counters[i]);
356
357         pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status =
358                 pmu->global_ovf_ctrl = 0;
359 }
360
361 struct kvm_pmu_ops intel_pmu_ops = {
362         .find_arch_event = intel_find_arch_event,
363         .find_fixed_event = intel_find_fixed_event,
364         .pmc_is_enabled = intel_pmc_is_enabled,
365         .pmc_idx_to_pmc = intel_pmc_idx_to_pmc,
366         .msr_idx_to_pmc = intel_msr_idx_to_pmc,
367         .is_valid_msr_idx = intel_is_valid_msr_idx,
368         .is_valid_msr = intel_is_valid_msr,
369         .get_msr = intel_pmu_get_msr,
370         .set_msr = intel_pmu_set_msr,
371         .refresh = intel_pmu_refresh,
372         .init = intel_pmu_init,
373         .reset = intel_pmu_reset,
374 };