aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/arm64/include/asm/kvm_host.h3
-rw-r--r--arch/arm64/kvm/Makefile1
-rw-r--r--arch/arm64/kvm/sys_regs.c139
-rw-r--r--include/kvm/arm_pmu.h11
-rw-r--r--virt/kvm/arm/pmu.c63
5 files changed, 213 insertions, 4 deletions
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 57a2d8f76c2f..4ae27fe34240 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -120,6 +120,9 @@ enum vcpu_sysreg {
120 /* Performance Monitors Registers */ 120 /* Performance Monitors Registers */
121 PMCR_EL0, /* Control Register */ 121 PMCR_EL0, /* Control Register */
122 PMSELR_EL0, /* Event Counter Selection Register */ 122 PMSELR_EL0, /* Event Counter Selection Register */
123 PMEVCNTR0_EL0, /* Event Counter Register (0-30) */
124 PMEVCNTR30_EL0 = PMEVCNTR0_EL0 + 30,
125 PMCCNTR_EL0, /* Cycle Counter Register */
123 126
124 /* 32bit specific registers. Keep them at the end of the range */ 127 /* 32bit specific registers. Keep them at the end of the range */
125 DACR32_EL2, /* Domain Access Control Register */ 128 DACR32_EL2, /* Domain Access Control Register */
diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile
index caee9ee8e12a..122cff482ac4 100644
--- a/arch/arm64/kvm/Makefile
+++ b/arch/arm64/kvm/Makefile
@@ -26,3 +26,4 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v2-emul.o
26kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v3.o 26kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v3.o
27kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v3-emul.o 27kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/vgic-v3-emul.o
28kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arch_timer.o 28kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arch_timer.o
29kvm-$(CONFIG_KVM_ARM_PMU) += $(KVM)/arm/pmu.o
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index ca8cdf6d83cf..ff3214b6fbc8 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -513,6 +513,56 @@ static bool access_pmceid(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
513 return true; 513 return true;
514} 514}
515 515
516static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx)
517{
518 u64 pmcr, val;
519
520 pmcr = vcpu_sys_reg(vcpu, PMCR_EL0);
521 val = (pmcr >> ARMV8_PMU_PMCR_N_SHIFT) & ARMV8_PMU_PMCR_N_MASK;
522 if (idx >= val && idx != ARMV8_PMU_CYCLE_IDX)
523 return false;
524
525 return true;
526}
527
528static bool access_pmu_evcntr(struct kvm_vcpu *vcpu,
529 struct sys_reg_params *p,
530 const struct sys_reg_desc *r)
531{
532 u64 idx;
533
534 if (!kvm_arm_pmu_v3_ready(vcpu))
535 return trap_raz_wi(vcpu, p, r);
536
537 if (r->CRn == 9 && r->CRm == 13) {
538 if (r->Op2 == 2) {
539 /* PMXEVCNTR_EL0 */
540 idx = vcpu_sys_reg(vcpu, PMSELR_EL0)
541 & ARMV8_PMU_COUNTER_MASK;
542 } else if (r->Op2 == 0) {
543 /* PMCCNTR_EL0 */
544 idx = ARMV8_PMU_CYCLE_IDX;
545 } else {
546 BUG();
547 }
548 } else if (r->CRn == 14 && (r->CRm & 12) == 8) {
549 /* PMEVCNTRn_EL0 */
550 idx = ((r->CRm & 3) << 3) | (r->Op2 & 7);
551 } else {
552 BUG();
553 }
554
555 if (!pmu_counter_idx_valid(vcpu, idx))
556 return false;
557
558 if (p->is_write)
559 kvm_pmu_set_counter_value(vcpu, idx, p->regval);
560 else
561 p->regval = kvm_pmu_get_counter_value(vcpu, idx);
562
563 return true;
564}
565
516/* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */ 566/* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
517#define DBG_BCR_BVR_WCR_WVR_EL1(n) \ 567#define DBG_BCR_BVR_WCR_WVR_EL1(n) \
518 /* DBGBVRn_EL1 */ \ 568 /* DBGBVRn_EL1 */ \
@@ -528,6 +578,13 @@ static bool access_pmceid(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
528 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b111), \ 578 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b111), \
529 trap_wcr, reset_wcr, n, 0, get_wcr, set_wcr } 579 trap_wcr, reset_wcr, n, 0, get_wcr, set_wcr }
530 580
581/* Macro to expand the PMEVCNTRn_EL0 register */
582#define PMU_PMEVCNTR_EL0(n) \
583 /* PMEVCNTRn_EL0 */ \
584 { Op0(0b11), Op1(0b011), CRn(0b1110), \
585 CRm((0b1000 | (((n) >> 3) & 0x3))), Op2(((n) & 0x7)), \
586 access_pmu_evcntr, reset_unknown, (PMEVCNTR0_EL0 + n), }
587
531/* 588/*
532 * Architected system registers. 589 * Architected system registers.
533 * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2 590 * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
@@ -721,13 +778,13 @@ static const struct sys_reg_desc sys_reg_descs[] = {
721 access_pmceid }, 778 access_pmceid },
722 /* PMCCNTR_EL0 */ 779 /* PMCCNTR_EL0 */
723 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b000), 780 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b000),
724 trap_raz_wi }, 781 access_pmu_evcntr, reset_unknown, PMCCNTR_EL0 },
725 /* PMXEVTYPER_EL0 */ 782 /* PMXEVTYPER_EL0 */
726 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b001), 783 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b001),
727 trap_raz_wi }, 784 trap_raz_wi },
728 /* PMXEVCNTR_EL0 */ 785 /* PMXEVCNTR_EL0 */
729 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b010), 786 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b010),
730 trap_raz_wi }, 787 access_pmu_evcntr },
731 /* PMUSERENR_EL0 */ 788 /* PMUSERENR_EL0 */
732 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b000), 789 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b000),
733 trap_raz_wi }, 790 trap_raz_wi },
@@ -742,6 +799,39 @@ static const struct sys_reg_desc sys_reg_descs[] = {
742 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b011), 799 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b011),
743 NULL, reset_unknown, TPIDRRO_EL0 }, 800 NULL, reset_unknown, TPIDRRO_EL0 },
744 801
802 /* PMEVCNTRn_EL0 */
803 PMU_PMEVCNTR_EL0(0),
804 PMU_PMEVCNTR_EL0(1),
805 PMU_PMEVCNTR_EL0(2),
806 PMU_PMEVCNTR_EL0(3),
807 PMU_PMEVCNTR_EL0(4),
808 PMU_PMEVCNTR_EL0(5),
809 PMU_PMEVCNTR_EL0(6),
810 PMU_PMEVCNTR_EL0(7),
811 PMU_PMEVCNTR_EL0(8),
812 PMU_PMEVCNTR_EL0(9),
813 PMU_PMEVCNTR_EL0(10),
814 PMU_PMEVCNTR_EL0(11),
815 PMU_PMEVCNTR_EL0(12),
816 PMU_PMEVCNTR_EL0(13),
817 PMU_PMEVCNTR_EL0(14),
818 PMU_PMEVCNTR_EL0(15),
819 PMU_PMEVCNTR_EL0(16),
820 PMU_PMEVCNTR_EL0(17),
821 PMU_PMEVCNTR_EL0(18),
822 PMU_PMEVCNTR_EL0(19),
823 PMU_PMEVCNTR_EL0(20),
824 PMU_PMEVCNTR_EL0(21),
825 PMU_PMEVCNTR_EL0(22),
826 PMU_PMEVCNTR_EL0(23),
827 PMU_PMEVCNTR_EL0(24),
828 PMU_PMEVCNTR_EL0(25),
829 PMU_PMEVCNTR_EL0(26),
830 PMU_PMEVCNTR_EL0(27),
831 PMU_PMEVCNTR_EL0(28),
832 PMU_PMEVCNTR_EL0(29),
833 PMU_PMEVCNTR_EL0(30),
834
745 /* DACR32_EL2 */ 835 /* DACR32_EL2 */
746 { Op0(0b11), Op1(0b100), CRn(0b0011), CRm(0b0000), Op2(0b000), 836 { Op0(0b11), Op1(0b100), CRn(0b0011), CRm(0b0000), Op2(0b000),
747 NULL, reset_unknown, DACR32_EL2 }, 837 NULL, reset_unknown, DACR32_EL2 },
@@ -931,6 +1021,13 @@ static const struct sys_reg_desc cp14_64_regs[] = {
931 { Op1( 0), CRm( 2), .access = trap_raz_wi }, 1021 { Op1( 0), CRm( 2), .access = trap_raz_wi },
932}; 1022};
933 1023
1024/* Macro to expand the PMEVCNTRn register */
1025#define PMU_PMEVCNTR(n) \
1026 /* PMEVCNTRn */ \
1027 { Op1(0), CRn(0b1110), \
1028 CRm((0b1000 | (((n) >> 3) & 0x3))), Op2(((n) & 0x7)), \
1029 access_pmu_evcntr }
1030
934/* 1031/*
935 * Trapped cp15 registers. TTBR0/TTBR1 get a double encoding, 1032 * Trapped cp15 registers. TTBR0/TTBR1 get a double encoding,
936 * depending on the way they are accessed (as a 32bit or a 64bit 1033 * depending on the way they are accessed (as a 32bit or a 64bit
@@ -966,9 +1063,9 @@ static const struct sys_reg_desc cp15_regs[] = {
966 { Op1( 0), CRn( 9), CRm(12), Op2( 5), access_pmselr }, 1063 { Op1( 0), CRn( 9), CRm(12), Op2( 5), access_pmselr },
967 { Op1( 0), CRn( 9), CRm(12), Op2( 6), access_pmceid }, 1064 { Op1( 0), CRn( 9), CRm(12), Op2( 6), access_pmceid },
968 { Op1( 0), CRn( 9), CRm(12), Op2( 7), access_pmceid }, 1065 { Op1( 0), CRn( 9), CRm(12), Op2( 7), access_pmceid },
969 { Op1( 0), CRn( 9), CRm(13), Op2( 0), trap_raz_wi }, 1066 { Op1( 0), CRn( 9), CRm(13), Op2( 0), access_pmu_evcntr },
970 { Op1( 0), CRn( 9), CRm(13), Op2( 1), trap_raz_wi }, 1067 { Op1( 0), CRn( 9), CRm(13), Op2( 1), trap_raz_wi },
971 { Op1( 0), CRn( 9), CRm(13), Op2( 2), trap_raz_wi }, 1068 { Op1( 0), CRn( 9), CRm(13), Op2( 2), access_pmu_evcntr },
972 { Op1( 0), CRn( 9), CRm(14), Op2( 0), trap_raz_wi }, 1069 { Op1( 0), CRn( 9), CRm(14), Op2( 0), trap_raz_wi },
973 { Op1( 0), CRn( 9), CRm(14), Op2( 1), trap_raz_wi }, 1070 { Op1( 0), CRn( 9), CRm(14), Op2( 1), trap_raz_wi },
974 { Op1( 0), CRn( 9), CRm(14), Op2( 2), trap_raz_wi }, 1071 { Op1( 0), CRn( 9), CRm(14), Op2( 2), trap_raz_wi },
@@ -982,10 +1079,44 @@ static const struct sys_reg_desc cp15_regs[] = {
982 { Op1( 0), CRn(12), CRm(12), Op2( 5), trap_raz_wi }, 1079 { Op1( 0), CRn(12), CRm(12), Op2( 5), trap_raz_wi },
983 1080
984 { Op1( 0), CRn(13), CRm( 0), Op2( 1), access_vm_reg, NULL, c13_CID }, 1081 { Op1( 0), CRn(13), CRm( 0), Op2( 1), access_vm_reg, NULL, c13_CID },
1082
1083 /* PMEVCNTRn */
1084 PMU_PMEVCNTR(0),
1085 PMU_PMEVCNTR(1),
1086 PMU_PMEVCNTR(2),
1087 PMU_PMEVCNTR(3),
1088 PMU_PMEVCNTR(4),
1089 PMU_PMEVCNTR(5),
1090 PMU_PMEVCNTR(6),
1091 PMU_PMEVCNTR(7),
1092 PMU_PMEVCNTR(8),
1093 PMU_PMEVCNTR(9),
1094 PMU_PMEVCNTR(10),
1095 PMU_PMEVCNTR(11),
1096 PMU_PMEVCNTR(12),
1097 PMU_PMEVCNTR(13),
1098 PMU_PMEVCNTR(14),
1099 PMU_PMEVCNTR(15),
1100 PMU_PMEVCNTR(16),
1101 PMU_PMEVCNTR(17),
1102 PMU_PMEVCNTR(18),
1103 PMU_PMEVCNTR(19),
1104 PMU_PMEVCNTR(20),
1105 PMU_PMEVCNTR(21),
1106 PMU_PMEVCNTR(22),
1107 PMU_PMEVCNTR(23),
1108 PMU_PMEVCNTR(24),
1109 PMU_PMEVCNTR(25),
1110 PMU_PMEVCNTR(26),
1111 PMU_PMEVCNTR(27),
1112 PMU_PMEVCNTR(28),
1113 PMU_PMEVCNTR(29),
1114 PMU_PMEVCNTR(30),
985}; 1115};
986 1116
987static const struct sys_reg_desc cp15_64_regs[] = { 1117static const struct sys_reg_desc cp15_64_regs[] = {
988 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR0 }, 1118 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR0 },
1119 { Op1( 0), CRn( 0), CRm( 9), Op2( 0), access_pmu_evcntr },
989 { Op1( 0), CRn( 0), CRm(12), Op2( 0), access_gic_sgi }, 1120 { Op1( 0), CRn( 0), CRm(12), Op2( 0), access_gic_sgi },
990 { Op1( 1), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR1 }, 1121 { Op1( 1), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR1 },
991}; 1122};
diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h
index 8157fe5bcbb0..bcb769805839 100644
--- a/include/kvm/arm_pmu.h
+++ b/include/kvm/arm_pmu.h
@@ -23,6 +23,8 @@
23#include <linux/perf_event.h> 23#include <linux/perf_event.h>
24#include <asm/perf_event.h> 24#include <asm/perf_event.h>
25 25
26#define ARMV8_PMU_CYCLE_IDX (ARMV8_PMU_MAX_COUNTERS - 1)
27
26struct kvm_pmc { 28struct kvm_pmc {
27 u8 idx; /* index into the pmu->pmc array */ 29 u8 idx; /* index into the pmu->pmc array */
28 struct perf_event *perf_event; 30 struct perf_event *perf_event;
@@ -36,11 +38,20 @@ struct kvm_pmu {
36}; 38};
37 39
38#define kvm_arm_pmu_v3_ready(v) ((v)->arch.pmu.ready) 40#define kvm_arm_pmu_v3_ready(v) ((v)->arch.pmu.ready)
41u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx);
42void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val);
39#else 43#else
40struct kvm_pmu { 44struct kvm_pmu {
41}; 45};
42 46
43#define kvm_arm_pmu_v3_ready(v) (false) 47#define kvm_arm_pmu_v3_ready(v) (false)
48static inline u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu,
49 u64 select_idx)
50{
51 return 0;
52}
53static inline void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu,
54 u64 select_idx, u64 val) {}
44#endif 55#endif
45 56
46#endif 57#endif
diff --git a/virt/kvm/arm/pmu.c b/virt/kvm/arm/pmu.c
new file mode 100644
index 000000000000..cd74e6367cd6
--- /dev/null
+++ b/virt/kvm/arm/pmu.c
@@ -0,0 +1,63 @@
1/*
2 * Copyright (C) 2015 Linaro Ltd.
3 * Author: Shannon Zhao <shannon.zhao@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <linux/cpu.h>
19#include <linux/kvm.h>
20#include <linux/kvm_host.h>
21#include <linux/perf_event.h>
22#include <asm/kvm_emulate.h>
23#include <kvm/arm_pmu.h>
24
25/**
26 * kvm_pmu_get_counter_value - get PMU counter value
27 * @vcpu: The vcpu pointer
28 * @select_idx: The counter index
29 */
30u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
31{
32 u64 counter, reg, enabled, running;
33 struct kvm_pmu *pmu = &vcpu->arch.pmu;
34 struct kvm_pmc *pmc = &pmu->pmc[select_idx];
35
36 reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
37 ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx;
38 counter = vcpu_sys_reg(vcpu, reg);
39
40 /* The real counter value is equal to the value of counter register plus
41 * the value perf event counts.
42 */
43 if (pmc->perf_event)
44 counter += perf_event_read_value(pmc->perf_event, &enabled,
45 &running);
46
47 return counter & pmc->bitmask;
48}
49
50/**
51 * kvm_pmu_set_counter_value - set PMU counter value
52 * @vcpu: The vcpu pointer
53 * @select_idx: The counter index
54 * @val: The counter value
55 */
56void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
57{
58 u64 reg;
59
60 reg = (select_idx == ARMV8_PMU_CYCLE_IDX)
61 ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx;
62 vcpu_sys_reg(vcpu, reg) += (s64)val - kvm_pmu_get_counter_value(vcpu, select_idx);
63}