1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Linaro Ltd.
4 * Author: Shannon Zhao <shannon.zhao@linaro.org>
9 #include <linux/kvm_host.h>
10 #include <linux/perf_event.h>
11 #include <linux/perf/arm_pmu.h>
12 #include <linux/uaccess.h>
13 #include <asm/kvm_emulate.h>
14 #include <kvm/arm_pmu.h>
15 #include <kvm/arm_vgic.h>
17 DEFINE_STATIC_KEY_FALSE(kvm_arm_pmu_available);
19 static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx);
20 static void kvm_pmu_update_pmc_chained(struct kvm_vcpu *vcpu, u64 select_idx);
21 static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc);
23 #define PERF_ATTR_CFG1_KVM_PMU_CHAINED 0x1
25 static u32 kvm_pmu_event_mask(struct kvm *kvm)
27 switch (kvm->arch.pmuver) {
28 case ID_AA64DFR0_PMUVER_8_0:
30 case ID_AA64DFR0_PMUVER_8_1:
31 case ID_AA64DFR0_PMUVER_8_4:
32 case ID_AA64DFR0_PMUVER_8_5:
33 case ID_AA64DFR0_PMUVER_8_7:
34 return GENMASK(15, 0);
35 default: /* Shouldn't be here, just for sanity */
36 WARN_ONCE(1, "Unknown PMU version %d\n", kvm->arch.pmuver);
42 * kvm_pmu_idx_is_64bit - determine if select_idx is a 64bit counter
43 * @vcpu: The vcpu pointer
44 * @select_idx: The counter index
46 static bool kvm_pmu_idx_is_64bit(struct kvm_vcpu *vcpu, u64 select_idx)
48 return (select_idx == ARMV8_PMU_CYCLE_IDX &&
49 __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC);
52 static struct kvm_vcpu *kvm_pmc_to_vcpu(struct kvm_pmc *pmc)
55 struct kvm_vcpu_arch *vcpu_arch;
58 pmu = container_of(pmc, struct kvm_pmu, pmc[0]);
59 vcpu_arch = container_of(pmu, struct kvm_vcpu_arch, pmu);
60 return container_of(vcpu_arch, struct kvm_vcpu, arch);
64 * kvm_pmu_pmc_is_chained - determine if the pmc is chained
65 * @pmc: The PMU counter pointer
67 static bool kvm_pmu_pmc_is_chained(struct kvm_pmc *pmc)
69 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
71 return test_bit(pmc->idx >> 1, vcpu->arch.pmu.chained);
75 * kvm_pmu_idx_is_high_counter - determine if select_idx is a high/low counter
76 * @select_idx: The counter index
78 static bool kvm_pmu_idx_is_high_counter(u64 select_idx)
80 return select_idx & 0x1;
84 * kvm_pmu_get_canonical_pmc - obtain the canonical pmc
85 * @pmc: The PMU counter pointer
87 * When a pair of PMCs are chained together we use the low counter (canonical)
88 * to hold the underlying perf event.
90 static struct kvm_pmc *kvm_pmu_get_canonical_pmc(struct kvm_pmc *pmc)
92 if (kvm_pmu_pmc_is_chained(pmc) &&
93 kvm_pmu_idx_is_high_counter(pmc->idx))
98 static struct kvm_pmc *kvm_pmu_get_alternate_pmc(struct kvm_pmc *pmc)
100 if (kvm_pmu_idx_is_high_counter(pmc->idx))
107 * kvm_pmu_idx_has_chain_evtype - determine if the event type is chain
108 * @vcpu: The vcpu pointer
109 * @select_idx: The counter index
111 static bool kvm_pmu_idx_has_chain_evtype(struct kvm_vcpu *vcpu, u64 select_idx)
117 if (select_idx == ARMV8_PMU_CYCLE_IDX)
120 reg = PMEVTYPER0_EL0 + select_idx;
121 eventsel = __vcpu_sys_reg(vcpu, reg) & kvm_pmu_event_mask(vcpu->kvm);
123 return eventsel == ARMV8_PMUV3_PERFCTR_CHAIN;
127 * kvm_pmu_get_pair_counter_value - get PMU counter value
128 * @vcpu: The vcpu pointer
129 * @pmc: The PMU counter pointer
131 static u64 kvm_pmu_get_pair_counter_value(struct kvm_vcpu *vcpu,
134 u64 counter, counter_high, reg, enabled, running;
136 if (kvm_pmu_pmc_is_chained(pmc)) {
137 pmc = kvm_pmu_get_canonical_pmc(pmc);
138 reg = PMEVCNTR0_EL0 + pmc->idx;
140 counter = __vcpu_sys_reg(vcpu, reg);
141 counter_high = __vcpu_sys_reg(vcpu, reg + 1);
143 counter = lower_32_bits(counter) | (counter_high << 32);
145 reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
146 ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
147 counter = __vcpu_sys_reg(vcpu, reg);
151 * The real counter value is equal to the value of counter register plus
152 * the value perf event counts.
155 counter += perf_event_read_value(pmc->perf_event, &enabled,
162 * kvm_pmu_get_counter_value - get PMU counter value
163 * @vcpu: The vcpu pointer
164 * @select_idx: The counter index
166 u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
169 struct kvm_pmu *pmu = &vcpu->arch.pmu;
170 struct kvm_pmc *pmc = &pmu->pmc[select_idx];
172 counter = kvm_pmu_get_pair_counter_value(vcpu, pmc);
174 if (kvm_pmu_pmc_is_chained(pmc) &&
175 kvm_pmu_idx_is_high_counter(select_idx))
176 counter = upper_32_bits(counter);
177 else if (select_idx != ARMV8_PMU_CYCLE_IDX)
178 counter = lower_32_bits(counter);
184 * kvm_pmu_set_counter_value - set PMU counter value
185 * @vcpu: The vcpu pointer
186 * @select_idx: The counter index
187 * @val: The counter value
189 void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
193 reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
194 ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx;
195 __vcpu_sys_reg(vcpu, reg) += (s64)val - kvm_pmu_get_counter_value(vcpu, select_idx);
197 /* Recreate the perf event to reflect the updated sample_period */
198 kvm_pmu_create_perf_event(vcpu, select_idx);
202 * kvm_pmu_release_perf_event - remove the perf event
203 * @pmc: The PMU counter pointer
205 static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc)
207 pmc = kvm_pmu_get_canonical_pmc(pmc);
208 if (pmc->perf_event) {
209 perf_event_disable(pmc->perf_event);
210 perf_event_release_kernel(pmc->perf_event);
211 pmc->perf_event = NULL;
216 * kvm_pmu_stop_counter - stop PMU counter
217 * @pmc: The PMU counter pointer
219 * If this counter has been configured to monitor some event, release it here.
221 static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
223 u64 counter, reg, val;
225 pmc = kvm_pmu_get_canonical_pmc(pmc);
226 if (!pmc->perf_event)
229 counter = kvm_pmu_get_pair_counter_value(vcpu, pmc);
231 if (pmc->idx == ARMV8_PMU_CYCLE_IDX) {
235 reg = PMEVCNTR0_EL0 + pmc->idx;
236 val = lower_32_bits(counter);
239 __vcpu_sys_reg(vcpu, reg) = val;
241 if (kvm_pmu_pmc_is_chained(pmc))
242 __vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
244 kvm_pmu_release_perf_event(pmc);
248 * kvm_pmu_vcpu_init - assign pmu counter idx for cpu
249 * @vcpu: The vcpu pointer
252 void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu)
255 struct kvm_pmu *pmu = &vcpu->arch.pmu;
257 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++)
262 * kvm_pmu_vcpu_reset - reset pmu state for cpu
263 * @vcpu: The vcpu pointer
266 void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu)
268 unsigned long mask = kvm_pmu_valid_counter_mask(vcpu);
269 struct kvm_pmu *pmu = &vcpu->arch.pmu;
272 for_each_set_bit(i, &mask, 32)
273 kvm_pmu_stop_counter(vcpu, &pmu->pmc[i]);
275 bitmap_zero(vcpu->arch.pmu.chained, ARMV8_PMU_MAX_COUNTER_PAIRS);
279 * kvm_pmu_vcpu_destroy - free perf event of PMU for cpu
280 * @vcpu: The vcpu pointer
283 void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
286 struct kvm_pmu *pmu = &vcpu->arch.pmu;
288 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++)
289 kvm_pmu_release_perf_event(&pmu->pmc[i]);
290 irq_work_sync(&vcpu->arch.pmu.overflow_work);
293 u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
295 u64 val = __vcpu_sys_reg(vcpu, PMCR_EL0) >> ARMV8_PMU_PMCR_N_SHIFT;
297 val &= ARMV8_PMU_PMCR_N_MASK;
299 return BIT(ARMV8_PMU_CYCLE_IDX);
301 return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX);
305 * kvm_pmu_enable_counter_mask - enable selected PMU counters
306 * @vcpu: The vcpu pointer
307 * @val: the value guest writes to PMCNTENSET register
309 * Call perf_event_enable to start counting the perf event
311 void kvm_pmu_enable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
314 struct kvm_pmu *pmu = &vcpu->arch.pmu;
317 if (!(__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) || !val)
320 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
326 /* A change in the enable state may affect the chain state */
327 kvm_pmu_update_pmc_chained(vcpu, i);
328 kvm_pmu_create_perf_event(vcpu, i);
330 /* At this point, pmc must be the canonical */
331 if (pmc->perf_event) {
332 perf_event_enable(pmc->perf_event);
333 if (pmc->perf_event->state != PERF_EVENT_STATE_ACTIVE)
334 kvm_debug("fail to enable perf event\n");
340 * kvm_pmu_disable_counter_mask - disable selected PMU counters
341 * @vcpu: The vcpu pointer
342 * @val: the value guest writes to PMCNTENCLR register
344 * Call perf_event_disable to stop counting the perf event
346 void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
349 struct kvm_pmu *pmu = &vcpu->arch.pmu;
355 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
361 /* A change in the enable state may affect the chain state */
362 kvm_pmu_update_pmc_chained(vcpu, i);
363 kvm_pmu_create_perf_event(vcpu, i);
365 /* At this point, pmc must be the canonical */
367 perf_event_disable(pmc->perf_event);
371 static u64 kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
375 if ((__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E)) {
376 reg = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
377 reg &= __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
378 reg &= __vcpu_sys_reg(vcpu, PMINTENSET_EL1);
384 static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
386 struct kvm_pmu *pmu = &vcpu->arch.pmu;
389 if (!kvm_vcpu_has_pmu(vcpu))
392 overflow = !!kvm_pmu_overflow_status(vcpu);
393 if (pmu->irq_level == overflow)
396 pmu->irq_level = overflow;
398 if (likely(irqchip_in_kernel(vcpu->kvm))) {
399 int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
400 pmu->irq_num, overflow, pmu);
405 bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
407 struct kvm_pmu *pmu = &vcpu->arch.pmu;
408 struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
409 bool run_level = sregs->device_irq_level & KVM_ARM_DEV_PMU;
411 if (likely(irqchip_in_kernel(vcpu->kvm)))
414 return pmu->irq_level != run_level;
418 * Reflect the PMU overflow interrupt output level into the kvm_run structure
420 void kvm_pmu_update_run(struct kvm_vcpu *vcpu)
422 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
424 /* Populate the timer bitmap for user space */
425 regs->device_irq_level &= ~KVM_ARM_DEV_PMU;
426 if (vcpu->arch.pmu.irq_level)
427 regs->device_irq_level |= KVM_ARM_DEV_PMU;
431 * kvm_pmu_flush_hwstate - flush pmu state to cpu
432 * @vcpu: The vcpu pointer
434 * Check if the PMU has overflowed while we were running in the host, and inject
435 * an interrupt if that was the case.
437 void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu)
439 kvm_pmu_update_state(vcpu);
443 * kvm_pmu_sync_hwstate - sync pmu state from cpu
444 * @vcpu: The vcpu pointer
446 * Check if the PMU has overflowed while we were running in the guest, and
447 * inject an interrupt if that was the case.
449 void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu)
451 kvm_pmu_update_state(vcpu);
455 * When perf interrupt is an NMI, we cannot safely notify the vcpu corresponding
457 * This is why we need a callback to do it once outside of the NMI context.
459 static void kvm_pmu_perf_overflow_notify_vcpu(struct irq_work *work)
461 struct kvm_vcpu *vcpu;
464 pmu = container_of(work, struct kvm_pmu, overflow_work);
465 vcpu = kvm_pmc_to_vcpu(pmu->pmc);
471 * When the perf event overflows, set the overflow status and inform the vcpu.
473 static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
474 struct perf_sample_data *data,
475 struct pt_regs *regs)
477 struct kvm_pmc *pmc = perf_event->overflow_handler_context;
478 struct arm_pmu *cpu_pmu = to_arm_pmu(perf_event->pmu);
479 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
483 cpu_pmu->pmu.stop(perf_event, PERF_EF_UPDATE);
486 * Reset the sample period to the architectural limit,
487 * i.e. the point where the counter overflows.
489 period = -(local64_read(&perf_event->count));
491 if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
492 period &= GENMASK(31, 0);
494 local64_set(&perf_event->hw.period_left, 0);
495 perf_event->attr.sample_period = period;
496 perf_event->hw.sample_period = period;
498 __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(idx);
500 if (kvm_pmu_overflow_status(vcpu)) {
501 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
506 irq_work_queue(&vcpu->arch.pmu.overflow_work);
509 cpu_pmu->pmu.start(perf_event, PERF_EF_RELOAD);
513 * kvm_pmu_software_increment - do software increment
514 * @vcpu: The vcpu pointer
515 * @val: the value guest writes to PMSWINC register
517 void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val)
519 struct kvm_pmu *pmu = &vcpu->arch.pmu;
522 if (!(__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E))
525 /* Weed out disabled counters */
526 val &= __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
528 for (i = 0; i < ARMV8_PMU_CYCLE_IDX; i++) {
534 /* PMSWINC only applies to ... SW_INC! */
535 type = __vcpu_sys_reg(vcpu, PMEVTYPER0_EL0 + i);
536 type &= kvm_pmu_event_mask(vcpu->kvm);
537 if (type != ARMV8_PMUV3_PERFCTR_SW_INCR)
540 /* increment this even SW_INC counter */
541 reg = __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i) + 1;
542 reg = lower_32_bits(reg);
543 __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i) = reg;
545 if (reg) /* no overflow on the low part */
548 if (kvm_pmu_pmc_is_chained(&pmu->pmc[i])) {
549 /* increment the high counter */
550 reg = __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i + 1) + 1;
551 reg = lower_32_bits(reg);
552 __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i + 1) = reg;
553 if (!reg) /* mark overflow on the high counter */
554 __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(i + 1);
556 /* mark overflow on low counter */
557 __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(i);
563 * kvm_pmu_handle_pmcr - handle PMCR register
564 * @vcpu: The vcpu pointer
565 * @val: the value guest writes to PMCR register
567 void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
571 if (val & ARMV8_PMU_PMCR_E) {
572 kvm_pmu_enable_counter_mask(vcpu,
573 __vcpu_sys_reg(vcpu, PMCNTENSET_EL0));
575 kvm_pmu_disable_counter_mask(vcpu,
576 __vcpu_sys_reg(vcpu, PMCNTENSET_EL0));
579 if (val & ARMV8_PMU_PMCR_C)
580 kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0);
582 if (val & ARMV8_PMU_PMCR_P) {
583 unsigned long mask = kvm_pmu_valid_counter_mask(vcpu);
584 mask &= ~BIT(ARMV8_PMU_CYCLE_IDX);
585 for_each_set_bit(i, &mask, 32)
586 kvm_pmu_set_counter_value(vcpu, i, 0);
590 static bool kvm_pmu_counter_is_enabled(struct kvm_vcpu *vcpu, u64 select_idx)
592 return (__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) &&
593 (__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & BIT(select_idx));
597 * kvm_pmu_create_perf_event - create a perf event for a counter
598 * @vcpu: The vcpu pointer
599 * @select_idx: The number of selected counter
601 static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx)
603 struct kvm_pmu *pmu = &vcpu->arch.pmu;
605 struct perf_event *event;
606 struct perf_event_attr attr;
607 u64 eventsel, counter, reg, data;
610 * For chained counters the event type and filtering attributes are
611 * obtained from the low/even counter. We also use this counter to
612 * determine if the event is enabled/disabled.
614 pmc = kvm_pmu_get_canonical_pmc(&pmu->pmc[select_idx]);
616 reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
617 ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + pmc->idx;
618 data = __vcpu_sys_reg(vcpu, reg);
620 kvm_pmu_stop_counter(vcpu, pmc);
621 if (pmc->idx == ARMV8_PMU_CYCLE_IDX)
622 eventsel = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;
624 eventsel = data & kvm_pmu_event_mask(vcpu->kvm);
626 /* Software increment event doesn't need to be backed by a perf event */
627 if (eventsel == ARMV8_PMUV3_PERFCTR_SW_INCR)
631 * If we have a filter in place and that the event isn't allowed, do
632 * not install a perf event either.
634 if (vcpu->kvm->arch.pmu_filter &&
635 !test_bit(eventsel, vcpu->kvm->arch.pmu_filter))
638 memset(&attr, 0, sizeof(struct perf_event_attr));
639 attr.type = PERF_TYPE_RAW;
640 attr.size = sizeof(attr);
642 attr.disabled = !kvm_pmu_counter_is_enabled(vcpu, pmc->idx);
643 attr.exclude_user = data & ARMV8_PMU_EXCLUDE_EL0 ? 1 : 0;
644 attr.exclude_kernel = data & ARMV8_PMU_EXCLUDE_EL1 ? 1 : 0;
645 attr.exclude_hv = 1; /* Don't count EL2 events */
646 attr.exclude_host = 1; /* Don't count host events */
647 attr.config = eventsel;
649 counter = kvm_pmu_get_pair_counter_value(vcpu, pmc);
651 if (kvm_pmu_pmc_is_chained(pmc)) {
653 * The initial sample period (overflow count) of an event. For
654 * chained counters we only support overflow interrupts on the
657 attr.sample_period = (-counter) & GENMASK(63, 0);
658 attr.config1 |= PERF_ATTR_CFG1_KVM_PMU_CHAINED;
660 event = perf_event_create_kernel_counter(&attr, -1, current,
661 kvm_pmu_perf_overflow,
664 /* The initial sample period (overflow count) of an event. */
665 if (kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
666 attr.sample_period = (-counter) & GENMASK(63, 0);
668 attr.sample_period = (-counter) & GENMASK(31, 0);
670 event = perf_event_create_kernel_counter(&attr, -1, current,
671 kvm_pmu_perf_overflow, pmc);
675 pr_err_once("kvm: pmu event creation failed %ld\n",
680 pmc->perf_event = event;
684 * kvm_pmu_update_pmc_chained - update chained bitmap
685 * @vcpu: The vcpu pointer
686 * @select_idx: The number of selected counter
688 * Update the chained bitmap based on the event type written in the
689 * typer register and the enable state of the odd register.
691 static void kvm_pmu_update_pmc_chained(struct kvm_vcpu *vcpu, u64 select_idx)
693 struct kvm_pmu *pmu = &vcpu->arch.pmu;
694 struct kvm_pmc *pmc = &pmu->pmc[select_idx], *canonical_pmc;
695 bool new_state, old_state;
697 old_state = kvm_pmu_pmc_is_chained(pmc);
698 new_state = kvm_pmu_idx_has_chain_evtype(vcpu, pmc->idx) &&
699 kvm_pmu_counter_is_enabled(vcpu, pmc->idx | 0x1);
701 if (old_state == new_state)
704 canonical_pmc = kvm_pmu_get_canonical_pmc(pmc);
705 kvm_pmu_stop_counter(vcpu, canonical_pmc);
708 * During promotion from !chained to chained we must ensure
709 * the adjacent counter is stopped and its event destroyed
711 kvm_pmu_stop_counter(vcpu, kvm_pmu_get_alternate_pmc(pmc));
712 set_bit(pmc->idx >> 1, vcpu->arch.pmu.chained);
715 clear_bit(pmc->idx >> 1, vcpu->arch.pmu.chained);
719 * kvm_pmu_set_counter_event_type - set selected counter to monitor some event
720 * @vcpu: The vcpu pointer
721 * @data: The data guest writes to PMXEVTYPER_EL0
722 * @select_idx: The number of selected counter
724 * When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an
725 * event with given hardware event number. Here we call perf_event API to
726 * emulate this action and create a kernel perf event for it.
728 void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
733 mask = ARMV8_PMU_EVTYPE_MASK;
734 mask &= ~ARMV8_PMU_EVTYPE_EVENT;
735 mask |= kvm_pmu_event_mask(vcpu->kvm);
737 reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
738 ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + select_idx;
740 __vcpu_sys_reg(vcpu, reg) = data & mask;
742 kvm_pmu_update_pmc_chained(vcpu, select_idx);
743 kvm_pmu_create_perf_event(vcpu, select_idx);
746 void kvm_host_pmu_init(struct arm_pmu *pmu)
748 if (pmu->pmuver != 0 && pmu->pmuver != ID_AA64DFR0_PMUVER_IMP_DEF &&
749 !kvm_arm_support_pmu_v3() && !is_protected_kvm_enabled())
750 static_branch_enable(&kvm_arm_pmu_available);
753 static int kvm_pmu_probe_pmuver(void)
755 struct perf_event_attr attr = { };
756 struct perf_event *event;
758 int pmuver = ID_AA64DFR0_PMUVER_IMP_DEF;
761 * Create a dummy event that only counts user cycles. As we'll never
762 * leave this function with the event being live, it will never
763 * count anything. But it allows us to probe some of the PMU
764 * details. Yes, this is terrible.
766 attr.type = PERF_TYPE_RAW;
767 attr.size = sizeof(attr);
770 attr.exclude_user = 0;
771 attr.exclude_kernel = 1;
773 attr.exclude_host = 1;
774 attr.config = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;
775 attr.sample_period = GENMASK(63, 0);
777 event = perf_event_create_kernel_counter(&attr, -1, current,
778 kvm_pmu_perf_overflow, &attr);
781 pr_err_once("kvm: pmu event creation failed %ld\n",
783 return ID_AA64DFR0_PMUVER_IMP_DEF;
787 pmu = to_arm_pmu(event->pmu);
789 pmuver = pmu->pmuver;
792 perf_event_disable(event);
793 perf_event_release_kernel(event);
798 u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
800 unsigned long *bmap = vcpu->kvm->arch.pmu_filter;
802 int base, i, nr_events;
805 val = read_sysreg(pmceid0_el0);
808 val = read_sysreg(pmceid1_el0);
810 * Don't advertise STALL_SLOT, as PMMIR_EL0 is handled
813 if (vcpu->kvm->arch.pmuver >= ID_AA64DFR0_PMUVER_8_4)
814 val &= ~BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32);
821 nr_events = kvm_pmu_event_mask(vcpu->kvm) + 1;
823 for (i = 0; i < 32; i += 8) {
826 byte = bitmap_get_value8(bmap, base + i);
828 if (nr_events >= (0x4000 + base + 32)) {
829 byte = bitmap_get_value8(bmap, 0x4000 + base + i);
830 mask |= byte << (32 + i);
837 int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)
839 if (!kvm_vcpu_has_pmu(vcpu))
842 if (!vcpu->arch.pmu.created)
846 * A valid interrupt configuration for the PMU is either to have a
847 * properly configured interrupt number and using an in-kernel
848 * irqchip, or to not have an in-kernel GIC and not set an IRQ.
850 if (irqchip_in_kernel(vcpu->kvm)) {
851 int irq = vcpu->arch.pmu.irq_num;
853 * If we are using an in-kernel vgic, at this point we know
854 * the vgic will be initialized, so we can check the PMU irq
855 * number against the dimensions of the vgic and make sure
858 if (!irq_is_ppi(irq) && !vgic_valid_spi(vcpu->kvm, irq))
860 } else if (kvm_arm_pmu_irq_initialized(vcpu)) {
864 /* One-off reload of the PMU on first run */
865 kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu);
870 static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)
872 if (irqchip_in_kernel(vcpu->kvm)) {
876 * If using the PMU with an in-kernel virtual GIC
877 * implementation, we require the GIC to be already
878 * initialized when initializing the PMU.
880 if (!vgic_initialized(vcpu->kvm))
883 if (!kvm_arm_pmu_irq_initialized(vcpu))
886 ret = kvm_vgic_set_owner(vcpu, vcpu->arch.pmu.irq_num,
892 init_irq_work(&vcpu->arch.pmu.overflow_work,
893 kvm_pmu_perf_overflow_notify_vcpu);
895 vcpu->arch.pmu.created = true;
900 * For one VM the interrupt type must be same for each vcpu.
901 * As a PPI, the interrupt number is the same for all vcpus,
902 * while as an SPI it must be a separate number per vcpu.
904 static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
907 struct kvm_vcpu *vcpu;
909 kvm_for_each_vcpu(i, vcpu, kvm) {
910 if (!kvm_arm_pmu_irq_initialized(vcpu))
913 if (irq_is_ppi(irq)) {
914 if (vcpu->arch.pmu.irq_num != irq)
917 if (vcpu->arch.pmu.irq_num == irq)
925 int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
927 if (!kvm_vcpu_has_pmu(vcpu))
930 if (vcpu->arch.pmu.created)
933 if (!vcpu->kvm->arch.pmuver)
934 vcpu->kvm->arch.pmuver = kvm_pmu_probe_pmuver();
936 if (vcpu->kvm->arch.pmuver == ID_AA64DFR0_PMUVER_IMP_DEF)
939 switch (attr->attr) {
940 case KVM_ARM_VCPU_PMU_V3_IRQ: {
941 int __user *uaddr = (int __user *)(long)attr->addr;
944 if (!irqchip_in_kernel(vcpu->kvm))
947 if (get_user(irq, uaddr))
950 /* The PMU overflow interrupt can be a PPI or a valid SPI. */
951 if (!(irq_is_ppi(irq) || irq_is_spi(irq)))
954 if (!pmu_irq_is_valid(vcpu->kvm, irq))
957 if (kvm_arm_pmu_irq_initialized(vcpu))
960 kvm_debug("Set kvm ARM PMU irq: %d\n", irq);
961 vcpu->arch.pmu.irq_num = irq;
964 case KVM_ARM_VCPU_PMU_V3_FILTER: {
965 struct kvm_pmu_event_filter __user *uaddr;
966 struct kvm_pmu_event_filter filter;
969 nr_events = kvm_pmu_event_mask(vcpu->kvm) + 1;
971 uaddr = (struct kvm_pmu_event_filter __user *)(long)attr->addr;
973 if (copy_from_user(&filter, uaddr, sizeof(filter)))
976 if (((u32)filter.base_event + filter.nevents) > nr_events ||
977 (filter.action != KVM_PMU_EVENT_ALLOW &&
978 filter.action != KVM_PMU_EVENT_DENY))
981 mutex_lock(&vcpu->kvm->lock);
983 if (!vcpu->kvm->arch.pmu_filter) {
984 vcpu->kvm->arch.pmu_filter = bitmap_alloc(nr_events, GFP_KERNEL_ACCOUNT);
985 if (!vcpu->kvm->arch.pmu_filter) {
986 mutex_unlock(&vcpu->kvm->lock);
991 * The default depends on the first applied filter.
992 * If it allows events, the default is to deny.
993 * Conversely, if the first filter denies a set of
994 * events, the default is to allow.
996 if (filter.action == KVM_PMU_EVENT_ALLOW)
997 bitmap_zero(vcpu->kvm->arch.pmu_filter, nr_events);
999 bitmap_fill(vcpu->kvm->arch.pmu_filter, nr_events);
1002 if (filter.action == KVM_PMU_EVENT_ALLOW)
1003 bitmap_set(vcpu->kvm->arch.pmu_filter, filter.base_event, filter.nevents);
1005 bitmap_clear(vcpu->kvm->arch.pmu_filter, filter.base_event, filter.nevents);
1007 mutex_unlock(&vcpu->kvm->lock);
1011 case KVM_ARM_VCPU_PMU_V3_INIT:
1012 return kvm_arm_pmu_v3_init(vcpu);
1018 int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
1020 switch (attr->attr) {
1021 case KVM_ARM_VCPU_PMU_V3_IRQ: {
1022 int __user *uaddr = (int __user *)(long)attr->addr;
1025 if (!irqchip_in_kernel(vcpu->kvm))
1028 if (!kvm_vcpu_has_pmu(vcpu))
1031 if (!kvm_arm_pmu_irq_initialized(vcpu))
1034 irq = vcpu->arch.pmu.irq_num;
1035 return put_user(irq, uaddr);
1042 int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
1044 switch (attr->attr) {
1045 case KVM_ARM_VCPU_PMU_V3_IRQ:
1046 case KVM_ARM_VCPU_PMU_V3_INIT:
1047 case KVM_ARM_VCPU_PMU_V3_FILTER:
1048 if (kvm_vcpu_has_pmu(vcpu))