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/list.h>
11 #include <linux/perf_event.h>
12 #include <linux/perf/arm_pmu.h>
13 #include <linux/uaccess.h>
14 #include <asm/kvm_emulate.h>
15 #include <kvm/arm_pmu.h>
16 #include <kvm/arm_vgic.h>
17 #include <asm/arm_pmuv3.h>
19 #define PERF_ATTR_CFG1_COUNTER_64BIT BIT(0)
21 DEFINE_STATIC_KEY_FALSE(kvm_arm_pmu_available);
23 static LIST_HEAD(arm_pmus);
24 static DEFINE_MUTEX(arm_pmus_lock);
26 static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc);
27 static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc);
29 static struct kvm_vcpu *kvm_pmc_to_vcpu(const struct kvm_pmc *pmc)
31 return container_of(pmc, struct kvm_vcpu, arch.pmu.pmc[pmc->idx]);
34 static struct kvm_pmc *kvm_vcpu_idx_to_pmc(struct kvm_vcpu *vcpu, int cnt_idx)
36 return &vcpu->arch.pmu.pmc[cnt_idx];
39 static u32 __kvm_pmu_event_mask(unsigned int pmuver)
42 case ID_AA64DFR0_EL1_PMUVer_IMP:
44 case ID_AA64DFR0_EL1_PMUVer_V3P1:
45 case ID_AA64DFR0_EL1_PMUVer_V3P4:
46 case ID_AA64DFR0_EL1_PMUVer_V3P5:
47 case ID_AA64DFR0_EL1_PMUVer_V3P7:
48 return GENMASK(15, 0);
49 default: /* Shouldn't be here, just for sanity */
50 WARN_ONCE(1, "Unknown PMU version %d\n", pmuver);
55 static u32 kvm_pmu_event_mask(struct kvm *kvm)
57 u64 dfr0 = IDREG(kvm, SYS_ID_AA64DFR0_EL1);
58 u8 pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, dfr0);
60 return __kvm_pmu_event_mask(pmuver);
64 * kvm_pmc_is_64bit - determine if counter is 64bit
65 * @pmc: counter context
67 static bool kvm_pmc_is_64bit(struct kvm_pmc *pmc)
69 return (pmc->idx == ARMV8_PMU_CYCLE_IDX ||
70 kvm_pmu_is_3p5(kvm_pmc_to_vcpu(pmc)));
73 static bool kvm_pmc_has_64bit_overflow(struct kvm_pmc *pmc)
75 u64 val = kvm_vcpu_read_pmcr(kvm_pmc_to_vcpu(pmc));
77 return (pmc->idx < ARMV8_PMU_CYCLE_IDX && (val & ARMV8_PMU_PMCR_LP)) ||
78 (pmc->idx == ARMV8_PMU_CYCLE_IDX && (val & ARMV8_PMU_PMCR_LC));
81 static bool kvm_pmu_counter_can_chain(struct kvm_pmc *pmc)
83 return (!(pmc->idx & 1) && (pmc->idx + 1) < ARMV8_PMU_CYCLE_IDX &&
84 !kvm_pmc_has_64bit_overflow(pmc));
87 static u32 counter_index_to_reg(u64 idx)
89 return (idx == ARMV8_PMU_CYCLE_IDX) ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + idx;
92 static u32 counter_index_to_evtreg(u64 idx)
94 return (idx == ARMV8_PMU_CYCLE_IDX) ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + idx;
97 static u64 kvm_pmu_get_pmc_value(struct kvm_pmc *pmc)
99 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
100 u64 counter, reg, enabled, running;
102 reg = counter_index_to_reg(pmc->idx);
103 counter = __vcpu_sys_reg(vcpu, reg);
106 * The real counter value is equal to the value of counter register plus
107 * the value perf event counts.
110 counter += perf_event_read_value(pmc->perf_event, &enabled,
113 if (!kvm_pmc_is_64bit(pmc))
114 counter = lower_32_bits(counter);
120 * kvm_pmu_get_counter_value - get PMU counter value
121 * @vcpu: The vcpu pointer
122 * @select_idx: The counter index
124 u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
126 if (!kvm_vcpu_has_pmu(vcpu))
129 return kvm_pmu_get_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, select_idx));
132 static void kvm_pmu_set_pmc_value(struct kvm_pmc *pmc, u64 val, bool force)
134 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
137 kvm_pmu_release_perf_event(pmc);
139 reg = counter_index_to_reg(pmc->idx);
141 if (vcpu_mode_is_32bit(vcpu) && pmc->idx != ARMV8_PMU_CYCLE_IDX &&
144 * Even with PMUv3p5, AArch32 cannot write to the top
145 * 32bit of the counters. The only possible course of
146 * action is to use PMCR.P, which will reset them to
147 * 0 (the only use of the 'force' parameter).
149 val = __vcpu_sys_reg(vcpu, reg) & GENMASK(63, 32);
150 val |= lower_32_bits(val);
153 __vcpu_sys_reg(vcpu, reg) = val;
155 /* Recreate the perf event to reflect the updated sample_period */
156 kvm_pmu_create_perf_event(pmc);
160 * kvm_pmu_set_counter_value - set PMU counter value
161 * @vcpu: The vcpu pointer
162 * @select_idx: The counter index
163 * @val: The counter value
165 void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
167 if (!kvm_vcpu_has_pmu(vcpu))
170 kvm_pmu_set_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, select_idx), val, false);
174 * kvm_pmu_release_perf_event - remove the perf event
175 * @pmc: The PMU counter pointer
177 static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc)
179 if (pmc->perf_event) {
180 perf_event_disable(pmc->perf_event);
181 perf_event_release_kernel(pmc->perf_event);
182 pmc->perf_event = NULL;
187 * kvm_pmu_stop_counter - stop PMU counter
188 * @pmc: The PMU counter pointer
190 * If this counter has been configured to monitor some event, release it here.
192 static void kvm_pmu_stop_counter(struct kvm_pmc *pmc)
194 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
197 if (!pmc->perf_event)
200 val = kvm_pmu_get_pmc_value(pmc);
202 reg = counter_index_to_reg(pmc->idx);
204 __vcpu_sys_reg(vcpu, reg) = val;
206 kvm_pmu_release_perf_event(pmc);
210 * kvm_pmu_vcpu_init - assign pmu counter idx for cpu
211 * @vcpu: The vcpu pointer
214 void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu)
217 struct kvm_pmu *pmu = &vcpu->arch.pmu;
219 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++)
224 * kvm_pmu_vcpu_reset - reset pmu state for cpu
225 * @vcpu: The vcpu pointer
228 void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu)
230 unsigned long mask = kvm_pmu_valid_counter_mask(vcpu);
233 for_each_set_bit(i, &mask, 32)
234 kvm_pmu_stop_counter(kvm_vcpu_idx_to_pmc(vcpu, i));
238 * kvm_pmu_vcpu_destroy - free perf event of PMU for cpu
239 * @vcpu: The vcpu pointer
242 void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
246 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++)
247 kvm_pmu_release_perf_event(kvm_vcpu_idx_to_pmc(vcpu, i));
248 irq_work_sync(&vcpu->arch.pmu.overflow_work);
251 u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
253 u64 val = kvm_vcpu_read_pmcr(vcpu) >> ARMV8_PMU_PMCR_N_SHIFT;
255 val &= ARMV8_PMU_PMCR_N_MASK;
257 return BIT(ARMV8_PMU_CYCLE_IDX);
259 return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX);
263 * kvm_pmu_enable_counter_mask - enable selected PMU counters
264 * @vcpu: The vcpu pointer
265 * @val: the value guest writes to PMCNTENSET register
267 * Call perf_event_enable to start counting the perf event
269 void kvm_pmu_enable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
272 if (!kvm_vcpu_has_pmu(vcpu))
275 if (!(kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E) || !val)
278 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
284 pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
286 if (!pmc->perf_event) {
287 kvm_pmu_create_perf_event(pmc);
289 perf_event_enable(pmc->perf_event);
290 if (pmc->perf_event->state != PERF_EVENT_STATE_ACTIVE)
291 kvm_debug("fail to enable perf event\n");
297 * kvm_pmu_disable_counter_mask - disable selected PMU counters
298 * @vcpu: The vcpu pointer
299 * @val: the value guest writes to PMCNTENCLR register
301 * Call perf_event_disable to stop counting the perf event
303 void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
307 if (!kvm_vcpu_has_pmu(vcpu) || !val)
310 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
316 pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
319 perf_event_disable(pmc->perf_event);
323 static u64 kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
327 if ((kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E)) {
328 reg = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
329 reg &= __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
330 reg &= __vcpu_sys_reg(vcpu, PMINTENSET_EL1);
336 static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
338 struct kvm_pmu *pmu = &vcpu->arch.pmu;
341 if (!kvm_vcpu_has_pmu(vcpu))
344 overflow = !!kvm_pmu_overflow_status(vcpu);
345 if (pmu->irq_level == overflow)
348 pmu->irq_level = overflow;
350 if (likely(irqchip_in_kernel(vcpu->kvm))) {
351 int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
352 pmu->irq_num, overflow, pmu);
357 bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
359 struct kvm_pmu *pmu = &vcpu->arch.pmu;
360 struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
361 bool run_level = sregs->device_irq_level & KVM_ARM_DEV_PMU;
363 if (likely(irqchip_in_kernel(vcpu->kvm)))
366 return pmu->irq_level != run_level;
370 * Reflect the PMU overflow interrupt output level into the kvm_run structure
372 void kvm_pmu_update_run(struct kvm_vcpu *vcpu)
374 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
376 /* Populate the timer bitmap for user space */
377 regs->device_irq_level &= ~KVM_ARM_DEV_PMU;
378 if (vcpu->arch.pmu.irq_level)
379 regs->device_irq_level |= KVM_ARM_DEV_PMU;
383 * kvm_pmu_flush_hwstate - flush pmu state to cpu
384 * @vcpu: The vcpu pointer
386 * Check if the PMU has overflowed while we were running in the host, and inject
387 * an interrupt if that was the case.
389 void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu)
391 kvm_pmu_update_state(vcpu);
395 * kvm_pmu_sync_hwstate - sync pmu state from cpu
396 * @vcpu: The vcpu pointer
398 * Check if the PMU has overflowed while we were running in the guest, and
399 * inject an interrupt if that was the case.
401 void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu)
403 kvm_pmu_update_state(vcpu);
407 * When perf interrupt is an NMI, we cannot safely notify the vcpu corresponding
409 * This is why we need a callback to do it once outside of the NMI context.
411 static void kvm_pmu_perf_overflow_notify_vcpu(struct irq_work *work)
413 struct kvm_vcpu *vcpu;
415 vcpu = container_of(work, struct kvm_vcpu, arch.pmu.overflow_work);
420 * Perform an increment on any of the counters described in @mask,
421 * generating the overflow if required, and propagate it as a chained
424 static void kvm_pmu_counter_increment(struct kvm_vcpu *vcpu,
425 unsigned long mask, u32 event)
429 if (!(kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E))
432 /* Weed out disabled counters */
433 mask &= __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
435 for_each_set_bit(i, &mask, ARMV8_PMU_CYCLE_IDX) {
436 struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
439 /* Filter on event type */
440 type = __vcpu_sys_reg(vcpu, counter_index_to_evtreg(i));
441 type &= kvm_pmu_event_mask(vcpu->kvm);
445 /* Increment this counter */
446 reg = __vcpu_sys_reg(vcpu, counter_index_to_reg(i)) + 1;
447 if (!kvm_pmc_is_64bit(pmc))
448 reg = lower_32_bits(reg);
449 __vcpu_sys_reg(vcpu, counter_index_to_reg(i)) = reg;
451 /* No overflow? move on */
452 if (kvm_pmc_has_64bit_overflow(pmc) ? reg : lower_32_bits(reg))
456 __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(i);
458 if (kvm_pmu_counter_can_chain(pmc))
459 kvm_pmu_counter_increment(vcpu, BIT(i + 1),
460 ARMV8_PMUV3_PERFCTR_CHAIN);
464 /* Compute the sample period for a given counter value */
465 static u64 compute_period(struct kvm_pmc *pmc, u64 counter)
469 if (kvm_pmc_is_64bit(pmc) && kvm_pmc_has_64bit_overflow(pmc))
470 val = (-counter) & GENMASK(63, 0);
472 val = (-counter) & GENMASK(31, 0);
478 * When the perf event overflows, set the overflow status and inform the vcpu.
480 static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
481 struct perf_sample_data *data,
482 struct pt_regs *regs)
484 struct kvm_pmc *pmc = perf_event->overflow_handler_context;
485 struct arm_pmu *cpu_pmu = to_arm_pmu(perf_event->pmu);
486 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
490 cpu_pmu->pmu.stop(perf_event, PERF_EF_UPDATE);
493 * Reset the sample period to the architectural limit,
494 * i.e. the point where the counter overflows.
496 period = compute_period(pmc, local64_read(&perf_event->count));
498 local64_set(&perf_event->hw.period_left, 0);
499 perf_event->attr.sample_period = period;
500 perf_event->hw.sample_period = period;
502 __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(idx);
504 if (kvm_pmu_counter_can_chain(pmc))
505 kvm_pmu_counter_increment(vcpu, BIT(idx + 1),
506 ARMV8_PMUV3_PERFCTR_CHAIN);
508 if (kvm_pmu_overflow_status(vcpu)) {
509 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
514 irq_work_queue(&vcpu->arch.pmu.overflow_work);
517 cpu_pmu->pmu.start(perf_event, PERF_EF_RELOAD);
521 * kvm_pmu_software_increment - do software increment
522 * @vcpu: The vcpu pointer
523 * @val: the value guest writes to PMSWINC register
525 void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val)
527 kvm_pmu_counter_increment(vcpu, val, ARMV8_PMUV3_PERFCTR_SW_INCR);
531 * kvm_pmu_handle_pmcr - handle PMCR register
532 * @vcpu: The vcpu pointer
533 * @val: the value guest writes to PMCR register
535 void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
539 if (!kvm_vcpu_has_pmu(vcpu))
542 /* Fixup PMCR_EL0 to reconcile the PMU version and the LP bit */
543 if (!kvm_pmu_is_3p5(vcpu))
544 val &= ~ARMV8_PMU_PMCR_LP;
546 /* The reset bits don't indicate any state, and shouldn't be saved. */
547 __vcpu_sys_reg(vcpu, PMCR_EL0) = val & ~(ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_P);
549 if (val & ARMV8_PMU_PMCR_E) {
550 kvm_pmu_enable_counter_mask(vcpu,
551 __vcpu_sys_reg(vcpu, PMCNTENSET_EL0));
553 kvm_pmu_disable_counter_mask(vcpu,
554 __vcpu_sys_reg(vcpu, PMCNTENSET_EL0));
557 if (val & ARMV8_PMU_PMCR_C)
558 kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0);
560 if (val & ARMV8_PMU_PMCR_P) {
561 unsigned long mask = kvm_pmu_valid_counter_mask(vcpu);
562 mask &= ~BIT(ARMV8_PMU_CYCLE_IDX);
563 for_each_set_bit(i, &mask, 32)
564 kvm_pmu_set_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, i), 0, true);
566 kvm_vcpu_pmu_restore_guest(vcpu);
569 static bool kvm_pmu_counter_is_enabled(struct kvm_pmc *pmc)
571 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
572 return (kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E) &&
573 (__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & BIT(pmc->idx));
577 * kvm_pmu_create_perf_event - create a perf event for a counter
578 * @pmc: Counter context
580 static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc)
582 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
583 struct arm_pmu *arm_pmu = vcpu->kvm->arch.arm_pmu;
584 struct perf_event *event;
585 struct perf_event_attr attr;
586 u64 eventsel, reg, data;
588 reg = counter_index_to_evtreg(pmc->idx);
589 data = __vcpu_sys_reg(vcpu, reg);
591 kvm_pmu_stop_counter(pmc);
592 if (pmc->idx == ARMV8_PMU_CYCLE_IDX)
593 eventsel = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;
595 eventsel = data & kvm_pmu_event_mask(vcpu->kvm);
598 * Neither SW increment nor chained events need to be backed
601 if (eventsel == ARMV8_PMUV3_PERFCTR_SW_INCR ||
602 eventsel == ARMV8_PMUV3_PERFCTR_CHAIN)
606 * If we have a filter in place and that the event isn't allowed, do
607 * not install a perf event either.
609 if (vcpu->kvm->arch.pmu_filter &&
610 !test_bit(eventsel, vcpu->kvm->arch.pmu_filter))
613 memset(&attr, 0, sizeof(struct perf_event_attr));
614 attr.type = arm_pmu->pmu.type;
615 attr.size = sizeof(attr);
617 attr.disabled = !kvm_pmu_counter_is_enabled(pmc);
618 attr.exclude_user = data & ARMV8_PMU_EXCLUDE_EL0 ? 1 : 0;
619 attr.exclude_kernel = data & ARMV8_PMU_EXCLUDE_EL1 ? 1 : 0;
620 attr.exclude_hv = 1; /* Don't count EL2 events */
621 attr.exclude_host = 1; /* Don't count host events */
622 attr.config = eventsel;
625 * If counting with a 64bit counter, advertise it to the perf
626 * code, carefully dealing with the initial sample period
627 * which also depends on the overflow.
629 if (kvm_pmc_is_64bit(pmc))
630 attr.config1 |= PERF_ATTR_CFG1_COUNTER_64BIT;
632 attr.sample_period = compute_period(pmc, kvm_pmu_get_pmc_value(pmc));
634 event = perf_event_create_kernel_counter(&attr, -1, current,
635 kvm_pmu_perf_overflow, pmc);
638 pr_err_once("kvm: pmu event creation failed %ld\n",
643 pmc->perf_event = event;
647 * kvm_pmu_set_counter_event_type - set selected counter to monitor some event
648 * @vcpu: The vcpu pointer
649 * @data: The data guest writes to PMXEVTYPER_EL0
650 * @select_idx: The number of selected counter
652 * When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an
653 * event with given hardware event number. Here we call perf_event API to
654 * emulate this action and create a kernel perf event for it.
656 void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
659 struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, select_idx);
662 if (!kvm_vcpu_has_pmu(vcpu))
665 mask = ARMV8_PMU_EVTYPE_MASK;
666 mask &= ~ARMV8_PMU_EVTYPE_EVENT;
667 mask |= kvm_pmu_event_mask(vcpu->kvm);
669 reg = counter_index_to_evtreg(pmc->idx);
671 __vcpu_sys_reg(vcpu, reg) = data & mask;
673 kvm_pmu_create_perf_event(pmc);
676 void kvm_host_pmu_init(struct arm_pmu *pmu)
678 struct arm_pmu_entry *entry;
681 * Check the sanitised PMU version for the system, as KVM does not
682 * support implementations where PMUv3 exists on a subset of CPUs.
684 if (!pmuv3_implemented(kvm_arm_pmu_get_pmuver_limit()))
687 mutex_lock(&arm_pmus_lock);
689 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
693 entry->arm_pmu = pmu;
694 list_add_tail(&entry->entry, &arm_pmus);
696 if (list_is_singular(&arm_pmus))
697 static_branch_enable(&kvm_arm_pmu_available);
700 mutex_unlock(&arm_pmus_lock);
703 static struct arm_pmu *kvm_pmu_probe_armpmu(void)
705 struct arm_pmu *tmp, *pmu = NULL;
706 struct arm_pmu_entry *entry;
709 mutex_lock(&arm_pmus_lock);
712 * It is safe to use a stale cpu to iterate the list of PMUs so long as
713 * the same value is used for the entirety of the loop. Given this, and
714 * the fact that no percpu data is used for the lookup there is no need
715 * to disable preemption.
717 * It is still necessary to get a valid cpu, though, to probe for the
718 * default PMU instance as userspace is not required to specify a PMU
719 * type. In order to uphold the preexisting behavior KVM selects the
720 * PMU instance for the core during vcpu init. A dependent use
721 * case would be a user with disdain of all things big.LITTLE that
722 * affines the VMM to a particular cluster of cores.
724 * In any case, userspace should just do the sane thing and use the UAPI
725 * to select a PMU type directly. But, be wary of the baggage being
728 cpu = raw_smp_processor_id();
729 list_for_each_entry(entry, &arm_pmus, entry) {
730 tmp = entry->arm_pmu;
732 if (cpumask_test_cpu(cpu, &tmp->supported_cpus)) {
738 mutex_unlock(&arm_pmus_lock);
743 u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
745 unsigned long *bmap = vcpu->kvm->arch.pmu_filter;
747 int base, i, nr_events;
749 if (!kvm_vcpu_has_pmu(vcpu))
753 val = read_sysreg(pmceid0_el0);
754 /* always support CHAIN */
755 val |= BIT(ARMV8_PMUV3_PERFCTR_CHAIN);
758 val = read_sysreg(pmceid1_el0);
760 * Don't advertise STALL_SLOT*, as PMMIR_EL0 is handled
763 val &= ~(BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32) |
764 BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND - 32) |
765 BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND - 32));
772 nr_events = kvm_pmu_event_mask(vcpu->kvm) + 1;
774 for (i = 0; i < 32; i += 8) {
777 byte = bitmap_get_value8(bmap, base + i);
779 if (nr_events >= (0x4000 + base + 32)) {
780 byte = bitmap_get_value8(bmap, 0x4000 + base + i);
781 mask |= byte << (32 + i);
788 int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)
790 if (!kvm_vcpu_has_pmu(vcpu))
793 if (!vcpu->arch.pmu.created)
797 * A valid interrupt configuration for the PMU is either to have a
798 * properly configured interrupt number and using an in-kernel
799 * irqchip, or to not have an in-kernel GIC and not set an IRQ.
801 if (irqchip_in_kernel(vcpu->kvm)) {
802 int irq = vcpu->arch.pmu.irq_num;
804 * If we are using an in-kernel vgic, at this point we know
805 * the vgic will be initialized, so we can check the PMU irq
806 * number against the dimensions of the vgic and make sure
809 if (!irq_is_ppi(irq) && !vgic_valid_spi(vcpu->kvm, irq))
811 } else if (kvm_arm_pmu_irq_initialized(vcpu)) {
815 /* One-off reload of the PMU on first run */
816 kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu);
821 static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)
823 if (irqchip_in_kernel(vcpu->kvm)) {
827 * If using the PMU with an in-kernel virtual GIC
828 * implementation, we require the GIC to be already
829 * initialized when initializing the PMU.
831 if (!vgic_initialized(vcpu->kvm))
834 if (!kvm_arm_pmu_irq_initialized(vcpu))
837 ret = kvm_vgic_set_owner(vcpu, vcpu->arch.pmu.irq_num,
843 init_irq_work(&vcpu->arch.pmu.overflow_work,
844 kvm_pmu_perf_overflow_notify_vcpu);
846 vcpu->arch.pmu.created = true;
851 * For one VM the interrupt type must be same for each vcpu.
852 * As a PPI, the interrupt number is the same for all vcpus,
853 * while as an SPI it must be a separate number per vcpu.
855 static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
858 struct kvm_vcpu *vcpu;
860 kvm_for_each_vcpu(i, vcpu, kvm) {
861 if (!kvm_arm_pmu_irq_initialized(vcpu))
864 if (irq_is_ppi(irq)) {
865 if (vcpu->arch.pmu.irq_num != irq)
868 if (vcpu->arch.pmu.irq_num == irq)
876 static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
878 lockdep_assert_held(&kvm->arch.config_lock);
880 kvm->arch.arm_pmu = arm_pmu;
884 * kvm_arm_set_default_pmu - No PMU set, get the default one.
885 * @kvm: The kvm pointer
887 * The observant among you will notice that the supported_cpus
888 * mask does not get updated for the default PMU even though it
889 * is quite possible the selected instance supports only a
890 * subset of cores in the system. This is intentional, and
891 * upholds the preexisting behavior on heterogeneous systems
892 * where vCPUs can be scheduled on any core but the guest
893 * counters could stop working.
895 int kvm_arm_set_default_pmu(struct kvm *kvm)
897 struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu();
902 kvm_arm_set_pmu(kvm, arm_pmu);
906 static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id)
908 struct kvm *kvm = vcpu->kvm;
909 struct arm_pmu_entry *entry;
910 struct arm_pmu *arm_pmu;
913 lockdep_assert_held(&kvm->arch.config_lock);
914 mutex_lock(&arm_pmus_lock);
916 list_for_each_entry(entry, &arm_pmus, entry) {
917 arm_pmu = entry->arm_pmu;
918 if (arm_pmu->pmu.type == pmu_id) {
919 if (kvm_vm_has_ran_once(kvm) ||
920 (kvm->arch.pmu_filter && kvm->arch.arm_pmu != arm_pmu)) {
925 kvm_arm_set_pmu(kvm, arm_pmu);
926 cpumask_copy(kvm->arch.supported_cpus, &arm_pmu->supported_cpus);
932 mutex_unlock(&arm_pmus_lock);
936 int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
938 struct kvm *kvm = vcpu->kvm;
940 lockdep_assert_held(&kvm->arch.config_lock);
942 if (!kvm_vcpu_has_pmu(vcpu))
945 if (vcpu->arch.pmu.created)
948 switch (attr->attr) {
949 case KVM_ARM_VCPU_PMU_V3_IRQ: {
950 int __user *uaddr = (int __user *)(long)attr->addr;
953 if (!irqchip_in_kernel(kvm))
956 if (get_user(irq, uaddr))
959 /* The PMU overflow interrupt can be a PPI or a valid SPI. */
960 if (!(irq_is_ppi(irq) || irq_is_spi(irq)))
963 if (!pmu_irq_is_valid(kvm, irq))
966 if (kvm_arm_pmu_irq_initialized(vcpu))
969 kvm_debug("Set kvm ARM PMU irq: %d\n", irq);
970 vcpu->arch.pmu.irq_num = irq;
973 case KVM_ARM_VCPU_PMU_V3_FILTER: {
974 u8 pmuver = kvm_arm_pmu_get_pmuver_limit();
975 struct kvm_pmu_event_filter __user *uaddr;
976 struct kvm_pmu_event_filter filter;
980 * Allow userspace to specify an event filter for the entire
981 * event range supported by PMUVer of the hardware, rather
982 * than the guest's PMUVer for KVM backward compatibility.
984 nr_events = __kvm_pmu_event_mask(pmuver) + 1;
986 uaddr = (struct kvm_pmu_event_filter __user *)(long)attr->addr;
988 if (copy_from_user(&filter, uaddr, sizeof(filter)))
991 if (((u32)filter.base_event + filter.nevents) > nr_events ||
992 (filter.action != KVM_PMU_EVENT_ALLOW &&
993 filter.action != KVM_PMU_EVENT_DENY))
996 if (kvm_vm_has_ran_once(kvm))
999 if (!kvm->arch.pmu_filter) {
1000 kvm->arch.pmu_filter = bitmap_alloc(nr_events, GFP_KERNEL_ACCOUNT);
1001 if (!kvm->arch.pmu_filter)
1005 * The default depends on the first applied filter.
1006 * If it allows events, the default is to deny.
1007 * Conversely, if the first filter denies a set of
1008 * events, the default is to allow.
1010 if (filter.action == KVM_PMU_EVENT_ALLOW)
1011 bitmap_zero(kvm->arch.pmu_filter, nr_events);
1013 bitmap_fill(kvm->arch.pmu_filter, nr_events);
1016 if (filter.action == KVM_PMU_EVENT_ALLOW)
1017 bitmap_set(kvm->arch.pmu_filter, filter.base_event, filter.nevents);
1019 bitmap_clear(kvm->arch.pmu_filter, filter.base_event, filter.nevents);
1023 case KVM_ARM_VCPU_PMU_V3_SET_PMU: {
1024 int __user *uaddr = (int __user *)(long)attr->addr;
1027 if (get_user(pmu_id, uaddr))
1030 return kvm_arm_pmu_v3_set_pmu(vcpu, pmu_id);
1032 case KVM_ARM_VCPU_PMU_V3_INIT:
1033 return kvm_arm_pmu_v3_init(vcpu);
1039 int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
1041 switch (attr->attr) {
1042 case KVM_ARM_VCPU_PMU_V3_IRQ: {
1043 int __user *uaddr = (int __user *)(long)attr->addr;
1046 if (!irqchip_in_kernel(vcpu->kvm))
1049 if (!kvm_vcpu_has_pmu(vcpu))
1052 if (!kvm_arm_pmu_irq_initialized(vcpu))
1055 irq = vcpu->arch.pmu.irq_num;
1056 return put_user(irq, uaddr);
1063 int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
1065 switch (attr->attr) {
1066 case KVM_ARM_VCPU_PMU_V3_IRQ:
1067 case KVM_ARM_VCPU_PMU_V3_INIT:
1068 case KVM_ARM_VCPU_PMU_V3_FILTER:
1069 case KVM_ARM_VCPU_PMU_V3_SET_PMU:
1070 if (kvm_vcpu_has_pmu(vcpu))
1077 u8 kvm_arm_pmu_get_pmuver_limit(void)
1081 tmp = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1);
1082 tmp = cpuid_feature_cap_perfmon_field(tmp,
1083 ID_AA64DFR0_EL1_PMUVer_SHIFT,
1084 ID_AA64DFR0_EL1_PMUVer_V3P5);
1085 return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), tmp);
1089 * kvm_vcpu_read_pmcr - Read PMCR_EL0 register for the vCPU
1090 * @vcpu: The vcpu pointer
1092 u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)
1094 return __vcpu_sys_reg(vcpu, PMCR_EL0);