1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2019 Arm Limited
4 * Author: Andrew Murray <Andrew.Murray@arm.com>
6 #include <linux/kvm_host.h>
7 #include <linux/perf_event.h>
9 static DEFINE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);
12 * Given the perf event attributes and system type, determine
13 * if we are going to need to switch counters at guest entry/exit.
15 static bool kvm_pmu_switch_needed(struct perf_event_attr *attr)
18 * With VHE the guest kernel runs at EL1 and the host at EL2,
19 * where user (EL0) is excluded then we have no reason to switch
22 if (has_vhe() && attr->exclude_user)
25 /* Only switch if attributes are different */
26 return (attr->exclude_host != attr->exclude_guest);
29 struct kvm_pmu_events *kvm_get_pmu_events(void)
31 return this_cpu_ptr(&kvm_pmu_events);
35 * Add events to track that we may want to switch at guest entry/exit
38 void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr)
40 struct kvm_pmu_events *pmu = kvm_get_pmu_events();
42 if (!kvm_arm_support_pmu_v3() || !pmu || !kvm_pmu_switch_needed(attr))
45 if (!attr->exclude_host)
46 pmu->events_host |= set;
47 if (!attr->exclude_guest)
48 pmu->events_guest |= set;
52 * Stop tracking events
54 void kvm_clr_pmu_events(u32 clr)
56 struct kvm_pmu_events *pmu = kvm_get_pmu_events();
58 if (!kvm_arm_support_pmu_v3() || !pmu)
61 pmu->events_host &= ~clr;
62 pmu->events_guest &= ~clr;
65 #define PMEVTYPER_READ_CASE(idx) \
67 return read_sysreg(pmevtyper##idx##_el0)
69 #define PMEVTYPER_WRITE_CASE(idx) \
71 write_sysreg(val, pmevtyper##idx##_el0); \
74 #define PMEVTYPER_CASES(readwrite) \
75 PMEVTYPER_##readwrite##_CASE(0); \
76 PMEVTYPER_##readwrite##_CASE(1); \
77 PMEVTYPER_##readwrite##_CASE(2); \
78 PMEVTYPER_##readwrite##_CASE(3); \
79 PMEVTYPER_##readwrite##_CASE(4); \
80 PMEVTYPER_##readwrite##_CASE(5); \
81 PMEVTYPER_##readwrite##_CASE(6); \
82 PMEVTYPER_##readwrite##_CASE(7); \
83 PMEVTYPER_##readwrite##_CASE(8); \
84 PMEVTYPER_##readwrite##_CASE(9); \
85 PMEVTYPER_##readwrite##_CASE(10); \
86 PMEVTYPER_##readwrite##_CASE(11); \
87 PMEVTYPER_##readwrite##_CASE(12); \
88 PMEVTYPER_##readwrite##_CASE(13); \
89 PMEVTYPER_##readwrite##_CASE(14); \
90 PMEVTYPER_##readwrite##_CASE(15); \
91 PMEVTYPER_##readwrite##_CASE(16); \
92 PMEVTYPER_##readwrite##_CASE(17); \
93 PMEVTYPER_##readwrite##_CASE(18); \
94 PMEVTYPER_##readwrite##_CASE(19); \
95 PMEVTYPER_##readwrite##_CASE(20); \
96 PMEVTYPER_##readwrite##_CASE(21); \
97 PMEVTYPER_##readwrite##_CASE(22); \
98 PMEVTYPER_##readwrite##_CASE(23); \
99 PMEVTYPER_##readwrite##_CASE(24); \
100 PMEVTYPER_##readwrite##_CASE(25); \
101 PMEVTYPER_##readwrite##_CASE(26); \
102 PMEVTYPER_##readwrite##_CASE(27); \
103 PMEVTYPER_##readwrite##_CASE(28); \
104 PMEVTYPER_##readwrite##_CASE(29); \
105 PMEVTYPER_##readwrite##_CASE(30)
108 * Read a value direct from PMEVTYPER<idx> where idx is 0-30
109 * or PMCCFILTR_EL0 where idx is ARMV8_PMU_CYCLE_IDX (31).
111 static u64 kvm_vcpu_pmu_read_evtype_direct(int idx)
114 PMEVTYPER_CASES(READ);
115 case ARMV8_PMU_CYCLE_IDX:
116 return read_sysreg(pmccfiltr_el0);
125 * Write a value direct to PMEVTYPER<idx> where idx is 0-30
126 * or PMCCFILTR_EL0 where idx is ARMV8_PMU_CYCLE_IDX (31).
128 static void kvm_vcpu_pmu_write_evtype_direct(int idx, u32 val)
131 PMEVTYPER_CASES(WRITE);
132 case ARMV8_PMU_CYCLE_IDX:
133 write_sysreg(val, pmccfiltr_el0);
141 * Modify ARMv8 PMU events to include EL0 counting
143 static void kvm_vcpu_pmu_enable_el0(unsigned long events)
148 for_each_set_bit(counter, &events, 32) {
149 typer = kvm_vcpu_pmu_read_evtype_direct(counter);
150 typer &= ~ARMV8_PMU_EXCLUDE_EL0;
151 kvm_vcpu_pmu_write_evtype_direct(counter, typer);
156 * Modify ARMv8 PMU events to exclude EL0 counting
158 static void kvm_vcpu_pmu_disable_el0(unsigned long events)
163 for_each_set_bit(counter, &events, 32) {
164 typer = kvm_vcpu_pmu_read_evtype_direct(counter);
165 typer |= ARMV8_PMU_EXCLUDE_EL0;
166 kvm_vcpu_pmu_write_evtype_direct(counter, typer);
171 * On VHE ensure that only guest events have EL0 counting enabled.
172 * This is called from both vcpu_{load,put} and the sysreg handling.
173 * Since the latter is preemptible, special care must be taken to
174 * disable preemption.
176 void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu)
178 struct kvm_pmu_events *pmu;
179 u32 events_guest, events_host;
181 if (!kvm_arm_support_pmu_v3() || !has_vhe())
185 pmu = kvm_get_pmu_events();
186 events_guest = pmu->events_guest;
187 events_host = pmu->events_host;
189 kvm_vcpu_pmu_enable_el0(events_guest);
190 kvm_vcpu_pmu_disable_el0(events_host);
195 * On VHE ensure that only host events have EL0 counting enabled
197 void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu)
199 struct kvm_pmu_events *pmu;
200 u32 events_guest, events_host;
202 if (!kvm_arm_support_pmu_v3() || !has_vhe())
205 pmu = kvm_get_pmu_events();
206 events_guest = pmu->events_guest;
207 events_host = pmu->events_host;
209 kvm_vcpu_pmu_enable_el0(events_host);
210 kvm_vcpu_pmu_disable_el0(events_guest);