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/arm-smccc.h>
19 #include <linux/preempt.h>
20 #include <linux/kvm_host.h>
21 #include <linux/wait.h>
23 #include <asm/cputype.h>
24 #include <asm/kvm_emulate.h>
25 #include <asm/kvm_host.h>
27 #include <kvm/arm_psci.h>
30 * This is an implementation of the Power State Coordination Interface
31 * as described in ARM document number ARM DEN 0022A.
34 #define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
36 static u32
smccc_get_function(struct kvm_vcpu
*vcpu
)
38 return vcpu_get_reg(vcpu
, 0);
41 static unsigned long smccc_get_arg1(struct kvm_vcpu
*vcpu
)
43 return vcpu_get_reg(vcpu
, 1);
46 static unsigned long smccc_get_arg2(struct kvm_vcpu
*vcpu
)
48 return vcpu_get_reg(vcpu
, 2);
51 static unsigned long smccc_get_arg3(struct kvm_vcpu
*vcpu
)
53 return vcpu_get_reg(vcpu
, 3);
56 static void smccc_set_retval(struct kvm_vcpu
*vcpu
,
62 vcpu_set_reg(vcpu
, 0, a0
);
63 vcpu_set_reg(vcpu
, 1, a1
);
64 vcpu_set_reg(vcpu
, 2, a2
);
65 vcpu_set_reg(vcpu
, 3, a3
);
68 static unsigned long psci_affinity_mask(unsigned long affinity_level
)
70 if (affinity_level
<= 3)
71 return MPIDR_HWID_BITMASK
& AFFINITY_MASK(affinity_level
);
76 static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu
*vcpu
)
79 * NOTE: For simplicity, we make VCPU suspend emulation to be
80 * same-as WFI (Wait-for-interrupt) emulation.
82 * This means for KVM the wakeup events are interrupts and
83 * this is consistent with intended use of StateID as described
84 * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A).
86 * Further, we also treat power-down request to be same as
87 * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2
88 * specification (ARM DEN 0022A). This means all suspend states
89 * for KVM will preserve the register state.
92 kvm_clear_request(KVM_REQ_UNHALT
, vcpu
);
94 return PSCI_RET_SUCCESS
;
97 static void kvm_psci_vcpu_off(struct kvm_vcpu
*vcpu
)
99 vcpu
->arch
.power_off
= true;
100 kvm_make_request(KVM_REQ_SLEEP
, vcpu
);
104 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu
*source_vcpu
)
106 struct kvm
*kvm
= source_vcpu
->kvm
;
107 struct kvm_vcpu
*vcpu
= NULL
;
108 struct swait_queue_head
*wq
;
109 unsigned long cpu_id
;
110 unsigned long context_id
;
111 phys_addr_t target_pc
;
113 cpu_id
= smccc_get_arg1(source_vcpu
) & MPIDR_HWID_BITMASK
;
114 if (vcpu_mode_is_32bit(source_vcpu
))
115 cpu_id
&= ~((u32
) 0);
117 vcpu
= kvm_mpidr_to_vcpu(kvm
, cpu_id
);
120 * Make sure the caller requested a valid CPU and that the CPU is
124 return PSCI_RET_INVALID_PARAMS
;
125 if (!vcpu
->arch
.power_off
) {
126 if (kvm_psci_version(source_vcpu
) != KVM_ARM_PSCI_0_1
)
127 return PSCI_RET_ALREADY_ON
;
129 return PSCI_RET_INVALID_PARAMS
;
132 target_pc
= smccc_get_arg2(source_vcpu
);
133 context_id
= smccc_get_arg3(source_vcpu
);
135 kvm_reset_vcpu(vcpu
);
137 /* Gracefully handle Thumb2 entry point */
138 if (vcpu_mode_is_32bit(vcpu
) && (target_pc
& 1)) {
139 target_pc
&= ~((phys_addr_t
) 1);
140 vcpu_set_thumb(vcpu
);
143 /* Propagate caller endianness */
144 if (kvm_vcpu_is_be(source_vcpu
))
145 kvm_vcpu_set_be(vcpu
);
147 *vcpu_pc(vcpu
) = target_pc
;
149 * NOTE: We always update r0 (or x0) because for PSCI v0.1
150 * the general puspose registers are undefined upon CPU_ON.
152 smccc_set_retval(vcpu
, context_id
, 0, 0, 0);
153 vcpu
->arch
.power_off
= false;
154 smp_mb(); /* Make sure the above is visible */
156 wq
= kvm_arch_vcpu_wq(vcpu
);
159 return PSCI_RET_SUCCESS
;
162 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu
*vcpu
)
164 int i
, matching_cpus
= 0;
166 unsigned long target_affinity
;
167 unsigned long target_affinity_mask
;
168 unsigned long lowest_affinity_level
;
169 struct kvm
*kvm
= vcpu
->kvm
;
170 struct kvm_vcpu
*tmp
;
172 target_affinity
= smccc_get_arg1(vcpu
);
173 lowest_affinity_level
= smccc_get_arg2(vcpu
);
175 /* Determine target affinity mask */
176 target_affinity_mask
= psci_affinity_mask(lowest_affinity_level
);
177 if (!target_affinity_mask
)
178 return PSCI_RET_INVALID_PARAMS
;
180 /* Ignore other bits of target affinity */
181 target_affinity
&= target_affinity_mask
;
184 * If one or more VCPU matching target affinity are running
187 kvm_for_each_vcpu(i
, tmp
, kvm
) {
188 mpidr
= kvm_vcpu_get_mpidr_aff(tmp
);
189 if ((mpidr
& target_affinity_mask
) == target_affinity
) {
191 if (!tmp
->arch
.power_off
)
192 return PSCI_0_2_AFFINITY_LEVEL_ON
;
197 return PSCI_RET_INVALID_PARAMS
;
199 return PSCI_0_2_AFFINITY_LEVEL_OFF
;
202 static void kvm_prepare_system_event(struct kvm_vcpu
*vcpu
, u32 type
)
205 struct kvm_vcpu
*tmp
;
208 * The KVM ABI specifies that a system event exit may call KVM_RUN
209 * again and may perform shutdown/reboot at a later time that when the
210 * actual request is made. Since we are implementing PSCI and a
211 * caller of PSCI reboot and shutdown expects that the system shuts
212 * down or reboots immediately, let's make sure that VCPUs are not run
213 * after this call is handled and before the VCPUs have been
216 kvm_for_each_vcpu(i
, tmp
, vcpu
->kvm
)
217 tmp
->arch
.power_off
= true;
218 kvm_make_all_cpus_request(vcpu
->kvm
, KVM_REQ_SLEEP
);
220 memset(&vcpu
->run
->system_event
, 0, sizeof(vcpu
->run
->system_event
));
221 vcpu
->run
->system_event
.type
= type
;
222 vcpu
->run
->exit_reason
= KVM_EXIT_SYSTEM_EVENT
;
225 static void kvm_psci_system_off(struct kvm_vcpu
*vcpu
)
227 kvm_prepare_system_event(vcpu
, KVM_SYSTEM_EVENT_SHUTDOWN
);
230 static void kvm_psci_system_reset(struct kvm_vcpu
*vcpu
)
232 kvm_prepare_system_event(vcpu
, KVM_SYSTEM_EVENT_RESET
);
235 int kvm_psci_version(struct kvm_vcpu
*vcpu
)
237 if (test_bit(KVM_ARM_VCPU_PSCI_0_2
, vcpu
->arch
.features
))
238 return KVM_ARM_PSCI_LATEST
;
240 return KVM_ARM_PSCI_0_1
;
243 static int kvm_psci_0_2_call(struct kvm_vcpu
*vcpu
)
245 struct kvm
*kvm
= vcpu
->kvm
;
246 u32 psci_fn
= smccc_get_function(vcpu
);
251 case PSCI_0_2_FN_PSCI_VERSION
:
253 * Bits[31:16] = Major Version = 0
254 * Bits[15:0] = Minor Version = 2
256 val
= KVM_ARM_PSCI_0_2
;
258 case PSCI_0_2_FN_CPU_SUSPEND
:
259 case PSCI_0_2_FN64_CPU_SUSPEND
:
260 val
= kvm_psci_vcpu_suspend(vcpu
);
262 case PSCI_0_2_FN_CPU_OFF
:
263 kvm_psci_vcpu_off(vcpu
);
264 val
= PSCI_RET_SUCCESS
;
266 case PSCI_0_2_FN_CPU_ON
:
267 case PSCI_0_2_FN64_CPU_ON
:
268 mutex_lock(&kvm
->lock
);
269 val
= kvm_psci_vcpu_on(vcpu
);
270 mutex_unlock(&kvm
->lock
);
272 case PSCI_0_2_FN_AFFINITY_INFO
:
273 case PSCI_0_2_FN64_AFFINITY_INFO
:
274 val
= kvm_psci_vcpu_affinity_info(vcpu
);
276 case PSCI_0_2_FN_MIGRATE_INFO_TYPE
:
278 * Trusted OS is MP hence does not require migration
280 * Trusted OS is not present
282 val
= PSCI_0_2_TOS_MP
;
284 case PSCI_0_2_FN_SYSTEM_OFF
:
285 kvm_psci_system_off(vcpu
);
287 * We should'nt be going back to guest VCPU after
288 * receiving SYSTEM_OFF request.
290 * If user space accidently/deliberately resumes
291 * guest VCPU after SYSTEM_OFF request then guest
292 * VCPU should see internal failure from PSCI return
293 * value. To achieve this, we preload r0 (or x0) with
294 * PSCI return value INTERNAL_FAILURE.
296 val
= PSCI_RET_INTERNAL_FAILURE
;
299 case PSCI_0_2_FN_SYSTEM_RESET
:
300 kvm_psci_system_reset(vcpu
);
302 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
303 * with PSCI return value INTERNAL_FAILURE.
305 val
= PSCI_RET_INTERNAL_FAILURE
;
309 val
= PSCI_RET_NOT_SUPPORTED
;
313 smccc_set_retval(vcpu
, val
, 0, 0, 0);
317 static int kvm_psci_1_0_call(struct kvm_vcpu
*vcpu
)
319 u32 psci_fn
= smccc_get_function(vcpu
);
325 case PSCI_0_2_FN_PSCI_VERSION
:
326 val
= KVM_ARM_PSCI_1_0
;
328 case PSCI_1_0_FN_PSCI_FEATURES
:
329 feature
= smccc_get_arg1(vcpu
);
331 case PSCI_0_2_FN_PSCI_VERSION
:
332 case PSCI_0_2_FN_CPU_SUSPEND
:
333 case PSCI_0_2_FN64_CPU_SUSPEND
:
334 case PSCI_0_2_FN_CPU_OFF
:
335 case PSCI_0_2_FN_CPU_ON
:
336 case PSCI_0_2_FN64_CPU_ON
:
337 case PSCI_0_2_FN_AFFINITY_INFO
:
338 case PSCI_0_2_FN64_AFFINITY_INFO
:
339 case PSCI_0_2_FN_MIGRATE_INFO_TYPE
:
340 case PSCI_0_2_FN_SYSTEM_OFF
:
341 case PSCI_0_2_FN_SYSTEM_RESET
:
342 case PSCI_1_0_FN_PSCI_FEATURES
:
343 case ARM_SMCCC_VERSION_FUNC_ID
:
347 val
= PSCI_RET_NOT_SUPPORTED
;
352 return kvm_psci_0_2_call(vcpu
);
355 smccc_set_retval(vcpu
, val
, 0, 0, 0);
359 static int kvm_psci_0_1_call(struct kvm_vcpu
*vcpu
)
361 struct kvm
*kvm
= vcpu
->kvm
;
362 u32 psci_fn
= smccc_get_function(vcpu
);
366 case KVM_PSCI_FN_CPU_OFF
:
367 kvm_psci_vcpu_off(vcpu
);
368 val
= PSCI_RET_SUCCESS
;
370 case KVM_PSCI_FN_CPU_ON
:
371 mutex_lock(&kvm
->lock
);
372 val
= kvm_psci_vcpu_on(vcpu
);
373 mutex_unlock(&kvm
->lock
);
376 val
= PSCI_RET_NOT_SUPPORTED
;
380 smccc_set_retval(vcpu
, val
, 0, 0, 0);
385 * kvm_psci_call - handle PSCI call if r0 value is in range
386 * @vcpu: Pointer to the VCPU struct
388 * Handle PSCI calls from guests through traps from HVC instructions.
389 * The calling convention is similar to SMC calls to the secure world
390 * where the function number is placed in r0.
392 * This function returns: > 0 (success), 0 (success but exit to user
393 * space), and < 0 (errors)
396 * -EINVAL: Unrecognized PSCI function
398 static int kvm_psci_call(struct kvm_vcpu
*vcpu
)
400 switch (kvm_psci_version(vcpu
)) {
401 case KVM_ARM_PSCI_1_0
:
402 return kvm_psci_1_0_call(vcpu
);
403 case KVM_ARM_PSCI_0_2
:
404 return kvm_psci_0_2_call(vcpu
);
405 case KVM_ARM_PSCI_0_1
:
406 return kvm_psci_0_1_call(vcpu
);
412 int kvm_hvc_call_handler(struct kvm_vcpu
*vcpu
)
414 u32 func_id
= smccc_get_function(vcpu
);
415 u32 val
= PSCI_RET_NOT_SUPPORTED
;
418 case ARM_SMCCC_VERSION_FUNC_ID
:
419 val
= ARM_SMCCC_VERSION_1_1
;
421 case ARM_SMCCC_ARCH_FEATURES_FUNC_ID
:
422 /* Nothing supported yet */
425 return kvm_psci_call(vcpu
);
428 smccc_set_retval(vcpu
, val
, 0, 0, 0);