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>
16 #include <kvm/arm_psci.h>
17 #include <kvm/arm_hypercalls.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 unsigned long psci_affinity_mask(unsigned long affinity_level)
28 if (affinity_level <= 3)
29 return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
34 static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
37 * NOTE: For simplicity, we make VCPU suspend emulation to be
38 * same-as WFI (Wait-for-interrupt) emulation.
40 * This means for KVM the wakeup events are interrupts and
41 * this is consistent with intended use of StateID as described
42 * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A).
44 * Further, we also treat power-down request to be same as
45 * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2
46 * specification (ARM DEN 0022A). This means all suspend states
47 * for KVM will preserve the register state.
51 return PSCI_RET_SUCCESS;
54 static inline bool kvm_psci_valid_affinity(struct kvm_vcpu *vcpu,
55 unsigned long affinity)
57 return !(affinity & ~MPIDR_HWID_BITMASK);
60 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
62 struct vcpu_reset_state *reset_state;
63 struct kvm *kvm = source_vcpu->kvm;
64 struct kvm_vcpu *vcpu = NULL;
65 int ret = PSCI_RET_SUCCESS;
68 cpu_id = smccc_get_arg1(source_vcpu);
69 if (!kvm_psci_valid_affinity(source_vcpu, cpu_id))
70 return PSCI_RET_INVALID_PARAMS;
72 vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
75 * Make sure the caller requested a valid CPU and that the CPU is
79 return PSCI_RET_INVALID_PARAMS;
81 spin_lock(&vcpu->arch.mp_state_lock);
82 if (!kvm_arm_vcpu_stopped(vcpu)) {
83 if (kvm_psci_version(source_vcpu) != KVM_ARM_PSCI_0_1)
84 ret = PSCI_RET_ALREADY_ON;
86 ret = PSCI_RET_INVALID_PARAMS;
91 reset_state = &vcpu->arch.reset_state;
93 reset_state->pc = smccc_get_arg2(source_vcpu);
95 /* Propagate caller endianness */
96 reset_state->be = kvm_vcpu_is_be(source_vcpu);
99 * NOTE: We always update r0 (or x0) because for PSCI v0.1
100 * the general purpose registers are undefined upon CPU_ON.
102 reset_state->r0 = smccc_get_arg3(source_vcpu);
104 reset_state->reset = true;
105 kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
108 * Make sure the reset request is observed if the RUNNABLE mp_state is
113 WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
114 kvm_vcpu_wake_up(vcpu);
117 spin_unlock(&vcpu->arch.mp_state_lock);
121 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
123 int matching_cpus = 0;
124 unsigned long i, mpidr;
125 unsigned long target_affinity;
126 unsigned long target_affinity_mask;
127 unsigned long lowest_affinity_level;
128 struct kvm *kvm = vcpu->kvm;
129 struct kvm_vcpu *tmp;
131 target_affinity = smccc_get_arg1(vcpu);
132 lowest_affinity_level = smccc_get_arg2(vcpu);
134 if (!kvm_psci_valid_affinity(vcpu, target_affinity))
135 return PSCI_RET_INVALID_PARAMS;
137 /* Determine target affinity mask */
138 target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
139 if (!target_affinity_mask)
140 return PSCI_RET_INVALID_PARAMS;
142 /* Ignore other bits of target affinity */
143 target_affinity &= target_affinity_mask;
146 * If one or more VCPU matching target affinity are running
149 kvm_for_each_vcpu(i, tmp, kvm) {
150 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
151 if ((mpidr & target_affinity_mask) == target_affinity) {
153 if (!kvm_arm_vcpu_stopped(tmp))
154 return PSCI_0_2_AFFINITY_LEVEL_ON;
159 return PSCI_RET_INVALID_PARAMS;
161 return PSCI_0_2_AFFINITY_LEVEL_OFF;
164 static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type, u64 flags)
167 struct kvm_vcpu *tmp;
170 * The KVM ABI specifies that a system event exit may call KVM_RUN
171 * again and may perform shutdown/reboot at a later time that when the
172 * actual request is made. Since we are implementing PSCI and a
173 * caller of PSCI reboot and shutdown expects that the system shuts
174 * down or reboots immediately, let's make sure that VCPUs are not run
175 * after this call is handled and before the VCPUs have been
178 kvm_for_each_vcpu(i, tmp, vcpu->kvm) {
179 spin_lock(&tmp->arch.mp_state_lock);
180 WRITE_ONCE(tmp->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
181 spin_unlock(&tmp->arch.mp_state_lock);
183 kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
185 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
186 vcpu->run->system_event.type = type;
187 vcpu->run->system_event.ndata = 1;
188 vcpu->run->system_event.data[0] = flags;
189 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
192 static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
194 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN, 0);
197 static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
199 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET, 0);
202 static void kvm_psci_system_reset2(struct kvm_vcpu *vcpu)
204 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET,
205 KVM_SYSTEM_EVENT_RESET_FLAG_PSCI_RESET2);
208 static void kvm_psci_system_suspend(struct kvm_vcpu *vcpu)
210 struct kvm_run *run = vcpu->run;
212 memset(&run->system_event, 0, sizeof(vcpu->run->system_event));
213 run->system_event.type = KVM_SYSTEM_EVENT_SUSPEND;
214 run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
217 static void kvm_psci_narrow_to_32bit(struct kvm_vcpu *vcpu)
222 * Zero the input registers' upper 32 bits. They will be fully
223 * zeroed on exit, so we're fine changing them in place.
225 for (i = 1; i < 4; i++)
226 vcpu_set_reg(vcpu, i, lower_32_bits(vcpu_get_reg(vcpu, i)));
229 static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32 fn)
232 * Prevent 32 bit guests from calling 64 bit PSCI functions.
234 if ((fn & PSCI_0_2_64BIT) && vcpu_mode_is_32bit(vcpu))
235 return PSCI_RET_NOT_SUPPORTED;
240 static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
242 u32 psci_fn = smccc_get_function(vcpu);
247 case PSCI_0_2_FN_PSCI_VERSION:
249 * Bits[31:16] = Major Version = 0
250 * Bits[15:0] = Minor Version = 2
252 val = KVM_ARM_PSCI_0_2;
254 case PSCI_0_2_FN_CPU_SUSPEND:
255 case PSCI_0_2_FN64_CPU_SUSPEND:
256 val = kvm_psci_vcpu_suspend(vcpu);
258 case PSCI_0_2_FN_CPU_OFF:
259 kvm_arm_vcpu_power_off(vcpu);
260 val = PSCI_RET_SUCCESS;
262 case PSCI_0_2_FN_CPU_ON:
263 kvm_psci_narrow_to_32bit(vcpu);
265 case PSCI_0_2_FN64_CPU_ON:
266 val = kvm_psci_vcpu_on(vcpu);
268 case PSCI_0_2_FN_AFFINITY_INFO:
269 kvm_psci_narrow_to_32bit(vcpu);
271 case PSCI_0_2_FN64_AFFINITY_INFO:
272 val = kvm_psci_vcpu_affinity_info(vcpu);
274 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
276 * Trusted OS is MP hence does not require migration
278 * Trusted OS is not present
280 val = PSCI_0_2_TOS_MP;
282 case PSCI_0_2_FN_SYSTEM_OFF:
283 kvm_psci_system_off(vcpu);
285 * We shouldn't be going back to guest VCPU after
286 * receiving SYSTEM_OFF request.
288 * If user space accidentally/deliberately resumes
289 * guest VCPU after SYSTEM_OFF request then guest
290 * VCPU should see internal failure from PSCI return
291 * value. To achieve this, we preload r0 (or x0) with
292 * PSCI return value INTERNAL_FAILURE.
294 val = PSCI_RET_INTERNAL_FAILURE;
297 case PSCI_0_2_FN_SYSTEM_RESET:
298 kvm_psci_system_reset(vcpu);
300 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
301 * with PSCI return value INTERNAL_FAILURE.
303 val = PSCI_RET_INTERNAL_FAILURE;
307 val = PSCI_RET_NOT_SUPPORTED;
311 smccc_set_retval(vcpu, val, 0, 0, 0);
315 static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor)
317 unsigned long val = PSCI_RET_NOT_SUPPORTED;
318 u32 psci_fn = smccc_get_function(vcpu);
319 struct kvm *kvm = vcpu->kvm;
324 case PSCI_0_2_FN_PSCI_VERSION:
325 val = minor == 0 ? KVM_ARM_PSCI_1_0 : KVM_ARM_PSCI_1_1;
327 case PSCI_1_0_FN_PSCI_FEATURES:
328 arg = smccc_get_arg1(vcpu);
329 val = kvm_psci_check_allowed_function(vcpu, arg);
333 val = PSCI_RET_NOT_SUPPORTED;
336 case PSCI_0_2_FN_PSCI_VERSION:
337 case PSCI_0_2_FN_CPU_SUSPEND:
338 case PSCI_0_2_FN64_CPU_SUSPEND:
339 case PSCI_0_2_FN_CPU_OFF:
340 case PSCI_0_2_FN_CPU_ON:
341 case PSCI_0_2_FN64_CPU_ON:
342 case PSCI_0_2_FN_AFFINITY_INFO:
343 case PSCI_0_2_FN64_AFFINITY_INFO:
344 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
345 case PSCI_0_2_FN_SYSTEM_OFF:
346 case PSCI_0_2_FN_SYSTEM_RESET:
347 case PSCI_1_0_FN_PSCI_FEATURES:
348 case ARM_SMCCC_VERSION_FUNC_ID:
351 case PSCI_1_0_FN_SYSTEM_SUSPEND:
352 case PSCI_1_0_FN64_SYSTEM_SUSPEND:
353 if (test_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags))
356 case PSCI_1_1_FN_SYSTEM_RESET2:
357 case PSCI_1_1_FN64_SYSTEM_RESET2:
363 case PSCI_1_0_FN_SYSTEM_SUSPEND:
364 kvm_psci_narrow_to_32bit(vcpu);
366 case PSCI_1_0_FN64_SYSTEM_SUSPEND:
368 * Return directly to userspace without changing the vCPU's
369 * registers. Userspace depends on reading the SMCCC parameters
370 * to implement SYSTEM_SUSPEND.
372 if (test_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags)) {
373 kvm_psci_system_suspend(vcpu);
377 case PSCI_1_1_FN_SYSTEM_RESET2:
378 kvm_psci_narrow_to_32bit(vcpu);
380 case PSCI_1_1_FN64_SYSTEM_RESET2:
382 arg = smccc_get_arg1(vcpu);
384 if (arg <= PSCI_1_1_RESET_TYPE_SYSTEM_WARM_RESET ||
385 arg >= PSCI_1_1_RESET_TYPE_VENDOR_START) {
386 kvm_psci_system_reset2(vcpu);
387 vcpu_set_reg(vcpu, 0, PSCI_RET_INTERNAL_FAILURE);
391 val = PSCI_RET_INVALID_PARAMS;
396 return kvm_psci_0_2_call(vcpu);
399 smccc_set_retval(vcpu, val, 0, 0, 0);
403 static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
405 u32 psci_fn = smccc_get_function(vcpu);
409 case KVM_PSCI_FN_CPU_OFF:
410 kvm_arm_vcpu_power_off(vcpu);
411 val = PSCI_RET_SUCCESS;
413 case KVM_PSCI_FN_CPU_ON:
414 val = kvm_psci_vcpu_on(vcpu);
417 val = PSCI_RET_NOT_SUPPORTED;
421 smccc_set_retval(vcpu, val, 0, 0, 0);
426 * kvm_psci_call - handle PSCI call if r0 value is in range
427 * @vcpu: Pointer to the VCPU struct
429 * Handle PSCI calls from guests through traps from HVC instructions.
430 * The calling convention is similar to SMC calls to the secure world
431 * where the function number is placed in r0.
433 * This function returns: > 0 (success), 0 (success but exit to user
434 * space), and < 0 (errors)
437 * -EINVAL: Unrecognized PSCI function
439 int kvm_psci_call(struct kvm_vcpu *vcpu)
441 u32 psci_fn = smccc_get_function(vcpu);
442 int version = kvm_psci_version(vcpu);
445 val = kvm_psci_check_allowed_function(vcpu, psci_fn);
447 smccc_set_retval(vcpu, val, 0, 0, 0);
452 case KVM_ARM_PSCI_1_1:
453 return kvm_psci_1_x_call(vcpu, 1);
454 case KVM_ARM_PSCI_1_0:
455 return kvm_psci_1_x_call(vcpu, 0);
456 case KVM_ARM_PSCI_0_2:
457 return kvm_psci_0_2_call(vcpu);
458 case KVM_ARM_PSCI_0_1:
459 return kvm_psci_0_1_call(vcpu);
461 WARN_ONCE(1, "Unknown PSCI version %d", version);
462 smccc_set_retval(vcpu, SMCCC_RET_NOT_SUPPORTED, 0, 0, 0);