2 * Copyright (C) 2012 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
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.
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.
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/>.
18 #include <linux/preempt.h>
19 #include <linux/kvm_host.h>
20 #include <linux/wait.h>
22 #include <asm/cputype.h>
23 #include <asm/kvm_emulate.h>
24 #include <asm/kvm_host.h>
26 #include <kvm/arm_psci.h>
29 * This is an implementation of the Power State Coordination Interface
30 * as described in ARM document number ARM DEN 0022A.
33 #define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
35 static u32 smccc_get_function(struct kvm_vcpu *vcpu)
37 return vcpu_get_reg(vcpu, 0);
40 static unsigned long smccc_get_arg1(struct kvm_vcpu *vcpu)
42 return vcpu_get_reg(vcpu, 1);
45 static unsigned long smccc_get_arg2(struct kvm_vcpu *vcpu)
47 return vcpu_get_reg(vcpu, 2);
50 static unsigned long smccc_get_arg3(struct kvm_vcpu *vcpu)
52 return vcpu_get_reg(vcpu, 3);
55 static void smccc_set_retval(struct kvm_vcpu *vcpu,
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);
67 static unsigned long psci_affinity_mask(unsigned long affinity_level)
69 if (affinity_level <= 3)
70 return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
75 static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
78 * NOTE: For simplicity, we make VCPU suspend emulation to be
79 * same-as WFI (Wait-for-interrupt) emulation.
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).
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.
91 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
93 return PSCI_RET_SUCCESS;
96 static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
98 vcpu->arch.power_off = true;
99 kvm_make_request(KVM_REQ_SLEEP, vcpu);
103 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
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;
112 cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
113 if (vcpu_mode_is_32bit(source_vcpu))
114 cpu_id &= ~((u32) 0);
116 vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
119 * Make sure the caller requested a valid CPU and that the CPU is
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;
128 return PSCI_RET_INVALID_PARAMS;
131 target_pc = smccc_get_arg2(source_vcpu);
132 context_id = smccc_get_arg3(source_vcpu);
134 kvm_reset_vcpu(vcpu);
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);
142 /* Propagate caller endianness */
143 if (kvm_vcpu_is_be(source_vcpu))
144 kvm_vcpu_set_be(vcpu);
146 *vcpu_pc(vcpu) = target_pc;
148 * NOTE: We always update r0 (or x0) because for PSCI v0.1
149 * the general puspose registers are undefined upon CPU_ON.
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 */
155 wq = kvm_arch_vcpu_wq(vcpu);
158 return PSCI_RET_SUCCESS;
161 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
163 int i, matching_cpus = 0;
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;
171 target_affinity = smccc_get_arg1(vcpu);
172 lowest_affinity_level = smccc_get_arg2(vcpu);
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;
179 /* Ignore other bits of target affinity */
180 target_affinity &= target_affinity_mask;
183 * If one or more VCPU matching target affinity are running
186 kvm_for_each_vcpu(i, tmp, kvm) {
187 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
188 if ((mpidr & target_affinity_mask) == target_affinity) {
190 if (!tmp->arch.power_off)
191 return PSCI_0_2_AFFINITY_LEVEL_ON;
196 return PSCI_RET_INVALID_PARAMS;
198 return PSCI_0_2_AFFINITY_LEVEL_OFF;
201 static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
204 struct kvm_vcpu *tmp;
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
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);
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;
224 static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
226 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
229 static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
231 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
234 int kvm_psci_version(struct kvm_vcpu *vcpu)
236 if (test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features))
237 return KVM_ARM_PSCI_LATEST;
239 return KVM_ARM_PSCI_0_1;
242 static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
244 struct kvm *kvm = vcpu->kvm;
245 u32 psci_fn = smccc_get_function(vcpu);
250 case PSCI_0_2_FN_PSCI_VERSION:
252 * Bits[31:16] = Major Version = 0
253 * Bits[15:0] = Minor Version = 2
255 val = KVM_ARM_PSCI_0_2;
257 case PSCI_0_2_FN_CPU_SUSPEND:
258 case PSCI_0_2_FN64_CPU_SUSPEND:
259 val = kvm_psci_vcpu_suspend(vcpu);
261 case PSCI_0_2_FN_CPU_OFF:
262 kvm_psci_vcpu_off(vcpu);
263 val = PSCI_RET_SUCCESS;
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);
271 case PSCI_0_2_FN_AFFINITY_INFO:
272 case PSCI_0_2_FN64_AFFINITY_INFO:
273 val = kvm_psci_vcpu_affinity_info(vcpu);
275 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
277 * Trusted OS is MP hence does not require migration
279 * Trusted OS is not present
281 val = PSCI_0_2_TOS_MP;
283 case PSCI_0_2_FN_SYSTEM_OFF:
284 kvm_psci_system_off(vcpu);
286 * We should'nt be going back to guest VCPU after
287 * receiving SYSTEM_OFF request.
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.
295 val = PSCI_RET_INTERNAL_FAILURE;
298 case PSCI_0_2_FN_SYSTEM_RESET:
299 kvm_psci_system_reset(vcpu);
301 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
302 * with PSCI return value INTERNAL_FAILURE.
304 val = PSCI_RET_INTERNAL_FAILURE;
308 val = PSCI_RET_NOT_SUPPORTED;
312 smccc_set_retval(vcpu, val, 0, 0, 0);
316 static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu)
318 u32 psci_fn = smccc_get_function(vcpu);
324 case PSCI_0_2_FN_PSCI_VERSION:
325 val = KVM_ARM_PSCI_1_0;
327 case PSCI_1_0_FN_PSCI_FEATURES:
328 feature = smccc_get_arg1(vcpu);
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:
345 val = PSCI_RET_NOT_SUPPORTED;
350 return kvm_psci_0_2_call(vcpu);
353 smccc_set_retval(vcpu, val, 0, 0, 0);
357 static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
359 struct kvm *kvm = vcpu->kvm;
360 u32 psci_fn = smccc_get_function(vcpu);
364 case KVM_PSCI_FN_CPU_OFF:
365 kvm_psci_vcpu_off(vcpu);
366 val = PSCI_RET_SUCCESS;
368 case KVM_PSCI_FN_CPU_ON:
369 mutex_lock(&kvm->lock);
370 val = kvm_psci_vcpu_on(vcpu);
371 mutex_unlock(&kvm->lock);
374 val = PSCI_RET_NOT_SUPPORTED;
378 smccc_set_retval(vcpu, val, 0, 0, 0);
383 * kvm_psci_call - handle PSCI call if r0 value is in range
384 * @vcpu: Pointer to the VCPU struct
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.
390 * This function returns: > 0 (success), 0 (success but exit to user
391 * space), and < 0 (errors)
394 * -EINVAL: Unrecognized PSCI function
396 int kvm_psci_call(struct kvm_vcpu *vcpu)
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);