blob: 9b83857da195e11107b3939f1e3d0b4c0012fb9e [file] [log] [blame]
Shannon Zhao051ff582015-12-08 15:29:06 +08001/*
2 * Copyright (C) 2015 Linaro Ltd.
3 * Author: Shannon Zhao <shannon.zhao@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <linux/cpu.h>
19#include <linux/kvm.h>
20#include <linux/kvm_host.h>
21#include <linux/perf_event.h>
22#include <asm/kvm_emulate.h>
23#include <kvm/arm_pmu.h>
Shannon Zhaob02386e2016-02-26 19:29:19 +080024#include <kvm/arm_vgic.h>
Shannon Zhao051ff582015-12-08 15:29:06 +080025
26/**
27 * kvm_pmu_get_counter_value - get PMU counter value
28 * @vcpu: The vcpu pointer
29 * @select_idx: The counter index
30 */
31u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
32{
33 u64 counter, reg, enabled, running;
34 struct kvm_pmu *pmu = &vcpu->arch.pmu;
35 struct kvm_pmc *pmc = &pmu->pmc[select_idx];
36
37 reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
38 ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx;
39 counter = vcpu_sys_reg(vcpu, reg);
40
41 /* The real counter value is equal to the value of counter register plus
42 * the value perf event counts.
43 */
44 if (pmc->perf_event)
45 counter += perf_event_read_value(pmc->perf_event, &enabled,
46 &running);
47
48 return counter & pmc->bitmask;
49}
50
51/**
52 * kvm_pmu_set_counter_value - set PMU counter value
53 * @vcpu: The vcpu pointer
54 * @select_idx: The counter index
55 * @val: The counter value
56 */
57void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
58{
59 u64 reg;
60
61 reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
62 ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx;
63 vcpu_sys_reg(vcpu, reg) += (s64)val - kvm_pmu_get_counter_value(vcpu, select_idx);
64}
Shannon Zhao96b0eeb2015-09-08 12:26:13 +080065
Shannon Zhao7f766352015-07-03 14:27:25 +080066/**
67 * kvm_pmu_stop_counter - stop PMU counter
68 * @pmc: The PMU counter pointer
69 *
70 * If this counter has been configured to monitor some event, release it here.
71 */
72static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
73{
74 u64 counter, reg;
75
76 if (pmc->perf_event) {
77 counter = kvm_pmu_get_counter_value(vcpu, pmc->idx);
78 reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
79 ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
80 vcpu_sys_reg(vcpu, reg) = counter;
81 perf_event_disable(pmc->perf_event);
82 perf_event_release_kernel(pmc->perf_event);
83 pmc->perf_event = NULL;
84 }
85}
86
Shannon Zhao2aa36e92015-09-11 11:30:22 +080087/**
88 * kvm_pmu_vcpu_reset - reset pmu state for cpu
89 * @vcpu: The vcpu pointer
90 *
91 */
92void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu)
93{
94 int i;
95 struct kvm_pmu *pmu = &vcpu->arch.pmu;
96
97 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
98 kvm_pmu_stop_counter(vcpu, &pmu->pmc[i]);
99 pmu->pmc[i].idx = i;
100 pmu->pmc[i].bitmask = 0xffffffffUL;
101 }
102}
103
Shannon Zhao5f0a7142015-09-11 15:18:05 +0800104/**
105 * kvm_pmu_vcpu_destroy - free perf event of PMU for cpu
106 * @vcpu: The vcpu pointer
107 *
108 */
109void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
110{
111 int i;
112 struct kvm_pmu *pmu = &vcpu->arch.pmu;
113
114 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
115 struct kvm_pmc *pmc = &pmu->pmc[i];
116
117 if (pmc->perf_event) {
118 perf_event_disable(pmc->perf_event);
119 perf_event_release_kernel(pmc->perf_event);
120 pmc->perf_event = NULL;
121 }
122 }
123}
124
Shannon Zhao96b0eeb2015-09-08 12:26:13 +0800125u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
126{
127 u64 val = vcpu_sys_reg(vcpu, PMCR_EL0) >> ARMV8_PMU_PMCR_N_SHIFT;
128
129 val &= ARMV8_PMU_PMCR_N_MASK;
130 if (val == 0)
131 return BIT(ARMV8_PMU_CYCLE_IDX);
132 else
133 return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX);
134}
135
136/**
137 * kvm_pmu_enable_counter - enable selected PMU counter
138 * @vcpu: The vcpu pointer
139 * @val: the value guest writes to PMCNTENSET register
140 *
141 * Call perf_event_enable to start counting the perf event
142 */
143void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val)
144{
145 int i;
146 struct kvm_pmu *pmu = &vcpu->arch.pmu;
147 struct kvm_pmc *pmc;
148
149 if (!(vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) || !val)
150 return;
151
152 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
153 if (!(val & BIT(i)))
154 continue;
155
156 pmc = &pmu->pmc[i];
157 if (pmc->perf_event) {
158 perf_event_enable(pmc->perf_event);
159 if (pmc->perf_event->state != PERF_EVENT_STATE_ACTIVE)
160 kvm_debug("fail to enable perf event\n");
161 }
162 }
163}
164
165/**
166 * kvm_pmu_disable_counter - disable selected PMU counter
167 * @vcpu: The vcpu pointer
168 * @val: the value guest writes to PMCNTENCLR register
169 *
170 * Call perf_event_disable to stop counting the perf event
171 */
172void kvm_pmu_disable_counter(struct kvm_vcpu *vcpu, u64 val)
173{
174 int i;
175 struct kvm_pmu *pmu = &vcpu->arch.pmu;
176 struct kvm_pmc *pmc;
177
178 if (!val)
179 return;
180
181 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
182 if (!(val & BIT(i)))
183 continue;
184
185 pmc = &pmu->pmc[i];
186 if (pmc->perf_event)
187 perf_event_disable(pmc->perf_event);
188 }
189}
Shannon Zhao7f766352015-07-03 14:27:25 +0800190
Shannon Zhao76d883c2015-09-08 15:03:26 +0800191static u64 kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
192{
193 u64 reg = 0;
194
195 if ((vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E))
196 reg = vcpu_sys_reg(vcpu, PMOVSSET_EL0);
197 reg &= vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
198 reg &= vcpu_sys_reg(vcpu, PMINTENSET_EL1);
199 reg &= kvm_pmu_valid_counter_mask(vcpu);
200
201 return reg;
202}
203
204/**
205 * kvm_pmu_overflow_set - set PMU overflow interrupt
206 * @vcpu: The vcpu pointer
207 * @val: the value guest writes to PMOVSSET register
208 */
209void kvm_pmu_overflow_set(struct kvm_vcpu *vcpu, u64 val)
210{
211 u64 reg;
212
213 if (val == 0)
214 return;
215
216 vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= val;
217 reg = kvm_pmu_overflow_status(vcpu);
218 if (reg != 0)
219 kvm_vcpu_kick(vcpu);
220}
221
Shannon Zhaob02386e2016-02-26 19:29:19 +0800222static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
223{
224 struct kvm_pmu *pmu = &vcpu->arch.pmu;
225 bool overflow;
226
227 if (!kvm_arm_pmu_v3_ready(vcpu))
228 return;
229
230 overflow = !!kvm_pmu_overflow_status(vcpu);
231 if (pmu->irq_level != overflow) {
232 pmu->irq_level = overflow;
233 kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
234 pmu->irq_num, overflow);
235 }
236}
237
238/**
239 * kvm_pmu_flush_hwstate - flush pmu state to cpu
240 * @vcpu: The vcpu pointer
241 *
242 * Check if the PMU has overflowed while we were running in the host, and inject
243 * an interrupt if that was the case.
244 */
245void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu)
246{
247 kvm_pmu_update_state(vcpu);
248}
249
250/**
251 * kvm_pmu_sync_hwstate - sync pmu state from cpu
252 * @vcpu: The vcpu pointer
253 *
254 * Check if the PMU has overflowed while we were running in the guest, and
255 * inject an interrupt if that was the case.
256 */
257void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu)
258{
259 kvm_pmu_update_state(vcpu);
260}
261
262static inline struct kvm_vcpu *kvm_pmc_to_vcpu(struct kvm_pmc *pmc)
263{
264 struct kvm_pmu *pmu;
265 struct kvm_vcpu_arch *vcpu_arch;
266
267 pmc -= pmc->idx;
268 pmu = container_of(pmc, struct kvm_pmu, pmc[0]);
269 vcpu_arch = container_of(pmu, struct kvm_vcpu_arch, pmu);
270 return container_of(vcpu_arch, struct kvm_vcpu, arch);
271}
272
273/**
274 * When perf event overflows, call kvm_pmu_overflow_set to set overflow status.
275 */
276static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
277 struct perf_sample_data *data,
278 struct pt_regs *regs)
279{
280 struct kvm_pmc *pmc = perf_event->overflow_handler_context;
281 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
282 int idx = pmc->idx;
283
284 kvm_pmu_overflow_set(vcpu, BIT(idx));
285}
286
Shannon Zhao7a0adc72015-09-08 15:49:39 +0800287/**
288 * kvm_pmu_software_increment - do software increment
289 * @vcpu: The vcpu pointer
290 * @val: the value guest writes to PMSWINC register
291 */
292void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val)
293{
294 int i;
295 u64 type, enable, reg;
296
297 if (val == 0)
298 return;
299
300 enable = vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
301 for (i = 0; i < ARMV8_PMU_CYCLE_IDX; i++) {
302 if (!(val & BIT(i)))
303 continue;
304 type = vcpu_sys_reg(vcpu, PMEVTYPER0_EL0 + i)
305 & ARMV8_PMU_EVTYPE_EVENT;
306 if ((type == ARMV8_PMU_EVTYPE_EVENT_SW_INCR)
307 && (enable & BIT(i))) {
308 reg = vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i) + 1;
309 reg = lower_32_bits(reg);
310 vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i) = reg;
311 if (!reg)
312 kvm_pmu_overflow_set(vcpu, BIT(i));
313 }
314 }
315}
316
Shannon Zhao76993732015-10-28 12:10:30 +0800317/**
318 * kvm_pmu_handle_pmcr - handle PMCR register
319 * @vcpu: The vcpu pointer
320 * @val: the value guest writes to PMCR register
321 */
322void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
323{
324 struct kvm_pmu *pmu = &vcpu->arch.pmu;
325 struct kvm_pmc *pmc;
326 u64 mask;
327 int i;
328
329 mask = kvm_pmu_valid_counter_mask(vcpu);
330 if (val & ARMV8_PMU_PMCR_E) {
331 kvm_pmu_enable_counter(vcpu,
332 vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & mask);
333 } else {
334 kvm_pmu_disable_counter(vcpu, mask);
335 }
336
337 if (val & ARMV8_PMU_PMCR_C)
338 kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0);
339
340 if (val & ARMV8_PMU_PMCR_P) {
341 for (i = 0; i < ARMV8_PMU_CYCLE_IDX; i++)
342 kvm_pmu_set_counter_value(vcpu, i, 0);
343 }
344
345 if (val & ARMV8_PMU_PMCR_LC) {
346 pmc = &pmu->pmc[ARMV8_PMU_CYCLE_IDX];
347 pmc->bitmask = 0xffffffffffffffffUL;
348 }
349}
350
Shannon Zhao7f766352015-07-03 14:27:25 +0800351static bool kvm_pmu_counter_is_enabled(struct kvm_vcpu *vcpu, u64 select_idx)
352{
353 return (vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) &&
354 (vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & BIT(select_idx));
355}
356
357/**
358 * kvm_pmu_set_counter_event_type - set selected counter to monitor some event
359 * @vcpu: The vcpu pointer
360 * @data: The data guest writes to PMXEVTYPER_EL0
361 * @select_idx: The number of selected counter
362 *
363 * When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an
364 * event with given hardware event number. Here we call perf_event API to
365 * emulate this action and create a kernel perf event for it.
366 */
367void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
368 u64 select_idx)
369{
370 struct kvm_pmu *pmu = &vcpu->arch.pmu;
371 struct kvm_pmc *pmc = &pmu->pmc[select_idx];
372 struct perf_event *event;
373 struct perf_event_attr attr;
374 u64 eventsel, counter;
375
376 kvm_pmu_stop_counter(vcpu, pmc);
377 eventsel = data & ARMV8_PMU_EVTYPE_EVENT;
378
Shannon Zhao7a0adc72015-09-08 15:49:39 +0800379 /* Software increment event does't need to be backed by a perf event */
380 if (eventsel == ARMV8_PMU_EVTYPE_EVENT_SW_INCR)
381 return;
382
Shannon Zhao7f766352015-07-03 14:27:25 +0800383 memset(&attr, 0, sizeof(struct perf_event_attr));
384 attr.type = PERF_TYPE_RAW;
385 attr.size = sizeof(attr);
386 attr.pinned = 1;
387 attr.disabled = !kvm_pmu_counter_is_enabled(vcpu, select_idx);
388 attr.exclude_user = data & ARMV8_PMU_EXCLUDE_EL0 ? 1 : 0;
389 attr.exclude_kernel = data & ARMV8_PMU_EXCLUDE_EL1 ? 1 : 0;
390 attr.exclude_hv = 1; /* Don't count EL2 events */
391 attr.exclude_host = 1; /* Don't count host events */
392 attr.config = eventsel;
393
394 counter = kvm_pmu_get_counter_value(vcpu, select_idx);
395 /* The initial sample period (overflow count) of an event. */
396 attr.sample_period = (-counter) & pmc->bitmask;
397
Shannon Zhaob02386e2016-02-26 19:29:19 +0800398 event = perf_event_create_kernel_counter(&attr, -1, current,
399 kvm_pmu_perf_overflow, pmc);
Shannon Zhao7f766352015-07-03 14:27:25 +0800400 if (IS_ERR(event)) {
401 pr_err_once("kvm: pmu event creation failed %ld\n",
402 PTR_ERR(event));
403 return;
404 }
405
406 pmc->perf_event = event;
407}