blob: 3d962257c166fcb4501ed0bc3041433012059370 [file] [log] [blame]
Marc Zyngieraa024c2f2013-01-20 18:28:13 -05001/*
2 * Copyright (C) 2012 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
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.
8 *
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.
13 *
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/>.
16 */
17
Mark Rutland6681f3c2018-04-12 12:11:28 +010018#include <linux/arm-smccc.h>
Christoffer Dallcf5d31882014-10-16 17:00:18 +020019#include <linux/preempt.h>
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050020#include <linux/kvm_host.h>
21#include <linux/wait.h>
22
Marc Zyngier79c64882013-10-18 18:19:03 +010023#include <asm/cputype.h>
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050024#include <asm/kvm_emulate.h>
Andre Przywara4429fc62014-06-02 15:37:13 +020025#include <asm/kvm_host.h>
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050026
Mark Rutland8b106af2018-04-12 12:11:24 +010027#include <kvm/arm_psci.h>
28
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050029/*
30 * This is an implementation of the Power State Coordination Interface
31 * as described in ARM document number ARM DEN 0022A.
32 */
33
Anup Patele6bc13c2014-04-29 11:24:21 +053034#define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
35
Mark Rutland54faafb2018-04-12 12:11:26 +010036static u32 smccc_get_function(struct kvm_vcpu *vcpu)
37{
38 return vcpu_get_reg(vcpu, 0);
39}
40
41static unsigned long smccc_get_arg1(struct kvm_vcpu *vcpu)
42{
43 return vcpu_get_reg(vcpu, 1);
44}
45
46static unsigned long smccc_get_arg2(struct kvm_vcpu *vcpu)
47{
48 return vcpu_get_reg(vcpu, 2);
49}
50
51static unsigned long smccc_get_arg3(struct kvm_vcpu *vcpu)
52{
53 return vcpu_get_reg(vcpu, 3);
54}
55
56static void smccc_set_retval(struct kvm_vcpu *vcpu,
57 unsigned long a0,
58 unsigned long a1,
59 unsigned long a2,
60 unsigned long a3)
61{
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);
66}
67
Anup Patele6bc13c2014-04-29 11:24:21 +053068static unsigned long psci_affinity_mask(unsigned long affinity_level)
69{
70 if (affinity_level <= 3)
71 return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
72
73 return 0;
74}
75
Anup Patelb376d022014-04-29 11:24:24 +053076static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
77{
78 /*
79 * NOTE: For simplicity, we make VCPU suspend emulation to be
80 * same-as WFI (Wait-for-interrupt) emulation.
81 *
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).
85 *
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.
90 */
91 kvm_vcpu_block(vcpu);
92
93 return PSCI_RET_SUCCESS;
94}
95
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050096static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
97{
Eric Auger37815282015-09-25 23:41:14 +020098 vcpu->arch.power_off = true;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050099}
100
101static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
102{
103 struct kvm *kvm = source_vcpu->kvm;
Andre Przywara4429fc62014-06-02 15:37:13 +0200104 struct kvm_vcpu *vcpu = NULL;
Marcelo Tosatti85773702016-02-19 09:46:39 +0100105 struct swait_queue_head *wq;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500106 unsigned long cpu_id;
Anup Patelaa8aeef2014-04-29 11:24:23 +0530107 unsigned long context_id;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500108 phys_addr_t target_pc;
109
Mark Rutland54faafb2018-04-12 12:11:26 +0100110 cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500111 if (vcpu_mode_is_32bit(source_vcpu))
112 cpu_id &= ~((u32) 0);
113
Andre Przywara4429fc62014-06-02 15:37:13 +0200114 vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
Marc Zyngier79c64882013-10-18 18:19:03 +0100115
Christoffer Dall478a8232013-11-19 17:43:19 -0800116 /*
117 * Make sure the caller requested a valid CPU and that the CPU is
118 * turned off.
119 */
Anup Patelaa8aeef2014-04-29 11:24:23 +0530120 if (!vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +0530121 return PSCI_RET_INVALID_PARAMS;
Eric Auger37815282015-09-25 23:41:14 +0200122 if (!vcpu->arch.power_off) {
Mark Rutland142cfd62018-04-12 12:11:30 +0100123 if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1)
Anup Patelaa8aeef2014-04-29 11:24:23 +0530124 return PSCI_RET_ALREADY_ON;
125 else
126 return PSCI_RET_INVALID_PARAMS;
127 }
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500128
Mark Rutland54faafb2018-04-12 12:11:26 +0100129 target_pc = smccc_get_arg2(source_vcpu);
130 context_id = smccc_get_arg3(source_vcpu);
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500131
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500132 kvm_reset_vcpu(vcpu);
133
134 /* Gracefully handle Thumb2 entry point */
135 if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
136 target_pc &= ~((phys_addr_t) 1);
137 vcpu_set_thumb(vcpu);
138 }
139
Marc Zyngierce94fe92013-11-05 14:12:15 +0000140 /* Propagate caller endianness */
141 if (kvm_vcpu_is_be(source_vcpu))
142 kvm_vcpu_set_be(vcpu);
143
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500144 *vcpu_pc(vcpu) = target_pc;
Anup Patelaa8aeef2014-04-29 11:24:23 +0530145 /*
146 * NOTE: We always update r0 (or x0) because for PSCI v0.1
147 * the general puspose registers are undefined upon CPU_ON.
148 */
Mark Rutland54faafb2018-04-12 12:11:26 +0100149 smccc_set_retval(vcpu, context_id, 0, 0, 0);
Eric Auger37815282015-09-25 23:41:14 +0200150 vcpu->arch.power_off = false;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500151 smp_mb(); /* Make sure the above is visible */
152
Christoffer Dall478a8232013-11-19 17:43:19 -0800153 wq = kvm_arch_vcpu_wq(vcpu);
Marcelo Tosatti85773702016-02-19 09:46:39 +0100154 swake_up(wq);
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500155
Anup Patel7d0f84a2014-04-29 11:24:16 +0530156 return PSCI_RET_SUCCESS;
157}
158
Anup Patele6bc13c2014-04-29 11:24:21 +0530159static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
160{
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200161 int i, matching_cpus = 0;
Anup Patele6bc13c2014-04-29 11:24:21 +0530162 unsigned long mpidr;
163 unsigned long target_affinity;
164 unsigned long target_affinity_mask;
165 unsigned long lowest_affinity_level;
166 struct kvm *kvm = vcpu->kvm;
167 struct kvm_vcpu *tmp;
168
Mark Rutland54faafb2018-04-12 12:11:26 +0100169 target_affinity = smccc_get_arg1(vcpu);
170 lowest_affinity_level = smccc_get_arg2(vcpu);
Anup Patele6bc13c2014-04-29 11:24:21 +0530171
172 /* Determine target affinity mask */
173 target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
174 if (!target_affinity_mask)
175 return PSCI_RET_INVALID_PARAMS;
176
177 /* Ignore other bits of target affinity */
178 target_affinity &= target_affinity_mask;
179
180 /*
181 * If one or more VCPU matching target affinity are running
182 * then ON else OFF
183 */
184 kvm_for_each_vcpu(i, tmp, kvm) {
Andre Przywara4429fc62014-06-02 15:37:13 +0200185 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200186 if ((mpidr & target_affinity_mask) == target_affinity) {
187 matching_cpus++;
Eric Auger37815282015-09-25 23:41:14 +0200188 if (!tmp->arch.power_off)
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200189 return PSCI_0_2_AFFINITY_LEVEL_ON;
Anup Patele6bc13c2014-04-29 11:24:21 +0530190 }
191 }
192
Alexander Spyridakis0c067292015-09-04 17:06:24 +0200193 if (!matching_cpus)
194 return PSCI_RET_INVALID_PARAMS;
195
Anup Patele6bc13c2014-04-29 11:24:21 +0530196 return PSCI_0_2_AFFINITY_LEVEL_OFF;
197}
198
Anup Patel4b123822014-04-29 11:24:20 +0530199static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
200{
Christoffer Dallcf5d31882014-10-16 17:00:18 +0200201 int i;
202 struct kvm_vcpu *tmp;
203
204 /*
205 * The KVM ABI specifies that a system event exit may call KVM_RUN
206 * again and may perform shutdown/reboot at a later time that when the
207 * actual request is made. Since we are implementing PSCI and a
208 * caller of PSCI reboot and shutdown expects that the system shuts
209 * down or reboots immediately, let's make sure that VCPUs are not run
210 * after this call is handled and before the VCPUs have been
211 * re-initialized.
212 */
213 kvm_for_each_vcpu(i, tmp, vcpu->kvm) {
Eric Auger37815282015-09-25 23:41:14 +0200214 tmp->arch.power_off = true;
Christoffer Dallcf5d31882014-10-16 17:00:18 +0200215 kvm_vcpu_kick(tmp);
216 }
217
Anup Patel4b123822014-04-29 11:24:20 +0530218 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
219 vcpu->run->system_event.type = type;
220 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
221}
222
223static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
224{
225 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
226}
227
228static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
229{
230 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
231}
232
Anup Patele8e7fcc2014-04-29 11:24:18 +0530233static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +0530234{
Andrew Jones7b0d4392017-04-18 17:59:58 +0200235 struct kvm *kvm = vcpu->kvm;
Mark Rutland54faafb2018-04-12 12:11:26 +0100236 unsigned long psci_fn = smccc_get_function(vcpu);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530237 unsigned long val;
Andrew Jones7b0d4392017-04-18 17:59:58 +0200238 int ret = 1;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530239
240 switch (psci_fn) {
241 case PSCI_0_2_FN_PSCI_VERSION:
242 /*
243 * Bits[31:16] = Major Version = 0
244 * Bits[15:0] = Minor Version = 2
245 */
Mark Rutland33e64842018-04-12 12:11:25 +0100246 val = KVM_ARM_PSCI_0_2;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530247 break;
Anup Patelb376d022014-04-29 11:24:24 +0530248 case PSCI_0_2_FN_CPU_SUSPEND:
249 case PSCI_0_2_FN64_CPU_SUSPEND:
250 val = kvm_psci_vcpu_suspend(vcpu);
251 break;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530252 case PSCI_0_2_FN_CPU_OFF:
253 kvm_psci_vcpu_off(vcpu);
254 val = PSCI_RET_SUCCESS;
255 break;
256 case PSCI_0_2_FN_CPU_ON:
257 case PSCI_0_2_FN64_CPU_ON:
Andrew Jones7b0d4392017-04-18 17:59:58 +0200258 mutex_lock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530259 val = kvm_psci_vcpu_on(vcpu);
Andrew Jones7b0d4392017-04-18 17:59:58 +0200260 mutex_unlock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530261 break;
Anup Patele6bc13c2014-04-29 11:24:21 +0530262 case PSCI_0_2_FN_AFFINITY_INFO:
263 case PSCI_0_2_FN64_AFFINITY_INFO:
264 val = kvm_psci_vcpu_affinity_info(vcpu);
265 break;
Anup Patelbab0b4302014-04-29 11:24:22 +0530266 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
267 /*
268 * Trusted OS is MP hence does not require migration
269 * or
270 * Trusted OS is not present
271 */
272 val = PSCI_0_2_TOS_MP;
273 break;
Anup Patel4b123822014-04-29 11:24:20 +0530274 case PSCI_0_2_FN_SYSTEM_OFF:
275 kvm_psci_system_off(vcpu);
276 /*
277 * We should'nt be going back to guest VCPU after
278 * receiving SYSTEM_OFF request.
279 *
280 * If user space accidently/deliberately resumes
281 * guest VCPU after SYSTEM_OFF request then guest
282 * VCPU should see internal failure from PSCI return
283 * value. To achieve this, we preload r0 (or x0) with
284 * PSCI return value INTERNAL_FAILURE.
285 */
286 val = PSCI_RET_INTERNAL_FAILURE;
287 ret = 0;
288 break;
289 case PSCI_0_2_FN_SYSTEM_RESET:
290 kvm_psci_system_reset(vcpu);
291 /*
292 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
293 * with PSCI return value INTERNAL_FAILURE.
294 */
295 val = PSCI_RET_INTERNAL_FAILURE;
296 ret = 0;
297 break;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530298 default:
Lorenzo Pieralisie2d99732015-06-10 15:19:24 +0100299 val = PSCI_RET_NOT_SUPPORTED;
300 break;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530301 }
302
Mark Rutland54faafb2018-04-12 12:11:26 +0100303 smccc_set_retval(vcpu, val, 0, 0, 0);
Anup Patel4b123822014-04-29 11:24:20 +0530304 return ret;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530305}
306
Mark Rutland4b1713f2018-04-12 12:11:27 +0100307static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu)
308{
309 u32 psci_fn = smccc_get_function(vcpu);
310 u32 feature;
311 unsigned long val;
312 int ret = 1;
313
314 switch(psci_fn) {
315 case PSCI_0_2_FN_PSCI_VERSION:
316 val = KVM_ARM_PSCI_1_0;
317 break;
318 case PSCI_1_0_FN_PSCI_FEATURES:
319 feature = smccc_get_arg1(vcpu);
320 switch(feature) {
321 case PSCI_0_2_FN_PSCI_VERSION:
322 case PSCI_0_2_FN_CPU_SUSPEND:
323 case PSCI_0_2_FN64_CPU_SUSPEND:
324 case PSCI_0_2_FN_CPU_OFF:
325 case PSCI_0_2_FN_CPU_ON:
326 case PSCI_0_2_FN64_CPU_ON:
327 case PSCI_0_2_FN_AFFINITY_INFO:
328 case PSCI_0_2_FN64_AFFINITY_INFO:
329 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
330 case PSCI_0_2_FN_SYSTEM_OFF:
331 case PSCI_0_2_FN_SYSTEM_RESET:
332 case PSCI_1_0_FN_PSCI_FEATURES:
Mark Rutland6681f3c2018-04-12 12:11:28 +0100333 case ARM_SMCCC_VERSION_FUNC_ID:
Mark Rutland4b1713f2018-04-12 12:11:27 +0100334 val = 0;
335 break;
336 default:
337 val = PSCI_RET_NOT_SUPPORTED;
338 break;
339 }
340 break;
341 default:
342 return kvm_psci_0_2_call(vcpu);
343 }
344
345 smccc_set_retval(vcpu, val, 0, 0, 0);
346 return ret;
347}
348
Anup Patele8e7fcc2014-04-29 11:24:18 +0530349static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +0530350{
Andrew Jones7b0d4392017-04-18 17:59:58 +0200351 struct kvm *kvm = vcpu->kvm;
Mark Rutland54faafb2018-04-12 12:11:26 +0100352 unsigned long psci_fn = smccc_get_function(vcpu);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530353 unsigned long val;
354
355 switch (psci_fn) {
356 case KVM_PSCI_FN_CPU_OFF:
357 kvm_psci_vcpu_off(vcpu);
358 val = PSCI_RET_SUCCESS;
359 break;
360 case KVM_PSCI_FN_CPU_ON:
Andrew Jones7b0d4392017-04-18 17:59:58 +0200361 mutex_lock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530362 val = kvm_psci_vcpu_on(vcpu);
Andrew Jones7b0d4392017-04-18 17:59:58 +0200363 mutex_unlock(&kvm->lock);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530364 break;
Lorenzo Pieralisie2d99732015-06-10 15:19:24 +0100365 default:
Anup Patel7d0f84a2014-04-29 11:24:16 +0530366 val = PSCI_RET_NOT_SUPPORTED;
367 break;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530368 }
369
Mark Rutland54faafb2018-04-12 12:11:26 +0100370 smccc_set_retval(vcpu, val, 0, 0, 0);
Anup Patele8e7fcc2014-04-29 11:24:18 +0530371 return 1;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500372}
373
374/**
375 * kvm_psci_call - handle PSCI call if r0 value is in range
376 * @vcpu: Pointer to the VCPU struct
377 *
Dave P Martin24a7f672013-05-01 17:49:28 +0100378 * Handle PSCI calls from guests through traps from HVC instructions.
Anup Patele8e7fcc2014-04-29 11:24:18 +0530379 * The calling convention is similar to SMC calls to the secure world
380 * where the function number is placed in r0.
381 *
382 * This function returns: > 0 (success), 0 (success but exit to user
383 * space), and < 0 (errors)
384 *
385 * Errors:
386 * -EINVAL: Unrecognized PSCI function
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500387 */
Mark Rutland6681f3c2018-04-12 12:11:28 +0100388static int kvm_psci_call(struct kvm_vcpu *vcpu)
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500389{
Mark Rutland142cfd62018-04-12 12:11:30 +0100390 switch (kvm_psci_version(vcpu, vcpu->kvm)) {
Mark Rutland4b1713f2018-04-12 12:11:27 +0100391 case KVM_ARM_PSCI_1_0:
392 return kvm_psci_1_0_call(vcpu);
Anup Patel7d0f84a2014-04-29 11:24:16 +0530393 case KVM_ARM_PSCI_0_2:
394 return kvm_psci_0_2_call(vcpu);
395 case KVM_ARM_PSCI_0_1:
396 return kvm_psci_0_1_call(vcpu);
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500397 default:
Anup Patele8e7fcc2014-04-29 11:24:18 +0530398 return -EINVAL;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530399 };
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500400}
Mark Rutland6681f3c2018-04-12 12:11:28 +0100401
402int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)
403{
404 u32 func_id = smccc_get_function(vcpu);
405 u32 val = PSCI_RET_NOT_SUPPORTED;
Mark Rutlandc9ae3d52018-04-12 12:11:31 +0100406 u32 feature;
Mark Rutland6681f3c2018-04-12 12:11:28 +0100407
408 switch (func_id) {
409 case ARM_SMCCC_VERSION_FUNC_ID:
410 val = ARM_SMCCC_VERSION_1_1;
411 break;
412 case ARM_SMCCC_ARCH_FEATURES_FUNC_ID:
Mark Rutlandc9ae3d52018-04-12 12:11:31 +0100413 feature = smccc_get_arg1(vcpu);
414 switch(feature) {
415 case ARM_SMCCC_ARCH_WORKAROUND_1:
416 if (kvm_arm_harden_branch_predictor())
417 val = 0;
418 break;
419 }
Mark Rutland6681f3c2018-04-12 12:11:28 +0100420 break;
421 default:
422 return kvm_psci_call(vcpu);
423 }
424
425 smccc_set_retval(vcpu, val, 0, 0, 0);
426 return 1;
427}