blob: 14e6fa6c8e35d8295e0b634185f32ea68f30bf91 [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
30static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
31{
32 vcpu->arch.pause = true;
33}
34
35static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
36{
37 struct kvm *kvm = source_vcpu->kvm;
Marc Zyngier79c64882013-10-18 18:19:03 +010038 struct kvm_vcpu *vcpu = NULL, *tmp;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050039 wait_queue_head_t *wq;
40 unsigned long cpu_id;
Marc Zyngier79c64882013-10-18 18:19:03 +010041 unsigned long mpidr;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050042 phys_addr_t target_pc;
Marc Zyngier79c64882013-10-18 18:19:03 +010043 int i;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050044
45 cpu_id = *vcpu_reg(source_vcpu, 1);
46 if (vcpu_mode_is_32bit(source_vcpu))
47 cpu_id &= ~((u32) 0);
48
Marc Zyngier79c64882013-10-18 18:19:03 +010049 kvm_for_each_vcpu(i, tmp, kvm) {
50 mpidr = kvm_vcpu_get_mpidr(tmp);
51 if ((mpidr & MPIDR_HWID_BITMASK) == (cpu_id & MPIDR_HWID_BITMASK)) {
52 vcpu = tmp;
53 break;
54 }
55 }
56
Christoffer Dall478a8232013-11-19 17:43:19 -080057 /*
58 * Make sure the caller requested a valid CPU and that the CPU is
59 * turned off.
60 */
61 if (!vcpu || !vcpu->arch.pause)
Anup Patel7d0f84a2014-04-29 11:24:16 +053062 return PSCI_RET_INVALID_PARAMS;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050063
64 target_pc = *vcpu_reg(source_vcpu, 2);
65
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050066 kvm_reset_vcpu(vcpu);
67
68 /* Gracefully handle Thumb2 entry point */
69 if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) {
70 target_pc &= ~((phys_addr_t) 1);
71 vcpu_set_thumb(vcpu);
72 }
73
Marc Zyngierce94fe92013-11-05 14:12:15 +000074 /* Propagate caller endianness */
75 if (kvm_vcpu_is_be(source_vcpu))
76 kvm_vcpu_set_be(vcpu);
77
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050078 *vcpu_pc(vcpu) = target_pc;
79 vcpu->arch.pause = false;
80 smp_mb(); /* Make sure the above is visible */
81
Christoffer Dall478a8232013-11-19 17:43:19 -080082 wq = kvm_arch_vcpu_wq(vcpu);
Marc Zyngieraa024c2f2013-01-20 18:28:13 -050083 wake_up_interruptible(wq);
84
Anup Patel7d0f84a2014-04-29 11:24:16 +053085 return PSCI_RET_SUCCESS;
86}
87
88int kvm_psci_version(struct kvm_vcpu *vcpu)
89{
90 if (test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features))
91 return KVM_ARM_PSCI_0_2;
92
93 return KVM_ARM_PSCI_0_1;
94}
95
Anup Patele8e7fcc2014-04-29 11:24:18 +053096static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +053097{
98 unsigned long psci_fn = *vcpu_reg(vcpu, 0) & ~((u32) 0);
99 unsigned long val;
100
101 switch (psci_fn) {
102 case PSCI_0_2_FN_PSCI_VERSION:
103 /*
104 * Bits[31:16] = Major Version = 0
105 * Bits[15:0] = Minor Version = 2
106 */
107 val = 2;
108 break;
109 case PSCI_0_2_FN_CPU_OFF:
110 kvm_psci_vcpu_off(vcpu);
111 val = PSCI_RET_SUCCESS;
112 break;
113 case PSCI_0_2_FN_CPU_ON:
114 case PSCI_0_2_FN64_CPU_ON:
115 val = kvm_psci_vcpu_on(vcpu);
116 break;
117 case PSCI_0_2_FN_CPU_SUSPEND:
118 case PSCI_0_2_FN_AFFINITY_INFO:
119 case PSCI_0_2_FN_MIGRATE:
120 case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
121 case PSCI_0_2_FN_MIGRATE_INFO_UP_CPU:
122 case PSCI_0_2_FN_SYSTEM_OFF:
123 case PSCI_0_2_FN_SYSTEM_RESET:
124 case PSCI_0_2_FN64_CPU_SUSPEND:
125 case PSCI_0_2_FN64_AFFINITY_INFO:
126 case PSCI_0_2_FN64_MIGRATE:
127 case PSCI_0_2_FN64_MIGRATE_INFO_UP_CPU:
128 val = PSCI_RET_NOT_SUPPORTED;
129 break;
130 default:
Anup Patele8e7fcc2014-04-29 11:24:18 +0530131 return -EINVAL;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530132 }
133
134 *vcpu_reg(vcpu, 0) = val;
Anup Patele8e7fcc2014-04-29 11:24:18 +0530135 return 1;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530136}
137
Anup Patele8e7fcc2014-04-29 11:24:18 +0530138static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
Anup Patel7d0f84a2014-04-29 11:24:16 +0530139{
140 unsigned long psci_fn = *vcpu_reg(vcpu, 0) & ~((u32) 0);
141 unsigned long val;
142
143 switch (psci_fn) {
144 case KVM_PSCI_FN_CPU_OFF:
145 kvm_psci_vcpu_off(vcpu);
146 val = PSCI_RET_SUCCESS;
147 break;
148 case KVM_PSCI_FN_CPU_ON:
149 val = kvm_psci_vcpu_on(vcpu);
150 break;
151 case KVM_PSCI_FN_CPU_SUSPEND:
152 case KVM_PSCI_FN_MIGRATE:
153 val = PSCI_RET_NOT_SUPPORTED;
154 break;
155 default:
Anup Patele8e7fcc2014-04-29 11:24:18 +0530156 return -EINVAL;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530157 }
158
159 *vcpu_reg(vcpu, 0) = val;
Anup Patele8e7fcc2014-04-29 11:24:18 +0530160 return 1;
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500161}
162
163/**
164 * kvm_psci_call - handle PSCI call if r0 value is in range
165 * @vcpu: Pointer to the VCPU struct
166 *
Dave P Martin24a7f672013-05-01 17:49:28 +0100167 * Handle PSCI calls from guests through traps from HVC instructions.
Anup Patele8e7fcc2014-04-29 11:24:18 +0530168 * The calling convention is similar to SMC calls to the secure world
169 * where the function number is placed in r0.
170 *
171 * This function returns: > 0 (success), 0 (success but exit to user
172 * space), and < 0 (errors)
173 *
174 * Errors:
175 * -EINVAL: Unrecognized PSCI function
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500176 */
Anup Patele8e7fcc2014-04-29 11:24:18 +0530177int kvm_psci_call(struct kvm_vcpu *vcpu)
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500178{
Anup Patel7d0f84a2014-04-29 11:24:16 +0530179 switch (kvm_psci_version(vcpu)) {
180 case KVM_ARM_PSCI_0_2:
181 return kvm_psci_0_2_call(vcpu);
182 case KVM_ARM_PSCI_0_1:
183 return kvm_psci_0_1_call(vcpu);
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500184 default:
Anup Patele8e7fcc2014-04-29 11:24:18 +0530185 return -EINVAL;
Anup Patel7d0f84a2014-04-29 11:24:16 +0530186 };
Marc Zyngieraa024c2f2013-01-20 18:28:13 -0500187}