Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Shannon Zhao | 051ff58 | 2015-12-08 15:29:06 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Linaro Ltd. |
| 4 | * Author: Shannon Zhao <shannon.zhao@linaro.org> |
Shannon Zhao | 051ff58 | 2015-12-08 15:29:06 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/cpu.h> |
| 8 | #include <linux/kvm.h> |
| 9 | #include <linux/kvm_host.h> |
| 10 | #include <linux/perf_event.h> |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 11 | #include <linux/uaccess.h> |
Shannon Zhao | 051ff58 | 2015-12-08 15:29:06 +0800 | [diff] [blame] | 12 | #include <asm/kvm_emulate.h> |
| 13 | #include <kvm/arm_pmu.h> |
Shannon Zhao | b02386e | 2016-02-26 19:29:19 +0800 | [diff] [blame] | 14 | #include <kvm/arm_vgic.h> |
Shannon Zhao | 051ff58 | 2015-12-08 15:29:06 +0800 | [diff] [blame] | 15 | |
Andrew Murray | 30d9775 | 2019-06-17 20:01:03 +0100 | [diff] [blame] | 16 | static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx); |
Andrew Murray | 218907c | 2019-06-17 20:01:04 +0100 | [diff] [blame] | 17 | |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 18 | #define PERF_ATTR_CFG1_KVM_PMU_CHAINED 0x1 |
| 19 | |
Andrew Murray | 218907c | 2019-06-17 20:01:04 +0100 | [diff] [blame] | 20 | /** |
| 21 | * kvm_pmu_idx_is_64bit - determine if select_idx is a 64bit counter |
| 22 | * @vcpu: The vcpu pointer |
| 23 | * @select_idx: The counter index |
| 24 | */ |
| 25 | static bool kvm_pmu_idx_is_64bit(struct kvm_vcpu *vcpu, u64 select_idx) |
| 26 | { |
| 27 | return (select_idx == ARMV8_PMU_CYCLE_IDX && |
| 28 | __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC); |
| 29 | } |
| 30 | |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 31 | static struct kvm_vcpu *kvm_pmc_to_vcpu(struct kvm_pmc *pmc) |
| 32 | { |
| 33 | struct kvm_pmu *pmu; |
| 34 | struct kvm_vcpu_arch *vcpu_arch; |
| 35 | |
| 36 | pmc -= pmc->idx; |
| 37 | pmu = container_of(pmc, struct kvm_pmu, pmc[0]); |
| 38 | vcpu_arch = container_of(pmu, struct kvm_vcpu_arch, pmu); |
| 39 | return container_of(vcpu_arch, struct kvm_vcpu, arch); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * kvm_pmu_pmc_is_chained - determine if the pmc is chained |
| 44 | * @pmc: The PMU counter pointer |
| 45 | */ |
| 46 | static bool kvm_pmu_pmc_is_chained(struct kvm_pmc *pmc) |
| 47 | { |
| 48 | struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc); |
| 49 | |
| 50 | return test_bit(pmc->idx >> 1, vcpu->arch.pmu.chained); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * kvm_pmu_idx_is_high_counter - determine if select_idx is a high/low counter |
| 55 | * @select_idx: The counter index |
| 56 | */ |
| 57 | static bool kvm_pmu_idx_is_high_counter(u64 select_idx) |
| 58 | { |
| 59 | return select_idx & 0x1; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * kvm_pmu_get_canonical_pmc - obtain the canonical pmc |
| 64 | * @pmc: The PMU counter pointer |
| 65 | * |
| 66 | * When a pair of PMCs are chained together we use the low counter (canonical) |
| 67 | * to hold the underlying perf event. |
| 68 | */ |
| 69 | static struct kvm_pmc *kvm_pmu_get_canonical_pmc(struct kvm_pmc *pmc) |
| 70 | { |
| 71 | if (kvm_pmu_pmc_is_chained(pmc) && |
| 72 | kvm_pmu_idx_is_high_counter(pmc->idx)) |
| 73 | return pmc - 1; |
| 74 | |
| 75 | return pmc; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * kvm_pmu_idx_has_chain_evtype - determine if the event type is chain |
| 80 | * @vcpu: The vcpu pointer |
| 81 | * @select_idx: The counter index |
| 82 | */ |
| 83 | static bool kvm_pmu_idx_has_chain_evtype(struct kvm_vcpu *vcpu, u64 select_idx) |
| 84 | { |
| 85 | u64 eventsel, reg; |
| 86 | |
| 87 | select_idx |= 0x1; |
| 88 | |
| 89 | if (select_idx == ARMV8_PMU_CYCLE_IDX) |
| 90 | return false; |
| 91 | |
| 92 | reg = PMEVTYPER0_EL0 + select_idx; |
| 93 | eventsel = __vcpu_sys_reg(vcpu, reg) & ARMV8_PMU_EVTYPE_EVENT; |
| 94 | |
| 95 | return eventsel == ARMV8_PMUV3_PERFCTR_CHAIN; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * kvm_pmu_get_pair_counter_value - get PMU counter value |
| 100 | * @vcpu: The vcpu pointer |
| 101 | * @pmc: The PMU counter pointer |
| 102 | */ |
| 103 | static u64 kvm_pmu_get_pair_counter_value(struct kvm_vcpu *vcpu, |
| 104 | struct kvm_pmc *pmc) |
| 105 | { |
| 106 | u64 counter, counter_high, reg, enabled, running; |
| 107 | |
| 108 | if (kvm_pmu_pmc_is_chained(pmc)) { |
| 109 | pmc = kvm_pmu_get_canonical_pmc(pmc); |
| 110 | reg = PMEVCNTR0_EL0 + pmc->idx; |
| 111 | |
| 112 | counter = __vcpu_sys_reg(vcpu, reg); |
| 113 | counter_high = __vcpu_sys_reg(vcpu, reg + 1); |
| 114 | |
| 115 | counter = lower_32_bits(counter) | (counter_high << 32); |
| 116 | } else { |
| 117 | reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX) |
| 118 | ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx; |
| 119 | counter = __vcpu_sys_reg(vcpu, reg); |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * The real counter value is equal to the value of counter register plus |
| 124 | * the value perf event counts. |
| 125 | */ |
| 126 | if (pmc->perf_event) |
| 127 | counter += perf_event_read_value(pmc->perf_event, &enabled, |
| 128 | &running); |
| 129 | |
| 130 | return counter; |
| 131 | } |
| 132 | |
Shannon Zhao | 051ff58 | 2015-12-08 15:29:06 +0800 | [diff] [blame] | 133 | /** |
| 134 | * kvm_pmu_get_counter_value - get PMU counter value |
| 135 | * @vcpu: The vcpu pointer |
| 136 | * @select_idx: The counter index |
| 137 | */ |
| 138 | u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx) |
| 139 | { |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 140 | u64 counter; |
Shannon Zhao | 051ff58 | 2015-12-08 15:29:06 +0800 | [diff] [blame] | 141 | struct kvm_pmu *pmu = &vcpu->arch.pmu; |
| 142 | struct kvm_pmc *pmc = &pmu->pmc[select_idx]; |
| 143 | |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 144 | counter = kvm_pmu_get_pair_counter_value(vcpu, pmc); |
Shannon Zhao | 051ff58 | 2015-12-08 15:29:06 +0800 | [diff] [blame] | 145 | |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 146 | if (kvm_pmu_pmc_is_chained(pmc) && |
| 147 | kvm_pmu_idx_is_high_counter(select_idx)) |
| 148 | counter = upper_32_bits(counter); |
Marc Zyngier | f4e23cf | 2019-10-03 18:02:08 +0100 | [diff] [blame] | 149 | else if (select_idx != ARMV8_PMU_CYCLE_IDX) |
Andrew Murray | 218907c | 2019-06-17 20:01:04 +0100 | [diff] [blame] | 150 | counter = lower_32_bits(counter); |
| 151 | |
| 152 | return counter; |
Shannon Zhao | 051ff58 | 2015-12-08 15:29:06 +0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * kvm_pmu_set_counter_value - set PMU counter value |
| 157 | * @vcpu: The vcpu pointer |
| 158 | * @select_idx: The counter index |
| 159 | * @val: The counter value |
| 160 | */ |
| 161 | void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val) |
| 162 | { |
| 163 | u64 reg; |
| 164 | |
| 165 | reg = (select_idx == ARMV8_PMU_CYCLE_IDX) |
| 166 | ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx; |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 167 | __vcpu_sys_reg(vcpu, reg) += (s64)val - kvm_pmu_get_counter_value(vcpu, select_idx); |
Andrew Murray | 30d9775 | 2019-06-17 20:01:03 +0100 | [diff] [blame] | 168 | |
| 169 | /* Recreate the perf event to reflect the updated sample_period */ |
| 170 | kvm_pmu_create_perf_event(vcpu, select_idx); |
Shannon Zhao | 051ff58 | 2015-12-08 15:29:06 +0800 | [diff] [blame] | 171 | } |
Shannon Zhao | 96b0eeb | 2015-09-08 12:26:13 +0800 | [diff] [blame] | 172 | |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 173 | /** |
Andrew Murray | 6f4d2a0 | 2019-06-17 20:01:02 +0100 | [diff] [blame] | 174 | * kvm_pmu_release_perf_event - remove the perf event |
| 175 | * @pmc: The PMU counter pointer |
| 176 | */ |
| 177 | static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc) |
| 178 | { |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 179 | pmc = kvm_pmu_get_canonical_pmc(pmc); |
Andrew Murray | 6f4d2a0 | 2019-06-17 20:01:02 +0100 | [diff] [blame] | 180 | if (pmc->perf_event) { |
| 181 | perf_event_disable(pmc->perf_event); |
| 182 | perf_event_release_kernel(pmc->perf_event); |
| 183 | pmc->perf_event = NULL; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 188 | * kvm_pmu_stop_counter - stop PMU counter |
| 189 | * @pmc: The PMU counter pointer |
| 190 | * |
| 191 | * If this counter has been configured to monitor some event, release it here. |
| 192 | */ |
| 193 | static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc) |
| 194 | { |
Marc Zyngier | f4e23cf | 2019-10-03 18:02:08 +0100 | [diff] [blame] | 195 | u64 counter, reg, val; |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 196 | |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 197 | pmc = kvm_pmu_get_canonical_pmc(pmc); |
| 198 | if (!pmc->perf_event) |
| 199 | return; |
| 200 | |
| 201 | counter = kvm_pmu_get_pair_counter_value(vcpu, pmc); |
| 202 | |
Marc Zyngier | f4e23cf | 2019-10-03 18:02:08 +0100 | [diff] [blame] | 203 | if (pmc->idx == ARMV8_PMU_CYCLE_IDX) { |
| 204 | reg = PMCCNTR_EL0; |
| 205 | val = counter; |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 206 | } else { |
Marc Zyngier | f4e23cf | 2019-10-03 18:02:08 +0100 | [diff] [blame] | 207 | reg = PMEVCNTR0_EL0 + pmc->idx; |
| 208 | val = lower_32_bits(counter); |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 209 | } |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 210 | |
Marc Zyngier | f4e23cf | 2019-10-03 18:02:08 +0100 | [diff] [blame] | 211 | __vcpu_sys_reg(vcpu, reg) = val; |
| 212 | |
| 213 | if (kvm_pmu_pmc_is_chained(pmc)) |
| 214 | __vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter); |
| 215 | |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 216 | kvm_pmu_release_perf_event(pmc); |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 217 | } |
| 218 | |
Shannon Zhao | 2aa36e9 | 2015-09-11 11:30:22 +0800 | [diff] [blame] | 219 | /** |
Zenghui Yu | bca031e | 2019-07-18 08:15:10 +0000 | [diff] [blame] | 220 | * kvm_pmu_vcpu_init - assign pmu counter idx for cpu |
| 221 | * @vcpu: The vcpu pointer |
| 222 | * |
| 223 | */ |
| 224 | void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu) |
| 225 | { |
| 226 | int i; |
| 227 | struct kvm_pmu *pmu = &vcpu->arch.pmu; |
| 228 | |
| 229 | for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) |
| 230 | pmu->pmc[i].idx = i; |
| 231 | } |
| 232 | |
| 233 | /** |
Shannon Zhao | 2aa36e9 | 2015-09-11 11:30:22 +0800 | [diff] [blame] | 234 | * kvm_pmu_vcpu_reset - reset pmu state for cpu |
| 235 | * @vcpu: The vcpu pointer |
| 236 | * |
| 237 | */ |
| 238 | void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) |
| 239 | { |
| 240 | int i; |
| 241 | struct kvm_pmu *pmu = &vcpu->arch.pmu; |
| 242 | |
Zenghui Yu | bca031e | 2019-07-18 08:15:10 +0000 | [diff] [blame] | 243 | for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) |
Shannon Zhao | 2aa36e9 | 2015-09-11 11:30:22 +0800 | [diff] [blame] | 244 | kvm_pmu_stop_counter(vcpu, &pmu->pmc[i]); |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 245 | |
| 246 | bitmap_zero(vcpu->arch.pmu.chained, ARMV8_PMU_MAX_COUNTER_PAIRS); |
Shannon Zhao | 2aa36e9 | 2015-09-11 11:30:22 +0800 | [diff] [blame] | 247 | } |
| 248 | |
Shannon Zhao | 5f0a714 | 2015-09-11 15:18:05 +0800 | [diff] [blame] | 249 | /** |
| 250 | * kvm_pmu_vcpu_destroy - free perf event of PMU for cpu |
| 251 | * @vcpu: The vcpu pointer |
| 252 | * |
| 253 | */ |
| 254 | void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 255 | { |
| 256 | int i; |
| 257 | struct kvm_pmu *pmu = &vcpu->arch.pmu; |
| 258 | |
Andrew Murray | 6f4d2a0 | 2019-06-17 20:01:02 +0100 | [diff] [blame] | 259 | for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) |
| 260 | kvm_pmu_release_perf_event(&pmu->pmc[i]); |
Shannon Zhao | 5f0a714 | 2015-09-11 15:18:05 +0800 | [diff] [blame] | 261 | } |
| 262 | |
Shannon Zhao | 96b0eeb | 2015-09-08 12:26:13 +0800 | [diff] [blame] | 263 | u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu) |
| 264 | { |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 265 | u64 val = __vcpu_sys_reg(vcpu, PMCR_EL0) >> ARMV8_PMU_PMCR_N_SHIFT; |
Shannon Zhao | 96b0eeb | 2015-09-08 12:26:13 +0800 | [diff] [blame] | 266 | |
| 267 | val &= ARMV8_PMU_PMCR_N_MASK; |
| 268 | if (val == 0) |
| 269 | return BIT(ARMV8_PMU_CYCLE_IDX); |
| 270 | else |
| 271 | return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX); |
| 272 | } |
| 273 | |
| 274 | /** |
Andrew Murray | 418e5ca | 2019-06-17 20:01:01 +0100 | [diff] [blame] | 275 | * kvm_pmu_enable_counter_mask - enable selected PMU counters |
Shannon Zhao | 96b0eeb | 2015-09-08 12:26:13 +0800 | [diff] [blame] | 276 | * @vcpu: The vcpu pointer |
| 277 | * @val: the value guest writes to PMCNTENSET register |
| 278 | * |
| 279 | * Call perf_event_enable to start counting the perf event |
| 280 | */ |
Andrew Murray | 418e5ca | 2019-06-17 20:01:01 +0100 | [diff] [blame] | 281 | void kvm_pmu_enable_counter_mask(struct kvm_vcpu *vcpu, u64 val) |
Shannon Zhao | 96b0eeb | 2015-09-08 12:26:13 +0800 | [diff] [blame] | 282 | { |
| 283 | int i; |
| 284 | struct kvm_pmu *pmu = &vcpu->arch.pmu; |
| 285 | struct kvm_pmc *pmc; |
| 286 | |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 287 | if (!(__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) || !val) |
Shannon Zhao | 96b0eeb | 2015-09-08 12:26:13 +0800 | [diff] [blame] | 288 | return; |
| 289 | |
| 290 | for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) { |
| 291 | if (!(val & BIT(i))) |
| 292 | continue; |
| 293 | |
| 294 | pmc = &pmu->pmc[i]; |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 295 | |
| 296 | /* |
| 297 | * For high counters of chained events we must recreate the |
| 298 | * perf event with the long (64bit) attribute set. |
| 299 | */ |
| 300 | if (kvm_pmu_pmc_is_chained(pmc) && |
| 301 | kvm_pmu_idx_is_high_counter(i)) { |
| 302 | kvm_pmu_create_perf_event(vcpu, i); |
| 303 | continue; |
| 304 | } |
| 305 | |
| 306 | /* At this point, pmc must be the canonical */ |
Shannon Zhao | 96b0eeb | 2015-09-08 12:26:13 +0800 | [diff] [blame] | 307 | if (pmc->perf_event) { |
| 308 | perf_event_enable(pmc->perf_event); |
| 309 | if (pmc->perf_event->state != PERF_EVENT_STATE_ACTIVE) |
| 310 | kvm_debug("fail to enable perf event\n"); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
Andrew Murray | 418e5ca | 2019-06-17 20:01:01 +0100 | [diff] [blame] | 316 | * kvm_pmu_disable_counter_mask - disable selected PMU counters |
Shannon Zhao | 96b0eeb | 2015-09-08 12:26:13 +0800 | [diff] [blame] | 317 | * @vcpu: The vcpu pointer |
| 318 | * @val: the value guest writes to PMCNTENCLR register |
| 319 | * |
| 320 | * Call perf_event_disable to stop counting the perf event |
| 321 | */ |
Andrew Murray | 418e5ca | 2019-06-17 20:01:01 +0100 | [diff] [blame] | 322 | void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val) |
Shannon Zhao | 96b0eeb | 2015-09-08 12:26:13 +0800 | [diff] [blame] | 323 | { |
| 324 | int i; |
| 325 | struct kvm_pmu *pmu = &vcpu->arch.pmu; |
| 326 | struct kvm_pmc *pmc; |
| 327 | |
| 328 | if (!val) |
| 329 | return; |
| 330 | |
| 331 | for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) { |
| 332 | if (!(val & BIT(i))) |
| 333 | continue; |
| 334 | |
| 335 | pmc = &pmu->pmc[i]; |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 336 | |
| 337 | /* |
| 338 | * For high counters of chained events we must recreate the |
| 339 | * perf event with the long (64bit) attribute unset. |
| 340 | */ |
| 341 | if (kvm_pmu_pmc_is_chained(pmc) && |
| 342 | kvm_pmu_idx_is_high_counter(i)) { |
| 343 | kvm_pmu_create_perf_event(vcpu, i); |
| 344 | continue; |
| 345 | } |
| 346 | |
| 347 | /* At this point, pmc must be the canonical */ |
Shannon Zhao | 96b0eeb | 2015-09-08 12:26:13 +0800 | [diff] [blame] | 348 | if (pmc->perf_event) |
| 349 | perf_event_disable(pmc->perf_event); |
| 350 | } |
| 351 | } |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 352 | |
Shannon Zhao | 76d883c | 2015-09-08 15:03:26 +0800 | [diff] [blame] | 353 | static u64 kvm_pmu_overflow_status(struct kvm_vcpu *vcpu) |
| 354 | { |
| 355 | u64 reg = 0; |
| 356 | |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 357 | if ((__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E)) { |
| 358 | reg = __vcpu_sys_reg(vcpu, PMOVSSET_EL0); |
| 359 | reg &= __vcpu_sys_reg(vcpu, PMCNTENSET_EL0); |
| 360 | reg &= __vcpu_sys_reg(vcpu, PMINTENSET_EL1); |
Shannon Zhao | 76d883c | 2015-09-08 15:03:26 +0800 | [diff] [blame] | 361 | reg &= kvm_pmu_valid_counter_mask(vcpu); |
Will Deacon | 7d4bd1d | 2016-04-01 12:12:22 +0100 | [diff] [blame] | 362 | } |
Shannon Zhao | 76d883c | 2015-09-08 15:03:26 +0800 | [diff] [blame] | 363 | |
| 364 | return reg; |
| 365 | } |
| 366 | |
Andrew Jones | d9f89b4 | 2017-07-01 18:26:54 +0200 | [diff] [blame] | 367 | static void kvm_pmu_update_state(struct kvm_vcpu *vcpu) |
Andrew Jones | b748493 | 2017-06-04 14:44:00 +0200 | [diff] [blame] | 368 | { |
| 369 | struct kvm_pmu *pmu = &vcpu->arch.pmu; |
Andrew Jones | d9f89b4 | 2017-07-01 18:26:54 +0200 | [diff] [blame] | 370 | bool overflow; |
Andrew Jones | b748493 | 2017-06-04 14:44:00 +0200 | [diff] [blame] | 371 | |
Andrew Jones | d9f89b4 | 2017-07-01 18:26:54 +0200 | [diff] [blame] | 372 | if (!kvm_arm_pmu_v3_ready(vcpu)) |
| 373 | return; |
| 374 | |
| 375 | overflow = !!kvm_pmu_overflow_status(vcpu); |
Andrew Jones | b748493 | 2017-06-04 14:44:00 +0200 | [diff] [blame] | 376 | if (pmu->irq_level == overflow) |
| 377 | return; |
| 378 | |
| 379 | pmu->irq_level = overflow; |
| 380 | |
| 381 | if (likely(irqchip_in_kernel(vcpu->kvm))) { |
| 382 | int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id, |
Andrew Jones | d9f89b4 | 2017-07-01 18:26:54 +0200 | [diff] [blame] | 383 | pmu->irq_num, overflow, pmu); |
Andrew Jones | b748493 | 2017-06-04 14:44:00 +0200 | [diff] [blame] | 384 | WARN_ON(ret); |
| 385 | } |
| 386 | } |
| 387 | |
Christoffer Dall | 3dbbdf7 | 2017-02-01 12:51:52 +0100 | [diff] [blame] | 388 | bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu) |
| 389 | { |
| 390 | struct kvm_pmu *pmu = &vcpu->arch.pmu; |
| 391 | struct kvm_sync_regs *sregs = &vcpu->run->s.regs; |
| 392 | bool run_level = sregs->device_irq_level & KVM_ARM_DEV_PMU; |
| 393 | |
| 394 | if (likely(irqchip_in_kernel(vcpu->kvm))) |
| 395 | return false; |
| 396 | |
| 397 | return pmu->irq_level != run_level; |
| 398 | } |
| 399 | |
| 400 | /* |
| 401 | * Reflect the PMU overflow interrupt output level into the kvm_run structure |
| 402 | */ |
| 403 | void kvm_pmu_update_run(struct kvm_vcpu *vcpu) |
| 404 | { |
| 405 | struct kvm_sync_regs *regs = &vcpu->run->s.regs; |
| 406 | |
| 407 | /* Populate the timer bitmap for user space */ |
| 408 | regs->device_irq_level &= ~KVM_ARM_DEV_PMU; |
| 409 | if (vcpu->arch.pmu.irq_level) |
| 410 | regs->device_irq_level |= KVM_ARM_DEV_PMU; |
| 411 | } |
| 412 | |
Shannon Zhao | b02386e | 2016-02-26 19:29:19 +0800 | [diff] [blame] | 413 | /** |
| 414 | * kvm_pmu_flush_hwstate - flush pmu state to cpu |
| 415 | * @vcpu: The vcpu pointer |
| 416 | * |
| 417 | * Check if the PMU has overflowed while we were running in the host, and inject |
| 418 | * an interrupt if that was the case. |
| 419 | */ |
| 420 | void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu) |
| 421 | { |
| 422 | kvm_pmu_update_state(vcpu); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * kvm_pmu_sync_hwstate - sync pmu state from cpu |
| 427 | * @vcpu: The vcpu pointer |
| 428 | * |
| 429 | * Check if the PMU has overflowed while we were running in the guest, and |
| 430 | * inject an interrupt if that was the case. |
| 431 | */ |
| 432 | void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu) |
| 433 | { |
| 434 | kvm_pmu_update_state(vcpu); |
| 435 | } |
| 436 | |
Shannon Zhao | b02386e | 2016-02-26 19:29:19 +0800 | [diff] [blame] | 437 | /** |
Andrew Jones | d9f89b4 | 2017-07-01 18:26:54 +0200 | [diff] [blame] | 438 | * When the perf event overflows, set the overflow status and inform the vcpu. |
Shannon Zhao | b02386e | 2016-02-26 19:29:19 +0800 | [diff] [blame] | 439 | */ |
| 440 | static void kvm_pmu_perf_overflow(struct perf_event *perf_event, |
| 441 | struct perf_sample_data *data, |
| 442 | struct pt_regs *regs) |
| 443 | { |
| 444 | struct kvm_pmc *pmc = perf_event->overflow_handler_context; |
| 445 | struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc); |
| 446 | int idx = pmc->idx; |
| 447 | |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 448 | __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(idx); |
Andrew Jones | d9f89b4 | 2017-07-01 18:26:54 +0200 | [diff] [blame] | 449 | |
| 450 | if (kvm_pmu_overflow_status(vcpu)) { |
| 451 | kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); |
| 452 | kvm_vcpu_kick(vcpu); |
| 453 | } |
Shannon Zhao | b02386e | 2016-02-26 19:29:19 +0800 | [diff] [blame] | 454 | } |
| 455 | |
Shannon Zhao | 7a0adc7 | 2015-09-08 15:49:39 +0800 | [diff] [blame] | 456 | /** |
| 457 | * kvm_pmu_software_increment - do software increment |
| 458 | * @vcpu: The vcpu pointer |
| 459 | * @val: the value guest writes to PMSWINC register |
| 460 | */ |
| 461 | void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val) |
| 462 | { |
| 463 | int i; |
| 464 | u64 type, enable, reg; |
| 465 | |
| 466 | if (val == 0) |
| 467 | return; |
| 468 | |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 469 | enable = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0); |
Shannon Zhao | 7a0adc7 | 2015-09-08 15:49:39 +0800 | [diff] [blame] | 470 | for (i = 0; i < ARMV8_PMU_CYCLE_IDX; i++) { |
| 471 | if (!(val & BIT(i))) |
| 472 | continue; |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 473 | type = __vcpu_sys_reg(vcpu, PMEVTYPER0_EL0 + i) |
Shannon Zhao | 7a0adc7 | 2015-09-08 15:49:39 +0800 | [diff] [blame] | 474 | & ARMV8_PMU_EVTYPE_EVENT; |
Wei Huang | b112c84 | 2016-11-16 11:09:20 -0600 | [diff] [blame] | 475 | if ((type == ARMV8_PMUV3_PERFCTR_SW_INCR) |
Shannon Zhao | 7a0adc7 | 2015-09-08 15:49:39 +0800 | [diff] [blame] | 476 | && (enable & BIT(i))) { |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 477 | reg = __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i) + 1; |
Shannon Zhao | 7a0adc7 | 2015-09-08 15:49:39 +0800 | [diff] [blame] | 478 | reg = lower_32_bits(reg); |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 479 | __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i) = reg; |
Shannon Zhao | 7a0adc7 | 2015-09-08 15:49:39 +0800 | [diff] [blame] | 480 | if (!reg) |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 481 | __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(i); |
Shannon Zhao | 7a0adc7 | 2015-09-08 15:49:39 +0800 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
Shannon Zhao | 7699373 | 2015-10-28 12:10:30 +0800 | [diff] [blame] | 486 | /** |
| 487 | * kvm_pmu_handle_pmcr - handle PMCR register |
| 488 | * @vcpu: The vcpu pointer |
| 489 | * @val: the value guest writes to PMCR register |
| 490 | */ |
| 491 | void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) |
| 492 | { |
Shannon Zhao | 7699373 | 2015-10-28 12:10:30 +0800 | [diff] [blame] | 493 | u64 mask; |
| 494 | int i; |
| 495 | |
| 496 | mask = kvm_pmu_valid_counter_mask(vcpu); |
| 497 | if (val & ARMV8_PMU_PMCR_E) { |
Andrew Murray | 418e5ca | 2019-06-17 20:01:01 +0100 | [diff] [blame] | 498 | kvm_pmu_enable_counter_mask(vcpu, |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 499 | __vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & mask); |
Shannon Zhao | 7699373 | 2015-10-28 12:10:30 +0800 | [diff] [blame] | 500 | } else { |
Andrew Murray | 418e5ca | 2019-06-17 20:01:01 +0100 | [diff] [blame] | 501 | kvm_pmu_disable_counter_mask(vcpu, mask); |
Shannon Zhao | 7699373 | 2015-10-28 12:10:30 +0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | if (val & ARMV8_PMU_PMCR_C) |
| 505 | kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0); |
| 506 | |
| 507 | if (val & ARMV8_PMU_PMCR_P) { |
| 508 | for (i = 0; i < ARMV8_PMU_CYCLE_IDX; i++) |
| 509 | kvm_pmu_set_counter_value(vcpu, i, 0); |
| 510 | } |
Shannon Zhao | 7699373 | 2015-10-28 12:10:30 +0800 | [diff] [blame] | 511 | } |
| 512 | |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 513 | static bool kvm_pmu_counter_is_enabled(struct kvm_vcpu *vcpu, u64 select_idx) |
| 514 | { |
Christoffer Dall | 8d404c4 | 2016-03-16 15:38:53 +0100 | [diff] [blame] | 515 | return (__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) && |
| 516 | (__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & BIT(select_idx)); |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /** |
Andrew Murray | 30d9775 | 2019-06-17 20:01:03 +0100 | [diff] [blame] | 520 | * kvm_pmu_create_perf_event - create a perf event for a counter |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 521 | * @vcpu: The vcpu pointer |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 522 | * @select_idx: The number of selected counter |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 523 | */ |
Andrew Murray | 30d9775 | 2019-06-17 20:01:03 +0100 | [diff] [blame] | 524 | static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx) |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 525 | { |
| 526 | struct kvm_pmu *pmu = &vcpu->arch.pmu; |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 527 | struct kvm_pmc *pmc; |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 528 | struct perf_event *event; |
| 529 | struct perf_event_attr attr; |
Andrew Murray | 30d9775 | 2019-06-17 20:01:03 +0100 | [diff] [blame] | 530 | u64 eventsel, counter, reg, data; |
| 531 | |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 532 | /* |
| 533 | * For chained counters the event type and filtering attributes are |
| 534 | * obtained from the low/even counter. We also use this counter to |
| 535 | * determine if the event is enabled/disabled. |
| 536 | */ |
| 537 | pmc = kvm_pmu_get_canonical_pmc(&pmu->pmc[select_idx]); |
| 538 | |
| 539 | reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX) |
| 540 | ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + pmc->idx; |
Andrew Murray | 30d9775 | 2019-06-17 20:01:03 +0100 | [diff] [blame] | 541 | data = __vcpu_sys_reg(vcpu, reg); |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 542 | |
| 543 | kvm_pmu_stop_counter(vcpu, pmc); |
| 544 | eventsel = data & ARMV8_PMU_EVTYPE_EVENT; |
| 545 | |
Shannon Zhao | 7a0adc7 | 2015-09-08 15:49:39 +0800 | [diff] [blame] | 546 | /* Software increment event does't need to be backed by a perf event */ |
Wei Huang | b112c84 | 2016-11-16 11:09:20 -0600 | [diff] [blame] | 547 | if (eventsel == ARMV8_PMUV3_PERFCTR_SW_INCR && |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 548 | pmc->idx != ARMV8_PMU_CYCLE_IDX) |
Shannon Zhao | 7a0adc7 | 2015-09-08 15:49:39 +0800 | [diff] [blame] | 549 | return; |
| 550 | |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 551 | memset(&attr, 0, sizeof(struct perf_event_attr)); |
| 552 | attr.type = PERF_TYPE_RAW; |
| 553 | attr.size = sizeof(attr); |
| 554 | attr.pinned = 1; |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 555 | attr.disabled = !kvm_pmu_counter_is_enabled(vcpu, pmc->idx); |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 556 | attr.exclude_user = data & ARMV8_PMU_EXCLUDE_EL0 ? 1 : 0; |
| 557 | attr.exclude_kernel = data & ARMV8_PMU_EXCLUDE_EL1 ? 1 : 0; |
| 558 | attr.exclude_hv = 1; /* Don't count EL2 events */ |
| 559 | attr.exclude_host = 1; /* Don't count host events */ |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 560 | attr.config = (pmc->idx == ARMV8_PMU_CYCLE_IDX) ? |
Wei Huang | b112c84 | 2016-11-16 11:09:20 -0600 | [diff] [blame] | 561 | ARMV8_PMUV3_PERFCTR_CPU_CYCLES : eventsel; |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 562 | |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 563 | counter = kvm_pmu_get_pair_counter_value(vcpu, pmc); |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 564 | |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 565 | if (kvm_pmu_idx_has_chain_evtype(vcpu, pmc->idx)) { |
| 566 | /** |
| 567 | * The initial sample period (overflow count) of an event. For |
| 568 | * chained counters we only support overflow interrupts on the |
| 569 | * high counter. |
| 570 | */ |
| 571 | attr.sample_period = (-counter) & GENMASK(63, 0); |
Marc Zyngier | 725ce66 | 2019-10-08 15:09:55 +0100 | [diff] [blame^] | 572 | if (kvm_pmu_counter_is_enabled(vcpu, pmc->idx + 1)) |
| 573 | attr.config1 |= PERF_ATTR_CFG1_KVM_PMU_CHAINED; |
| 574 | |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 575 | event = perf_event_create_kernel_counter(&attr, -1, current, |
| 576 | kvm_pmu_perf_overflow, |
| 577 | pmc + 1); |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 578 | } else { |
| 579 | /* The initial sample period (overflow count) of an event. */ |
| 580 | if (kvm_pmu_idx_is_64bit(vcpu, pmc->idx)) |
| 581 | attr.sample_period = (-counter) & GENMASK(63, 0); |
| 582 | else |
| 583 | attr.sample_period = (-counter) & GENMASK(31, 0); |
| 584 | |
| 585 | event = perf_event_create_kernel_counter(&attr, -1, current, |
Shannon Zhao | b02386e | 2016-02-26 19:29:19 +0800 | [diff] [blame] | 586 | kvm_pmu_perf_overflow, pmc); |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Shannon Zhao | 7f76635 | 2015-07-03 14:27:25 +0800 | [diff] [blame] | 589 | if (IS_ERR(event)) { |
| 590 | pr_err_once("kvm: pmu event creation failed %ld\n", |
| 591 | PTR_ERR(event)); |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | pmc->perf_event = event; |
| 596 | } |
Shannon Zhao | 808e738 | 2016-01-11 22:46:15 +0800 | [diff] [blame] | 597 | |
Andrew Murray | 30d9775 | 2019-06-17 20:01:03 +0100 | [diff] [blame] | 598 | /** |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 599 | * kvm_pmu_update_pmc_chained - update chained bitmap |
| 600 | * @vcpu: The vcpu pointer |
| 601 | * @select_idx: The number of selected counter |
| 602 | * |
| 603 | * Update the chained bitmap based on the event type written in the |
| 604 | * typer register. |
| 605 | */ |
| 606 | static void kvm_pmu_update_pmc_chained(struct kvm_vcpu *vcpu, u64 select_idx) |
| 607 | { |
| 608 | struct kvm_pmu *pmu = &vcpu->arch.pmu; |
| 609 | struct kvm_pmc *pmc = &pmu->pmc[select_idx]; |
| 610 | |
| 611 | if (kvm_pmu_idx_has_chain_evtype(vcpu, pmc->idx)) { |
| 612 | /* |
| 613 | * During promotion from !chained to chained we must ensure |
| 614 | * the adjacent counter is stopped and its event destroyed |
| 615 | */ |
| 616 | if (!kvm_pmu_pmc_is_chained(pmc)) |
| 617 | kvm_pmu_stop_counter(vcpu, pmc); |
| 618 | |
| 619 | set_bit(pmc->idx >> 1, vcpu->arch.pmu.chained); |
| 620 | } else { |
| 621 | clear_bit(pmc->idx >> 1, vcpu->arch.pmu.chained); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /** |
Andrew Murray | 30d9775 | 2019-06-17 20:01:03 +0100 | [diff] [blame] | 626 | * kvm_pmu_set_counter_event_type - set selected counter to monitor some event |
| 627 | * @vcpu: The vcpu pointer |
| 628 | * @data: The data guest writes to PMXEVTYPER_EL0 |
| 629 | * @select_idx: The number of selected counter |
| 630 | * |
| 631 | * When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an |
| 632 | * event with given hardware event number. Here we call perf_event API to |
| 633 | * emulate this action and create a kernel perf event for it. |
| 634 | */ |
| 635 | void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data, |
| 636 | u64 select_idx) |
| 637 | { |
| 638 | u64 reg, event_type = data & ARMV8_PMU_EVTYPE_MASK; |
| 639 | |
| 640 | reg = (select_idx == ARMV8_PMU_CYCLE_IDX) |
| 641 | ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + select_idx; |
| 642 | |
| 643 | __vcpu_sys_reg(vcpu, reg) = event_type; |
Andrew Murray | 80f393a | 2019-06-17 20:01:05 +0100 | [diff] [blame] | 644 | |
| 645 | kvm_pmu_update_pmc_chained(vcpu, select_idx); |
Andrew Murray | 30d9775 | 2019-06-17 20:01:03 +0100 | [diff] [blame] | 646 | kvm_pmu_create_perf_event(vcpu, select_idx); |
| 647 | } |
| 648 | |
Shannon Zhao | 808e738 | 2016-01-11 22:46:15 +0800 | [diff] [blame] | 649 | bool kvm_arm_support_pmu_v3(void) |
| 650 | { |
| 651 | /* |
| 652 | * Check if HW_PERF_EVENTS are supported by checking the number of |
| 653 | * hardware performance counters. This could ensure the presence of |
| 654 | * a physical PMU and CONFIG_PERF_EVENT is selected. |
| 655 | */ |
| 656 | return (perf_num_counters() > 0); |
| 657 | } |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 658 | |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 659 | int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu) |
| 660 | { |
| 661 | if (!vcpu->arch.pmu.created) |
| 662 | return 0; |
| 663 | |
| 664 | /* |
| 665 | * A valid interrupt configuration for the PMU is either to have a |
| 666 | * properly configured interrupt number and using an in-kernel |
Christoffer Dall | ebb127f | 2017-05-16 19:53:50 +0200 | [diff] [blame] | 667 | * irqchip, or to not have an in-kernel GIC and not set an IRQ. |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 668 | */ |
Christoffer Dall | ebb127f | 2017-05-16 19:53:50 +0200 | [diff] [blame] | 669 | if (irqchip_in_kernel(vcpu->kvm)) { |
| 670 | int irq = vcpu->arch.pmu.irq_num; |
| 671 | if (!kvm_arm_pmu_irq_initialized(vcpu)) |
| 672 | return -EINVAL; |
| 673 | |
| 674 | /* |
| 675 | * If we are using an in-kernel vgic, at this point we know |
| 676 | * the vgic will be initialized, so we can check the PMU irq |
| 677 | * number against the dimensions of the vgic and make sure |
| 678 | * it's valid. |
| 679 | */ |
| 680 | if (!irq_is_ppi(irq) && !vgic_valid_spi(vcpu->kvm, irq)) |
| 681 | return -EINVAL; |
| 682 | } else if (kvm_arm_pmu_irq_initialized(vcpu)) { |
| 683 | return -EINVAL; |
| 684 | } |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 685 | |
| 686 | kvm_pmu_vcpu_reset(vcpu); |
| 687 | vcpu->arch.pmu.ready = true; |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 692 | static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu) |
| 693 | { |
| 694 | if (!kvm_arm_support_pmu_v3()) |
| 695 | return -ENODEV; |
| 696 | |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 697 | if (!test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features)) |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 698 | return -ENXIO; |
| 699 | |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 700 | if (vcpu->arch.pmu.created) |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 701 | return -EBUSY; |
| 702 | |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 703 | if (irqchip_in_kernel(vcpu->kvm)) { |
Christoffer Dall | abcb851 | 2017-05-04 13:32:53 +0200 | [diff] [blame] | 704 | int ret; |
| 705 | |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 706 | /* |
| 707 | * If using the PMU with an in-kernel virtual GIC |
| 708 | * implementation, we require the GIC to be already |
| 709 | * initialized when initializing the PMU. |
| 710 | */ |
| 711 | if (!vgic_initialized(vcpu->kvm)) |
| 712 | return -ENODEV; |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 713 | |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 714 | if (!kvm_arm_pmu_irq_initialized(vcpu)) |
| 715 | return -ENXIO; |
Christoffer Dall | abcb851 | 2017-05-04 13:32:53 +0200 | [diff] [blame] | 716 | |
| 717 | ret = kvm_vgic_set_owner(vcpu, vcpu->arch.pmu.irq_num, |
| 718 | &vcpu->arch.pmu); |
| 719 | if (ret) |
| 720 | return ret; |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | vcpu->arch.pmu.created = true; |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 724 | return 0; |
| 725 | } |
| 726 | |
Andre Przywara | 2defaff | 2016-03-07 17:32:29 +0700 | [diff] [blame] | 727 | /* |
| 728 | * For one VM the interrupt type must be same for each vcpu. |
| 729 | * As a PPI, the interrupt number is the same for all vcpus, |
| 730 | * while as an SPI it must be a separate number per vcpu. |
| 731 | */ |
| 732 | static bool pmu_irq_is_valid(struct kvm *kvm, int irq) |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 733 | { |
| 734 | int i; |
| 735 | struct kvm_vcpu *vcpu; |
| 736 | |
| 737 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 738 | if (!kvm_arm_pmu_irq_initialized(vcpu)) |
| 739 | continue; |
| 740 | |
Andre Przywara | 2defaff | 2016-03-07 17:32:29 +0700 | [diff] [blame] | 741 | if (irq_is_ppi(irq)) { |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 742 | if (vcpu->arch.pmu.irq_num != irq) |
| 743 | return false; |
| 744 | } else { |
| 745 | if (vcpu->arch.pmu.irq_num == irq) |
| 746 | return false; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | return true; |
| 751 | } |
| 752 | |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 753 | int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) |
| 754 | { |
| 755 | switch (attr->attr) { |
| 756 | case KVM_ARM_VCPU_PMU_V3_IRQ: { |
| 757 | int __user *uaddr = (int __user *)(long)attr->addr; |
| 758 | int irq; |
| 759 | |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 760 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 761 | return -EINVAL; |
| 762 | |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 763 | if (!test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features)) |
| 764 | return -ENODEV; |
| 765 | |
| 766 | if (get_user(irq, uaddr)) |
| 767 | return -EFAULT; |
| 768 | |
Andre Przywara | 2defaff | 2016-03-07 17:32:29 +0700 | [diff] [blame] | 769 | /* The PMU overflow interrupt can be a PPI or a valid SPI. */ |
Christoffer Dall | ebb127f | 2017-05-16 19:53:50 +0200 | [diff] [blame] | 770 | if (!(irq_is_ppi(irq) || irq_is_spi(irq))) |
Andre Przywara | 2defaff | 2016-03-07 17:32:29 +0700 | [diff] [blame] | 771 | return -EINVAL; |
| 772 | |
| 773 | if (!pmu_irq_is_valid(vcpu->kvm, irq)) |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 774 | return -EINVAL; |
| 775 | |
| 776 | if (kvm_arm_pmu_irq_initialized(vcpu)) |
| 777 | return -EBUSY; |
| 778 | |
| 779 | kvm_debug("Set kvm ARM PMU irq: %d\n", irq); |
| 780 | vcpu->arch.pmu.irq_num = irq; |
| 781 | return 0; |
| 782 | } |
| 783 | case KVM_ARM_VCPU_PMU_V3_INIT: |
| 784 | return kvm_arm_pmu_v3_init(vcpu); |
| 785 | } |
| 786 | |
| 787 | return -ENXIO; |
| 788 | } |
| 789 | |
| 790 | int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) |
| 791 | { |
| 792 | switch (attr->attr) { |
| 793 | case KVM_ARM_VCPU_PMU_V3_IRQ: { |
| 794 | int __user *uaddr = (int __user *)(long)attr->addr; |
| 795 | int irq; |
| 796 | |
Christoffer Dall | a2befac | 2017-05-02 13:41:02 +0200 | [diff] [blame] | 797 | if (!irqchip_in_kernel(vcpu->kvm)) |
| 798 | return -EINVAL; |
| 799 | |
Shannon Zhao | bb0c70b | 2016-01-11 21:35:32 +0800 | [diff] [blame] | 800 | if (!test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features)) |
| 801 | return -ENODEV; |
| 802 | |
| 803 | if (!kvm_arm_pmu_irq_initialized(vcpu)) |
| 804 | return -ENXIO; |
| 805 | |
| 806 | irq = vcpu->arch.pmu.irq_num; |
| 807 | return put_user(irq, uaddr); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | return -ENXIO; |
| 812 | } |
| 813 | |
| 814 | int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) |
| 815 | { |
| 816 | switch (attr->attr) { |
| 817 | case KVM_ARM_VCPU_PMU_V3_IRQ: |
| 818 | case KVM_ARM_VCPU_PMU_V3_INIT: |
| 819 | if (kvm_arm_support_pmu_v3() && |
| 820 | test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features)) |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | return -ENXIO; |
| 825 | } |