1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #include <linux/arm-smccc.h>
8 #include <linux/preempt.h>
9 #include <linux/kvm_host.h>
10 #include <linux/uaccess.h>
11 #include <linux/wait.h>
13 #include <asm/cputype.h>
14 #include <asm/kvm_emulate.h>
15 #include <asm/kvm_host.h>
17 #include <kvm/arm_psci.h>
20 * This is an implementation of the Power State Coordination Interface
21 * as described in ARM document number ARM DEN 0022A.
24 #define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
26 static u32 smccc_get_function(struct kvm_vcpu *vcpu)
28 return vcpu_get_reg(vcpu, 0);
31 static unsigned long smccc_get_arg1(struct kvm_vcpu *vcpu)
33 return vcpu_get_reg(vcpu, 1);
36 static unsigned long smccc_get_arg2(struct kvm_vcpu *vcpu)
38 return vcpu_get_reg(vcpu, 2);
41 static unsigned long smccc_get_arg3(struct kvm_vcpu *vcpu)
43 return vcpu_get_reg(vcpu, 3);
46 static void smccc_set_retval(struct kvm_vcpu *vcpu,
52 vcpu_set_reg(vcpu, 0, a0);
53 vcpu_set_reg(vcpu, 1, a1);
54 vcpu_set_reg(vcpu, 2, a2);
55 vcpu_set_reg(vcpu, 3, a3);
58 static unsigned long psci_affinity_mask(unsigned long affinity_level)
60 if (affinity_level <= 3)
61 return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
66 static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
69 * NOTE: For simplicity, we make VCPU suspend emulation to be
70 * same-as WFI (Wait-for-interrupt) emulation.
72 * This means for KVM the wakeup events are interrupts and
73 * this is consistent with intended use of StateID as described
74 * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A).
76 * Further, we also treat power-down request to be same as
77 * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2
78 * specification (ARM DEN 0022A). This means all suspend states
79 * for KVM will preserve the register state.
82 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
84 return PSCI_RET_SUCCESS;
87 static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
89 vcpu->arch.power_off = true;
90 kvm_make_request(KVM_REQ_SLEEP, vcpu);
94 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
96 struct vcpu_reset_state *reset_state;
97 struct kvm *kvm = source_vcpu->kvm;
98 struct kvm_vcpu *vcpu = NULL;
101 cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
102 if (vcpu_mode_is_32bit(source_vcpu))
103 cpu_id &= ~((u32) 0);
105 vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
108 * Make sure the caller requested a valid CPU and that the CPU is
112 return PSCI_RET_INVALID_PARAMS;
113 if (!vcpu->arch.power_off) {
114 if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1)
115 return PSCI_RET_ALREADY_ON;
117 return PSCI_RET_INVALID_PARAMS;
120 reset_state = &vcpu->arch.reset_state;
122 reset_state->pc = smccc_get_arg2(source_vcpu);
124 /* Propagate caller endianness */
125 reset_state->be = kvm_vcpu_is_be(source_vcpu);
128 * NOTE: We always update r0 (or x0) because for PSCI v0.1
129 * the general puspose registers are undefined upon CPU_ON.
131 reset_state->r0 = smccc_get_arg3(source_vcpu);
133 WRITE_ONCE(reset_state->reset, true);
134 kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
137 * Make sure the reset request is observed if the change to
138 * power_state is observed.
142 vcpu->arch.power_off = false;
143 kvm_vcpu_wake_up(vcpu);
145 return PSCI_RET_SUCCESS;
148 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
150 int i, matching_cpus = 0;
152 unsigned long target_affinity;
153 unsigned long target_affinity_mask;
154 unsigned long lowest_affinity_level;
155 struct kvm *kvm = vcpu->kvm;
156 struct kvm_vcpu *tmp;
158 target_affinity = smccc_get_arg1(vcpu);
159 lowest_affinity_level = smccc_get_arg2(vcpu);
161 /* Determine target affinity mask */
162 target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
163 if (!target_affinity_mask)
164 return PSCI_RET_INVALID_PARAMS;
166 /* Ignore other bits of target affinity */
167 target_affinity &= target_affinity_mask;
170 * If one or more VCPU matching target affinity are running
173 kvm_for_each_vcpu(i, tmp, kvm) {
174 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
175 if ((mpidr & target_affinity_mask) == target_affinity) {
177 if (!tmp->arch.power_off)
178 return PSCI_0_2_AFFINITY_LEVEL_ON;
183 return PSCI_RET_INVALID_PARAMS;
185 return PSCI_0_2_AFFINITY_LEVEL_OFF;
188 static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
191 struct kvm_vcpu *tmp;
194 * The KVM ABI specifies that a system event exit may call KVM_RUN
195 * again and may perform shutdown/reboot at a later time that when the
196 * actual request is made. Since we are implementing PSCI and a
197 * caller of PSCI reboot and shutdown expects that the system shuts
198 * down or reboots immediately, let's make sure that VCPUs are not run
199 * after this call is handled and before the VCPUs have been
202 kvm_for_each_vcpu(i, tmp, vcpu->kvm)
203 tmp->arch.power_off = true;
204 kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
206 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
207 vcpu->run->system_event.type = type;
208 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
211 static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
213 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
216 static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
218 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
221 static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
223 struct kvm *kvm = vcpu->kvm;
224 u32 psci_fn = smccc_get_function(vcpu);
229 case PSCI_0_2_FN_PSCI_VERSION:
231 * Bits[31:16] = Major Version = 0
232 * Bits[15:0] = Minor Version = 2
234 val = KVM_ARM_PSCI_0_2;
236 case PSCI_0_2_FN_CPU_SUSPEND:
237 case PSCI_0_2_FN64_CPU_SUSPEND:
238 val = kvm_psci_vcpu_suspend(vcpu);
240 case PSCI_0_2_FN_CPU_OFF:
241 kvm_psci_vcpu_off(vcpu);
242 val = PSCI_RET_SUCCESS;
244 case PSCI_0_2_FN_CPU_ON:
245 case PSCI_0_2_FN64_CPU_ON:
246 mutex_lock(&kvm->lock);
247 val = kvm_psci_vcpu_on(vcpu);
248 mutex_unlock(&kvm->lock);
250 case PSCI_0_2_FN_AFFINITY_INFO:
251 case PSCI_0_2_FN64_AFFINITY_INFO:
252 val = kvm_psci_vcpu_affinity_info(vcpu);
254 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
256 * Trusted OS is MP hence does not require migration
258 * Trusted OS is not present
260 val = PSCI_0_2_TOS_MP;
262 case PSCI_0_2_FN_SYSTEM_OFF:
263 kvm_psci_system_off(vcpu);
265 * We should'nt be going back to guest VCPU after
266 * receiving SYSTEM_OFF request.
268 * If user space accidently/deliberately resumes
269 * guest VCPU after SYSTEM_OFF request then guest
270 * VCPU should see internal failure from PSCI return
271 * value. To achieve this, we preload r0 (or x0) with
272 * PSCI return value INTERNAL_FAILURE.
274 val = PSCI_RET_INTERNAL_FAILURE;
277 case PSCI_0_2_FN_SYSTEM_RESET:
278 kvm_psci_system_reset(vcpu);
280 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
281 * with PSCI return value INTERNAL_FAILURE.
283 val = PSCI_RET_INTERNAL_FAILURE;
287 val = PSCI_RET_NOT_SUPPORTED;
291 smccc_set_retval(vcpu, val, 0, 0, 0);
295 static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu)
297 u32 psci_fn = smccc_get_function(vcpu);
303 case PSCI_0_2_FN_PSCI_VERSION:
304 val = KVM_ARM_PSCI_1_0;
306 case PSCI_1_0_FN_PSCI_FEATURES:
307 feature = smccc_get_arg1(vcpu);
309 case PSCI_0_2_FN_PSCI_VERSION:
310 case PSCI_0_2_FN_CPU_SUSPEND:
311 case PSCI_0_2_FN64_CPU_SUSPEND:
312 case PSCI_0_2_FN_CPU_OFF:
313 case PSCI_0_2_FN_CPU_ON:
314 case PSCI_0_2_FN64_CPU_ON:
315 case PSCI_0_2_FN_AFFINITY_INFO:
316 case PSCI_0_2_FN64_AFFINITY_INFO:
317 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
318 case PSCI_0_2_FN_SYSTEM_OFF:
319 case PSCI_0_2_FN_SYSTEM_RESET:
320 case PSCI_1_0_FN_PSCI_FEATURES:
321 case ARM_SMCCC_VERSION_FUNC_ID:
325 val = PSCI_RET_NOT_SUPPORTED;
330 return kvm_psci_0_2_call(vcpu);
333 smccc_set_retval(vcpu, val, 0, 0, 0);
337 static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
339 struct kvm *kvm = vcpu->kvm;
340 u32 psci_fn = smccc_get_function(vcpu);
344 case KVM_PSCI_FN_CPU_OFF:
345 kvm_psci_vcpu_off(vcpu);
346 val = PSCI_RET_SUCCESS;
348 case KVM_PSCI_FN_CPU_ON:
349 mutex_lock(&kvm->lock);
350 val = kvm_psci_vcpu_on(vcpu);
351 mutex_unlock(&kvm->lock);
354 val = PSCI_RET_NOT_SUPPORTED;
358 smccc_set_retval(vcpu, val, 0, 0, 0);
363 * kvm_psci_call - handle PSCI call if r0 value is in range
364 * @vcpu: Pointer to the VCPU struct
366 * Handle PSCI calls from guests through traps from HVC instructions.
367 * The calling convention is similar to SMC calls to the secure world
368 * where the function number is placed in r0.
370 * This function returns: > 0 (success), 0 (success but exit to user
371 * space), and < 0 (errors)
374 * -EINVAL: Unrecognized PSCI function
376 static int kvm_psci_call(struct kvm_vcpu *vcpu)
378 switch (kvm_psci_version(vcpu, vcpu->kvm)) {
379 case KVM_ARM_PSCI_1_0:
380 return kvm_psci_1_0_call(vcpu);
381 case KVM_ARM_PSCI_0_2:
382 return kvm_psci_0_2_call(vcpu);
383 case KVM_ARM_PSCI_0_1:
384 return kvm_psci_0_1_call(vcpu);
390 int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)
392 u32 func_id = smccc_get_function(vcpu);
393 u32 val = SMCCC_RET_NOT_SUPPORTED;
397 case ARM_SMCCC_VERSION_FUNC_ID:
398 val = ARM_SMCCC_VERSION_1_1;
400 case ARM_SMCCC_ARCH_FEATURES_FUNC_ID:
401 feature = smccc_get_arg1(vcpu);
403 case ARM_SMCCC_ARCH_WORKAROUND_1:
404 if (kvm_arm_harden_branch_predictor())
405 val = SMCCC_RET_SUCCESS;
407 case ARM_SMCCC_ARCH_WORKAROUND_2:
408 switch (kvm_arm_have_ssbd()) {
409 case KVM_SSBD_FORCE_DISABLE:
410 case KVM_SSBD_UNKNOWN:
412 case KVM_SSBD_KERNEL:
413 val = SMCCC_RET_SUCCESS;
415 case KVM_SSBD_FORCE_ENABLE:
416 case KVM_SSBD_MITIGATED:
417 val = SMCCC_RET_NOT_REQUIRED;
424 return kvm_psci_call(vcpu);
427 smccc_set_retval(vcpu, val, 0, 0, 0);
431 int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)
433 return 1; /* PSCI version */
436 int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
438 if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices))
444 int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
446 if (reg->id == KVM_REG_ARM_PSCI_VERSION) {
447 void __user *uaddr = (void __user *)(long)reg->addr;
450 val = kvm_psci_version(vcpu, vcpu->kvm);
451 if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)))
460 int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
462 if (reg->id == KVM_REG_ARM_PSCI_VERSION) {
463 void __user *uaddr = (void __user *)(long)reg->addr;
467 if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id)))
470 wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features);
473 case KVM_ARM_PSCI_0_1:
476 vcpu->kvm->arch.psci_version = val;
478 case KVM_ARM_PSCI_0_2:
479 case KVM_ARM_PSCI_1_0:
482 vcpu->kvm->arch.psci_version = val;