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;
67 cpu_id = smccc_get_arg1(source_vcpu);
68 if (!kvm_psci_valid_affinity(source_vcpu, cpu_id))
69 return PSCI_RET_INVALID_PARAMS;
71 vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
74 * Make sure the caller requested a valid CPU and that the CPU is
78 return PSCI_RET_INVALID_PARAMS;
79 if (!kvm_arm_vcpu_stopped(vcpu)) {
80 if (kvm_psci_version(source_vcpu) != KVM_ARM_PSCI_0_1)
81 return PSCI_RET_ALREADY_ON;
83 return PSCI_RET_INVALID_PARAMS;
86 reset_state = &vcpu->arch.reset_state;
88 reset_state->pc = smccc_get_arg2(source_vcpu);
90 /* Propagate caller endianness */
91 reset_state->be = kvm_vcpu_is_be(source_vcpu);
94 * NOTE: We always update r0 (or x0) because for PSCI v0.1
95 * the general purpose registers are undefined upon CPU_ON.
97 reset_state->r0 = smccc_get_arg3(source_vcpu);
99 WRITE_ONCE(reset_state->reset, true);
100 kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
103 * Make sure the reset request is observed if the RUNNABLE mp_state is
108 vcpu->arch.mp_state.mp_state = KVM_MP_STATE_RUNNABLE;
109 kvm_vcpu_wake_up(vcpu);
111 return PSCI_RET_SUCCESS;
114 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
116 int matching_cpus = 0;
117 unsigned long i, mpidr;
118 unsigned long target_affinity;
119 unsigned long target_affinity_mask;
120 unsigned long lowest_affinity_level;
121 struct kvm *kvm = vcpu->kvm;
122 struct kvm_vcpu *tmp;
124 target_affinity = smccc_get_arg1(vcpu);
125 lowest_affinity_level = smccc_get_arg2(vcpu);
127 if (!kvm_psci_valid_affinity(vcpu, target_affinity))
128 return PSCI_RET_INVALID_PARAMS;
130 /* Determine target affinity mask */
131 target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
132 if (!target_affinity_mask)
133 return PSCI_RET_INVALID_PARAMS;
135 /* Ignore other bits of target affinity */
136 target_affinity &= target_affinity_mask;
139 * If one or more VCPU matching target affinity are running
142 kvm_for_each_vcpu(i, tmp, kvm) {
143 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
144 if ((mpidr & target_affinity_mask) == target_affinity) {
146 if (!kvm_arm_vcpu_stopped(tmp))
147 return PSCI_0_2_AFFINITY_LEVEL_ON;
152 return PSCI_RET_INVALID_PARAMS;
154 return PSCI_0_2_AFFINITY_LEVEL_OFF;
157 static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type, u64 flags)
160 struct kvm_vcpu *tmp;
163 * The KVM ABI specifies that a system event exit may call KVM_RUN
164 * again and may perform shutdown/reboot at a later time that when the
165 * actual request is made. Since we are implementing PSCI and a
166 * caller of PSCI reboot and shutdown expects that the system shuts
167 * down or reboots immediately, let's make sure that VCPUs are not run
168 * after this call is handled and before the VCPUs have been
171 kvm_for_each_vcpu(i, tmp, vcpu->kvm)
172 tmp->arch.mp_state.mp_state = KVM_MP_STATE_STOPPED;
173 kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
175 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
176 vcpu->run->system_event.type = type;
177 vcpu->run->system_event.ndata = 1;
178 vcpu->run->system_event.data[0] = flags;
179 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
182 static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
184 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN, 0);
187 static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
189 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET, 0);
192 static void kvm_psci_system_reset2(struct kvm_vcpu *vcpu)
194 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET,
195 KVM_SYSTEM_EVENT_RESET_FLAG_PSCI_RESET2);
198 static void kvm_psci_system_suspend(struct kvm_vcpu *vcpu)
200 struct kvm_run *run = vcpu->run;
202 memset(&run->system_event, 0, sizeof(vcpu->run->system_event));
203 run->system_event.type = KVM_SYSTEM_EVENT_SUSPEND;
204 run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
207 static void kvm_psci_narrow_to_32bit(struct kvm_vcpu *vcpu)
212 * Zero the input registers' upper 32 bits. They will be fully
213 * zeroed on exit, so we're fine changing them in place.
215 for (i = 1; i < 4; i++)
216 vcpu_set_reg(vcpu, i, lower_32_bits(vcpu_get_reg(vcpu, i)));
219 static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32 fn)
222 * Prevent 32 bit guests from calling 64 bit PSCI functions.
224 if ((fn & PSCI_0_2_64BIT) && vcpu_mode_is_32bit(vcpu))
225 return PSCI_RET_NOT_SUPPORTED;
230 static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
232 struct kvm *kvm = vcpu->kvm;
233 u32 psci_fn = smccc_get_function(vcpu);
238 case PSCI_0_2_FN_PSCI_VERSION:
240 * Bits[31:16] = Major Version = 0
241 * Bits[15:0] = Minor Version = 2
243 val = KVM_ARM_PSCI_0_2;
245 case PSCI_0_2_FN_CPU_SUSPEND:
246 case PSCI_0_2_FN64_CPU_SUSPEND:
247 val = kvm_psci_vcpu_suspend(vcpu);
249 case PSCI_0_2_FN_CPU_OFF:
250 kvm_arm_vcpu_power_off(vcpu);
251 val = PSCI_RET_SUCCESS;
253 case PSCI_0_2_FN_CPU_ON:
254 kvm_psci_narrow_to_32bit(vcpu);
256 case PSCI_0_2_FN64_CPU_ON:
257 mutex_lock(&kvm->lock);
258 val = kvm_psci_vcpu_on(vcpu);
259 mutex_unlock(&kvm->lock);
261 case PSCI_0_2_FN_AFFINITY_INFO:
262 kvm_psci_narrow_to_32bit(vcpu);
264 case PSCI_0_2_FN64_AFFINITY_INFO:
265 val = kvm_psci_vcpu_affinity_info(vcpu);
267 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
269 * Trusted OS is MP hence does not require migration
271 * Trusted OS is not present
273 val = PSCI_0_2_TOS_MP;
275 case PSCI_0_2_FN_SYSTEM_OFF:
276 kvm_psci_system_off(vcpu);
278 * We shouldn't be going back to guest VCPU after
279 * receiving SYSTEM_OFF request.
281 * If user space accidentally/deliberately resumes
282 * guest VCPU after SYSTEM_OFF request then guest
283 * VCPU should see internal failure from PSCI return
284 * value. To achieve this, we preload r0 (or x0) with
285 * PSCI return value INTERNAL_FAILURE.
287 val = PSCI_RET_INTERNAL_FAILURE;
290 case PSCI_0_2_FN_SYSTEM_RESET:
291 kvm_psci_system_reset(vcpu);
293 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
294 * with PSCI return value INTERNAL_FAILURE.
296 val = PSCI_RET_INTERNAL_FAILURE;
300 val = PSCI_RET_NOT_SUPPORTED;
304 smccc_set_retval(vcpu, val, 0, 0, 0);
308 static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor)
310 unsigned long val = PSCI_RET_NOT_SUPPORTED;
311 u32 psci_fn = smccc_get_function(vcpu);
312 struct kvm *kvm = vcpu->kvm;
317 case PSCI_0_2_FN_PSCI_VERSION:
318 val = minor == 0 ? KVM_ARM_PSCI_1_0 : KVM_ARM_PSCI_1_1;
320 case PSCI_1_0_FN_PSCI_FEATURES:
321 arg = smccc_get_arg1(vcpu);
322 val = kvm_psci_check_allowed_function(vcpu, arg);
326 val = PSCI_RET_NOT_SUPPORTED;
329 case PSCI_0_2_FN_PSCI_VERSION:
330 case PSCI_0_2_FN_CPU_SUSPEND:
331 case PSCI_0_2_FN64_CPU_SUSPEND:
332 case PSCI_0_2_FN_CPU_OFF:
333 case PSCI_0_2_FN_CPU_ON:
334 case PSCI_0_2_FN64_CPU_ON:
335 case PSCI_0_2_FN_AFFINITY_INFO:
336 case PSCI_0_2_FN64_AFFINITY_INFO:
337 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
338 case PSCI_0_2_FN_SYSTEM_OFF:
339 case PSCI_0_2_FN_SYSTEM_RESET:
340 case PSCI_1_0_FN_PSCI_FEATURES:
341 case ARM_SMCCC_VERSION_FUNC_ID:
344 case PSCI_1_0_FN_SYSTEM_SUSPEND:
345 case PSCI_1_0_FN64_SYSTEM_SUSPEND:
346 if (test_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags))
349 case PSCI_1_1_FN_SYSTEM_RESET2:
350 case PSCI_1_1_FN64_SYSTEM_RESET2:
356 case PSCI_1_0_FN_SYSTEM_SUSPEND:
357 kvm_psci_narrow_to_32bit(vcpu);
359 case PSCI_1_0_FN64_SYSTEM_SUSPEND:
361 * Return directly to userspace without changing the vCPU's
362 * registers. Userspace depends on reading the SMCCC parameters
363 * to implement SYSTEM_SUSPEND.
365 if (test_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags)) {
366 kvm_psci_system_suspend(vcpu);
370 case PSCI_1_1_FN_SYSTEM_RESET2:
371 kvm_psci_narrow_to_32bit(vcpu);
373 case PSCI_1_1_FN64_SYSTEM_RESET2:
375 arg = smccc_get_arg1(vcpu);
377 if (arg <= PSCI_1_1_RESET_TYPE_SYSTEM_WARM_RESET ||
378 arg >= PSCI_1_1_RESET_TYPE_VENDOR_START) {
379 kvm_psci_system_reset2(vcpu);
380 vcpu_set_reg(vcpu, 0, PSCI_RET_INTERNAL_FAILURE);
384 val = PSCI_RET_INVALID_PARAMS;
389 return kvm_psci_0_2_call(vcpu);
392 smccc_set_retval(vcpu, val, 0, 0, 0);
396 static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
398 struct kvm *kvm = vcpu->kvm;
399 u32 psci_fn = smccc_get_function(vcpu);
403 case KVM_PSCI_FN_CPU_OFF:
404 kvm_arm_vcpu_power_off(vcpu);
405 val = PSCI_RET_SUCCESS;
407 case KVM_PSCI_FN_CPU_ON:
408 mutex_lock(&kvm->lock);
409 val = kvm_psci_vcpu_on(vcpu);
410 mutex_unlock(&kvm->lock);
413 val = PSCI_RET_NOT_SUPPORTED;
417 smccc_set_retval(vcpu, val, 0, 0, 0);
422 * kvm_psci_call - handle PSCI call if r0 value is in range
423 * @vcpu: Pointer to the VCPU struct
425 * Handle PSCI calls from guests through traps from HVC instructions.
426 * The calling convention is similar to SMC calls to the secure world
427 * where the function number is placed in r0.
429 * This function returns: > 0 (success), 0 (success but exit to user
430 * space), and < 0 (errors)
433 * -EINVAL: Unrecognized PSCI function
435 int kvm_psci_call(struct kvm_vcpu *vcpu)
437 u32 psci_fn = smccc_get_function(vcpu);
440 val = kvm_psci_check_allowed_function(vcpu, psci_fn);
442 smccc_set_retval(vcpu, val, 0, 0, 0);
446 switch (kvm_psci_version(vcpu)) {
447 case KVM_ARM_PSCI_1_1:
448 return kvm_psci_1_x_call(vcpu, 1);
449 case KVM_ARM_PSCI_1_0:
450 return kvm_psci_1_x_call(vcpu, 0);
451 case KVM_ARM_PSCI_0_2:
452 return kvm_psci_0_2_call(vcpu);
453 case KVM_ARM_PSCI_0_1:
454 return kvm_psci_0_1_call(vcpu);