blob: 24d213bd42d4993f7422155c04e3fd8abd6491ef [file] [log] [blame]
Gleb Natapovf5132b02011-11-10 14:57:22 +02001/*
Guo Chaoc7a70622012-06-28 15:23:08 +08002 * Kernel-based Virtual Machine -- Performance Monitoring Unit support
Gleb Natapovf5132b02011-11-10 14:57:22 +02003 *
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
15#include <linux/types.h>
16#include <linux/kvm_host.h>
17#include <linux/perf_event.h>
Nadav Amitd27aa7f2014-08-20 13:25:52 +030018#include <asm/perf_event.h>
Gleb Natapovf5132b02011-11-10 14:57:22 +020019#include "x86.h"
20#include "cpuid.h"
21#include "lapic.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020022#include "pmu.h"
Gleb Natapovf5132b02011-11-10 14:57:22 +020023
Wei Huang474a5bb2015-06-19 13:54:23 +020024static struct kvm_event_hw_type_mapping arch_events[] = {
Gleb Natapovf5132b02011-11-10 14:57:22 +020025 /* Index must match CPUID 0x0A.EBX bit vector */
26 [0] = { 0x3c, 0x00, PERF_COUNT_HW_CPU_CYCLES },
27 [1] = { 0xc0, 0x00, PERF_COUNT_HW_INSTRUCTIONS },
28 [2] = { 0x3c, 0x01, PERF_COUNT_HW_BUS_CYCLES },
29 [3] = { 0x2e, 0x4f, PERF_COUNT_HW_CACHE_REFERENCES },
30 [4] = { 0x2e, 0x41, PERF_COUNT_HW_CACHE_MISSES },
31 [5] = { 0xc4, 0x00, PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
32 [6] = { 0xc5, 0x00, PERF_COUNT_HW_BRANCH_MISSES },
Gleb Natapov62079d82012-02-26 16:55:42 +020033 [7] = { 0x00, 0x30, PERF_COUNT_HW_REF_CPU_CYCLES },
Gleb Natapovf5132b02011-11-10 14:57:22 +020034};
35
36/* mapping between fixed pmc index and arch_events array */
Xiubo Li52eb5a62015-03-13 17:39:45 +080037static int fixed_pmc_events[] = {1, 0, 7};
Gleb Natapovf5132b02011-11-10 14:57:22 +020038
39static bool pmc_is_gp(struct kvm_pmc *pmc)
40{
41 return pmc->type == KVM_PMC_GP;
42}
43
44static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
45{
Wei Huang212dba12015-06-19 14:00:33 +020046 struct kvm_pmu *pmu = pmc_to_pmu(pmc);
Gleb Natapovf5132b02011-11-10 14:57:22 +020047
48 return pmu->counter_bitmask[pmc->type];
49}
50
Wei Huangc6702c92015-06-19 13:44:45 +020051static inline bool pmc_is_enabled(struct kvm_pmc *pmc)
Gleb Natapovf5132b02011-11-10 14:57:22 +020052{
Wei Huang212dba12015-06-19 14:00:33 +020053 struct kvm_pmu *pmu = pmc_to_pmu(pmc);
Gleb Natapovf5132b02011-11-10 14:57:22 +020054 return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl);
55}
56
57static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,
58 u32 base)
59{
60 if (msr >= base && msr < base + pmu->nr_arch_gp_counters)
61 return &pmu->gp_counters[msr - base];
62 return NULL;
63}
64
65static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)
66{
67 int base = MSR_CORE_PERF_FIXED_CTR0;
68 if (msr >= base && msr < base + pmu->nr_arch_fixed_counters)
69 return &pmu->fixed_counters[msr - base];
70 return NULL;
71}
72
73static inline struct kvm_pmc *get_fixed_pmc_idx(struct kvm_pmu *pmu, int idx)
74{
75 return get_fixed_pmc(pmu, MSR_CORE_PERF_FIXED_CTR0 + idx);
76}
77
78static struct kvm_pmc *global_idx_to_pmc(struct kvm_pmu *pmu, int idx)
79{
Robert Richter15c7ad52012-06-20 20:46:33 +020080 if (idx < INTEL_PMC_IDX_FIXED)
Gleb Natapovf5132b02011-11-10 14:57:22 +020081 return get_gp_pmc(pmu, MSR_P6_EVNTSEL0 + idx, MSR_P6_EVNTSEL0);
82 else
Robert Richter15c7ad52012-06-20 20:46:33 +020083 return get_fixed_pmc_idx(pmu, idx - INTEL_PMC_IDX_FIXED);
Gleb Natapovf5132b02011-11-10 14:57:22 +020084}
85
Wei Huangc6702c92015-06-19 13:44:45 +020086void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu)
Gleb Natapovf5132b02011-11-10 14:57:22 +020087{
88 if (vcpu->arch.apic)
89 kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC);
90}
91
Wei Huangc6702c92015-06-19 13:44:45 +020092static void kvm_pmi_trigger_fn(struct irq_work *irq_work)
Gleb Natapovf5132b02011-11-10 14:57:22 +020093{
Wei Huang212dba12015-06-19 14:00:33 +020094 struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu, irq_work);
95 struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu);
Gleb Natapovf5132b02011-11-10 14:57:22 +020096
Wei Huangc6702c92015-06-19 13:44:45 +020097 kvm_pmu_deliver_pmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +020098}
99
100static void kvm_perf_overflow(struct perf_event *perf_event,
101 struct perf_sample_data *data,
102 struct pt_regs *regs)
103{
104 struct kvm_pmc *pmc = perf_event->overflow_handler_context;
Wei Huang212dba12015-06-19 14:00:33 +0200105 struct kvm_pmu *pmu = pmc_to_pmu(pmc);
Wei Huange84cfe42015-06-19 14:15:28 +0200106
107 if (!test_and_set_bit(pmc->idx,
108 (unsigned long *)&pmu->reprogram_pmi)) {
Nadav Amit671bd992014-04-18 03:35:08 +0300109 __set_bit(pmc->idx, (unsigned long *)&pmu->global_status);
110 kvm_make_request(KVM_REQ_PMU, pmc->vcpu);
111 }
Gleb Natapovf5132b02011-11-10 14:57:22 +0200112}
113
114static void kvm_perf_overflow_intr(struct perf_event *perf_event,
Wei Huange84cfe42015-06-19 14:15:28 +0200115 struct perf_sample_data *data,
116 struct pt_regs *regs)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200117{
118 struct kvm_pmc *pmc = perf_event->overflow_handler_context;
Wei Huang212dba12015-06-19 14:00:33 +0200119 struct kvm_pmu *pmu = pmc_to_pmu(pmc);
Wei Huange84cfe42015-06-19 14:15:28 +0200120
121 if (!test_and_set_bit(pmc->idx,
122 (unsigned long *)&pmu->reprogram_pmi)) {
Nadav Amit671bd992014-04-18 03:35:08 +0300123 __set_bit(pmc->idx, (unsigned long *)&pmu->global_status);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200124 kvm_make_request(KVM_REQ_PMU, pmc->vcpu);
Wei Huange84cfe42015-06-19 14:15:28 +0200125
Gleb Natapovf5132b02011-11-10 14:57:22 +0200126 /*
127 * Inject PMI. If vcpu was in a guest mode during NMI PMI
128 * can be ejected on a guest mode re-entry. Otherwise we can't
129 * be sure that vcpu wasn't executing hlt instruction at the
Wei Huange84cfe42015-06-19 14:15:28 +0200130 * time of vmexit and is not going to re-enter guest mode until
Gleb Natapovf5132b02011-11-10 14:57:22 +0200131 * woken up. So we should wake it, but this is impossible from
132 * NMI context. Do it from irq work instead.
133 */
134 if (!kvm_is_in_guest())
Wei Huang212dba12015-06-19 14:00:33 +0200135 irq_work_queue(&pmc_to_pmu(pmc)->irq_work);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200136 else
137 kvm_make_request(KVM_REQ_PMI, pmc->vcpu);
138 }
139}
140
Wei Huangc6702c92015-06-19 13:44:45 +0200141static u64 pmc_read_counter(struct kvm_pmc *pmc)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200142{
143 u64 counter, enabled, running;
144
145 counter = pmc->counter;
146
147 if (pmc->perf_event)
148 counter += perf_event_read_value(pmc->perf_event,
149 &enabled, &running);
150
151 /* FIXME: Scaling needed? */
152
153 return counter & pmc_bitmask(pmc);
154}
155
Wei Huangc6702c92015-06-19 13:44:45 +0200156static void pmc_stop_counter(struct kvm_pmc *pmc)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200157{
158 if (pmc->perf_event) {
Wei Huangc6702c92015-06-19 13:44:45 +0200159 pmc->counter = pmc_read_counter(pmc);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200160 perf_event_release_kernel(pmc->perf_event);
161 pmc->perf_event = NULL;
162 }
163}
164
Wei Huangc6702c92015-06-19 13:44:45 +0200165static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type,
Wei Huange84cfe42015-06-19 14:15:28 +0200166 unsigned config, bool exclude_user,
167 bool exclude_kernel, bool intr,
168 bool in_tx, bool in_tx_cp)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200169{
170 struct perf_event *event;
171 struct perf_event_attr attr = {
172 .type = type,
173 .size = sizeof(attr),
174 .pinned = true,
175 .exclude_idle = true,
176 .exclude_host = 1,
177 .exclude_user = exclude_user,
178 .exclude_kernel = exclude_kernel,
179 .config = config,
180 };
Wei Huange84cfe42015-06-19 14:15:28 +0200181
Andi Kleen103af0a2013-07-18 15:57:02 -0700182 if (in_tx)
183 attr.config |= HSW_IN_TX;
184 if (in_tx_cp)
185 attr.config |= HSW_IN_TX_CHECKPOINTED;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200186
187 attr.sample_period = (-pmc->counter) & pmc_bitmask(pmc);
188
189 event = perf_event_create_kernel_counter(&attr, -1, current,
190 intr ? kvm_perf_overflow_intr :
191 kvm_perf_overflow, pmc);
192 if (IS_ERR(event)) {
Wei Huange84cfe42015-06-19 14:15:28 +0200193 printk_once("kvm_pmu: event creation failed %ld\n",
194 PTR_ERR(event));
Gleb Natapovf5132b02011-11-10 14:57:22 +0200195 return;
196 }
197
198 pmc->perf_event = event;
Wei Huang212dba12015-06-19 14:00:33 +0200199 clear_bit(pmc->idx, (unsigned long*)&pmc_to_pmu(pmc)->reprogram_pmi);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200200}
201
202static unsigned find_arch_event(struct kvm_pmu *pmu, u8 event_select,
203 u8 unit_mask)
204{
205 int i;
206
207 for (i = 0; i < ARRAY_SIZE(arch_events); i++)
208 if (arch_events[i].eventsel == event_select
209 && arch_events[i].unit_mask == unit_mask
210 && (pmu->available_event_types & (1 << i)))
211 break;
212
213 if (i == ARRAY_SIZE(arch_events))
214 return PERF_COUNT_HW_MAX;
215
216 return arch_events[i].event_type;
217}
218
219static void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
220{
221 unsigned config, type = PERF_TYPE_RAW;
222 u8 event_select, unit_mask;
223
Gleb Natapova7b9d2c2012-02-26 16:55:40 +0200224 if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL)
225 printk_once("kvm pmu: pin control bit is ignored\n");
226
Gleb Natapovf5132b02011-11-10 14:57:22 +0200227 pmc->eventsel = eventsel;
228
Wei Huangc6702c92015-06-19 13:44:45 +0200229 pmc_stop_counter(pmc);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200230
Wei Huangc6702c92015-06-19 13:44:45 +0200231 if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_is_enabled(pmc))
Gleb Natapovf5132b02011-11-10 14:57:22 +0200232 return;
233
234 event_select = eventsel & ARCH_PERFMON_EVENTSEL_EVENT;
235 unit_mask = (eventsel & ARCH_PERFMON_EVENTSEL_UMASK) >> 8;
236
Gleb Natapovfac33682012-02-26 16:55:41 +0200237 if (!(eventsel & (ARCH_PERFMON_EVENTSEL_EDGE |
Wei Huange84cfe42015-06-19 14:15:28 +0200238 ARCH_PERFMON_EVENTSEL_INV |
239 ARCH_PERFMON_EVENTSEL_CMASK |
240 HSW_IN_TX |
241 HSW_IN_TX_CHECKPOINTED))) {
Wei Huang212dba12015-06-19 14:00:33 +0200242 config = find_arch_event(pmc_to_pmu(pmc), event_select,
Gleb Natapovf5132b02011-11-10 14:57:22 +0200243 unit_mask);
244 if (config != PERF_COUNT_HW_MAX)
245 type = PERF_TYPE_HARDWARE;
246 }
247
248 if (type == PERF_TYPE_RAW)
249 config = eventsel & X86_RAW_EVENT_MASK;
250
Wei Huangc6702c92015-06-19 13:44:45 +0200251 pmc_reprogram_counter(pmc, type, config,
Wei Huange84cfe42015-06-19 14:15:28 +0200252 !(eventsel & ARCH_PERFMON_EVENTSEL_USR),
253 !(eventsel & ARCH_PERFMON_EVENTSEL_OS),
254 eventsel & ARCH_PERFMON_EVENTSEL_INT,
255 (eventsel & HSW_IN_TX),
256 (eventsel & HSW_IN_TX_CHECKPOINTED));
Gleb Natapovf5132b02011-11-10 14:57:22 +0200257}
258
Wei Huange84cfe42015-06-19 14:15:28 +0200259static void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int idx)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200260{
Wei Huange84cfe42015-06-19 14:15:28 +0200261 unsigned en_field = ctrl & 0x3;
262 bool pmi = ctrl & 0x8;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200263
Wei Huangc6702c92015-06-19 13:44:45 +0200264 pmc_stop_counter(pmc);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200265
Wei Huange84cfe42015-06-19 14:15:28 +0200266 if (!en_field || !pmc_is_enabled(pmc))
Gleb Natapovf5132b02011-11-10 14:57:22 +0200267 return;
268
Wei Huangc6702c92015-06-19 13:44:45 +0200269 pmc_reprogram_counter(pmc, PERF_TYPE_HARDWARE,
Wei Huange84cfe42015-06-19 14:15:28 +0200270 arch_events[fixed_pmc_events[idx]].event_type,
271 !(en_field & 0x2), /* exclude user */
272 !(en_field & 0x1), /* exclude kernel */
273 pmi, false, false);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200274}
275
Wei Huangc6702c92015-06-19 13:44:45 +0200276static inline u8 fixed_ctrl_field(u64 ctrl, int idx)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200277{
278 return (ctrl >> (idx * 4)) & 0xf;
279}
280
281static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
282{
283 int i;
284
285 for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
Wei Huange84cfe42015-06-19 14:15:28 +0200286 u8 old_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i);
287 u8 new_ctrl = fixed_ctrl_field(data, i);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200288 struct kvm_pmc *pmc = get_fixed_pmc_idx(pmu, i);
289
Wei Huange84cfe42015-06-19 14:15:28 +0200290 if (old_ctrl == new_ctrl)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200291 continue;
292
Wei Huange84cfe42015-06-19 14:15:28 +0200293 reprogram_fixed_counter(pmc, new_ctrl, i);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200294 }
295
296 pmu->fixed_ctr_ctrl = data;
297}
298
Wei Huange84cfe42015-06-19 14:15:28 +0200299static void reprogram_counter(struct kvm_pmu *pmu, int pmc_idx)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200300{
Wei Huange84cfe42015-06-19 14:15:28 +0200301 struct kvm_pmc *pmc = global_idx_to_pmc(pmu, pmc_idx);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200302
303 if (!pmc)
304 return;
305
306 if (pmc_is_gp(pmc))
307 reprogram_gp_counter(pmc, pmc->eventsel);
308 else {
Wei Huange84cfe42015-06-19 14:15:28 +0200309 int idx = pmc_idx - INTEL_PMC_IDX_FIXED;
310 u8 ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, idx);
311
312 reprogram_fixed_counter(pmc, ctrl, idx);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200313 }
314}
315
316static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data)
317{
318 int bit;
319 u64 diff = pmu->global_ctrl ^ data;
320
321 pmu->global_ctrl = data;
322
323 for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX)
Wei Huangc6702c92015-06-19 13:44:45 +0200324 reprogram_counter(pmu, bit);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200325}
326
Wei Huangc6702c92015-06-19 13:44:45 +0200327bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200328{
Wei Huang212dba12015-06-19 14:00:33 +0200329 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200330 int ret;
331
332 switch (msr) {
333 case MSR_CORE_PERF_FIXED_CTR_CTRL:
334 case MSR_CORE_PERF_GLOBAL_STATUS:
335 case MSR_CORE_PERF_GLOBAL_CTRL:
336 case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
337 ret = pmu->version > 1;
338 break;
339 default:
340 ret = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)
341 || get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0)
342 || get_fixed_pmc(pmu, msr);
343 break;
344 }
345 return ret;
346}
347
348int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
349{
Wei Huang212dba12015-06-19 14:00:33 +0200350 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200351 struct kvm_pmc *pmc;
352
353 switch (index) {
354 case MSR_CORE_PERF_FIXED_CTR_CTRL:
355 *data = pmu->fixed_ctr_ctrl;
356 return 0;
357 case MSR_CORE_PERF_GLOBAL_STATUS:
358 *data = pmu->global_status;
359 return 0;
360 case MSR_CORE_PERF_GLOBAL_CTRL:
361 *data = pmu->global_ctrl;
362 return 0;
363 case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
364 *data = pmu->global_ovf_ctrl;
365 return 0;
366 default:
367 if ((pmc = get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) ||
368 (pmc = get_fixed_pmc(pmu, index))) {
Wei Huangc6702c92015-06-19 13:44:45 +0200369 *data = pmc_read_counter(pmc);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200370 return 0;
371 } else if ((pmc = get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) {
372 *data = pmc->eventsel;
373 return 0;
374 }
375 }
376 return 1;
377}
378
Paolo Bonziniafd80d82013-03-28 17:18:35 +0100379int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200380{
Wei Huang212dba12015-06-19 14:00:33 +0200381 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200382 struct kvm_pmc *pmc;
Paolo Bonziniafd80d82013-03-28 17:18:35 +0100383 u32 index = msr_info->index;
384 u64 data = msr_info->data;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200385
386 switch (index) {
387 case MSR_CORE_PERF_FIXED_CTR_CTRL:
388 if (pmu->fixed_ctr_ctrl == data)
389 return 0;
Sasikantha babufea52952012-03-21 18:49:00 +0530390 if (!(data & 0xfffffffffffff444ull)) {
Gleb Natapovf5132b02011-11-10 14:57:22 +0200391 reprogram_fixed_counters(pmu, data);
392 return 0;
393 }
394 break;
395 case MSR_CORE_PERF_GLOBAL_STATUS:
Paolo Bonziniafd80d82013-03-28 17:18:35 +0100396 if (msr_info->host_initiated) {
397 pmu->global_status = data;
398 return 0;
399 }
Gleb Natapovf5132b02011-11-10 14:57:22 +0200400 break; /* RO MSR */
401 case MSR_CORE_PERF_GLOBAL_CTRL:
402 if (pmu->global_ctrl == data)
403 return 0;
404 if (!(data & pmu->global_ctrl_mask)) {
405 global_ctrl_changed(pmu, data);
406 return 0;
407 }
408 break;
409 case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
410 if (!(data & (pmu->global_ctrl_mask & ~(3ull<<62)))) {
Paolo Bonziniafd80d82013-03-28 17:18:35 +0100411 if (!msr_info->host_initiated)
412 pmu->global_status &= ~data;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200413 pmu->global_ovf_ctrl = data;
414 return 0;
415 }
416 break;
417 default:
418 if ((pmc = get_gp_pmc(pmu, index, MSR_IA32_PERFCTR0)) ||
419 (pmc = get_fixed_pmc(pmu, index))) {
Paolo Bonziniafd80d82013-03-28 17:18:35 +0100420 if (!msr_info->host_initiated)
421 data = (s64)(s32)data;
Wei Huangc6702c92015-06-19 13:44:45 +0200422 pmc->counter += data - pmc_read_counter(pmc);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200423 return 0;
424 } else if ((pmc = get_gp_pmc(pmu, index, MSR_P6_EVNTSEL0))) {
425 if (data == pmc->eventsel)
426 return 0;
Andi Kleen103af0a2013-07-18 15:57:02 -0700427 if (!(data & pmu->reserved_bits)) {
Gleb Natapovf5132b02011-11-10 14:57:22 +0200428 reprogram_gp_counter(pmc, data);
429 return 0;
430 }
431 }
432 }
433 return 1;
434}
435
Wei Huange84cfe42015-06-19 14:15:28 +0200436/* check if idx is a valid index to access PMU */
437int kvm_pmu_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx)
Nadav Amit67f4d422014-06-02 18:34:09 +0300438{
Wei Huang212dba12015-06-19 14:00:33 +0200439 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Wei Huange84cfe42015-06-19 14:15:28 +0200440 bool fixed = idx & (1u << 30);
441 idx &= ~(3u << 30);
442 return (!fixed && idx >= pmu->nr_arch_gp_counters) ||
443 (fixed && idx >= pmu->nr_arch_fixed_counters);
Nadav Amit67f4d422014-06-02 18:34:09 +0300444}
445
Wei Huange84cfe42015-06-19 14:15:28 +0200446int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200447{
Wei Huang212dba12015-06-19 14:00:33 +0200448 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Wei Huange84cfe42015-06-19 14:15:28 +0200449 bool fast_mode = idx & (1u << 31);
450 bool fixed = idx & (1u << 30);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200451 struct kvm_pmc *counters;
Wei Huange84cfe42015-06-19 14:15:28 +0200452 u64 ctr_val;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200453
Wei Huange84cfe42015-06-19 14:15:28 +0200454 idx &= ~(3u << 30);
455 if (!fixed && idx >= pmu->nr_arch_gp_counters)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200456 return 1;
Wei Huange84cfe42015-06-19 14:15:28 +0200457 if (fixed && idx >= pmu->nr_arch_fixed_counters)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200458 return 1;
459 counters = fixed ? pmu->fixed_counters : pmu->gp_counters;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200460
Wei Huange84cfe42015-06-19 14:15:28 +0200461 ctr_val = pmc_read_counter(&counters[idx]);
462 if (fast_mode)
463 ctr_val = (u32)ctr_val;
464
465 *data = ctr_val;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200466 return 0;
467}
468
Wei Huange84cfe42015-06-19 14:15:28 +0200469/* refresh PMU settings. This function generally is called when underlying
470 * settings are changed (such as changes of PMU CPUID by guest VMs), which
471 * should rarely happen.
472 */
Wei Huangc6702c92015-06-19 13:44:45 +0200473void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200474{
Wei Huang212dba12015-06-19 14:00:33 +0200475 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200476 struct kvm_cpuid_entry2 *entry;
Nadav Amitd27aa7f2014-08-20 13:25:52 +0300477 union cpuid10_eax eax;
478 union cpuid10_edx edx;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200479
480 pmu->nr_arch_gp_counters = 0;
481 pmu->nr_arch_fixed_counters = 0;
482 pmu->counter_bitmask[KVM_PMC_GP] = 0;
483 pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
484 pmu->version = 0;
Andi Kleen103af0a2013-07-18 15:57:02 -0700485 pmu->reserved_bits = 0xffffffff00200000ull;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200486
487 entry = kvm_find_cpuid_entry(vcpu, 0xa, 0);
488 if (!entry)
489 return;
Nadav Amitd27aa7f2014-08-20 13:25:52 +0300490 eax.full = entry->eax;
491 edx.full = entry->edx;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200492
Nadav Amitd27aa7f2014-08-20 13:25:52 +0300493 pmu->version = eax.split.version_id;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200494 if (!pmu->version)
495 return;
496
Nadav Amitd27aa7f2014-08-20 13:25:52 +0300497 pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters,
498 INTEL_PMC_MAX_GENERIC);
499 pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << eax.split.bit_width) - 1;
500 pmu->available_event_types = ~entry->ebx &
501 ((1ull << eax.split.mask_length) - 1);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200502
503 if (pmu->version == 1) {
Gleb Natapovf19a0c22012-04-09 17:38:35 +0300504 pmu->nr_arch_fixed_counters = 0;
505 } else {
Nadav Amitd27aa7f2014-08-20 13:25:52 +0300506 pmu->nr_arch_fixed_counters =
507 min_t(int, edx.split.num_counters_fixed,
Robert Richter15c7ad52012-06-20 20:46:33 +0200508 INTEL_PMC_MAX_FIXED);
Gleb Natapovf19a0c22012-04-09 17:38:35 +0300509 pmu->counter_bitmask[KVM_PMC_FIXED] =
Nadav Amitd27aa7f2014-08-20 13:25:52 +0300510 ((u64)1 << edx.split.bit_width_fixed) - 1;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200511 }
512
Gleb Natapovf19a0c22012-04-09 17:38:35 +0300513 pmu->global_ctrl = ((1 << pmu->nr_arch_gp_counters) - 1) |
Robert Richter15c7ad52012-06-20 20:46:33 +0200514 (((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED);
Gleb Natapovf19a0c22012-04-09 17:38:35 +0300515 pmu->global_ctrl_mask = ~pmu->global_ctrl;
Andi Kleen103af0a2013-07-18 15:57:02 -0700516
517 entry = kvm_find_cpuid_entry(vcpu, 7, 0);
518 if (entry &&
519 (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
520 (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM)))
521 pmu->reserved_bits ^= HSW_IN_TX|HSW_IN_TX_CHECKPOINTED;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200522}
523
524void kvm_pmu_init(struct kvm_vcpu *vcpu)
525{
526 int i;
Wei Huang212dba12015-06-19 14:00:33 +0200527 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200528
529 memset(pmu, 0, sizeof(*pmu));
Robert Richter15c7ad52012-06-20 20:46:33 +0200530 for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) {
Gleb Natapovf5132b02011-11-10 14:57:22 +0200531 pmu->gp_counters[i].type = KVM_PMC_GP;
532 pmu->gp_counters[i].vcpu = vcpu;
533 pmu->gp_counters[i].idx = i;
534 }
Robert Richter15c7ad52012-06-20 20:46:33 +0200535 for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) {
Gleb Natapovf5132b02011-11-10 14:57:22 +0200536 pmu->fixed_counters[i].type = KVM_PMC_FIXED;
537 pmu->fixed_counters[i].vcpu = vcpu;
Robert Richter15c7ad52012-06-20 20:46:33 +0200538 pmu->fixed_counters[i].idx = i + INTEL_PMC_IDX_FIXED;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200539 }
Wei Huangc6702c92015-06-19 13:44:45 +0200540 init_irq_work(&pmu->irq_work, kvm_pmi_trigger_fn);
541 kvm_pmu_refresh(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200542}
543
544void kvm_pmu_reset(struct kvm_vcpu *vcpu)
545{
Wei Huang212dba12015-06-19 14:00:33 +0200546 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200547 int i;
548
549 irq_work_sync(&pmu->irq_work);
Robert Richter15c7ad52012-06-20 20:46:33 +0200550 for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) {
Gleb Natapovf5132b02011-11-10 14:57:22 +0200551 struct kvm_pmc *pmc = &pmu->gp_counters[i];
Wei Huangc6702c92015-06-19 13:44:45 +0200552 pmc_stop_counter(pmc);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200553 pmc->counter = pmc->eventsel = 0;
554 }
555
Robert Richter15c7ad52012-06-20 20:46:33 +0200556 for (i = 0; i < INTEL_PMC_MAX_FIXED; i++)
Wei Huangc6702c92015-06-19 13:44:45 +0200557 pmc_stop_counter(&pmu->fixed_counters[i]);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200558
559 pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status =
560 pmu->global_ovf_ctrl = 0;
561}
562
563void kvm_pmu_destroy(struct kvm_vcpu *vcpu)
564{
565 kvm_pmu_reset(vcpu);
566}
567
Wei Huangc6702c92015-06-19 13:44:45 +0200568void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200569{
Wei Huang212dba12015-06-19 14:00:33 +0200570 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200571 u64 bitmask;
572 int bit;
573
574 bitmask = pmu->reprogram_pmi;
575
576 for_each_set_bit(bit, (unsigned long *)&bitmask, X86_PMC_IDX_MAX) {
577 struct kvm_pmc *pmc = global_idx_to_pmc(pmu, bit);
578
579 if (unlikely(!pmc || !pmc->perf_event)) {
580 clear_bit(bit, (unsigned long *)&pmu->reprogram_pmi);
581 continue;
582 }
583
Wei Huangc6702c92015-06-19 13:44:45 +0200584 reprogram_counter(pmu, bit);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200585 }
586}