]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - virt/kvm/arm/psci.c
arm/arm64: KVM: Implement PSCI 1.0 support
[mirror_ubuntu-artful-kernel.git] / virt / kvm / arm / psci.c
CommitLineData
aa024c2f
MZ
1/*
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
cf5d3188 18#include <linux/preempt.h>
aa024c2f
MZ
19#include <linux/kvm_host.h>
20#include <linux/wait.h>
21
79c64880 22#include <asm/cputype.h>
aa024c2f 23#include <asm/kvm_emulate.h>
4429fc64 24#include <asm/kvm_host.h>
aa024c2f 25
51c1296c
MZ
26#include <kvm/arm_psci.h>
27
aa024c2f
MZ
28/*
29 * This is an implementation of the Power State Coordination Interface
30 * as described in ARM document number ARM DEN 0022A.
31 */
32
e6bc13c8
AP
33#define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
34
153dc8be
MZ
35static u32 smccc_get_function(struct kvm_vcpu *vcpu)
36{
37 return vcpu_get_reg(vcpu, 0);
38}
39
40static unsigned long smccc_get_arg1(struct kvm_vcpu *vcpu)
41{
42 return vcpu_get_reg(vcpu, 1);
43}
44
45static unsigned long smccc_get_arg2(struct kvm_vcpu *vcpu)
46{
47 return vcpu_get_reg(vcpu, 2);
48}
49
50static unsigned long smccc_get_arg3(struct kvm_vcpu *vcpu)
51{
52 return vcpu_get_reg(vcpu, 3);
53}
54
55static void smccc_set_retval(struct kvm_vcpu *vcpu,
56 unsigned long a0,
57 unsigned long a1,
58 unsigned long a2,
59 unsigned long a3)
60{
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);
65}
66
e6bc13c8
AP
67static unsigned long psci_affinity_mask(unsigned long affinity_level)
68{
69 if (affinity_level <= 3)
70 return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
71
72 return 0;
73}
74
b376d02b
AP
75static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
76{
77 /*
78 * NOTE: For simplicity, we make VCPU suspend emulation to be
79 * same-as WFI (Wait-for-interrupt) emulation.
80 *
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).
84 *
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.
89 */
90 kvm_vcpu_block(vcpu);
6a6d73be 91 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
b376d02b
AP
92
93 return PSCI_RET_SUCCESS;
94}
95
aa024c2f
MZ
96static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
97{
3781528e 98 vcpu->arch.power_off = true;
7b244e2b 99 kvm_make_request(KVM_REQ_SLEEP, vcpu);
424c989b 100 kvm_vcpu_kick(vcpu);
aa024c2f
MZ
101}
102
103static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
104{
105 struct kvm *kvm = source_vcpu->kvm;
4429fc64 106 struct kvm_vcpu *vcpu = NULL;
8577370f 107 struct swait_queue_head *wq;
aa024c2f 108 unsigned long cpu_id;
aa8aeefe 109 unsigned long context_id;
aa024c2f
MZ
110 phys_addr_t target_pc;
111
153dc8be 112 cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
aa024c2f
MZ
113 if (vcpu_mode_is_32bit(source_vcpu))
114 cpu_id &= ~((u32) 0);
115
4429fc64 116 vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
79c64880 117
478a8237
CD
118 /*
119 * Make sure the caller requested a valid CPU and that the CPU is
120 * turned off.
121 */
aa8aeefe 122 if (!vcpu)
7d0f84aa 123 return PSCI_RET_INVALID_PARAMS;
3781528e 124 if (!vcpu->arch.power_off) {
aa8aeefe
AP
125 if (kvm_psci_version(source_vcpu) != KVM_ARM_PSCI_0_1)
126 return PSCI_RET_ALREADY_ON;
127 else
128 return PSCI_RET_INVALID_PARAMS;
129 }
aa024c2f 130
153dc8be
MZ
131 target_pc = smccc_get_arg2(source_vcpu);
132 context_id = smccc_get_arg3(source_vcpu);
aa024c2f 133
aa024c2f
MZ
134 kvm_reset_vcpu(vcpu);
135
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);
140 }
141
ce94fe93
MZ
142 /* Propagate caller endianness */
143 if (kvm_vcpu_is_be(source_vcpu))
144 kvm_vcpu_set_be(vcpu);
145
aa024c2f 146 *vcpu_pc(vcpu) = target_pc;
aa8aeefe
AP
147 /*
148 * NOTE: We always update r0 (or x0) because for PSCI v0.1
149 * the general puspose registers are undefined upon CPU_ON.
150 */
153dc8be 151 smccc_set_retval(vcpu, context_id, 0, 0, 0);
3781528e 152 vcpu->arch.power_off = false;
aa024c2f
MZ
153 smp_mb(); /* Make sure the above is visible */
154
478a8237 155 wq = kvm_arch_vcpu_wq(vcpu);
8577370f 156 swake_up(wq);
aa024c2f 157
7d0f84aa 158 return PSCI_RET_SUCCESS;
aa024c2f
MZ
159}
160
e6bc13c8
AP
161static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
162{
0c067292 163 int i, matching_cpus = 0;
e6bc13c8
AP
164 unsigned long mpidr;
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;
170
153dc8be
MZ
171 target_affinity = smccc_get_arg1(vcpu);
172 lowest_affinity_level = smccc_get_arg2(vcpu);
e6bc13c8
AP
173
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;
178
179 /* Ignore other bits of target affinity */
180 target_affinity &= target_affinity_mask;
181
182 /*
183 * If one or more VCPU matching target affinity are running
184 * then ON else OFF
185 */
186 kvm_for_each_vcpu(i, tmp, kvm) {
4429fc64 187 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
0c067292
AS
188 if ((mpidr & target_affinity_mask) == target_affinity) {
189 matching_cpus++;
3781528e 190 if (!tmp->arch.power_off)
0c067292 191 return PSCI_0_2_AFFINITY_LEVEL_ON;
e6bc13c8
AP
192 }
193 }
194
0c067292
AS
195 if (!matching_cpus)
196 return PSCI_RET_INVALID_PARAMS;
197
e6bc13c8
AP
198 return PSCI_0_2_AFFINITY_LEVEL_OFF;
199}
200
4b123826
AP
201static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
202{
cf5d3188
CD
203 int i;
204 struct kvm_vcpu *tmp;
205
206 /*
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
213 * re-initialized.
214 */
cc9b43f9 215 kvm_for_each_vcpu(i, tmp, vcpu->kvm)
3781528e 216 tmp->arch.power_off = true;
7b244e2b 217 kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
cf5d3188 218
4b123826
AP
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;
222}
223
224static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
225{
226 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
227}
228
229static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
230{
231 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
232}
233
7d0f84aa
AP
234int kvm_psci_version(struct kvm_vcpu *vcpu)
235{
236 if (test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features))
6a2b29fa 237 return KVM_ARM_PSCI_LATEST;
7d0f84aa
AP
238
239 return KVM_ARM_PSCI_0_1;
240}
241
e8e7fcc5 242static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
7d0f84aa 243{
6c7a5dce 244 struct kvm *kvm = vcpu->kvm;
153dc8be 245 u32 psci_fn = smccc_get_function(vcpu);
7d0f84aa 246 unsigned long val;
6c7a5dce 247 int ret = 1;
7d0f84aa
AP
248
249 switch (psci_fn) {
250 case PSCI_0_2_FN_PSCI_VERSION:
251 /*
252 * Bits[31:16] = Major Version = 0
253 * Bits[15:0] = Minor Version = 2
254 */
7b40027a 255 val = KVM_ARM_PSCI_0_2;
7d0f84aa 256 break;
b376d02b
AP
257 case PSCI_0_2_FN_CPU_SUSPEND:
258 case PSCI_0_2_FN64_CPU_SUSPEND:
259 val = kvm_psci_vcpu_suspend(vcpu);
260 break;
7d0f84aa
AP
261 case PSCI_0_2_FN_CPU_OFF:
262 kvm_psci_vcpu_off(vcpu);
263 val = PSCI_RET_SUCCESS;
264 break;
265 case PSCI_0_2_FN_CPU_ON:
266 case PSCI_0_2_FN64_CPU_ON:
6c7a5dce 267 mutex_lock(&kvm->lock);
7d0f84aa 268 val = kvm_psci_vcpu_on(vcpu);
6c7a5dce 269 mutex_unlock(&kvm->lock);
7d0f84aa 270 break;
e6bc13c8
AP
271 case PSCI_0_2_FN_AFFINITY_INFO:
272 case PSCI_0_2_FN64_AFFINITY_INFO:
273 val = kvm_psci_vcpu_affinity_info(vcpu);
274 break;
bab0b430
AP
275 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
276 /*
277 * Trusted OS is MP hence does not require migration
278 * or
279 * Trusted OS is not present
280 */
281 val = PSCI_0_2_TOS_MP;
282 break;
4b123826
AP
283 case PSCI_0_2_FN_SYSTEM_OFF:
284 kvm_psci_system_off(vcpu);
285 /*
286 * We should'nt be going back to guest VCPU after
287 * receiving SYSTEM_OFF request.
288 *
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.
294 */
295 val = PSCI_RET_INTERNAL_FAILURE;
296 ret = 0;
297 break;
298 case PSCI_0_2_FN_SYSTEM_RESET:
299 kvm_psci_system_reset(vcpu);
300 /*
301 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
302 * with PSCI return value INTERNAL_FAILURE.
303 */
304 val = PSCI_RET_INTERNAL_FAILURE;
305 ret = 0;
306 break;
7d0f84aa 307 default:
e2d99736
LP
308 val = PSCI_RET_NOT_SUPPORTED;
309 break;
7d0f84aa
AP
310 }
311
153dc8be 312 smccc_set_retval(vcpu, val, 0, 0, 0);
4b123826 313 return ret;
7d0f84aa
AP
314}
315
6a2b29fa
MZ
316static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu)
317{
318 u32 psci_fn = smccc_get_function(vcpu);
319 u32 feature;
320 unsigned long val;
321 int ret = 1;
322
323 switch(psci_fn) {
324 case PSCI_0_2_FN_PSCI_VERSION:
325 val = KVM_ARM_PSCI_1_0;
326 break;
327 case PSCI_1_0_FN_PSCI_FEATURES:
328 feature = smccc_get_arg1(vcpu);
329 switch(feature) {
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:
342 val = 0;
343 break;
344 default:
345 val = PSCI_RET_NOT_SUPPORTED;
346 break;
347 }
348 break;
349 default:
350 return kvm_psci_0_2_call(vcpu);
351 }
352
353 smccc_set_retval(vcpu, val, 0, 0, 0);
354 return ret;
355}
356
e8e7fcc5 357static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
aa024c2f 358{
6c7a5dce 359 struct kvm *kvm = vcpu->kvm;
153dc8be 360 u32 psci_fn = smccc_get_function(vcpu);
aa024c2f
MZ
361 unsigned long val;
362
363 switch (psci_fn) {
364 case KVM_PSCI_FN_CPU_OFF:
365 kvm_psci_vcpu_off(vcpu);
7d0f84aa 366 val = PSCI_RET_SUCCESS;
aa024c2f
MZ
367 break;
368 case KVM_PSCI_FN_CPU_ON:
6c7a5dce 369 mutex_lock(&kvm->lock);
aa024c2f 370 val = kvm_psci_vcpu_on(vcpu);
6c7a5dce 371 mutex_unlock(&kvm->lock);
aa024c2f 372 break;
e2d99736 373 default:
7d0f84aa 374 val = PSCI_RET_NOT_SUPPORTED;
aa024c2f 375 break;
aa024c2f
MZ
376 }
377
153dc8be 378 smccc_set_retval(vcpu, val, 0, 0, 0);
e8e7fcc5 379 return 1;
aa024c2f 380}
7d0f84aa
AP
381
382/**
383 * kvm_psci_call - handle PSCI call if r0 value is in range
384 * @vcpu: Pointer to the VCPU struct
385 *
386 * Handle PSCI calls from guests through traps from HVC instructions.
e8e7fcc5
AP
387 * The calling convention is similar to SMC calls to the secure world
388 * where the function number is placed in r0.
389 *
390 * This function returns: > 0 (success), 0 (success but exit to user
391 * space), and < 0 (errors)
392 *
393 * Errors:
394 * -EINVAL: Unrecognized PSCI function
7d0f84aa 395 */
e8e7fcc5 396int kvm_psci_call(struct kvm_vcpu *vcpu)
7d0f84aa
AP
397{
398 switch (kvm_psci_version(vcpu)) {
6a2b29fa
MZ
399 case KVM_ARM_PSCI_1_0:
400 return kvm_psci_1_0_call(vcpu);
7d0f84aa
AP
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);
405 default:
e8e7fcc5 406 return -EINVAL;
7d0f84aa
AP
407 };
408}