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
) != 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 static int kvm_psci_0_2_call(struct kvm_vcpu
*vcpu
)
237 struct kvm
*kvm
= vcpu
->kvm
;
238 u32 psci_fn
= smccc_get_function(vcpu
);
243 case PSCI_0_2_FN_PSCI_VERSION
:
245 * Bits[31:16] = Major Version = 0
246 * Bits[15:0] = Minor Version = 2
248 val
= KVM_ARM_PSCI_0_2
;
250 case PSCI_0_2_FN_CPU_SUSPEND
:
251 case PSCI_0_2_FN64_CPU_SUSPEND
:
252 val
= kvm_psci_vcpu_suspend(vcpu
);
254 case PSCI_0_2_FN_CPU_OFF
:
255 kvm_psci_vcpu_off(vcpu
);
256 val
= PSCI_RET_SUCCESS
;
258 case PSCI_0_2_FN_CPU_ON
:
259 case PSCI_0_2_FN64_CPU_ON
:
260 mutex_lock(&kvm
->lock
);
261 val
= kvm_psci_vcpu_on(vcpu
);
262 mutex_unlock(&kvm
->lock
);
264 case PSCI_0_2_FN_AFFINITY_INFO
:
265 case PSCI_0_2_FN64_AFFINITY_INFO
:
266 val
= kvm_psci_vcpu_affinity_info(vcpu
);
268 case PSCI_0_2_FN_MIGRATE_INFO_TYPE
:
270 * Trusted OS is MP hence does not require migration
272 * Trusted OS is not present
274 val
= PSCI_0_2_TOS_MP
;
276 case PSCI_0_2_FN_SYSTEM_OFF
:
277 kvm_psci_system_off(vcpu
);
279 * We should'nt be going back to guest VCPU after
280 * receiving SYSTEM_OFF request.
282 * If user space accidently/deliberately resumes
283 * guest VCPU after SYSTEM_OFF request then guest
284 * VCPU should see internal failure from PSCI return
285 * value. To achieve this, we preload r0 (or x0) with
286 * PSCI return value INTERNAL_FAILURE.
288 val
= PSCI_RET_INTERNAL_FAILURE
;
291 case PSCI_0_2_FN_SYSTEM_RESET
:
292 kvm_psci_system_reset(vcpu
);
294 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
295 * with PSCI return value INTERNAL_FAILURE.
297 val
= PSCI_RET_INTERNAL_FAILURE
;
301 val
= PSCI_RET_NOT_SUPPORTED
;
305 smccc_set_retval(vcpu
, val
, 0, 0, 0);
309 static int kvm_psci_1_0_call(struct kvm_vcpu
*vcpu
)
311 u32 psci_fn
= smccc_get_function(vcpu
);
317 case PSCI_0_2_FN_PSCI_VERSION
:
318 val
= KVM_ARM_PSCI_1_0
;
320 case PSCI_1_0_FN_PSCI_FEATURES
:
321 feature
= smccc_get_arg1(vcpu
);
323 case PSCI_0_2_FN_PSCI_VERSION
:
324 case PSCI_0_2_FN_CPU_SUSPEND
:
325 case PSCI_0_2_FN64_CPU_SUSPEND
:
326 case PSCI_0_2_FN_CPU_OFF
:
327 case PSCI_0_2_FN_CPU_ON
:
328 case PSCI_0_2_FN64_CPU_ON
:
329 case PSCI_0_2_FN_AFFINITY_INFO
:
330 case PSCI_0_2_FN64_AFFINITY_INFO
:
331 case PSCI_0_2_FN_MIGRATE_INFO_TYPE
:
332 case PSCI_0_2_FN_SYSTEM_OFF
:
333 case PSCI_0_2_FN_SYSTEM_RESET
:
334 case PSCI_1_0_FN_PSCI_FEATURES
:
335 case ARM_SMCCC_VERSION_FUNC_ID
:
339 val
= PSCI_RET_NOT_SUPPORTED
;
344 return kvm_psci_0_2_call(vcpu
);
347 smccc_set_retval(vcpu
, val
, 0, 0, 0);
351 static int kvm_psci_0_1_call(struct kvm_vcpu
*vcpu
)
353 struct kvm
*kvm
= vcpu
->kvm
;
354 u32 psci_fn
= smccc_get_function(vcpu
);
358 case KVM_PSCI_FN_CPU_OFF
:
359 kvm_psci_vcpu_off(vcpu
);
360 val
= PSCI_RET_SUCCESS
;
362 case KVM_PSCI_FN_CPU_ON
:
363 mutex_lock(&kvm
->lock
);
364 val
= kvm_psci_vcpu_on(vcpu
);
365 mutex_unlock(&kvm
->lock
);
368 val
= PSCI_RET_NOT_SUPPORTED
;
372 smccc_set_retval(vcpu
, val
, 0, 0, 0);
377 * kvm_psci_call - handle PSCI call if r0 value is in range
378 * @vcpu: Pointer to the VCPU struct
380 * Handle PSCI calls from guests through traps from HVC instructions.
381 * The calling convention is similar to SMC calls to the secure world
382 * where the function number is placed in r0.
384 * This function returns: > 0 (success), 0 (success but exit to user
385 * space), and < 0 (errors)
388 * -EINVAL: Unrecognized PSCI function
390 static int kvm_psci_call(struct kvm_vcpu
*vcpu
)
392 switch (kvm_psci_version(vcpu
, vcpu
->kvm
)) {
393 case KVM_ARM_PSCI_1_0
:
394 return kvm_psci_1_0_call(vcpu
);
395 case KVM_ARM_PSCI_0_2
:
396 return kvm_psci_0_2_call(vcpu
);
397 case KVM_ARM_PSCI_0_1
:
398 return kvm_psci_0_1_call(vcpu
);
404 int kvm_hvc_call_handler(struct kvm_vcpu
*vcpu
)
406 u32 func_id
= smccc_get_function(vcpu
);
407 u32 val
= PSCI_RET_NOT_SUPPORTED
;
411 case ARM_SMCCC_VERSION_FUNC_ID
:
412 val
= ARM_SMCCC_VERSION_1_1
;
414 case ARM_SMCCC_ARCH_FEATURES_FUNC_ID
:
415 feature
= smccc_get_arg1(vcpu
);
417 case ARM_SMCCC_ARCH_WORKAROUND_1
:
418 if (kvm_arm_harden_branch_predictor())
424 return kvm_psci_call(vcpu
);
427 smccc_set_retval(vcpu
, val
, 0, 0, 0);