]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - virt/kvm/arm/psci.c
arm/arm64: KVM: Implement PSCI 1.0 support
[mirror_ubuntu-artful-kernel.git] / virt / kvm / arm / psci.c
1 /*
2 * Copyright (C) 2012 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
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/preempt.h>
19 #include <linux/kvm_host.h>
20 #include <linux/wait.h>
21
22 #include <asm/cputype.h>
23 #include <asm/kvm_emulate.h>
24 #include <asm/kvm_host.h>
25
26 #include <kvm/arm_psci.h>
27
28 /*
29 * This is an implementation of the Power State Coordination Interface
30 * as described in ARM document number ARM DEN 0022A.
31 */
32
33 #define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
34
35 static u32 smccc_get_function(struct kvm_vcpu *vcpu)
36 {
37 return vcpu_get_reg(vcpu, 0);
38 }
39
40 static unsigned long smccc_get_arg1(struct kvm_vcpu *vcpu)
41 {
42 return vcpu_get_reg(vcpu, 1);
43 }
44
45 static unsigned long smccc_get_arg2(struct kvm_vcpu *vcpu)
46 {
47 return vcpu_get_reg(vcpu, 2);
48 }
49
50 static unsigned long smccc_get_arg3(struct kvm_vcpu *vcpu)
51 {
52 return vcpu_get_reg(vcpu, 3);
53 }
54
55 static void smccc_set_retval(struct kvm_vcpu *vcpu,
56 unsigned long a0,
57 unsigned long a1,
58 unsigned long a2,
59 unsigned long a3)
60 {
61 vcpu_set_reg(vcpu, 0, a0);
62 vcpu_set_reg(vcpu, 1, a1);
63 vcpu_set_reg(vcpu, 2, a2);
64 vcpu_set_reg(vcpu, 3, a3);
65 }
66
67 static unsigned long psci_affinity_mask(unsigned long affinity_level)
68 {
69 if (affinity_level <= 3)
70 return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
71
72 return 0;
73 }
74
75 static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
76 {
77 /*
78 * NOTE: For simplicity, we make VCPU suspend emulation to be
79 * same-as WFI (Wait-for-interrupt) emulation.
80 *
81 * This means for KVM the wakeup events are interrupts and
82 * this is consistent with intended use of StateID as described
83 * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A).
84 *
85 * Further, we also treat power-down request to be same as
86 * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2
87 * specification (ARM DEN 0022A). This means all suspend states
88 * for KVM will preserve the register state.
89 */
90 kvm_vcpu_block(vcpu);
91 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
92
93 return PSCI_RET_SUCCESS;
94 }
95
96 static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
97 {
98 vcpu->arch.power_off = true;
99 kvm_make_request(KVM_REQ_SLEEP, vcpu);
100 kvm_vcpu_kick(vcpu);
101 }
102
103 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
104 {
105 struct kvm *kvm = source_vcpu->kvm;
106 struct kvm_vcpu *vcpu = NULL;
107 struct swait_queue_head *wq;
108 unsigned long cpu_id;
109 unsigned long context_id;
110 phys_addr_t target_pc;
111
112 cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
113 if (vcpu_mode_is_32bit(source_vcpu))
114 cpu_id &= ~((u32) 0);
115
116 vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
117
118 /*
119 * Make sure the caller requested a valid CPU and that the CPU is
120 * turned off.
121 */
122 if (!vcpu)
123 return PSCI_RET_INVALID_PARAMS;
124 if (!vcpu->arch.power_off) {
125 if (kvm_psci_version(source_vcpu) != KVM_ARM_PSCI_0_1)
126 return PSCI_RET_ALREADY_ON;
127 else
128 return PSCI_RET_INVALID_PARAMS;
129 }
130
131 target_pc = smccc_get_arg2(source_vcpu);
132 context_id = smccc_get_arg3(source_vcpu);
133
134 kvm_reset_vcpu(vcpu);
135
136 /* Gracefully handle Thumb2 entry point */
137 if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
138 target_pc &= ~((phys_addr_t) 1);
139 vcpu_set_thumb(vcpu);
140 }
141
142 /* Propagate caller endianness */
143 if (kvm_vcpu_is_be(source_vcpu))
144 kvm_vcpu_set_be(vcpu);
145
146 *vcpu_pc(vcpu) = target_pc;
147 /*
148 * NOTE: We always update r0 (or x0) because for PSCI v0.1
149 * the general puspose registers are undefined upon CPU_ON.
150 */
151 smccc_set_retval(vcpu, context_id, 0, 0, 0);
152 vcpu->arch.power_off = false;
153 smp_mb(); /* Make sure the above is visible */
154
155 wq = kvm_arch_vcpu_wq(vcpu);
156 swake_up(wq);
157
158 return PSCI_RET_SUCCESS;
159 }
160
161 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
162 {
163 int i, matching_cpus = 0;
164 unsigned long mpidr;
165 unsigned long target_affinity;
166 unsigned long target_affinity_mask;
167 unsigned long lowest_affinity_level;
168 struct kvm *kvm = vcpu->kvm;
169 struct kvm_vcpu *tmp;
170
171 target_affinity = smccc_get_arg1(vcpu);
172 lowest_affinity_level = smccc_get_arg2(vcpu);
173
174 /* Determine target affinity mask */
175 target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
176 if (!target_affinity_mask)
177 return PSCI_RET_INVALID_PARAMS;
178
179 /* Ignore other bits of target affinity */
180 target_affinity &= target_affinity_mask;
181
182 /*
183 * If one or more VCPU matching target affinity are running
184 * then ON else OFF
185 */
186 kvm_for_each_vcpu(i, tmp, kvm) {
187 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
188 if ((mpidr & target_affinity_mask) == target_affinity) {
189 matching_cpus++;
190 if (!tmp->arch.power_off)
191 return PSCI_0_2_AFFINITY_LEVEL_ON;
192 }
193 }
194
195 if (!matching_cpus)
196 return PSCI_RET_INVALID_PARAMS;
197
198 return PSCI_0_2_AFFINITY_LEVEL_OFF;
199 }
200
201 static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
202 {
203 int i;
204 struct kvm_vcpu *tmp;
205
206 /*
207 * The KVM ABI specifies that a system event exit may call KVM_RUN
208 * again and may perform shutdown/reboot at a later time that when the
209 * actual request is made. Since we are implementing PSCI and a
210 * caller of PSCI reboot and shutdown expects that the system shuts
211 * down or reboots immediately, let's make sure that VCPUs are not run
212 * after this call is handled and before the VCPUs have been
213 * re-initialized.
214 */
215 kvm_for_each_vcpu(i, tmp, vcpu->kvm)
216 tmp->arch.power_off = true;
217 kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
218
219 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
220 vcpu->run->system_event.type = type;
221 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
222 }
223
224 static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
225 {
226 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
227 }
228
229 static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
230 {
231 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
232 }
233
234 int kvm_psci_version(struct kvm_vcpu *vcpu)
235 {
236 if (test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features))
237 return KVM_ARM_PSCI_LATEST;
238
239 return KVM_ARM_PSCI_0_1;
240 }
241
242 static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
243 {
244 struct kvm *kvm = vcpu->kvm;
245 u32 psci_fn = smccc_get_function(vcpu);
246 unsigned long val;
247 int ret = 1;
248
249 switch (psci_fn) {
250 case PSCI_0_2_FN_PSCI_VERSION:
251 /*
252 * Bits[31:16] = Major Version = 0
253 * Bits[15:0] = Minor Version = 2
254 */
255 val = KVM_ARM_PSCI_0_2;
256 break;
257 case PSCI_0_2_FN_CPU_SUSPEND:
258 case PSCI_0_2_FN64_CPU_SUSPEND:
259 val = kvm_psci_vcpu_suspend(vcpu);
260 break;
261 case PSCI_0_2_FN_CPU_OFF:
262 kvm_psci_vcpu_off(vcpu);
263 val = PSCI_RET_SUCCESS;
264 break;
265 case PSCI_0_2_FN_CPU_ON:
266 case PSCI_0_2_FN64_CPU_ON:
267 mutex_lock(&kvm->lock);
268 val = kvm_psci_vcpu_on(vcpu);
269 mutex_unlock(&kvm->lock);
270 break;
271 case PSCI_0_2_FN_AFFINITY_INFO:
272 case PSCI_0_2_FN64_AFFINITY_INFO:
273 val = kvm_psci_vcpu_affinity_info(vcpu);
274 break;
275 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
276 /*
277 * Trusted OS is MP hence does not require migration
278 * or
279 * Trusted OS is not present
280 */
281 val = PSCI_0_2_TOS_MP;
282 break;
283 case PSCI_0_2_FN_SYSTEM_OFF:
284 kvm_psci_system_off(vcpu);
285 /*
286 * We should'nt be going back to guest VCPU after
287 * receiving SYSTEM_OFF request.
288 *
289 * If user space accidently/deliberately resumes
290 * guest VCPU after SYSTEM_OFF request then guest
291 * VCPU should see internal failure from PSCI return
292 * value. To achieve this, we preload r0 (or x0) with
293 * PSCI return value INTERNAL_FAILURE.
294 */
295 val = PSCI_RET_INTERNAL_FAILURE;
296 ret = 0;
297 break;
298 case PSCI_0_2_FN_SYSTEM_RESET:
299 kvm_psci_system_reset(vcpu);
300 /*
301 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
302 * with PSCI return value INTERNAL_FAILURE.
303 */
304 val = PSCI_RET_INTERNAL_FAILURE;
305 ret = 0;
306 break;
307 default:
308 val = PSCI_RET_NOT_SUPPORTED;
309 break;
310 }
311
312 smccc_set_retval(vcpu, val, 0, 0, 0);
313 return ret;
314 }
315
316 static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu)
317 {
318 u32 psci_fn = smccc_get_function(vcpu);
319 u32 feature;
320 unsigned long val;
321 int ret = 1;
322
323 switch(psci_fn) {
324 case PSCI_0_2_FN_PSCI_VERSION:
325 val = KVM_ARM_PSCI_1_0;
326 break;
327 case PSCI_1_0_FN_PSCI_FEATURES:
328 feature = smccc_get_arg1(vcpu);
329 switch(feature) {
330 case PSCI_0_2_FN_PSCI_VERSION:
331 case PSCI_0_2_FN_CPU_SUSPEND:
332 case PSCI_0_2_FN64_CPU_SUSPEND:
333 case PSCI_0_2_FN_CPU_OFF:
334 case PSCI_0_2_FN_CPU_ON:
335 case PSCI_0_2_FN64_CPU_ON:
336 case PSCI_0_2_FN_AFFINITY_INFO:
337 case PSCI_0_2_FN64_AFFINITY_INFO:
338 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
339 case PSCI_0_2_FN_SYSTEM_OFF:
340 case PSCI_0_2_FN_SYSTEM_RESET:
341 case PSCI_1_0_FN_PSCI_FEATURES:
342 val = 0;
343 break;
344 default:
345 val = PSCI_RET_NOT_SUPPORTED;
346 break;
347 }
348 break;
349 default:
350 return kvm_psci_0_2_call(vcpu);
351 }
352
353 smccc_set_retval(vcpu, val, 0, 0, 0);
354 return ret;
355 }
356
357 static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
358 {
359 struct kvm *kvm = vcpu->kvm;
360 u32 psci_fn = smccc_get_function(vcpu);
361 unsigned long val;
362
363 switch (psci_fn) {
364 case KVM_PSCI_FN_CPU_OFF:
365 kvm_psci_vcpu_off(vcpu);
366 val = PSCI_RET_SUCCESS;
367 break;
368 case KVM_PSCI_FN_CPU_ON:
369 mutex_lock(&kvm->lock);
370 val = kvm_psci_vcpu_on(vcpu);
371 mutex_unlock(&kvm->lock);
372 break;
373 default:
374 val = PSCI_RET_NOT_SUPPORTED;
375 break;
376 }
377
378 smccc_set_retval(vcpu, val, 0, 0, 0);
379 return 1;
380 }
381
382 /**
383 * kvm_psci_call - handle PSCI call if r0 value is in range
384 * @vcpu: Pointer to the VCPU struct
385 *
386 * Handle PSCI calls from guests through traps from HVC instructions.
387 * The calling convention is similar to SMC calls to the secure world
388 * where the function number is placed in r0.
389 *
390 * This function returns: > 0 (success), 0 (success but exit to user
391 * space), and < 0 (errors)
392 *
393 * Errors:
394 * -EINVAL: Unrecognized PSCI function
395 */
396 int kvm_psci_call(struct kvm_vcpu *vcpu)
397 {
398 switch (kvm_psci_version(vcpu)) {
399 case KVM_ARM_PSCI_1_0:
400 return kvm_psci_1_0_call(vcpu);
401 case KVM_ARM_PSCI_0_2:
402 return kvm_psci_0_2_call(vcpu);
403 case KVM_ARM_PSCI_0_1:
404 return kvm_psci_0_1_call(vcpu);
405 default:
406 return -EINVAL;
407 };
408 }