2 * Copyright (C) 2015 Linaro Ltd.
3 * Author: Shannon Zhao <shannon.zhao@linaro.org>
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.
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.
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/>.
18 #include <linux/cpu.h>
19 #include <linux/kvm.h>
20 #include <linux/kvm_host.h>
21 #include <linux/perf_event.h>
22 #include <linux/uaccess.h>
23 #include <asm/kvm_emulate.h>
24 #include <kvm/arm_pmu.h>
25 #include <kvm/arm_vgic.h>
28 * kvm_pmu_get_counter_value - get PMU counter value
29 * @vcpu: The vcpu pointer
30 * @select_idx: The counter index
32 u64
kvm_pmu_get_counter_value(struct kvm_vcpu
*vcpu
, u64 select_idx
)
34 u64 counter
, reg
, enabled
, running
;
35 struct kvm_pmu
*pmu
= &vcpu
->arch
.pmu
;
36 struct kvm_pmc
*pmc
= &pmu
->pmc
[select_idx
];
38 reg
= (select_idx
== ARMV8_PMU_CYCLE_IDX
)
39 ? PMCCNTR_EL0
: PMEVCNTR0_EL0
+ select_idx
;
40 counter
= __vcpu_sys_reg(vcpu
, reg
);
42 /* The real counter value is equal to the value of counter register plus
43 * the value perf event counts.
46 counter
+= perf_event_read_value(pmc
->perf_event
, &enabled
,
49 return counter
& pmc
->bitmask
;
53 * kvm_pmu_set_counter_value - set PMU counter value
54 * @vcpu: The vcpu pointer
55 * @select_idx: The counter index
56 * @val: The counter value
58 void kvm_pmu_set_counter_value(struct kvm_vcpu
*vcpu
, u64 select_idx
, u64 val
)
62 reg
= (select_idx
== ARMV8_PMU_CYCLE_IDX
)
63 ? PMCCNTR_EL0
: PMEVCNTR0_EL0
+ select_idx
;
64 __vcpu_sys_reg(vcpu
, reg
) += (s64
)val
- kvm_pmu_get_counter_value(vcpu
, select_idx
);
68 * kvm_pmu_stop_counter - stop PMU counter
69 * @pmc: The PMU counter pointer
71 * If this counter has been configured to monitor some event, release it here.
73 static void kvm_pmu_stop_counter(struct kvm_vcpu
*vcpu
, struct kvm_pmc
*pmc
)
77 if (pmc
->perf_event
) {
78 counter
= kvm_pmu_get_counter_value(vcpu
, pmc
->idx
);
79 reg
= (pmc
->idx
== ARMV8_PMU_CYCLE_IDX
)
80 ? PMCCNTR_EL0
: PMEVCNTR0_EL0
+ pmc
->idx
;
81 __vcpu_sys_reg(vcpu
, reg
) = counter
;
82 perf_event_disable(pmc
->perf_event
);
83 perf_event_release_kernel(pmc
->perf_event
);
84 pmc
->perf_event
= NULL
;
89 * kvm_pmu_vcpu_reset - reset pmu state for cpu
90 * @vcpu: The vcpu pointer
93 void kvm_pmu_vcpu_reset(struct kvm_vcpu
*vcpu
)
96 struct kvm_pmu
*pmu
= &vcpu
->arch
.pmu
;
98 for (i
= 0; i
< ARMV8_PMU_MAX_COUNTERS
; i
++) {
99 kvm_pmu_stop_counter(vcpu
, &pmu
->pmc
[i
]);
101 pmu
->pmc
[i
].bitmask
= 0xffffffffUL
;
106 * kvm_pmu_vcpu_destroy - free perf event of PMU for cpu
107 * @vcpu: The vcpu pointer
110 void kvm_pmu_vcpu_destroy(struct kvm_vcpu
*vcpu
)
113 struct kvm_pmu
*pmu
= &vcpu
->arch
.pmu
;
115 for (i
= 0; i
< ARMV8_PMU_MAX_COUNTERS
; i
++) {
116 struct kvm_pmc
*pmc
= &pmu
->pmc
[i
];
118 if (pmc
->perf_event
) {
119 perf_event_disable(pmc
->perf_event
);
120 perf_event_release_kernel(pmc
->perf_event
);
121 pmc
->perf_event
= NULL
;
126 u64
kvm_pmu_valid_counter_mask(struct kvm_vcpu
*vcpu
)
128 u64 val
= __vcpu_sys_reg(vcpu
, PMCR_EL0
) >> ARMV8_PMU_PMCR_N_SHIFT
;
130 val
&= ARMV8_PMU_PMCR_N_MASK
;
132 return BIT(ARMV8_PMU_CYCLE_IDX
);
134 return GENMASK(val
- 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX
);
138 * kvm_pmu_enable_counter - enable selected PMU counter
139 * @vcpu: The vcpu pointer
140 * @val: the value guest writes to PMCNTENSET register
142 * Call perf_event_enable to start counting the perf event
144 void kvm_pmu_enable_counter(struct kvm_vcpu
*vcpu
, u64 val
)
147 struct kvm_pmu
*pmu
= &vcpu
->arch
.pmu
;
150 if (!(__vcpu_sys_reg(vcpu
, PMCR_EL0
) & ARMV8_PMU_PMCR_E
) || !val
)
153 for (i
= 0; i
< ARMV8_PMU_MAX_COUNTERS
; i
++) {
158 if (pmc
->perf_event
) {
159 perf_event_enable(pmc
->perf_event
);
160 if (pmc
->perf_event
->state
!= PERF_EVENT_STATE_ACTIVE
)
161 kvm_debug("fail to enable perf event\n");
167 * kvm_pmu_disable_counter - disable selected PMU counter
168 * @vcpu: The vcpu pointer
169 * @val: the value guest writes to PMCNTENCLR register
171 * Call perf_event_disable to stop counting the perf event
173 void kvm_pmu_disable_counter(struct kvm_vcpu
*vcpu
, u64 val
)
176 struct kvm_pmu
*pmu
= &vcpu
->arch
.pmu
;
182 for (i
= 0; i
< ARMV8_PMU_MAX_COUNTERS
; i
++) {
188 perf_event_disable(pmc
->perf_event
);
192 static u64
kvm_pmu_overflow_status(struct kvm_vcpu
*vcpu
)
196 if ((__vcpu_sys_reg(vcpu
, PMCR_EL0
) & ARMV8_PMU_PMCR_E
)) {
197 reg
= __vcpu_sys_reg(vcpu
, PMOVSSET_EL0
);
198 reg
&= __vcpu_sys_reg(vcpu
, PMCNTENSET_EL0
);
199 reg
&= __vcpu_sys_reg(vcpu
, PMINTENSET_EL1
);
200 reg
&= kvm_pmu_valid_counter_mask(vcpu
);
206 static void kvm_pmu_update_state(struct kvm_vcpu
*vcpu
)
208 struct kvm_pmu
*pmu
= &vcpu
->arch
.pmu
;
211 if (!kvm_arm_pmu_v3_ready(vcpu
))
214 overflow
= !!kvm_pmu_overflow_status(vcpu
);
215 if (pmu
->irq_level
== overflow
)
218 pmu
->irq_level
= overflow
;
220 if (likely(irqchip_in_kernel(vcpu
->kvm
))) {
221 int ret
= kvm_vgic_inject_irq(vcpu
->kvm
, vcpu
->vcpu_id
,
222 pmu
->irq_num
, overflow
, pmu
);
227 bool kvm_pmu_should_notify_user(struct kvm_vcpu
*vcpu
)
229 struct kvm_pmu
*pmu
= &vcpu
->arch
.pmu
;
230 struct kvm_sync_regs
*sregs
= &vcpu
->run
->s
.regs
;
231 bool run_level
= sregs
->device_irq_level
& KVM_ARM_DEV_PMU
;
233 if (likely(irqchip_in_kernel(vcpu
->kvm
)))
236 return pmu
->irq_level
!= run_level
;
240 * Reflect the PMU overflow interrupt output level into the kvm_run structure
242 void kvm_pmu_update_run(struct kvm_vcpu
*vcpu
)
244 struct kvm_sync_regs
*regs
= &vcpu
->run
->s
.regs
;
246 /* Populate the timer bitmap for user space */
247 regs
->device_irq_level
&= ~KVM_ARM_DEV_PMU
;
248 if (vcpu
->arch
.pmu
.irq_level
)
249 regs
->device_irq_level
|= KVM_ARM_DEV_PMU
;
253 * kvm_pmu_flush_hwstate - flush pmu state to cpu
254 * @vcpu: The vcpu pointer
256 * Check if the PMU has overflowed while we were running in the host, and inject
257 * an interrupt if that was the case.
259 void kvm_pmu_flush_hwstate(struct kvm_vcpu
*vcpu
)
261 kvm_pmu_update_state(vcpu
);
265 * kvm_pmu_sync_hwstate - sync pmu state from cpu
266 * @vcpu: The vcpu pointer
268 * Check if the PMU has overflowed while we were running in the guest, and
269 * inject an interrupt if that was the case.
271 void kvm_pmu_sync_hwstate(struct kvm_vcpu
*vcpu
)
273 kvm_pmu_update_state(vcpu
);
276 static inline struct kvm_vcpu
*kvm_pmc_to_vcpu(struct kvm_pmc
*pmc
)
279 struct kvm_vcpu_arch
*vcpu_arch
;
282 pmu
= container_of(pmc
, struct kvm_pmu
, pmc
[0]);
283 vcpu_arch
= container_of(pmu
, struct kvm_vcpu_arch
, pmu
);
284 return container_of(vcpu_arch
, struct kvm_vcpu
, arch
);
288 * When the perf event overflows, set the overflow status and inform the vcpu.
290 static void kvm_pmu_perf_overflow(struct perf_event
*perf_event
,
291 struct perf_sample_data
*data
,
292 struct pt_regs
*regs
)
294 struct kvm_pmc
*pmc
= perf_event
->overflow_handler_context
;
295 struct kvm_vcpu
*vcpu
= kvm_pmc_to_vcpu(pmc
);
298 __vcpu_sys_reg(vcpu
, PMOVSSET_EL0
) |= BIT(idx
);
300 if (kvm_pmu_overflow_status(vcpu
)) {
301 kvm_make_request(KVM_REQ_IRQ_PENDING
, vcpu
);
307 * kvm_pmu_software_increment - do software increment
308 * @vcpu: The vcpu pointer
309 * @val: the value guest writes to PMSWINC register
311 void kvm_pmu_software_increment(struct kvm_vcpu
*vcpu
, u64 val
)
314 u64 type
, enable
, reg
;
319 enable
= __vcpu_sys_reg(vcpu
, PMCNTENSET_EL0
);
320 for (i
= 0; i
< ARMV8_PMU_CYCLE_IDX
; i
++) {
323 type
= __vcpu_sys_reg(vcpu
, PMEVTYPER0_EL0
+ i
)
324 & ARMV8_PMU_EVTYPE_EVENT
;
325 if ((type
== ARMV8_PMUV3_PERFCTR_SW_INCR
)
326 && (enable
& BIT(i
))) {
327 reg
= __vcpu_sys_reg(vcpu
, PMEVCNTR0_EL0
+ i
) + 1;
328 reg
= lower_32_bits(reg
);
329 __vcpu_sys_reg(vcpu
, PMEVCNTR0_EL0
+ i
) = reg
;
331 __vcpu_sys_reg(vcpu
, PMOVSSET_EL0
) |= BIT(i
);
337 * kvm_pmu_handle_pmcr - handle PMCR register
338 * @vcpu: The vcpu pointer
339 * @val: the value guest writes to PMCR register
341 void kvm_pmu_handle_pmcr(struct kvm_vcpu
*vcpu
, u64 val
)
343 struct kvm_pmu
*pmu
= &vcpu
->arch
.pmu
;
348 mask
= kvm_pmu_valid_counter_mask(vcpu
);
349 if (val
& ARMV8_PMU_PMCR_E
) {
350 kvm_pmu_enable_counter(vcpu
,
351 __vcpu_sys_reg(vcpu
, PMCNTENSET_EL0
) & mask
);
353 kvm_pmu_disable_counter(vcpu
, mask
);
356 if (val
& ARMV8_PMU_PMCR_C
)
357 kvm_pmu_set_counter_value(vcpu
, ARMV8_PMU_CYCLE_IDX
, 0);
359 if (val
& ARMV8_PMU_PMCR_P
) {
360 for (i
= 0; i
< ARMV8_PMU_CYCLE_IDX
; i
++)
361 kvm_pmu_set_counter_value(vcpu
, i
, 0);
364 if (val
& ARMV8_PMU_PMCR_LC
) {
365 pmc
= &pmu
->pmc
[ARMV8_PMU_CYCLE_IDX
];
366 pmc
->bitmask
= 0xffffffffffffffffUL
;
370 static bool kvm_pmu_counter_is_enabled(struct kvm_vcpu
*vcpu
, u64 select_idx
)
372 return (__vcpu_sys_reg(vcpu
, PMCR_EL0
) & ARMV8_PMU_PMCR_E
) &&
373 (__vcpu_sys_reg(vcpu
, PMCNTENSET_EL0
) & BIT(select_idx
));
377 * kvm_pmu_set_counter_event_type - set selected counter to monitor some event
378 * @vcpu: The vcpu pointer
379 * @data: The data guest writes to PMXEVTYPER_EL0
380 * @select_idx: The number of selected counter
382 * When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an
383 * event with given hardware event number. Here we call perf_event API to
384 * emulate this action and create a kernel perf event for it.
386 void kvm_pmu_set_counter_event_type(struct kvm_vcpu
*vcpu
, u64 data
,
389 struct kvm_pmu
*pmu
= &vcpu
->arch
.pmu
;
390 struct kvm_pmc
*pmc
= &pmu
->pmc
[select_idx
];
391 struct perf_event
*event
;
392 struct perf_event_attr attr
;
393 u64 eventsel
, counter
;
395 kvm_pmu_stop_counter(vcpu
, pmc
);
396 eventsel
= data
& ARMV8_PMU_EVTYPE_EVENT
;
398 /* Software increment event does't need to be backed by a perf event */
399 if (eventsel
== ARMV8_PMUV3_PERFCTR_SW_INCR
&&
400 select_idx
!= ARMV8_PMU_CYCLE_IDX
)
403 memset(&attr
, 0, sizeof(struct perf_event_attr
));
404 attr
.type
= PERF_TYPE_RAW
;
405 attr
.size
= sizeof(attr
);
407 attr
.disabled
= !kvm_pmu_counter_is_enabled(vcpu
, select_idx
);
408 attr
.exclude_user
= data
& ARMV8_PMU_EXCLUDE_EL0
? 1 : 0;
409 attr
.exclude_kernel
= data
& ARMV8_PMU_EXCLUDE_EL1
? 1 : 0;
410 attr
.exclude_hv
= 1; /* Don't count EL2 events */
411 attr
.exclude_host
= 1; /* Don't count host events */
412 attr
.config
= (select_idx
== ARMV8_PMU_CYCLE_IDX
) ?
413 ARMV8_PMUV3_PERFCTR_CPU_CYCLES
: eventsel
;
415 counter
= kvm_pmu_get_counter_value(vcpu
, select_idx
);
416 /* The initial sample period (overflow count) of an event. */
417 attr
.sample_period
= (-counter
) & pmc
->bitmask
;
419 event
= perf_event_create_kernel_counter(&attr
, -1, current
,
420 kvm_pmu_perf_overflow
, pmc
);
422 pr_err_once("kvm: pmu event creation failed %ld\n",
427 pmc
->perf_event
= event
;
430 bool kvm_arm_support_pmu_v3(void)
433 * Check if HW_PERF_EVENTS are supported by checking the number of
434 * hardware performance counters. This could ensure the presence of
435 * a physical PMU and CONFIG_PERF_EVENT is selected.
437 return (perf_num_counters() > 0);
440 int kvm_arm_pmu_v3_enable(struct kvm_vcpu
*vcpu
)
442 if (!vcpu
->arch
.pmu
.created
)
446 * A valid interrupt configuration for the PMU is either to have a
447 * properly configured interrupt number and using an in-kernel
448 * irqchip, or to not have an in-kernel GIC and not set an IRQ.
450 if (irqchip_in_kernel(vcpu
->kvm
)) {
451 int irq
= vcpu
->arch
.pmu
.irq_num
;
452 if (!kvm_arm_pmu_irq_initialized(vcpu
))
456 * If we are using an in-kernel vgic, at this point we know
457 * the vgic will be initialized, so we can check the PMU irq
458 * number against the dimensions of the vgic and make sure
461 if (!irq_is_ppi(irq
) && !vgic_valid_spi(vcpu
->kvm
, irq
))
463 } else if (kvm_arm_pmu_irq_initialized(vcpu
)) {
467 kvm_pmu_vcpu_reset(vcpu
);
468 vcpu
->arch
.pmu
.ready
= true;
473 static int kvm_arm_pmu_v3_init(struct kvm_vcpu
*vcpu
)
475 if (!kvm_arm_support_pmu_v3())
478 if (!test_bit(KVM_ARM_VCPU_PMU_V3
, vcpu
->arch
.features
))
481 if (vcpu
->arch
.pmu
.created
)
484 if (irqchip_in_kernel(vcpu
->kvm
)) {
488 * If using the PMU with an in-kernel virtual GIC
489 * implementation, we require the GIC to be already
490 * initialized when initializing the PMU.
492 if (!vgic_initialized(vcpu
->kvm
))
495 if (!kvm_arm_pmu_irq_initialized(vcpu
))
498 ret
= kvm_vgic_set_owner(vcpu
, vcpu
->arch
.pmu
.irq_num
,
504 vcpu
->arch
.pmu
.created
= true;
509 * For one VM the interrupt type must be same for each vcpu.
510 * As a PPI, the interrupt number is the same for all vcpus,
511 * while as an SPI it must be a separate number per vcpu.
513 static bool pmu_irq_is_valid(struct kvm
*kvm
, int irq
)
516 struct kvm_vcpu
*vcpu
;
518 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
519 if (!kvm_arm_pmu_irq_initialized(vcpu
))
522 if (irq_is_ppi(irq
)) {
523 if (vcpu
->arch
.pmu
.irq_num
!= irq
)
526 if (vcpu
->arch
.pmu
.irq_num
== irq
)
534 int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu
*vcpu
, struct kvm_device_attr
*attr
)
536 switch (attr
->attr
) {
537 case KVM_ARM_VCPU_PMU_V3_IRQ
: {
538 int __user
*uaddr
= (int __user
*)(long)attr
->addr
;
541 if (!irqchip_in_kernel(vcpu
->kvm
))
544 if (!test_bit(KVM_ARM_VCPU_PMU_V3
, vcpu
->arch
.features
))
547 if (get_user(irq
, uaddr
))
550 /* The PMU overflow interrupt can be a PPI or a valid SPI. */
551 if (!(irq_is_ppi(irq
) || irq_is_spi(irq
)))
554 if (!pmu_irq_is_valid(vcpu
->kvm
, irq
))
557 if (kvm_arm_pmu_irq_initialized(vcpu
))
560 kvm_debug("Set kvm ARM PMU irq: %d\n", irq
);
561 vcpu
->arch
.pmu
.irq_num
= irq
;
564 case KVM_ARM_VCPU_PMU_V3_INIT
:
565 return kvm_arm_pmu_v3_init(vcpu
);
571 int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu
*vcpu
, struct kvm_device_attr
*attr
)
573 switch (attr
->attr
) {
574 case KVM_ARM_VCPU_PMU_V3_IRQ
: {
575 int __user
*uaddr
= (int __user
*)(long)attr
->addr
;
578 if (!irqchip_in_kernel(vcpu
->kvm
))
581 if (!test_bit(KVM_ARM_VCPU_PMU_V3
, vcpu
->arch
.features
))
584 if (!kvm_arm_pmu_irq_initialized(vcpu
))
587 irq
= vcpu
->arch
.pmu
.irq_num
;
588 return put_user(irq
, uaddr
);
595 int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu
*vcpu
, struct kvm_device_attr
*attr
)
597 switch (attr
->attr
) {
598 case KVM_ARM_VCPU_PMU_V3_IRQ
:
599 case KVM_ARM_VCPU_PMU_V3_INIT
:
600 if (kvm_arm_support_pmu_v3() &&
601 test_bit(KVM_ARM_VCPU_PMU_V3
, vcpu
->arch
.features
))