blob: 10d3860a9c0a78a0fc3507f5f3a8948f8ed059a2 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Shannon Zhao051ff582015-12-08 15:29:06 +08002/*
3 * Copyright (C) 2015 Linaro Ltd.
4 * Author: Shannon Zhao <shannon.zhao@linaro.org>
Shannon Zhao051ff582015-12-08 15:29:06 +08005 */
6
7#include <linux/cpu.h>
8#include <linux/kvm.h>
9#include <linux/kvm_host.h>
10#include <linux/perf_event.h>
Marc Zyngier8c3252c2019-10-06 10:28:50 +010011#include <linux/perf/arm_pmu.h>
Shannon Zhaobb0c70b2016-01-11 21:35:32 +080012#include <linux/uaccess.h>
Shannon Zhao051ff582015-12-08 15:29:06 +080013#include <asm/kvm_emulate.h>
14#include <kvm/arm_pmu.h>
Shannon Zhaob02386e2016-02-26 19:29:19 +080015#include <kvm/arm_vgic.h>
Shannon Zhao051ff582015-12-08 15:29:06 +080016
Andrew Murray30d97752019-06-17 20:01:03 +010017static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx);
Eric Auger76c9fc52020-01-24 15:25:33 +010018static void kvm_pmu_update_pmc_chained(struct kvm_vcpu *vcpu, u64 select_idx);
19static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc);
Andrew Murray218907c2019-06-17 20:01:04 +010020
Andrew Murray80f393a2019-06-17 20:01:05 +010021#define PERF_ATTR_CFG1_KVM_PMU_CHAINED 0x1
22
Marc Zyngierfd65a3b2020-03-17 11:11:56 +000023static u32 kvm_pmu_event_mask(struct kvm *kvm)
24{
25 switch (kvm->arch.pmuver) {
26 case 1: /* ARMv8.0 */
27 return GENMASK(9, 0);
28 case 4: /* ARMv8.1 */
29 case 5: /* ARMv8.4 */
30 case 6: /* ARMv8.5 */
31 return GENMASK(15, 0);
32 default: /* Shouldn't be here, just for sanity */
33 WARN_ONCE(1, "Unknown PMU version %d\n", kvm->arch.pmuver);
34 return 0;
35 }
36}
37
Andrew Murray218907c2019-06-17 20:01:04 +010038/**
39 * kvm_pmu_idx_is_64bit - determine if select_idx is a 64bit counter
40 * @vcpu: The vcpu pointer
41 * @select_idx: The counter index
42 */
43static bool kvm_pmu_idx_is_64bit(struct kvm_vcpu *vcpu, u64 select_idx)
44{
45 return (select_idx == ARMV8_PMU_CYCLE_IDX &&
46 __vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_LC);
47}
48
Andrew Murray80f393a2019-06-17 20:01:05 +010049static struct kvm_vcpu *kvm_pmc_to_vcpu(struct kvm_pmc *pmc)
50{
51 struct kvm_pmu *pmu;
52 struct kvm_vcpu_arch *vcpu_arch;
53
54 pmc -= pmc->idx;
55 pmu = container_of(pmc, struct kvm_pmu, pmc[0]);
56 vcpu_arch = container_of(pmu, struct kvm_vcpu_arch, pmu);
57 return container_of(vcpu_arch, struct kvm_vcpu, arch);
58}
59
60/**
61 * kvm_pmu_pmc_is_chained - determine if the pmc is chained
62 * @pmc: The PMU counter pointer
63 */
64static bool kvm_pmu_pmc_is_chained(struct kvm_pmc *pmc)
65{
66 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
67
68 return test_bit(pmc->idx >> 1, vcpu->arch.pmu.chained);
69}
70
71/**
72 * kvm_pmu_idx_is_high_counter - determine if select_idx is a high/low counter
73 * @select_idx: The counter index
74 */
75static bool kvm_pmu_idx_is_high_counter(u64 select_idx)
76{
77 return select_idx & 0x1;
78}
79
80/**
81 * kvm_pmu_get_canonical_pmc - obtain the canonical pmc
82 * @pmc: The PMU counter pointer
83 *
84 * When a pair of PMCs are chained together we use the low counter (canonical)
85 * to hold the underlying perf event.
86 */
87static struct kvm_pmc *kvm_pmu_get_canonical_pmc(struct kvm_pmc *pmc)
88{
89 if (kvm_pmu_pmc_is_chained(pmc) &&
90 kvm_pmu_idx_is_high_counter(pmc->idx))
91 return pmc - 1;
92
93 return pmc;
94}
Eric Auger76c9fc52020-01-24 15:25:33 +010095static struct kvm_pmc *kvm_pmu_get_alternate_pmc(struct kvm_pmc *pmc)
96{
97 if (kvm_pmu_idx_is_high_counter(pmc->idx))
98 return pmc - 1;
99 else
100 return pmc + 1;
101}
Andrew Murray80f393a2019-06-17 20:01:05 +0100102
103/**
104 * kvm_pmu_idx_has_chain_evtype - determine if the event type is chain
105 * @vcpu: The vcpu pointer
106 * @select_idx: The counter index
107 */
108static bool kvm_pmu_idx_has_chain_evtype(struct kvm_vcpu *vcpu, u64 select_idx)
109{
110 u64 eventsel, reg;
111
112 select_idx |= 0x1;
113
114 if (select_idx == ARMV8_PMU_CYCLE_IDX)
115 return false;
116
117 reg = PMEVTYPER0_EL0 + select_idx;
Marc Zyngierfd65a3b2020-03-17 11:11:56 +0000118 eventsel = __vcpu_sys_reg(vcpu, reg) & kvm_pmu_event_mask(vcpu->kvm);
Andrew Murray80f393a2019-06-17 20:01:05 +0100119
120 return eventsel == ARMV8_PMUV3_PERFCTR_CHAIN;
121}
122
123/**
124 * kvm_pmu_get_pair_counter_value - get PMU counter value
125 * @vcpu: The vcpu pointer
126 * @pmc: The PMU counter pointer
127 */
128static u64 kvm_pmu_get_pair_counter_value(struct kvm_vcpu *vcpu,
129 struct kvm_pmc *pmc)
130{
131 u64 counter, counter_high, reg, enabled, running;
132
133 if (kvm_pmu_pmc_is_chained(pmc)) {
134 pmc = kvm_pmu_get_canonical_pmc(pmc);
135 reg = PMEVCNTR0_EL0 + pmc->idx;
136
137 counter = __vcpu_sys_reg(vcpu, reg);
138 counter_high = __vcpu_sys_reg(vcpu, reg + 1);
139
140 counter = lower_32_bits(counter) | (counter_high << 32);
141 } else {
142 reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
143 ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + pmc->idx;
144 counter = __vcpu_sys_reg(vcpu, reg);
145 }
146
147 /*
148 * The real counter value is equal to the value of counter register plus
149 * the value perf event counts.
150 */
151 if (pmc->perf_event)
152 counter += perf_event_read_value(pmc->perf_event, &enabled,
153 &running);
154
155 return counter;
156}
157
Shannon Zhao051ff582015-12-08 15:29:06 +0800158/**
159 * kvm_pmu_get_counter_value - get PMU counter value
160 * @vcpu: The vcpu pointer
161 * @select_idx: The counter index
162 */
163u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
164{
Andrew Murray80f393a2019-06-17 20:01:05 +0100165 u64 counter;
Shannon Zhao051ff582015-12-08 15:29:06 +0800166 struct kvm_pmu *pmu = &vcpu->arch.pmu;
167 struct kvm_pmc *pmc = &pmu->pmc[select_idx];
168
Andrew Murray80f393a2019-06-17 20:01:05 +0100169 counter = kvm_pmu_get_pair_counter_value(vcpu, pmc);
Shannon Zhao051ff582015-12-08 15:29:06 +0800170
Andrew Murray80f393a2019-06-17 20:01:05 +0100171 if (kvm_pmu_pmc_is_chained(pmc) &&
172 kvm_pmu_idx_is_high_counter(select_idx))
173 counter = upper_32_bits(counter);
Marc Zyngierf4e23cf2019-10-03 18:02:08 +0100174 else if (select_idx != ARMV8_PMU_CYCLE_IDX)
Andrew Murray218907c2019-06-17 20:01:04 +0100175 counter = lower_32_bits(counter);
176
177 return counter;
Shannon Zhao051ff582015-12-08 15:29:06 +0800178}
179
180/**
181 * kvm_pmu_set_counter_value - set PMU counter value
182 * @vcpu: The vcpu pointer
183 * @select_idx: The counter index
184 * @val: The counter value
185 */
186void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
187{
188 u64 reg;
189
190 reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
191 ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx;
Christoffer Dall8d404c42016-03-16 15:38:53 +0100192 __vcpu_sys_reg(vcpu, reg) += (s64)val - kvm_pmu_get_counter_value(vcpu, select_idx);
Andrew Murray30d97752019-06-17 20:01:03 +0100193
194 /* Recreate the perf event to reflect the updated sample_period */
195 kvm_pmu_create_perf_event(vcpu, select_idx);
Shannon Zhao051ff582015-12-08 15:29:06 +0800196}
Shannon Zhao96b0eeb2015-09-08 12:26:13 +0800197
Shannon Zhao7f766352015-07-03 14:27:25 +0800198/**
Andrew Murray6f4d2a02019-06-17 20:01:02 +0100199 * kvm_pmu_release_perf_event - remove the perf event
200 * @pmc: The PMU counter pointer
201 */
202static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc)
203{
Andrew Murray80f393a2019-06-17 20:01:05 +0100204 pmc = kvm_pmu_get_canonical_pmc(pmc);
Andrew Murray6f4d2a02019-06-17 20:01:02 +0100205 if (pmc->perf_event) {
206 perf_event_disable(pmc->perf_event);
207 perf_event_release_kernel(pmc->perf_event);
208 pmc->perf_event = NULL;
209 }
210}
211
212/**
Shannon Zhao7f766352015-07-03 14:27:25 +0800213 * kvm_pmu_stop_counter - stop PMU counter
214 * @pmc: The PMU counter pointer
215 *
216 * If this counter has been configured to monitor some event, release it here.
217 */
218static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc)
219{
Marc Zyngierf4e23cf2019-10-03 18:02:08 +0100220 u64 counter, reg, val;
Shannon Zhao7f766352015-07-03 14:27:25 +0800221
Andrew Murray80f393a2019-06-17 20:01:05 +0100222 pmc = kvm_pmu_get_canonical_pmc(pmc);
223 if (!pmc->perf_event)
224 return;
225
226 counter = kvm_pmu_get_pair_counter_value(vcpu, pmc);
227
Marc Zyngierf4e23cf2019-10-03 18:02:08 +0100228 if (pmc->idx == ARMV8_PMU_CYCLE_IDX) {
229 reg = PMCCNTR_EL0;
230 val = counter;
Andrew Murray80f393a2019-06-17 20:01:05 +0100231 } else {
Marc Zyngierf4e23cf2019-10-03 18:02:08 +0100232 reg = PMEVCNTR0_EL0 + pmc->idx;
233 val = lower_32_bits(counter);
Shannon Zhao7f766352015-07-03 14:27:25 +0800234 }
Andrew Murray80f393a2019-06-17 20:01:05 +0100235
Marc Zyngierf4e23cf2019-10-03 18:02:08 +0100236 __vcpu_sys_reg(vcpu, reg) = val;
237
238 if (kvm_pmu_pmc_is_chained(pmc))
239 __vcpu_sys_reg(vcpu, reg + 1) = upper_32_bits(counter);
240
Andrew Murray80f393a2019-06-17 20:01:05 +0100241 kvm_pmu_release_perf_event(pmc);
Shannon Zhao7f766352015-07-03 14:27:25 +0800242}
243
Shannon Zhao2aa36e92015-09-11 11:30:22 +0800244/**
Zenghui Yubca031e2019-07-18 08:15:10 +0000245 * kvm_pmu_vcpu_init - assign pmu counter idx for cpu
246 * @vcpu: The vcpu pointer
247 *
248 */
249void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu)
250{
251 int i;
252 struct kvm_pmu *pmu = &vcpu->arch.pmu;
253
254 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++)
255 pmu->pmc[i].idx = i;
256}
257
258/**
Shannon Zhao2aa36e92015-09-11 11:30:22 +0800259 * kvm_pmu_vcpu_reset - reset pmu state for cpu
260 * @vcpu: The vcpu pointer
261 *
262 */
263void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu)
264{
Eric Augerc01d6a12020-01-24 15:25:35 +0100265 unsigned long mask = kvm_pmu_valid_counter_mask(vcpu);
Shannon Zhao2aa36e92015-09-11 11:30:22 +0800266 struct kvm_pmu *pmu = &vcpu->arch.pmu;
Eric Augerc01d6a12020-01-24 15:25:35 +0100267 int i;
Shannon Zhao2aa36e92015-09-11 11:30:22 +0800268
Eric Augerc01d6a12020-01-24 15:25:35 +0100269 for_each_set_bit(i, &mask, 32)
Shannon Zhao2aa36e92015-09-11 11:30:22 +0800270 kvm_pmu_stop_counter(vcpu, &pmu->pmc[i]);
Andrew Murray80f393a2019-06-17 20:01:05 +0100271
272 bitmap_zero(vcpu->arch.pmu.chained, ARMV8_PMU_MAX_COUNTER_PAIRS);
Shannon Zhao2aa36e92015-09-11 11:30:22 +0800273}
274
Shannon Zhao5f0a7142015-09-11 15:18:05 +0800275/**
276 * kvm_pmu_vcpu_destroy - free perf event of PMU for cpu
277 * @vcpu: The vcpu pointer
278 *
279 */
280void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
281{
282 int i;
283 struct kvm_pmu *pmu = &vcpu->arch.pmu;
284
Andrew Murray6f4d2a02019-06-17 20:01:02 +0100285 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++)
286 kvm_pmu_release_perf_event(&pmu->pmc[i]);
Shannon Zhao5f0a7142015-09-11 15:18:05 +0800287}
288
Shannon Zhao96b0eeb2015-09-08 12:26:13 +0800289u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
290{
Christoffer Dall8d404c42016-03-16 15:38:53 +0100291 u64 val = __vcpu_sys_reg(vcpu, PMCR_EL0) >> ARMV8_PMU_PMCR_N_SHIFT;
Shannon Zhao96b0eeb2015-09-08 12:26:13 +0800292
293 val &= ARMV8_PMU_PMCR_N_MASK;
294 if (val == 0)
295 return BIT(ARMV8_PMU_CYCLE_IDX);
296 else
297 return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX);
298}
299
300/**
Andrew Murray418e5ca2019-06-17 20:01:01 +0100301 * kvm_pmu_enable_counter_mask - enable selected PMU counters
Shannon Zhao96b0eeb2015-09-08 12:26:13 +0800302 * @vcpu: The vcpu pointer
303 * @val: the value guest writes to PMCNTENSET register
304 *
305 * Call perf_event_enable to start counting the perf event
306 */
Andrew Murray418e5ca2019-06-17 20:01:01 +0100307void kvm_pmu_enable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
Shannon Zhao96b0eeb2015-09-08 12:26:13 +0800308{
309 int i;
310 struct kvm_pmu *pmu = &vcpu->arch.pmu;
311 struct kvm_pmc *pmc;
312
Christoffer Dall8d404c42016-03-16 15:38:53 +0100313 if (!(__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) || !val)
Shannon Zhao96b0eeb2015-09-08 12:26:13 +0800314 return;
315
316 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
317 if (!(val & BIT(i)))
318 continue;
319
320 pmc = &pmu->pmc[i];
Andrew Murray80f393a2019-06-17 20:01:05 +0100321
Eric Auger76c9fc52020-01-24 15:25:33 +0100322 /* A change in the enable state may affect the chain state */
323 kvm_pmu_update_pmc_chained(vcpu, i);
324 kvm_pmu_create_perf_event(vcpu, i);
Andrew Murray80f393a2019-06-17 20:01:05 +0100325
326 /* At this point, pmc must be the canonical */
Shannon Zhao96b0eeb2015-09-08 12:26:13 +0800327 if (pmc->perf_event) {
328 perf_event_enable(pmc->perf_event);
329 if (pmc->perf_event->state != PERF_EVENT_STATE_ACTIVE)
330 kvm_debug("fail to enable perf event\n");
331 }
332 }
333}
334
335/**
Andrew Murray418e5ca2019-06-17 20:01:01 +0100336 * kvm_pmu_disable_counter_mask - disable selected PMU counters
Shannon Zhao96b0eeb2015-09-08 12:26:13 +0800337 * @vcpu: The vcpu pointer
338 * @val: the value guest writes to PMCNTENCLR register
339 *
340 * Call perf_event_disable to stop counting the perf event
341 */
Andrew Murray418e5ca2019-06-17 20:01:01 +0100342void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
Shannon Zhao96b0eeb2015-09-08 12:26:13 +0800343{
344 int i;
345 struct kvm_pmu *pmu = &vcpu->arch.pmu;
346 struct kvm_pmc *pmc;
347
348 if (!val)
349 return;
350
351 for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
352 if (!(val & BIT(i)))
353 continue;
354
355 pmc = &pmu->pmc[i];
Andrew Murray80f393a2019-06-17 20:01:05 +0100356
Eric Auger76c9fc52020-01-24 15:25:33 +0100357 /* A change in the enable state may affect the chain state */
358 kvm_pmu_update_pmc_chained(vcpu, i);
359 kvm_pmu_create_perf_event(vcpu, i);
Andrew Murray80f393a2019-06-17 20:01:05 +0100360
361 /* At this point, pmc must be the canonical */
Shannon Zhao96b0eeb2015-09-08 12:26:13 +0800362 if (pmc->perf_event)
363 perf_event_disable(pmc->perf_event);
364 }
365}
Shannon Zhao7f766352015-07-03 14:27:25 +0800366
Shannon Zhao76d883c2015-09-08 15:03:26 +0800367static u64 kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
368{
369 u64 reg = 0;
370
Christoffer Dall8d404c42016-03-16 15:38:53 +0100371 if ((__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E)) {
372 reg = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
373 reg &= __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
374 reg &= __vcpu_sys_reg(vcpu, PMINTENSET_EL1);
Shannon Zhao76d883c2015-09-08 15:03:26 +0800375 reg &= kvm_pmu_valid_counter_mask(vcpu);
Will Deacon7d4bd1d2016-04-01 12:12:22 +0100376 }
Shannon Zhao76d883c2015-09-08 15:03:26 +0800377
378 return reg;
379}
380
Andrew Jonesd9f89b42017-07-01 18:26:54 +0200381static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
Andrew Jonesb7484932017-06-04 14:44:00 +0200382{
383 struct kvm_pmu *pmu = &vcpu->arch.pmu;
Andrew Jonesd9f89b42017-07-01 18:26:54 +0200384 bool overflow;
Andrew Jonesb7484932017-06-04 14:44:00 +0200385
Andrew Jonesd9f89b42017-07-01 18:26:54 +0200386 if (!kvm_arm_pmu_v3_ready(vcpu))
387 return;
388
389 overflow = !!kvm_pmu_overflow_status(vcpu);
Andrew Jonesb7484932017-06-04 14:44:00 +0200390 if (pmu->irq_level == overflow)
391 return;
392
393 pmu->irq_level = overflow;
394
395 if (likely(irqchip_in_kernel(vcpu->kvm))) {
396 int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu->vcpu_id,
Andrew Jonesd9f89b42017-07-01 18:26:54 +0200397 pmu->irq_num, overflow, pmu);
Andrew Jonesb7484932017-06-04 14:44:00 +0200398 WARN_ON(ret);
399 }
400}
401
Christoffer Dall3dbbdf72017-02-01 12:51:52 +0100402bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
403{
404 struct kvm_pmu *pmu = &vcpu->arch.pmu;
405 struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
406 bool run_level = sregs->device_irq_level & KVM_ARM_DEV_PMU;
407
408 if (likely(irqchip_in_kernel(vcpu->kvm)))
409 return false;
410
411 return pmu->irq_level != run_level;
412}
413
414/*
415 * Reflect the PMU overflow interrupt output level into the kvm_run structure
416 */
417void kvm_pmu_update_run(struct kvm_vcpu *vcpu)
418{
419 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
420
421 /* Populate the timer bitmap for user space */
422 regs->device_irq_level &= ~KVM_ARM_DEV_PMU;
423 if (vcpu->arch.pmu.irq_level)
424 regs->device_irq_level |= KVM_ARM_DEV_PMU;
425}
426
Shannon Zhaob02386e2016-02-26 19:29:19 +0800427/**
428 * kvm_pmu_flush_hwstate - flush pmu state to cpu
429 * @vcpu: The vcpu pointer
430 *
431 * Check if the PMU has overflowed while we were running in the host, and inject
432 * an interrupt if that was the case.
433 */
434void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu)
435{
436 kvm_pmu_update_state(vcpu);
437}
438
439/**
440 * kvm_pmu_sync_hwstate - sync pmu state from cpu
441 * @vcpu: The vcpu pointer
442 *
443 * Check if the PMU has overflowed while we were running in the guest, and
444 * inject an interrupt if that was the case.
445 */
446void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu)
447{
448 kvm_pmu_update_state(vcpu);
449}
450
Shannon Zhaob02386e2016-02-26 19:29:19 +0800451/**
Andrew Jonesd9f89b42017-07-01 18:26:54 +0200452 * When the perf event overflows, set the overflow status and inform the vcpu.
Shannon Zhaob02386e2016-02-26 19:29:19 +0800453 */
454static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
455 struct perf_sample_data *data,
456 struct pt_regs *regs)
457{
458 struct kvm_pmc *pmc = perf_event->overflow_handler_context;
Marc Zyngier8c3252c2019-10-06 10:28:50 +0100459 struct arm_pmu *cpu_pmu = to_arm_pmu(perf_event->pmu);
Shannon Zhaob02386e2016-02-26 19:29:19 +0800460 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
461 int idx = pmc->idx;
Marc Zyngier8c3252c2019-10-06 10:28:50 +0100462 u64 period;
463
464 cpu_pmu->pmu.stop(perf_event, PERF_EF_UPDATE);
465
466 /*
467 * Reset the sample period to the architectural limit,
468 * i.e. the point where the counter overflows.
469 */
470 period = -(local64_read(&perf_event->count));
471
472 if (!kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
473 period &= GENMASK(31, 0);
474
475 local64_set(&perf_event->hw.period_left, 0);
476 perf_event->attr.sample_period = period;
477 perf_event->hw.sample_period = period;
Shannon Zhaob02386e2016-02-26 19:29:19 +0800478
Christoffer Dall8d404c42016-03-16 15:38:53 +0100479 __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(idx);
Andrew Jonesd9f89b42017-07-01 18:26:54 +0200480
481 if (kvm_pmu_overflow_status(vcpu)) {
482 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
483 kvm_vcpu_kick(vcpu);
484 }
Marc Zyngier8c3252c2019-10-06 10:28:50 +0100485
486 cpu_pmu->pmu.start(perf_event, PERF_EF_RELOAD);
Shannon Zhaob02386e2016-02-26 19:29:19 +0800487}
488
Shannon Zhao7a0adc72015-09-08 15:49:39 +0800489/**
490 * kvm_pmu_software_increment - do software increment
491 * @vcpu: The vcpu pointer
492 * @val: the value guest writes to PMSWINC register
493 */
494void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val)
495{
Eric Augeraa768292020-01-24 15:25:34 +0100496 struct kvm_pmu *pmu = &vcpu->arch.pmu;
Shannon Zhao7a0adc72015-09-08 15:49:39 +0800497 int i;
Shannon Zhao7a0adc72015-09-08 15:49:39 +0800498
Eric Auger38374072020-01-24 15:25:32 +0100499 if (!(__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E))
500 return;
501
Eric Augeraa768292020-01-24 15:25:34 +0100502 /* Weed out disabled counters */
503 val &= __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
504
Shannon Zhao7a0adc72015-09-08 15:49:39 +0800505 for (i = 0; i < ARMV8_PMU_CYCLE_IDX; i++) {
Eric Augeraa768292020-01-24 15:25:34 +0100506 u64 type, reg;
507
Shannon Zhao7a0adc72015-09-08 15:49:39 +0800508 if (!(val & BIT(i)))
509 continue;
Eric Augeraa768292020-01-24 15:25:34 +0100510
511 /* PMSWINC only applies to ... SW_INC! */
512 type = __vcpu_sys_reg(vcpu, PMEVTYPER0_EL0 + i);
Marc Zyngierfd65a3b2020-03-17 11:11:56 +0000513 type &= kvm_pmu_event_mask(vcpu->kvm);
Eric Augeraa768292020-01-24 15:25:34 +0100514 if (type != ARMV8_PMUV3_PERFCTR_SW_INCR)
515 continue;
516
517 /* increment this even SW_INC counter */
518 reg = __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i) + 1;
519 reg = lower_32_bits(reg);
520 __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i) = reg;
521
522 if (reg) /* no overflow on the low part */
523 continue;
524
525 if (kvm_pmu_pmc_is_chained(&pmu->pmc[i])) {
526 /* increment the high counter */
527 reg = __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i + 1) + 1;
Shannon Zhao7a0adc72015-09-08 15:49:39 +0800528 reg = lower_32_bits(reg);
Eric Augeraa768292020-01-24 15:25:34 +0100529 __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i + 1) = reg;
530 if (!reg) /* mark overflow on the high counter */
531 __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(i + 1);
532 } else {
533 /* mark overflow on low counter */
534 __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(i);
Shannon Zhao7a0adc72015-09-08 15:49:39 +0800535 }
536 }
537}
538
Shannon Zhao76993732015-10-28 12:10:30 +0800539/**
540 * kvm_pmu_handle_pmcr - handle PMCR register
541 * @vcpu: The vcpu pointer
542 * @val: the value guest writes to PMCR register
543 */
544void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
545{
Eric Augerc01d6a12020-01-24 15:25:35 +0100546 unsigned long mask = kvm_pmu_valid_counter_mask(vcpu);
Shannon Zhao76993732015-10-28 12:10:30 +0800547 int i;
548
Shannon Zhao76993732015-10-28 12:10:30 +0800549 if (val & ARMV8_PMU_PMCR_E) {
Andrew Murray418e5ca2019-06-17 20:01:01 +0100550 kvm_pmu_enable_counter_mask(vcpu,
Christoffer Dall8d404c42016-03-16 15:38:53 +0100551 __vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & mask);
Shannon Zhao76993732015-10-28 12:10:30 +0800552 } else {
Andrew Murray418e5ca2019-06-17 20:01:01 +0100553 kvm_pmu_disable_counter_mask(vcpu, mask);
Shannon Zhao76993732015-10-28 12:10:30 +0800554 }
555
556 if (val & ARMV8_PMU_PMCR_C)
557 kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0);
558
559 if (val & ARMV8_PMU_PMCR_P) {
Eric Augerc01d6a12020-01-24 15:25:35 +0100560 for_each_set_bit(i, &mask, 32)
Shannon Zhao76993732015-10-28 12:10:30 +0800561 kvm_pmu_set_counter_value(vcpu, i, 0);
562 }
Shannon Zhao76993732015-10-28 12:10:30 +0800563}
564
Shannon Zhao7f766352015-07-03 14:27:25 +0800565static bool kvm_pmu_counter_is_enabled(struct kvm_vcpu *vcpu, u64 select_idx)
566{
Christoffer Dall8d404c42016-03-16 15:38:53 +0100567 return (__vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) &&
568 (__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & BIT(select_idx));
Shannon Zhao7f766352015-07-03 14:27:25 +0800569}
570
571/**
Andrew Murray30d97752019-06-17 20:01:03 +0100572 * kvm_pmu_create_perf_event - create a perf event for a counter
Shannon Zhao7f766352015-07-03 14:27:25 +0800573 * @vcpu: The vcpu pointer
Shannon Zhao7f766352015-07-03 14:27:25 +0800574 * @select_idx: The number of selected counter
Shannon Zhao7f766352015-07-03 14:27:25 +0800575 */
Andrew Murray30d97752019-06-17 20:01:03 +0100576static void kvm_pmu_create_perf_event(struct kvm_vcpu *vcpu, u64 select_idx)
Shannon Zhao7f766352015-07-03 14:27:25 +0800577{
578 struct kvm_pmu *pmu = &vcpu->arch.pmu;
Andrew Murray80f393a2019-06-17 20:01:05 +0100579 struct kvm_pmc *pmc;
Shannon Zhao7f766352015-07-03 14:27:25 +0800580 struct perf_event *event;
581 struct perf_event_attr attr;
Andrew Murray30d97752019-06-17 20:01:03 +0100582 u64 eventsel, counter, reg, data;
583
Andrew Murray80f393a2019-06-17 20:01:05 +0100584 /*
585 * For chained counters the event type and filtering attributes are
586 * obtained from the low/even counter. We also use this counter to
587 * determine if the event is enabled/disabled.
588 */
589 pmc = kvm_pmu_get_canonical_pmc(&pmu->pmc[select_idx]);
590
591 reg = (pmc->idx == ARMV8_PMU_CYCLE_IDX)
592 ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + pmc->idx;
Andrew Murray30d97752019-06-17 20:01:03 +0100593 data = __vcpu_sys_reg(vcpu, reg);
Shannon Zhao7f766352015-07-03 14:27:25 +0800594
595 kvm_pmu_stop_counter(vcpu, pmc);
Marc Zyngierfd65a3b2020-03-17 11:11:56 +0000596 eventsel = data & kvm_pmu_event_mask(vcpu->kvm);;
Shannon Zhao7f766352015-07-03 14:27:25 +0800597
Shannon Zhao7a0adc72015-09-08 15:49:39 +0800598 /* Software increment event does't need to be backed by a perf event */
Wei Huangb112c842016-11-16 11:09:20 -0600599 if (eventsel == ARMV8_PMUV3_PERFCTR_SW_INCR &&
Andrew Murray80f393a2019-06-17 20:01:05 +0100600 pmc->idx != ARMV8_PMU_CYCLE_IDX)
Shannon Zhao7a0adc72015-09-08 15:49:39 +0800601 return;
602
Shannon Zhao7f766352015-07-03 14:27:25 +0800603 memset(&attr, 0, sizeof(struct perf_event_attr));
604 attr.type = PERF_TYPE_RAW;
605 attr.size = sizeof(attr);
606 attr.pinned = 1;
Andrew Murray80f393a2019-06-17 20:01:05 +0100607 attr.disabled = !kvm_pmu_counter_is_enabled(vcpu, pmc->idx);
Shannon Zhao7f766352015-07-03 14:27:25 +0800608 attr.exclude_user = data & ARMV8_PMU_EXCLUDE_EL0 ? 1 : 0;
609 attr.exclude_kernel = data & ARMV8_PMU_EXCLUDE_EL1 ? 1 : 0;
610 attr.exclude_hv = 1; /* Don't count EL2 events */
611 attr.exclude_host = 1; /* Don't count host events */
Andrew Murray80f393a2019-06-17 20:01:05 +0100612 attr.config = (pmc->idx == ARMV8_PMU_CYCLE_IDX) ?
Wei Huangb112c842016-11-16 11:09:20 -0600613 ARMV8_PMUV3_PERFCTR_CPU_CYCLES : eventsel;
Shannon Zhao7f766352015-07-03 14:27:25 +0800614
Andrew Murray80f393a2019-06-17 20:01:05 +0100615 counter = kvm_pmu_get_pair_counter_value(vcpu, pmc);
Shannon Zhao7f766352015-07-03 14:27:25 +0800616
Eric Auger76c9fc52020-01-24 15:25:33 +0100617 if (kvm_pmu_pmc_is_chained(pmc)) {
Andrew Murray80f393a2019-06-17 20:01:05 +0100618 /**
619 * The initial sample period (overflow count) of an event. For
620 * chained counters we only support overflow interrupts on the
621 * high counter.
622 */
623 attr.sample_period = (-counter) & GENMASK(63, 0);
Eric Auger76c9fc52020-01-24 15:25:33 +0100624 attr.config1 |= PERF_ATTR_CFG1_KVM_PMU_CHAINED;
Marc Zyngier725ce662019-10-08 15:09:55 +0100625
Andrew Murray80f393a2019-06-17 20:01:05 +0100626 event = perf_event_create_kernel_counter(&attr, -1, current,
627 kvm_pmu_perf_overflow,
628 pmc + 1);
Andrew Murray80f393a2019-06-17 20:01:05 +0100629 } else {
630 /* The initial sample period (overflow count) of an event. */
631 if (kvm_pmu_idx_is_64bit(vcpu, pmc->idx))
632 attr.sample_period = (-counter) & GENMASK(63, 0);
633 else
634 attr.sample_period = (-counter) & GENMASK(31, 0);
635
636 event = perf_event_create_kernel_counter(&attr, -1, current,
Shannon Zhaob02386e2016-02-26 19:29:19 +0800637 kvm_pmu_perf_overflow, pmc);
Andrew Murray80f393a2019-06-17 20:01:05 +0100638 }
639
Shannon Zhao7f766352015-07-03 14:27:25 +0800640 if (IS_ERR(event)) {
641 pr_err_once("kvm: pmu event creation failed %ld\n",
642 PTR_ERR(event));
643 return;
644 }
645
646 pmc->perf_event = event;
647}
Shannon Zhao808e7382016-01-11 22:46:15 +0800648
Andrew Murray30d97752019-06-17 20:01:03 +0100649/**
Andrew Murray80f393a2019-06-17 20:01:05 +0100650 * kvm_pmu_update_pmc_chained - update chained bitmap
651 * @vcpu: The vcpu pointer
652 * @select_idx: The number of selected counter
653 *
654 * Update the chained bitmap based on the event type written in the
Eric Auger76c9fc52020-01-24 15:25:33 +0100655 * typer register and the enable state of the odd register.
Andrew Murray80f393a2019-06-17 20:01:05 +0100656 */
657static void kvm_pmu_update_pmc_chained(struct kvm_vcpu *vcpu, u64 select_idx)
658{
659 struct kvm_pmu *pmu = &vcpu->arch.pmu;
Eric Auger76c9fc52020-01-24 15:25:33 +0100660 struct kvm_pmc *pmc = &pmu->pmc[select_idx], *canonical_pmc;
661 bool new_state, old_state;
Andrew Murray80f393a2019-06-17 20:01:05 +0100662
Eric Auger76c9fc52020-01-24 15:25:33 +0100663 old_state = kvm_pmu_pmc_is_chained(pmc);
664 new_state = kvm_pmu_idx_has_chain_evtype(vcpu, pmc->idx) &&
665 kvm_pmu_counter_is_enabled(vcpu, pmc->idx | 0x1);
666
667 if (old_state == new_state)
668 return;
669
670 canonical_pmc = kvm_pmu_get_canonical_pmc(pmc);
671 kvm_pmu_stop_counter(vcpu, canonical_pmc);
672 if (new_state) {
Andrew Murray80f393a2019-06-17 20:01:05 +0100673 /*
674 * During promotion from !chained to chained we must ensure
675 * the adjacent counter is stopped and its event destroyed
676 */
Eric Auger76c9fc52020-01-24 15:25:33 +0100677 kvm_pmu_stop_counter(vcpu, kvm_pmu_get_alternate_pmc(pmc));
Andrew Murray80f393a2019-06-17 20:01:05 +0100678 set_bit(pmc->idx >> 1, vcpu->arch.pmu.chained);
Eric Auger76c9fc52020-01-24 15:25:33 +0100679 return;
Andrew Murray80f393a2019-06-17 20:01:05 +0100680 }
Eric Auger76c9fc52020-01-24 15:25:33 +0100681 clear_bit(pmc->idx >> 1, vcpu->arch.pmu.chained);
Andrew Murray80f393a2019-06-17 20:01:05 +0100682}
683
684/**
Andrew Murray30d97752019-06-17 20:01:03 +0100685 * kvm_pmu_set_counter_event_type - set selected counter to monitor some event
686 * @vcpu: The vcpu pointer
687 * @data: The data guest writes to PMXEVTYPER_EL0
688 * @select_idx: The number of selected counter
689 *
690 * When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an
691 * event with given hardware event number. Here we call perf_event API to
692 * emulate this action and create a kernel perf event for it.
693 */
694void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
695 u64 select_idx)
696{
Marc Zyngierfd65a3b2020-03-17 11:11:56 +0000697 u64 reg, mask;
698
699 mask = ARMV8_PMU_EVTYPE_MASK;
700 mask &= ~ARMV8_PMU_EVTYPE_EVENT;
701 mask |= kvm_pmu_event_mask(vcpu->kvm);
Andrew Murray30d97752019-06-17 20:01:03 +0100702
703 reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
704 ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + select_idx;
705
Marc Zyngierfd65a3b2020-03-17 11:11:56 +0000706 __vcpu_sys_reg(vcpu, reg) = data & mask;
Andrew Murray80f393a2019-06-17 20:01:05 +0100707
708 kvm_pmu_update_pmc_chained(vcpu, select_idx);
Andrew Murray30d97752019-06-17 20:01:03 +0100709 kvm_pmu_create_perf_event(vcpu, select_idx);
710}
711
Marc Zyngierfd65a3b2020-03-17 11:11:56 +0000712static int kvm_pmu_probe_pmuver(void)
713{
714 struct perf_event_attr attr = { };
715 struct perf_event *event;
716 struct arm_pmu *pmu;
717 int pmuver = 0xf;
718
719 /*
720 * Create a dummy event that only counts user cycles. As we'll never
721 * leave this function with the event being live, it will never
722 * count anything. But it allows us to probe some of the PMU
723 * details. Yes, this is terrible.
724 */
725 attr.type = PERF_TYPE_RAW;
726 attr.size = sizeof(attr);
727 attr.pinned = 1;
728 attr.disabled = 0;
729 attr.exclude_user = 0;
730 attr.exclude_kernel = 1;
731 attr.exclude_hv = 1;
732 attr.exclude_host = 1;
733 attr.config = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;
734 attr.sample_period = GENMASK(63, 0);
735
736 event = perf_event_create_kernel_counter(&attr, -1, current,
737 kvm_pmu_perf_overflow, &attr);
738
739 if (IS_ERR(event)) {
740 pr_err_once("kvm: pmu event creation failed %ld\n",
741 PTR_ERR(event));
742 return 0xf;
743 }
744
745 if (event->pmu) {
746 pmu = to_arm_pmu(event->pmu);
747 if (pmu->pmuver)
748 pmuver = pmu->pmuver;
749 }
750
751 perf_event_disable(event);
752 perf_event_release_kernel(event);
753
754 return pmuver;
755}
756
Shannon Zhao808e7382016-01-11 22:46:15 +0800757bool kvm_arm_support_pmu_v3(void)
758{
759 /*
760 * Check if HW_PERF_EVENTS are supported by checking the number of
761 * hardware performance counters. This could ensure the presence of
762 * a physical PMU and CONFIG_PERF_EVENT is selected.
763 */
764 return (perf_num_counters() > 0);
765}
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800766
Christoffer Dalla2befac2017-05-02 13:41:02 +0200767int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)
768{
769 if (!vcpu->arch.pmu.created)
770 return 0;
771
772 /*
773 * A valid interrupt configuration for the PMU is either to have a
774 * properly configured interrupt number and using an in-kernel
Christoffer Dallebb127f2017-05-16 19:53:50 +0200775 * irqchip, or to not have an in-kernel GIC and not set an IRQ.
Christoffer Dalla2befac2017-05-02 13:41:02 +0200776 */
Christoffer Dallebb127f2017-05-16 19:53:50 +0200777 if (irqchip_in_kernel(vcpu->kvm)) {
778 int irq = vcpu->arch.pmu.irq_num;
779 if (!kvm_arm_pmu_irq_initialized(vcpu))
780 return -EINVAL;
781
782 /*
783 * If we are using an in-kernel vgic, at this point we know
784 * the vgic will be initialized, so we can check the PMU irq
785 * number against the dimensions of the vgic and make sure
786 * it's valid.
787 */
788 if (!irq_is_ppi(irq) && !vgic_valid_spi(vcpu->kvm, irq))
789 return -EINVAL;
790 } else if (kvm_arm_pmu_irq_initialized(vcpu)) {
791 return -EINVAL;
792 }
Christoffer Dalla2befac2017-05-02 13:41:02 +0200793
794 kvm_pmu_vcpu_reset(vcpu);
795 vcpu->arch.pmu.ready = true;
796
797 return 0;
798}
799
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800800static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)
801{
Christoffer Dalla2befac2017-05-02 13:41:02 +0200802 if (irqchip_in_kernel(vcpu->kvm)) {
Christoffer Dallabcb8512017-05-04 13:32:53 +0200803 int ret;
804
Christoffer Dalla2befac2017-05-02 13:41:02 +0200805 /*
806 * If using the PMU with an in-kernel virtual GIC
807 * implementation, we require the GIC to be already
808 * initialized when initializing the PMU.
809 */
810 if (!vgic_initialized(vcpu->kvm))
811 return -ENODEV;
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800812
Christoffer Dalla2befac2017-05-02 13:41:02 +0200813 if (!kvm_arm_pmu_irq_initialized(vcpu))
814 return -ENXIO;
Christoffer Dallabcb8512017-05-04 13:32:53 +0200815
816 ret = kvm_vgic_set_owner(vcpu, vcpu->arch.pmu.irq_num,
817 &vcpu->arch.pmu);
818 if (ret)
819 return ret;
Christoffer Dalla2befac2017-05-02 13:41:02 +0200820 }
821
822 vcpu->arch.pmu.created = true;
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800823 return 0;
824}
825
Andre Przywara2defaff2016-03-07 17:32:29 +0700826/*
827 * For one VM the interrupt type must be same for each vcpu.
828 * As a PPI, the interrupt number is the same for all vcpus,
829 * while as an SPI it must be a separate number per vcpu.
830 */
831static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800832{
833 int i;
834 struct kvm_vcpu *vcpu;
835
836 kvm_for_each_vcpu(i, vcpu, kvm) {
837 if (!kvm_arm_pmu_irq_initialized(vcpu))
838 continue;
839
Andre Przywara2defaff2016-03-07 17:32:29 +0700840 if (irq_is_ppi(irq)) {
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800841 if (vcpu->arch.pmu.irq_num != irq)
842 return false;
843 } else {
844 if (vcpu->arch.pmu.irq_num == irq)
845 return false;
846 }
847 }
848
849 return true;
850}
851
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800852int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
853{
Marc Zyngier42223fb2020-03-12 17:27:36 +0000854 if (!kvm_arm_support_pmu_v3() ||
855 !test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features))
856 return -ENODEV;
857
858 if (vcpu->arch.pmu.created)
859 return -EBUSY;
860
Marc Zyngierfd65a3b2020-03-17 11:11:56 +0000861 if (!vcpu->kvm->arch.pmuver)
862 vcpu->kvm->arch.pmuver = kvm_pmu_probe_pmuver();
863
864 if (vcpu->kvm->arch.pmuver == 0xf)
865 return -ENODEV;
866
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800867 switch (attr->attr) {
868 case KVM_ARM_VCPU_PMU_V3_IRQ: {
869 int __user *uaddr = (int __user *)(long)attr->addr;
870 int irq;
871
Christoffer Dalla2befac2017-05-02 13:41:02 +0200872 if (!irqchip_in_kernel(vcpu->kvm))
873 return -EINVAL;
874
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800875 if (get_user(irq, uaddr))
876 return -EFAULT;
877
Andre Przywara2defaff2016-03-07 17:32:29 +0700878 /* The PMU overflow interrupt can be a PPI or a valid SPI. */
Christoffer Dallebb127f2017-05-16 19:53:50 +0200879 if (!(irq_is_ppi(irq) || irq_is_spi(irq)))
Andre Przywara2defaff2016-03-07 17:32:29 +0700880 return -EINVAL;
881
882 if (!pmu_irq_is_valid(vcpu->kvm, irq))
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800883 return -EINVAL;
884
885 if (kvm_arm_pmu_irq_initialized(vcpu))
886 return -EBUSY;
887
888 kvm_debug("Set kvm ARM PMU irq: %d\n", irq);
889 vcpu->arch.pmu.irq_num = irq;
890 return 0;
891 }
892 case KVM_ARM_VCPU_PMU_V3_INIT:
893 return kvm_arm_pmu_v3_init(vcpu);
894 }
895
896 return -ENXIO;
897}
898
899int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
900{
901 switch (attr->attr) {
902 case KVM_ARM_VCPU_PMU_V3_IRQ: {
903 int __user *uaddr = (int __user *)(long)attr->addr;
904 int irq;
905
Christoffer Dalla2befac2017-05-02 13:41:02 +0200906 if (!irqchip_in_kernel(vcpu->kvm))
907 return -EINVAL;
908
Shannon Zhaobb0c70b2016-01-11 21:35:32 +0800909 if (!test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features))
910 return -ENODEV;
911
912 if (!kvm_arm_pmu_irq_initialized(vcpu))
913 return -ENXIO;
914
915 irq = vcpu->arch.pmu.irq_num;
916 return put_user(irq, uaddr);
917 }
918 }
919
920 return -ENXIO;
921}
922
923int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
924{
925 switch (attr->attr) {
926 case KVM_ARM_VCPU_PMU_V3_IRQ:
927 case KVM_ARM_VCPU_PMU_V3_INIT:
928 if (kvm_arm_support_pmu_v3() &&
929 test_bit(KVM_ARM_VCPU_PMU_V3, vcpu->arch.features))
930 return 0;
931 }
932
933 return -ENXIO;
934}