• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2019 Arm Limited
4  * Author: Andrew Murray <Andrew.Murray@arm.com>
5  */
6 #include <linux/kvm_host.h>
7 #include <linux/perf_event.h>
8 #include <asm/kvm_hyp.h>
9 
10 /*
11  * Given the perf event attributes and system type, determine
12  * if we are going to need to switch counters at guest entry/exit.
13  */
kvm_pmu_switch_needed(struct perf_event_attr * attr)14 static bool kvm_pmu_switch_needed(struct perf_event_attr *attr)
15 {
16 	/**
17 	 * With VHE the guest kernel runs at EL1 and the host at EL2,
18 	 * where user (EL0) is excluded then we have no reason to switch
19 	 * counters.
20 	 */
21 	if (has_vhe() && attr->exclude_user)
22 		return false;
23 
24 	/* Only switch if attributes are different */
25 	return (attr->exclude_host != attr->exclude_guest);
26 }
27 
28 /*
29  * Add events to track that we may want to switch at guest entry/exit
30  * time.
31  */
kvm_set_pmu_events(u32 set,struct perf_event_attr * attr)32 void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr)
33 {
34 	struct kvm_host_data *ctx;
35 
36 	if (!kvm_arm_support_pmu_v3())
37 		return;
38 
39 	ctx = this_cpu_ptr_hyp_sym(kvm_host_data);
40 	if (!ctx || !kvm_pmu_switch_needed(attr))
41 		return;
42 
43 	if (!attr->exclude_host)
44 		ctx->pmu_events.events_host |= set;
45 	if (!attr->exclude_guest)
46 		ctx->pmu_events.events_guest |= set;
47 }
48 
49 /*
50  * Stop tracking events
51  */
kvm_clr_pmu_events(u32 clr)52 void kvm_clr_pmu_events(u32 clr)
53 {
54 	struct kvm_host_data *ctx;
55 
56 	if (!kvm_arm_support_pmu_v3())
57 		return;
58 
59 	ctx = this_cpu_ptr_hyp_sym(kvm_host_data);
60 	if (!ctx)
61 		return;
62 
63 	ctx->pmu_events.events_host &= ~clr;
64 	ctx->pmu_events.events_guest &= ~clr;
65 }
66 
67 #define PMEVTYPER_READ_CASE(idx)				\
68 	case idx:						\
69 		return read_sysreg(pmevtyper##idx##_el0)
70 
71 #define PMEVTYPER_WRITE_CASE(idx)				\
72 	case idx:						\
73 		write_sysreg(val, pmevtyper##idx##_el0);	\
74 		break
75 
76 #define PMEVTYPER_CASES(readwrite)				\
77 	PMEVTYPER_##readwrite##_CASE(0);			\
78 	PMEVTYPER_##readwrite##_CASE(1);			\
79 	PMEVTYPER_##readwrite##_CASE(2);			\
80 	PMEVTYPER_##readwrite##_CASE(3);			\
81 	PMEVTYPER_##readwrite##_CASE(4);			\
82 	PMEVTYPER_##readwrite##_CASE(5);			\
83 	PMEVTYPER_##readwrite##_CASE(6);			\
84 	PMEVTYPER_##readwrite##_CASE(7);			\
85 	PMEVTYPER_##readwrite##_CASE(8);			\
86 	PMEVTYPER_##readwrite##_CASE(9);			\
87 	PMEVTYPER_##readwrite##_CASE(10);			\
88 	PMEVTYPER_##readwrite##_CASE(11);			\
89 	PMEVTYPER_##readwrite##_CASE(12);			\
90 	PMEVTYPER_##readwrite##_CASE(13);			\
91 	PMEVTYPER_##readwrite##_CASE(14);			\
92 	PMEVTYPER_##readwrite##_CASE(15);			\
93 	PMEVTYPER_##readwrite##_CASE(16);			\
94 	PMEVTYPER_##readwrite##_CASE(17);			\
95 	PMEVTYPER_##readwrite##_CASE(18);			\
96 	PMEVTYPER_##readwrite##_CASE(19);			\
97 	PMEVTYPER_##readwrite##_CASE(20);			\
98 	PMEVTYPER_##readwrite##_CASE(21);			\
99 	PMEVTYPER_##readwrite##_CASE(22);			\
100 	PMEVTYPER_##readwrite##_CASE(23);			\
101 	PMEVTYPER_##readwrite##_CASE(24);			\
102 	PMEVTYPER_##readwrite##_CASE(25);			\
103 	PMEVTYPER_##readwrite##_CASE(26);			\
104 	PMEVTYPER_##readwrite##_CASE(27);			\
105 	PMEVTYPER_##readwrite##_CASE(28);			\
106 	PMEVTYPER_##readwrite##_CASE(29);			\
107 	PMEVTYPER_##readwrite##_CASE(30)
108 
109 /*
110  * Read a value direct from PMEVTYPER<idx> where idx is 0-30
111  * or PMCCFILTR_EL0 where idx is ARMV8_PMU_CYCLE_IDX (31).
112  */
kvm_vcpu_pmu_read_evtype_direct(int idx)113 static u64 kvm_vcpu_pmu_read_evtype_direct(int idx)
114 {
115 	switch (idx) {
116 	PMEVTYPER_CASES(READ);
117 	case ARMV8_PMU_CYCLE_IDX:
118 		return read_sysreg(pmccfiltr_el0);
119 	default:
120 		WARN_ON(1);
121 	}
122 
123 	return 0;
124 }
125 
126 /*
127  * Write a value direct to PMEVTYPER<idx> where idx is 0-30
128  * or PMCCFILTR_EL0 where idx is ARMV8_PMU_CYCLE_IDX (31).
129  */
kvm_vcpu_pmu_write_evtype_direct(int idx,u32 val)130 static void kvm_vcpu_pmu_write_evtype_direct(int idx, u32 val)
131 {
132 	switch (idx) {
133 	PMEVTYPER_CASES(WRITE);
134 	case ARMV8_PMU_CYCLE_IDX:
135 		write_sysreg(val, pmccfiltr_el0);
136 		break;
137 	default:
138 		WARN_ON(1);
139 	}
140 }
141 
142 /*
143  * Modify ARMv8 PMU events to include EL0 counting
144  */
kvm_vcpu_pmu_enable_el0(unsigned long events)145 static void kvm_vcpu_pmu_enable_el0(unsigned long events)
146 {
147 	u64 typer;
148 	u32 counter;
149 
150 	for_each_set_bit(counter, &events, 32) {
151 		typer = kvm_vcpu_pmu_read_evtype_direct(counter);
152 		typer &= ~ARMV8_PMU_EXCLUDE_EL0;
153 		kvm_vcpu_pmu_write_evtype_direct(counter, typer);
154 	}
155 }
156 
157 /*
158  * Modify ARMv8 PMU events to exclude EL0 counting
159  */
kvm_vcpu_pmu_disable_el0(unsigned long events)160 static void kvm_vcpu_pmu_disable_el0(unsigned long events)
161 {
162 	u64 typer;
163 	u32 counter;
164 
165 	for_each_set_bit(counter, &events, 32) {
166 		typer = kvm_vcpu_pmu_read_evtype_direct(counter);
167 		typer |= ARMV8_PMU_EXCLUDE_EL0;
168 		kvm_vcpu_pmu_write_evtype_direct(counter, typer);
169 	}
170 }
171 
172 /*
173  * On VHE ensure that only guest events have EL0 counting enabled.
174  * This is called from both vcpu_{load,put} and the sysreg handling.
175  * Since the latter is preemptible, special care must be taken to
176  * disable preemption.
177  */
kvm_vcpu_pmu_restore_guest(struct kvm_vcpu * vcpu)178 void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu)
179 {
180 	struct kvm_host_data *host;
181 	u32 events_guest, events_host;
182 
183 	if (!kvm_arm_support_pmu_v3() || !has_vhe())
184 		return;
185 
186 	preempt_disable();
187 	host = this_cpu_ptr_hyp_sym(kvm_host_data);
188 	events_guest = host->pmu_events.events_guest;
189 	events_host = host->pmu_events.events_host;
190 
191 	kvm_vcpu_pmu_enable_el0(events_guest);
192 	kvm_vcpu_pmu_disable_el0(events_host);
193 	preempt_enable();
194 }
195 
196 /*
197  * On VHE ensure that only host events have EL0 counting enabled
198  */
kvm_vcpu_pmu_restore_host(struct kvm_vcpu * vcpu)199 void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu)
200 {
201 	struct kvm_host_data *host;
202 	u32 events_guest, events_host;
203 
204 	if (!kvm_arm_support_pmu_v3() || !has_vhe())
205 		return;
206 
207 	host = this_cpu_ptr_hyp_sym(kvm_host_data);
208 	events_guest = host->pmu_events.events_guest;
209 	events_host = host->pmu_events.events_host;
210 
211 	kvm_vcpu_pmu_enable_el0(events_host);
212 	kvm_vcpu_pmu_disable_el0(events_guest);
213 }
214