blob: ae0e06b6a49297efe546db1226a4601c83398be4 [file] [log] [blame]
Marc Zyngieraa024c22013-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
18#include <linux/kvm_host.h>
19#include <linux/wait.h>
20
21#include <asm/kvm_emulate.h>
22#include <asm/kvm_psci.h>
23
24/*
25 * This is an implementation of the Power State Coordination Interface
26 * as described in ARM document number ARM DEN 0022A.
27 */
28
29static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
30{
31 vcpu->arch.pause = true;
32}
33
34static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
35{
36 struct kvm *kvm = source_vcpu->kvm;
37 struct kvm_vcpu *vcpu;
38 wait_queue_head_t *wq;
39 unsigned long cpu_id;
40 phys_addr_t target_pc;
41
42 cpu_id = *vcpu_reg(source_vcpu, 1);
43 if (vcpu_mode_is_32bit(source_vcpu))
44 cpu_id &= ~((u32) 0);
45
46 if (cpu_id >= atomic_read(&kvm->online_vcpus))
47 return KVM_PSCI_RET_INVAL;
48
49 target_pc = *vcpu_reg(source_vcpu, 2);
50
51 vcpu = kvm_get_vcpu(kvm, cpu_id);
52
53 wq = kvm_arch_vcpu_wq(vcpu);
54 if (!waitqueue_active(wq))
55 return KVM_PSCI_RET_INVAL;
56
57 kvm_reset_vcpu(vcpu);
58
59 /* Gracefully handle Thumb2 entry point */
60 if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
61 target_pc &= ~((phys_addr_t) 1);
62 vcpu_set_thumb(vcpu);
63 }
64
Marc Zyngierce94fe92013-11-05 14:12:15 +000065 /* Propagate caller endianness */
66 if (kvm_vcpu_is_be(source_vcpu))
67 kvm_vcpu_set_be(vcpu);
68
Marc Zyngieraa024c22013-01-20 18:28:13 -050069 *vcpu_pc(vcpu) = target_pc;
70 vcpu->arch.pause = false;
71 smp_mb(); /* Make sure the above is visible */
72
73 wake_up_interruptible(wq);
74
75 return KVM_PSCI_RET_SUCCESS;
76}
77
78/**
79 * kvm_psci_call - handle PSCI call if r0 value is in range
80 * @vcpu: Pointer to the VCPU struct
81 *
Dave P Martin24a7f672013-05-01 17:49:28 +010082 * Handle PSCI calls from guests through traps from HVC instructions.
Marc Zyngieraa024c22013-01-20 18:28:13 -050083 * The calling convention is similar to SMC calls to the secure world where
84 * the function number is placed in r0 and this function returns true if the
85 * function number specified in r0 is withing the PSCI range, and false
86 * otherwise.
87 */
88bool kvm_psci_call(struct kvm_vcpu *vcpu)
89{
90 unsigned long psci_fn = *vcpu_reg(vcpu, 0) & ~((u32) 0);
91 unsigned long val;
92
93 switch (psci_fn) {
94 case KVM_PSCI_FN_CPU_OFF:
95 kvm_psci_vcpu_off(vcpu);
96 val = KVM_PSCI_RET_SUCCESS;
97 break;
98 case KVM_PSCI_FN_CPU_ON:
99 val = kvm_psci_vcpu_on(vcpu);
100 break;
101 case KVM_PSCI_FN_CPU_SUSPEND:
102 case KVM_PSCI_FN_MIGRATE:
103 val = KVM_PSCI_RET_NI;
104 break;
105
106 default:
107 return false;
108 }
109
110 *vcpu_reg(vcpu, 0) = val;
111 return true;
112}