blob: 1067579c733654408806b52c80f43214c1a3f37e [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
18#include <linux/kvm_host.h>
19#include <linux/wait.h>
20
Marc Zyngier79c64882013-10-18 18:19:03 +010021#include <asm/cputype.h>
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050022#include <asm/kvm_emulate.h>
23#include <asm/kvm_psci.h>
24
25/*
26 * This is an implementation of the Power State Coordination Interface
27 * as described in ARM document number ARM DEN 0022A.
28 */
29
Anup Patele6bc13c2014-04-29 11:24:21 +053030#define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1)
31
32static unsigned long psci_affinity_mask(unsigned long affinity_level)
33{
34 if (affinity_level <= 3)
35 return MPIDR_HWID_BITMASK & AFFINITY_MASK(affinity_level);
36
37 return 0;
38}
39
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050040static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
41{
42 vcpu->arch.pause = true;
43}
44
45static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
46{
47 struct kvm *kvm = source_vcpu->kvm;
Marc Zyngier79c64882013-10-18 18:19:03 +010048 struct kvm_vcpu *vcpu = NULL, *tmp;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050049 wait_queue_head_t *wq;
50 unsigned long cpu_id;
Anup Patelaa8aeef2014-04-29 11:24:23 +053051 unsigned long context_id;
Marc Zyngier79c64882013-10-18 18:19:03 +010052 unsigned long mpidr;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050053 phys_addr_t target_pc;
Marc Zyngier79c64882013-10-18 18:19:03 +010054 int i;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050055
56 cpu_id = *vcpu_reg(source_vcpu, 1);
57 if (vcpu_mode_is_32bit(source_vcpu))
58 cpu_id &= ~((u32) 0);
59
Marc Zyngier79c64882013-10-18 18:19:03 +010060 kvm_for_each_vcpu(i, tmp, kvm) {
61 mpidr = kvm_vcpu_get_mpidr(tmp);
62 if ((mpidr & MPIDR_HWID_BITMASK) == (cpu_id & MPIDR_HWID_BITMASK)) {
63 vcpu = tmp;
64 break;
65 }
66 }
67
Christoffer Dall478a8232013-11-19 17:43:19 -080068 /*
69 * Make sure the caller requested a valid CPU and that the CPU is
70 * turned off.
71 */
Anup Patelaa8aeef2014-04-29 11:24:23 +053072 if (!vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +053073 return PSCI_RET_INVALID_PARAMS;
Anup Patelaa8aeef2014-04-29 11:24:23 +053074 if (!vcpu->arch.pause) {
75 if (kvm_psci_version(source_vcpu) != KVM_ARM_PSCI_0_1)
76 return PSCI_RET_ALREADY_ON;
77 else
78 return PSCI_RET_INVALID_PARAMS;
79 }
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050080
81 target_pc = *vcpu_reg(source_vcpu, 2);
Anup Patelaa8aeef2014-04-29 11:24:23 +053082 context_id = *vcpu_reg(source_vcpu, 3);
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050083
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050084 kvm_reset_vcpu(vcpu);
85
86 /* Gracefully handle Thumb2 entry point */
87 if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
88 target_pc &= ~((phys_addr_t) 1);
89 vcpu_set_thumb(vcpu);
90 }
91
Marc Zyngierce94fe92013-11-05 14:12:15 +000092 /* Propagate caller endianness */
93 if (kvm_vcpu_is_be(source_vcpu))
94 kvm_vcpu_set_be(vcpu);
95
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050096 *vcpu_pc(vcpu) = target_pc;
Anup Patelaa8aeef2014-04-29 11:24:23 +053097 /*
98 * NOTE: We always update r0 (or x0) because for PSCI v0.1
99 * the general puspose registers are undefined upon CPU_ON.
100 */
101 *vcpu_reg(vcpu, 0) = context_id;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500102 vcpu->arch.pause = false;
103 smp_mb(); /* Make sure the above is visible */
104
Christoffer Dall478a8232013-11-19 17:43:19 -0800105 wq = kvm_arch_vcpu_wq(vcpu);
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500106 wake_up_interruptible(wq);
107
Anup Patel7d0f84a2014-04-29 11:24:16 +0530108 return PSCI_RET_SUCCESS;
109}
110
Anup Patele6bc13c2014-04-29 11:24:21 +0530111static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
112{
113 int i;
114 unsigned long mpidr;
115 unsigned long target_affinity;
116 unsigned long target_affinity_mask;
117 unsigned long lowest_affinity_level;
118 struct kvm *kvm = vcpu->kvm;
119 struct kvm_vcpu *tmp;
120
121 target_affinity = *vcpu_reg(vcpu, 1);
122 lowest_affinity_level = *vcpu_reg(vcpu, 2);
123
124 /* Determine target affinity mask */
125 target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
126 if (!target_affinity_mask)
127 return PSCI_RET_INVALID_PARAMS;
128
129 /* Ignore other bits of target affinity */
130 target_affinity &= target_affinity_mask;
131
132 /*
133 * If one or more VCPU matching target affinity are running
134 * then ON else OFF
135 */
136 kvm_for_each_vcpu(i, tmp, kvm) {
137 mpidr = kvm_vcpu_get_mpidr(tmp);
138 if (((mpidr & target_affinity_mask) == target_affinity) &&
139 !tmp->arch.pause) {
140 return PSCI_0_2_AFFINITY_LEVEL_ON;
141 }
142 }
143
144 return PSCI_0_2_AFFINITY_LEVEL_OFF;
145}
146
Anup Patel4b123822014-04-29 11:24:20 +0530147static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type)
148{
149 memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
150 vcpu->run->system_event.type = type;
151 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
152}
153
154static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
155{
156 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN);
157}
158
159static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
160{
161 kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET);
162}
163
Anup Patel7d0f84a2014-04-29 11:24:16 +0530164int kvm_psci_version(struct kvm_vcpu *vcpu)
165{
166 if (test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features))
167 return KVM_ARM_PSCI_0_2;
168
169 return KVM_ARM_PSCI_0_1;
170}
171
Anup Patele8e7fcc2014-04-29 11:24:18 +0530172static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +0530173{
Anup Patel4b123822014-04-29 11:24:20 +0530174 int ret = 1;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530175 unsigned long psci_fn = *vcpu_reg(vcpu, 0) & ~((u32) 0);
176 unsigned long val;
177
178 switch (psci_fn) {
179 case PSCI_0_2_FN_PSCI_VERSION:
180 /*
181 * Bits[31:16] = Major Version = 0
182 * Bits[15:0] = Minor Version = 2
183 */
184 val = 2;
185 break;
186 case PSCI_0_2_FN_CPU_OFF:
187 kvm_psci_vcpu_off(vcpu);
188 val = PSCI_RET_SUCCESS;
189 break;
190 case PSCI_0_2_FN_CPU_ON:
191 case PSCI_0_2_FN64_CPU_ON:
192 val = kvm_psci_vcpu_on(vcpu);
193 break;
Anup Patele6bc13c2014-04-29 11:24:21 +0530194 case PSCI_0_2_FN_AFFINITY_INFO:
195 case PSCI_0_2_FN64_AFFINITY_INFO:
196 val = kvm_psci_vcpu_affinity_info(vcpu);
197 break;
Anup Patelbab0b4302014-04-29 11:24:22 +0530198 case PSCI_0_2_FN_MIGRATE:
199 case PSCI_0_2_FN64_MIGRATE:
200 val = PSCI_RET_NOT_SUPPORTED;
201 break;
202 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
203 /*
204 * Trusted OS is MP hence does not require migration
205 * or
206 * Trusted OS is not present
207 */
208 val = PSCI_0_2_TOS_MP;
209 break;
210 case PSCI_0_2_FN_MIGRATE_INFO_UP_CPU:
211 case PSCI_0_2_FN64_MIGRATE_INFO_UP_CPU:
212 val = PSCI_RET_NOT_SUPPORTED;
213 break;
Anup Patel4b123822014-04-29 11:24:20 +0530214 case PSCI_0_2_FN_SYSTEM_OFF:
215 kvm_psci_system_off(vcpu);
216 /*
217 * We should'nt be going back to guest VCPU after
218 * receiving SYSTEM_OFF request.
219 *
220 * If user space accidently/deliberately resumes
221 * guest VCPU after SYSTEM_OFF request then guest
222 * VCPU should see internal failure from PSCI return
223 * value. To achieve this, we preload r0 (or x0) with
224 * PSCI return value INTERNAL_FAILURE.
225 */
226 val = PSCI_RET_INTERNAL_FAILURE;
227 ret = 0;
228 break;
229 case PSCI_0_2_FN_SYSTEM_RESET:
230 kvm_psci_system_reset(vcpu);
231 /*
232 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
233 * with PSCI return value INTERNAL_FAILURE.
234 */
235 val = PSCI_RET_INTERNAL_FAILURE;
236 ret = 0;
237 break;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530238 case PSCI_0_2_FN_CPU_SUSPEND:
Anup Patel7d0f84a2014-04-29 11:24:16 +0530239 case PSCI_0_2_FN64_CPU_SUSPEND:
Anup Patel7d0f84a2014-04-29 11:24:16 +0530240 val = PSCI_RET_NOT_SUPPORTED;
241 break;
242 default:
Anup Patele8e7fcc2014-04-29 11:24:18 +0530243 return -EINVAL;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530244 }
245
246 *vcpu_reg(vcpu, 0) = val;
Anup Patel4b123822014-04-29 11:24:20 +0530247 return ret;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530248}
249
Anup Patele8e7fcc2014-04-29 11:24:18 +0530250static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +0530251{
252 unsigned long psci_fn = *vcpu_reg(vcpu, 0) & ~((u32) 0);
253 unsigned long val;
254
255 switch (psci_fn) {
256 case KVM_PSCI_FN_CPU_OFF:
257 kvm_psci_vcpu_off(vcpu);
258 val = PSCI_RET_SUCCESS;
259 break;
260 case KVM_PSCI_FN_CPU_ON:
261 val = kvm_psci_vcpu_on(vcpu);
262 break;
263 case KVM_PSCI_FN_CPU_SUSPEND:
264 case KVM_PSCI_FN_MIGRATE:
265 val = PSCI_RET_NOT_SUPPORTED;
266 break;
267 default:
Anup Patele8e7fcc2014-04-29 11:24:18 +0530268 return -EINVAL;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530269 }
270
271 *vcpu_reg(vcpu, 0) = val;
Anup Patele8e7fcc2014-04-29 11:24:18 +0530272 return 1;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500273}
274
275/**
276 * kvm_psci_call - handle PSCI call if r0 value is in range
277 * @vcpu: Pointer to the VCPU struct
278 *
Dave P Martin24a7f672013-05-01 17:49:28 +0100279 * Handle PSCI calls from guests through traps from HVC instructions.
Anup Patele8e7fcc2014-04-29 11:24:18 +0530280 * The calling convention is similar to SMC calls to the secure world
281 * where the function number is placed in r0.
282 *
283 * This function returns: > 0 (success), 0 (success but exit to user
284 * space), and < 0 (errors)
285 *
286 * Errors:
287 * -EINVAL: Unrecognized PSCI function
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500288 */
Anup Patele8e7fcc2014-04-29 11:24:18 +0530289int kvm_psci_call(struct kvm_vcpu *vcpu)
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500290{
Anup Patel7d0f84a2014-04-29 11:24:16 +0530291 switch (kvm_psci_version(vcpu)) {
292 case KVM_ARM_PSCI_0_2:
293 return kvm_psci_0_2_call(vcpu);
294 case KVM_ARM_PSCI_0_1:
295 return kvm_psci_0_1_call(vcpu);
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500296 default:
Anup Patele8e7fcc2014-04-29 11:24:18 +0530297 return -EINVAL;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530298 };
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500299}