Merge tag 'pm-5.5-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-2.6-microblaze.git] / virt / kvm / arm / psci.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6
7 #include <linux/arm-smccc.h>
8 #include <linux/preempt.h>
9 #include <linux/kvm_host.h>
10 #include <linux/uaccess.h>
11 #include <linux/wait.h>
12
13 #include <asm/cputype.h>
14 #include <asm/kvm_emulate.h>
15 #include <asm/kvm_host.h>
16
17 #include <kvm/arm_psci.h>
18 #include <kvm/arm_hypercalls.h>
19
20 /*
21  * This is an implementation of the Power State Coordination Interface
22  * as described in ARM document number ARM DEN 0022A.
23  */
24
25 #define AFFINITY_MASK(level)    ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
26
27 static unsigned long psci_affinity_mask(unsigned long affinity_level)
28 {
29         if (affinity_level <= 3)
30                 return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
31
32         return 0;
33 }
34
35 static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
36 {
37         /*
38          * NOTE: For simplicity, we make VCPU suspend emulation to be
39          * same-as WFI (Wait-for-interrupt) emulation.
40          *
41          * This means for KVM the wakeup events are interrupts and
42          * this is consistent with intended use of StateID as described
43          * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A).
44          *
45          * Further, we also treat power-down request to be same as
46          * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2
47          * specification (ARM DEN 0022A). This means all suspend states
48          * for KVM will preserve the register state.
49          */
50         kvm_vcpu_block(vcpu);
51         kvm_clear_request(KVM_REQ_UNHALT, vcpu);
52
53         return PSCI_RET_SUCCESS;
54 }
55
56 static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
57 {
58         vcpu->arch.power_off = true;
59         kvm_make_request(KVM_REQ_SLEEP, vcpu);
60         kvm_vcpu_kick(vcpu);
61 }
62
63 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
64 {
65         struct vcpu_reset_state *reset_state;
66         struct kvm *kvm = source_vcpu->kvm;
67         struct kvm_vcpu *vcpu = NULL;
68         unsigned long cpu_id;
69
70         cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
71         if (vcpu_mode_is_32bit(source_vcpu))
72                 cpu_id &= ~((u32) 0);
73
74         vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
75
76         /*
77          * Make sure the caller requested a valid CPU and that the CPU is
78          * turned off.
79          */
80         if (!vcpu)
81                 return PSCI_RET_INVALID_PARAMS;
82         if (!vcpu->arch.power_off) {
83                 if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1)
84                         return PSCI_RET_ALREADY_ON;
85                 else
86                         return PSCI_RET_INVALID_PARAMS;
87         }
88
89         reset_state = &vcpu->arch.reset_state;
90
91         reset_state->pc = smccc_get_arg2(source_vcpu);
92
93         /* Propagate caller endianness */
94         reset_state->be = kvm_vcpu_is_be(source_vcpu);
95
96         /*
97          * NOTE: We always update r0 (or x0) because for PSCI v0.1
98          * the general puspose registers are undefined upon CPU_ON.
99          */
100         reset_state->r0 = smccc_get_arg3(source_vcpu);
101
102         WRITE_ONCE(reset_state->reset, true);
103         kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
104
105         /*
106          * Make sure the reset request is observed if the change to
107          * power_state is observed.
108          */
109         smp_wmb();
110
111         vcpu->arch.power_off = false;
112         kvm_vcpu_wake_up(vcpu);
113
114         return PSCI_RET_SUCCESS;
115 }
116
117 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
118 {
119         int i, matching_cpus = 0;
120         unsigned long mpidr;
121         unsigned long target_affinity;
122         unsigned long target_affinity_mask;
123         unsigned long lowest_affinity_level;
124         struct kvm *kvm = vcpu->kvm;
125         struct kvm_vcpu *tmp;
126
127         target_affinity = smccc_get_arg1(vcpu);
128         lowest_affinity_level = smccc_get_arg2(vcpu);
129
130         /* Determine target affinity mask */
131         target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
132         if (!target_affinity_mask)
133                 return PSCI_RET_INVALID_PARAMS;
134
135         /* Ignore other bits of target affinity */
136         target_affinity &= target_affinity_mask;
137
138         /*
139          * If one or more VCPU matching target affinity are running
140          * then ON else OFF
141          */
142         kvm_for_each_vcpu(i, tmp, kvm) {
143                 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
144                 if ((mpidr & target_affinity_mask) == target_affinity) {
145                         matching_cpus++;
146                         if (!tmp->arch.power_off)
147                                 return PSCI_0_2_AFFINITY_LEVEL_ON;
148                 }
149         }
150
151         if (!matching_cpus)
152                 return PSCI_RET_INVALID_PARAMS;
153
154         return PSCI_0_2_AFFINITY_LEVEL_OFF;
155 }
156
157 static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
158 {
159         int i;
160         struct kvm_vcpu *tmp;
161
162         /*
163          * The KVM ABI specifies that a system event exit may call KVM_RUN
164          * again and may perform shutdown/reboot at a later time that when the
165          * actual request is made.  Since we are implementing PSCI and a
166          * caller of PSCI reboot and shutdown expects that the system shuts
167          * down or reboots immediately, let's make sure that VCPUs are not run
168          * after this call is handled and before the VCPUs have been
169          * re-initialized.
170          */
171         kvm_for_each_vcpu(i, tmp, vcpu->kvm)
172                 tmp->arch.power_off = true;
173         kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
174
175         memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
176         vcpu->run->system_event.type = type;
177         vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
178 }
179
180 static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
181 {
182         kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
183 }
184
185 static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
186 {
187         kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
188 }
189
190 static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
191 {
192         struct kvm *kvm = vcpu->kvm;
193         u32 psci_fn = smccc_get_function(vcpu);
194         unsigned long val;
195         int ret = 1;
196
197         switch (psci_fn) {
198         case PSCI_0_2_FN_PSCI_VERSION:
199                 /*
200                  * Bits[31:16] = Major Version = 0
201                  * Bits[15:0] = Minor Version = 2
202                  */
203                 val = KVM_ARM_PSCI_0_2;
204                 break;
205         case PSCI_0_2_FN_CPU_SUSPEND:
206         case PSCI_0_2_FN64_CPU_SUSPEND:
207                 val = kvm_psci_vcpu_suspend(vcpu);
208                 break;
209         case PSCI_0_2_FN_CPU_OFF:
210                 kvm_psci_vcpu_off(vcpu);
211                 val = PSCI_RET_SUCCESS;
212                 break;
213         case PSCI_0_2_FN_CPU_ON:
214         case PSCI_0_2_FN64_CPU_ON:
215                 mutex_lock(&kvm->lock);
216                 val = kvm_psci_vcpu_on(vcpu);
217                 mutex_unlock(&kvm->lock);
218                 break;
219         case PSCI_0_2_FN_AFFINITY_INFO:
220         case PSCI_0_2_FN64_AFFINITY_INFO:
221                 val = kvm_psci_vcpu_affinity_info(vcpu);
222                 break;
223         case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
224                 /*
225                  * Trusted OS is MP hence does not require migration
226                  * or
227                  * Trusted OS is not present
228                  */
229                 val = PSCI_0_2_TOS_MP;
230                 break;
231         case PSCI_0_2_FN_SYSTEM_OFF:
232                 kvm_psci_system_off(vcpu);
233                 /*
234                  * We should'nt be going back to guest VCPU after
235                  * receiving SYSTEM_OFF request.
236                  *
237                  * If user space accidently/deliberately resumes
238                  * guest VCPU after SYSTEM_OFF request then guest
239                  * VCPU should see internal failure from PSCI return
240                  * value. To achieve this, we preload r0 (or x0) with
241                  * PSCI return value INTERNAL_FAILURE.
242                  */
243                 val = PSCI_RET_INTERNAL_FAILURE;
244                 ret = 0;
245                 break;
246         case PSCI_0_2_FN_SYSTEM_RESET:
247                 kvm_psci_system_reset(vcpu);
248                 /*
249                  * Same reason as SYSTEM_OFF for preloading r0 (or x0)
250                  * with PSCI return value INTERNAL_FAILURE.
251                  */
252                 val = PSCI_RET_INTERNAL_FAILURE;
253                 ret = 0;
254                 break;
255         default:
256                 val = PSCI_RET_NOT_SUPPORTED;
257                 break;
258         }
259
260         smccc_set_retval(vcpu, val, 0, 0, 0);
261         return ret;
262 }
263
264 static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu)
265 {
266         u32 psci_fn = smccc_get_function(vcpu);
267         u32 feature;
268         unsigned long val;
269         int ret = 1;
270
271         switch(psci_fn) {
272         case PSCI_0_2_FN_PSCI_VERSION:
273                 val = KVM_ARM_PSCI_1_0;
274                 break;
275         case PSCI_1_0_FN_PSCI_FEATURES:
276                 feature = smccc_get_arg1(vcpu);
277                 switch(feature) {
278                 case PSCI_0_2_FN_PSCI_VERSION:
279                 case PSCI_0_2_FN_CPU_SUSPEND:
280                 case PSCI_0_2_FN64_CPU_SUSPEND:
281                 case PSCI_0_2_FN_CPU_OFF:
282                 case PSCI_0_2_FN_CPU_ON:
283                 case PSCI_0_2_FN64_CPU_ON:
284                 case PSCI_0_2_FN_AFFINITY_INFO:
285                 case PSCI_0_2_FN64_AFFINITY_INFO:
286                 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
287                 case PSCI_0_2_FN_SYSTEM_OFF:
288                 case PSCI_0_2_FN_SYSTEM_RESET:
289                 case PSCI_1_0_FN_PSCI_FEATURES:
290                 case ARM_SMCCC_VERSION_FUNC_ID:
291                         val = 0;
292                         break;
293                 default:
294                         val = PSCI_RET_NOT_SUPPORTED;
295                         break;
296                 }
297                 break;
298         default:
299                 return kvm_psci_0_2_call(vcpu);
300         }
301
302         smccc_set_retval(vcpu, val, 0, 0, 0);
303         return ret;
304 }
305
306 static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
307 {
308         struct kvm *kvm = vcpu->kvm;
309         u32 psci_fn = smccc_get_function(vcpu);
310         unsigned long val;
311
312         switch (psci_fn) {
313         case KVM_PSCI_FN_CPU_OFF:
314                 kvm_psci_vcpu_off(vcpu);
315                 val = PSCI_RET_SUCCESS;
316                 break;
317         case KVM_PSCI_FN_CPU_ON:
318                 mutex_lock(&kvm->lock);
319                 val = kvm_psci_vcpu_on(vcpu);
320                 mutex_unlock(&kvm->lock);
321                 break;
322         default:
323                 val = PSCI_RET_NOT_SUPPORTED;
324                 break;
325         }
326
327         smccc_set_retval(vcpu, val, 0, 0, 0);
328         return 1;
329 }
330
331 /**
332  * kvm_psci_call - handle PSCI call if r0 value is in range
333  * @vcpu: Pointer to the VCPU struct
334  *
335  * Handle PSCI calls from guests through traps from HVC instructions.
336  * The calling convention is similar to SMC calls to the secure world
337  * where the function number is placed in r0.
338  *
339  * This function returns: > 0 (success), 0 (success but exit to user
340  * space), and < 0 (errors)
341  *
342  * Errors:
343  * -EINVAL: Unrecognized PSCI function
344  */
345 int kvm_psci_call(struct kvm_vcpu *vcpu)
346 {
347         switch (kvm_psci_version(vcpu, vcpu->kvm)) {
348         case KVM_ARM_PSCI_1_0:
349                 return kvm_psci_1_0_call(vcpu);
350         case KVM_ARM_PSCI_0_2:
351                 return kvm_psci_0_2_call(vcpu);
352         case KVM_ARM_PSCI_0_1:
353                 return kvm_psci_0_1_call(vcpu);
354         default:
355                 return -EINVAL;
356         };
357 }
358
359 int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)
360 {
361         return 3;               /* PSCI version and two workaround registers */
362 }
363
364 int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
365 {
366         if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices++))
367                 return -EFAULT;
368
369         if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, uindices++))
370                 return -EFAULT;
371
372         if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, uindices++))
373                 return -EFAULT;
374
375         return 0;
376 }
377
378 #define KVM_REG_FEATURE_LEVEL_WIDTH     4
379 #define KVM_REG_FEATURE_LEVEL_MASK      (BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1)
380
381 /*
382  * Convert the workaround level into an easy-to-compare number, where higher
383  * values mean better protection.
384  */
385 static int get_kernel_wa_level(u64 regid)
386 {
387         switch (regid) {
388         case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
389                 switch (kvm_arm_harden_branch_predictor()) {
390                 case KVM_BP_HARDEN_UNKNOWN:
391                         return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
392                 case KVM_BP_HARDEN_WA_NEEDED:
393                         return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL;
394                 case KVM_BP_HARDEN_NOT_REQUIRED:
395                         return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED;
396                 }
397                 return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
398         case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
399                 switch (kvm_arm_have_ssbd()) {
400                 case KVM_SSBD_FORCE_DISABLE:
401                         return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL;
402                 case KVM_SSBD_KERNEL:
403                         return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL;
404                 case KVM_SSBD_FORCE_ENABLE:
405                 case KVM_SSBD_MITIGATED:
406                         return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED;
407                 case KVM_SSBD_UNKNOWN:
408                 default:
409                         return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN;
410                 }
411         }
412
413         return -EINVAL;
414 }
415
416 int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
417 {
418         void __user *uaddr = (void __user *)(long)reg->addr;
419         u64 val;
420
421         switch (reg->id) {
422         case KVM_REG_ARM_PSCI_VERSION:
423                 val = kvm_psci_version(vcpu, vcpu->kvm);
424                 break;
425         case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
426                 val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK;
427                 break;
428         case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
429                 val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK;
430
431                 if (val == KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL &&
432                     kvm_arm_get_vcpu_workaround_2_flag(vcpu))
433                         val |= KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED;
434                 break;
435         default:
436                 return -ENOENT;
437         }
438
439         if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)))
440                 return -EFAULT;
441
442         return 0;
443 }
444
445 int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
446 {
447         void __user *uaddr = (void __user *)(long)reg->addr;
448         u64 val;
449         int wa_level;
450
451         if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id)))
452                 return -EFAULT;
453
454         switch (reg->id) {
455         case KVM_REG_ARM_PSCI_VERSION:
456         {
457                 bool wants_02;
458
459                 wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features);
460
461                 switch (val) {
462                 case KVM_ARM_PSCI_0_1:
463                         if (wants_02)
464                                 return -EINVAL;
465                         vcpu->kvm->arch.psci_version = val;
466                         return 0;
467                 case KVM_ARM_PSCI_0_2:
468                 case KVM_ARM_PSCI_1_0:
469                         if (!wants_02)
470                                 return -EINVAL;
471                         vcpu->kvm->arch.psci_version = val;
472                         return 0;
473                 }
474                 break;
475         }
476
477         case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
478                 if (val & ~KVM_REG_FEATURE_LEVEL_MASK)
479                         return -EINVAL;
480
481                 if (get_kernel_wa_level(reg->id) < val)
482                         return -EINVAL;
483
484                 return 0;
485
486         case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
487                 if (val & ~(KVM_REG_FEATURE_LEVEL_MASK |
488                             KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED))
489                         return -EINVAL;
490
491                 wa_level = val & KVM_REG_FEATURE_LEVEL_MASK;
492
493                 if (get_kernel_wa_level(reg->id) < wa_level)
494                         return -EINVAL;
495
496                 /* The enabled bit must not be set unless the level is AVAIL. */
497                 if (wa_level != KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL &&
498                     wa_level != val)
499                         return -EINVAL;
500
501                 /* Are we finished or do we need to check the enable bit ? */
502                 if (kvm_arm_have_ssbd() != KVM_SSBD_KERNEL)
503                         return 0;
504
505                 /*
506                  * If this kernel supports the workaround to be switched on
507                  * or off, make sure it matches the requested setting.
508                  */
509                 switch (wa_level) {
510                 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL:
511                         kvm_arm_set_vcpu_workaround_2_flag(vcpu,
512                             val & KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED);
513                         break;
514                 case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED:
515                         kvm_arm_set_vcpu_workaround_2_flag(vcpu, true);
516                         break;
517                 }
518
519                 return 0;
520         default:
521                 return -ENOENT;
522         }
523
524         return -EINVAL;
525 }