arm/arm64: KVM: Advertise SMCCC v1.1
[linux-block.git] / virt / kvm / arm / psci.c
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
18 #include <linux/arm-smccc.h>
19 #include <linux/preempt.h>
20 #include <linux/kvm_host.h>
21 #include <linux/wait.h>
22
23 #include <asm/cputype.h>
24 #include <asm/kvm_emulate.h>
25 #include <asm/kvm_host.h>
26
27 #include <kvm/arm_psci.h>
28
29 /*
30  * This is an implementation of the Power State Coordination Interface
31  * as described in ARM document number ARM DEN 0022A.
32  */
33
34 #define AFFINITY_MASK(level)    ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
35
36 static u32 smccc_get_function(struct kvm_vcpu *vcpu)
37 {
38         return vcpu_get_reg(vcpu, 0);
39 }
40
41 static unsigned long smccc_get_arg1(struct kvm_vcpu *vcpu)
42 {
43         return vcpu_get_reg(vcpu, 1);
44 }
45
46 static unsigned long smccc_get_arg2(struct kvm_vcpu *vcpu)
47 {
48         return vcpu_get_reg(vcpu, 2);
49 }
50
51 static unsigned long smccc_get_arg3(struct kvm_vcpu *vcpu)
52 {
53         return vcpu_get_reg(vcpu, 3);
54 }
55
56 static 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
68 static 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
76 static 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         kvm_clear_request(KVM_REQ_UNHALT, vcpu);
93
94         return PSCI_RET_SUCCESS;
95 }
96
97 static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
98 {
99         vcpu->arch.power_off = true;
100         kvm_make_request(KVM_REQ_SLEEP, vcpu);
101         kvm_vcpu_kick(vcpu);
102 }
103
104 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
105 {
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;
112
113         cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK;
114         if (vcpu_mode_is_32bit(source_vcpu))
115                 cpu_id &= ~((u32) 0);
116
117         vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
118
119         /*
120          * Make sure the caller requested a valid CPU and that the CPU is
121          * turned off.
122          */
123         if (!vcpu)
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;
128                 else
129                         return PSCI_RET_INVALID_PARAMS;
130         }
131
132         target_pc = smccc_get_arg2(source_vcpu);
133         context_id = smccc_get_arg3(source_vcpu);
134
135         kvm_reset_vcpu(vcpu);
136
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);
141         }
142
143         /* Propagate caller endianness */
144         if (kvm_vcpu_is_be(source_vcpu))
145                 kvm_vcpu_set_be(vcpu);
146
147         *vcpu_pc(vcpu) = target_pc;
148         /*
149          * NOTE: We always update r0 (or x0) because for PSCI v0.1
150          * the general puspose registers are undefined upon CPU_ON.
151          */
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 */
155
156         wq = kvm_arch_vcpu_wq(vcpu);
157         swake_up(wq);
158
159         return PSCI_RET_SUCCESS;
160 }
161
162 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
163 {
164         int i, matching_cpus = 0;
165         unsigned long mpidr;
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;
171
172         target_affinity = smccc_get_arg1(vcpu);
173         lowest_affinity_level = smccc_get_arg2(vcpu);
174
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;
179
180         /* Ignore other bits of target affinity */
181         target_affinity &= target_affinity_mask;
182
183         /*
184          * If one or more VCPU matching target affinity are running
185          * then ON else OFF
186          */
187         kvm_for_each_vcpu(i, tmp, kvm) {
188                 mpidr = kvm_vcpu_get_mpidr_aff(tmp);
189                 if ((mpidr & target_affinity_mask) == target_affinity) {
190                         matching_cpus++;
191                         if (!tmp->arch.power_off)
192                                 return PSCI_0_2_AFFINITY_LEVEL_ON;
193                 }
194         }
195
196         if (!matching_cpus)
197                 return PSCI_RET_INVALID_PARAMS;
198
199         return PSCI_0_2_AFFINITY_LEVEL_OFF;
200 }
201
202 static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
203 {
204         int i;
205         struct kvm_vcpu *tmp;
206
207         /*
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
214          * re-initialized.
215          */
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);
219
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;
223 }
224
225 static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
226 {
227         kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
228 }
229
230 static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
231 {
232         kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
233 }
234
235 int kvm_psci_version(struct kvm_vcpu *vcpu)
236 {
237         if (test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features))
238                 return KVM_ARM_PSCI_LATEST;
239
240         return KVM_ARM_PSCI_0_1;
241 }
242
243 static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
244 {
245         struct kvm *kvm = vcpu->kvm;
246         u32 psci_fn = smccc_get_function(vcpu);
247         unsigned long val;
248         int ret = 1;
249
250         switch (psci_fn) {
251         case PSCI_0_2_FN_PSCI_VERSION:
252                 /*
253                  * Bits[31:16] = Major Version = 0
254                  * Bits[15:0] = Minor Version = 2
255                  */
256                 val = KVM_ARM_PSCI_0_2;
257                 break;
258         case PSCI_0_2_FN_CPU_SUSPEND:
259         case PSCI_0_2_FN64_CPU_SUSPEND:
260                 val = kvm_psci_vcpu_suspend(vcpu);
261                 break;
262         case PSCI_0_2_FN_CPU_OFF:
263                 kvm_psci_vcpu_off(vcpu);
264                 val = PSCI_RET_SUCCESS;
265                 break;
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);
271                 break;
272         case PSCI_0_2_FN_AFFINITY_INFO:
273         case PSCI_0_2_FN64_AFFINITY_INFO:
274                 val = kvm_psci_vcpu_affinity_info(vcpu);
275                 break;
276         case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
277                 /*
278                  * Trusted OS is MP hence does not require migration
279                  * or
280                  * Trusted OS is not present
281                  */
282                 val = PSCI_0_2_TOS_MP;
283                 break;
284         case PSCI_0_2_FN_SYSTEM_OFF:
285                 kvm_psci_system_off(vcpu);
286                 /*
287                  * We should'nt be going back to guest VCPU after
288                  * receiving SYSTEM_OFF request.
289                  *
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.
295                  */
296                 val = PSCI_RET_INTERNAL_FAILURE;
297                 ret = 0;
298                 break;
299         case PSCI_0_2_FN_SYSTEM_RESET:
300                 kvm_psci_system_reset(vcpu);
301                 /*
302                  * Same reason as SYSTEM_OFF for preloading r0 (or x0)
303                  * with PSCI return value INTERNAL_FAILURE.
304                  */
305                 val = PSCI_RET_INTERNAL_FAILURE;
306                 ret = 0;
307                 break;
308         default:
309                 val = PSCI_RET_NOT_SUPPORTED;
310                 break;
311         }
312
313         smccc_set_retval(vcpu, val, 0, 0, 0);
314         return ret;
315 }
316
317 static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu)
318 {
319         u32 psci_fn = smccc_get_function(vcpu);
320         u32 feature;
321         unsigned long val;
322         int ret = 1;
323
324         switch(psci_fn) {
325         case PSCI_0_2_FN_PSCI_VERSION:
326                 val = KVM_ARM_PSCI_1_0;
327                 break;
328         case PSCI_1_0_FN_PSCI_FEATURES:
329                 feature = smccc_get_arg1(vcpu);
330                 switch(feature) {
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:
344                         val = 0;
345                         break;
346                 default:
347                         val = PSCI_RET_NOT_SUPPORTED;
348                         break;
349                 }
350                 break;
351         default:
352                 return kvm_psci_0_2_call(vcpu);
353         }
354
355         smccc_set_retval(vcpu, val, 0, 0, 0);
356         return ret;
357 }
358
359 static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
360 {
361         struct kvm *kvm = vcpu->kvm;
362         u32 psci_fn = smccc_get_function(vcpu);
363         unsigned long val;
364
365         switch (psci_fn) {
366         case KVM_PSCI_FN_CPU_OFF:
367                 kvm_psci_vcpu_off(vcpu);
368                 val = PSCI_RET_SUCCESS;
369                 break;
370         case KVM_PSCI_FN_CPU_ON:
371                 mutex_lock(&kvm->lock);
372                 val = kvm_psci_vcpu_on(vcpu);
373                 mutex_unlock(&kvm->lock);
374                 break;
375         default:
376                 val = PSCI_RET_NOT_SUPPORTED;
377                 break;
378         }
379
380         smccc_set_retval(vcpu, val, 0, 0, 0);
381         return 1;
382 }
383
384 /**
385  * kvm_psci_call - handle PSCI call if r0 value is in range
386  * @vcpu: Pointer to the VCPU struct
387  *
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.
391  *
392  * This function returns: > 0 (success), 0 (success but exit to user
393  * space), and < 0 (errors)
394  *
395  * Errors:
396  * -EINVAL: Unrecognized PSCI function
397  */
398 static int kvm_psci_call(struct kvm_vcpu *vcpu)
399 {
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);
407         default:
408                 return -EINVAL;
409         };
410 }
411
412 int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)
413 {
414         u32 func_id = smccc_get_function(vcpu);
415         u32 val = PSCI_RET_NOT_SUPPORTED;
416
417         switch (func_id) {
418         case ARM_SMCCC_VERSION_FUNC_ID:
419                 val = ARM_SMCCC_VERSION_1_1;
420                 break;
421         case ARM_SMCCC_ARCH_FEATURES_FUNC_ID:
422                 /* Nothing supported yet */
423                 break;
424         default:
425                 return kvm_psci_call(vcpu);
426         }
427
428         smccc_set_retval(vcpu, val, 0, 0, 0);
429         return 1;
430 }