Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 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 | |
Mark Rutland | 6681f3c | 2018-04-12 12:11:28 +0100 | [diff] [blame] | 18 | #include <linux/arm-smccc.h> |
Christoffer Dall | cf5d3188 | 2014-10-16 17:00:18 +0200 | [diff] [blame] | 19 | #include <linux/preempt.h> |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 20 | #include <linux/kvm_host.h> |
| 21 | #include <linux/wait.h> |
| 22 | |
Marc Zyngier | 79c6488 | 2013-10-18 18:19:03 +0100 | [diff] [blame] | 23 | #include <asm/cputype.h> |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 24 | #include <asm/kvm_emulate.h> |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 25 | #include <asm/kvm_host.h> |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 26 | |
Mark Rutland | 8b106af | 2018-04-12 12:11:24 +0100 | [diff] [blame] | 27 | #include <kvm/arm_psci.h> |
| 28 | |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 29 | /* |
| 30 | * This is an implementation of the Power State Coordination Interface |
| 31 | * as described in ARM document number ARM DEN 0022A. |
| 32 | */ |
| 33 | |
Anup Patel | e6bc13c | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 34 | #define AFFINITY_MASK(level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1) |
| 35 | |
Mark Rutland | 54faafb | 2018-04-12 12:11:26 +0100 | [diff] [blame] | 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 | |
Anup Patel | e6bc13c | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 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 | |
Anup Patel | b376d02 | 2014-04-29 11:24:24 +0530 | [diff] [blame] | 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 | |
| 93 | return PSCI_RET_SUCCESS; |
| 94 | } |
| 95 | |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 96 | static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu) |
| 97 | { |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 98 | vcpu->arch.power_off = true; |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) |
| 102 | { |
| 103 | struct kvm *kvm = source_vcpu->kvm; |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 104 | struct kvm_vcpu *vcpu = NULL; |
Marcelo Tosatti | 8577370 | 2016-02-19 09:46:39 +0100 | [diff] [blame] | 105 | struct swait_queue_head *wq; |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 106 | unsigned long cpu_id; |
Anup Patel | aa8aeef | 2014-04-29 11:24:23 +0530 | [diff] [blame] | 107 | unsigned long context_id; |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 108 | phys_addr_t target_pc; |
| 109 | |
Mark Rutland | 54faafb | 2018-04-12 12:11:26 +0100 | [diff] [blame] | 110 | cpu_id = smccc_get_arg1(source_vcpu) & MPIDR_HWID_BITMASK; |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 111 | if (vcpu_mode_is_32bit(source_vcpu)) |
| 112 | cpu_id &= ~((u32) 0); |
| 113 | |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 114 | vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id); |
Marc Zyngier | 79c6488 | 2013-10-18 18:19:03 +0100 | [diff] [blame] | 115 | |
Christoffer Dall | 478a823 | 2013-11-19 17:43:19 -0800 | [diff] [blame] | 116 | /* |
| 117 | * Make sure the caller requested a valid CPU and that the CPU is |
| 118 | * turned off. |
| 119 | */ |
Anup Patel | aa8aeef | 2014-04-29 11:24:23 +0530 | [diff] [blame] | 120 | if (!vcpu) |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 121 | return PSCI_RET_INVALID_PARAMS; |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 122 | if (!vcpu->arch.power_off) { |
Mark Rutland | 142cfd6 | 2018-04-12 12:11:30 +0100 | [diff] [blame] | 123 | if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1) |
Anup Patel | aa8aeef | 2014-04-29 11:24:23 +0530 | [diff] [blame] | 124 | return PSCI_RET_ALREADY_ON; |
| 125 | else |
| 126 | return PSCI_RET_INVALID_PARAMS; |
| 127 | } |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 128 | |
Mark Rutland | 54faafb | 2018-04-12 12:11:26 +0100 | [diff] [blame] | 129 | target_pc = smccc_get_arg2(source_vcpu); |
| 130 | context_id = smccc_get_arg3(source_vcpu); |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 131 | |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 132 | kvm_reset_vcpu(vcpu); |
| 133 | |
| 134 | /* Gracefully handle Thumb2 entry point */ |
| 135 | if (vcpu_mode_is_32bit(vcpu) && (target_pc & 1)) { |
| 136 | target_pc &= ~((phys_addr_t) 1); |
| 137 | vcpu_set_thumb(vcpu); |
| 138 | } |
| 139 | |
Marc Zyngier | ce94fe9 | 2013-11-05 14:12:15 +0000 | [diff] [blame] | 140 | /* Propagate caller endianness */ |
| 141 | if (kvm_vcpu_is_be(source_vcpu)) |
| 142 | kvm_vcpu_set_be(vcpu); |
| 143 | |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 144 | *vcpu_pc(vcpu) = target_pc; |
Anup Patel | aa8aeef | 2014-04-29 11:24:23 +0530 | [diff] [blame] | 145 | /* |
| 146 | * NOTE: We always update r0 (or x0) because for PSCI v0.1 |
| 147 | * the general puspose registers are undefined upon CPU_ON. |
| 148 | */ |
Mark Rutland | 54faafb | 2018-04-12 12:11:26 +0100 | [diff] [blame] | 149 | smccc_set_retval(vcpu, context_id, 0, 0, 0); |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 150 | vcpu->arch.power_off = false; |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 151 | smp_mb(); /* Make sure the above is visible */ |
| 152 | |
Christoffer Dall | 478a823 | 2013-11-19 17:43:19 -0800 | [diff] [blame] | 153 | wq = kvm_arch_vcpu_wq(vcpu); |
Marcelo Tosatti | 8577370 | 2016-02-19 09:46:39 +0100 | [diff] [blame] | 154 | swake_up(wq); |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 155 | |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 156 | return PSCI_RET_SUCCESS; |
| 157 | } |
| 158 | |
Anup Patel | e6bc13c | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 159 | static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu) |
| 160 | { |
Alexander Spyridakis | 0c06729 | 2015-09-04 17:06:24 +0200 | [diff] [blame] | 161 | int i, matching_cpus = 0; |
Anup Patel | e6bc13c | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 162 | unsigned long mpidr; |
| 163 | unsigned long target_affinity; |
| 164 | unsigned long target_affinity_mask; |
| 165 | unsigned long lowest_affinity_level; |
| 166 | struct kvm *kvm = vcpu->kvm; |
| 167 | struct kvm_vcpu *tmp; |
| 168 | |
Mark Rutland | 54faafb | 2018-04-12 12:11:26 +0100 | [diff] [blame] | 169 | target_affinity = smccc_get_arg1(vcpu); |
| 170 | lowest_affinity_level = smccc_get_arg2(vcpu); |
Anup Patel | e6bc13c | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 171 | |
| 172 | /* Determine target affinity mask */ |
| 173 | target_affinity_mask = psci_affinity_mask(lowest_affinity_level); |
| 174 | if (!target_affinity_mask) |
| 175 | return PSCI_RET_INVALID_PARAMS; |
| 176 | |
| 177 | /* Ignore other bits of target affinity */ |
| 178 | target_affinity &= target_affinity_mask; |
| 179 | |
| 180 | /* |
| 181 | * If one or more VCPU matching target affinity are running |
| 182 | * then ON else OFF |
| 183 | */ |
| 184 | kvm_for_each_vcpu(i, tmp, kvm) { |
Andre Przywara | 4429fc6 | 2014-06-02 15:37:13 +0200 | [diff] [blame] | 185 | mpidr = kvm_vcpu_get_mpidr_aff(tmp); |
Alexander Spyridakis | 0c06729 | 2015-09-04 17:06:24 +0200 | [diff] [blame] | 186 | if ((mpidr & target_affinity_mask) == target_affinity) { |
| 187 | matching_cpus++; |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 188 | if (!tmp->arch.power_off) |
Alexander Spyridakis | 0c06729 | 2015-09-04 17:06:24 +0200 | [diff] [blame] | 189 | return PSCI_0_2_AFFINITY_LEVEL_ON; |
Anup Patel | e6bc13c | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Alexander Spyridakis | 0c06729 | 2015-09-04 17:06:24 +0200 | [diff] [blame] | 193 | if (!matching_cpus) |
| 194 | return PSCI_RET_INVALID_PARAMS; |
| 195 | |
Anup Patel | e6bc13c | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 196 | return PSCI_0_2_AFFINITY_LEVEL_OFF; |
| 197 | } |
| 198 | |
Anup Patel | 4b12382 | 2014-04-29 11:24:20 +0530 | [diff] [blame] | 199 | static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type) |
| 200 | { |
Christoffer Dall | cf5d3188 | 2014-10-16 17:00:18 +0200 | [diff] [blame] | 201 | int i; |
| 202 | struct kvm_vcpu *tmp; |
| 203 | |
| 204 | /* |
| 205 | * The KVM ABI specifies that a system event exit may call KVM_RUN |
| 206 | * again and may perform shutdown/reboot at a later time that when the |
| 207 | * actual request is made. Since we are implementing PSCI and a |
| 208 | * caller of PSCI reboot and shutdown expects that the system shuts |
| 209 | * down or reboots immediately, let's make sure that VCPUs are not run |
| 210 | * after this call is handled and before the VCPUs have been |
| 211 | * re-initialized. |
| 212 | */ |
| 213 | kvm_for_each_vcpu(i, tmp, vcpu->kvm) { |
Eric Auger | 3781528 | 2015-09-25 23:41:14 +0200 | [diff] [blame] | 214 | tmp->arch.power_off = true; |
Christoffer Dall | cf5d3188 | 2014-10-16 17:00:18 +0200 | [diff] [blame] | 215 | kvm_vcpu_kick(tmp); |
| 216 | } |
| 217 | |
Anup Patel | 4b12382 | 2014-04-29 11:24:20 +0530 | [diff] [blame] | 218 | memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event)); |
| 219 | vcpu->run->system_event.type = type; |
| 220 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; |
| 221 | } |
| 222 | |
| 223 | static void kvm_psci_system_off(struct kvm_vcpu *vcpu) |
| 224 | { |
| 225 | kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN); |
| 226 | } |
| 227 | |
| 228 | static void kvm_psci_system_reset(struct kvm_vcpu *vcpu) |
| 229 | { |
| 230 | kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET); |
| 231 | } |
| 232 | |
Anup Patel | e8e7fcc | 2014-04-29 11:24:18 +0530 | [diff] [blame] | 233 | static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu) |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 234 | { |
Andrew Jones | 7b0d439 | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 235 | struct kvm *kvm = vcpu->kvm; |
Mark Rutland | 54faafb | 2018-04-12 12:11:26 +0100 | [diff] [blame] | 236 | unsigned long psci_fn = smccc_get_function(vcpu); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 237 | unsigned long val; |
Andrew Jones | 7b0d439 | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 238 | int ret = 1; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 239 | |
| 240 | switch (psci_fn) { |
| 241 | case PSCI_0_2_FN_PSCI_VERSION: |
| 242 | /* |
| 243 | * Bits[31:16] = Major Version = 0 |
| 244 | * Bits[15:0] = Minor Version = 2 |
| 245 | */ |
Mark Rutland | 33e6484 | 2018-04-12 12:11:25 +0100 | [diff] [blame] | 246 | val = KVM_ARM_PSCI_0_2; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 247 | break; |
Anup Patel | b376d02 | 2014-04-29 11:24:24 +0530 | [diff] [blame] | 248 | case PSCI_0_2_FN_CPU_SUSPEND: |
| 249 | case PSCI_0_2_FN64_CPU_SUSPEND: |
| 250 | val = kvm_psci_vcpu_suspend(vcpu); |
| 251 | break; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 252 | case PSCI_0_2_FN_CPU_OFF: |
| 253 | kvm_psci_vcpu_off(vcpu); |
| 254 | val = PSCI_RET_SUCCESS; |
| 255 | break; |
| 256 | case PSCI_0_2_FN_CPU_ON: |
| 257 | case PSCI_0_2_FN64_CPU_ON: |
Andrew Jones | 7b0d439 | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 258 | mutex_lock(&kvm->lock); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 259 | val = kvm_psci_vcpu_on(vcpu); |
Andrew Jones | 7b0d439 | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 260 | mutex_unlock(&kvm->lock); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 261 | break; |
Anup Patel | e6bc13c | 2014-04-29 11:24:21 +0530 | [diff] [blame] | 262 | case PSCI_0_2_FN_AFFINITY_INFO: |
| 263 | case PSCI_0_2_FN64_AFFINITY_INFO: |
| 264 | val = kvm_psci_vcpu_affinity_info(vcpu); |
| 265 | break; |
Anup Patel | bab0b430 | 2014-04-29 11:24:22 +0530 | [diff] [blame] | 266 | case PSCI_0_2_FN_MIGRATE_INFO_TYPE: |
| 267 | /* |
| 268 | * Trusted OS is MP hence does not require migration |
| 269 | * or |
| 270 | * Trusted OS is not present |
| 271 | */ |
| 272 | val = PSCI_0_2_TOS_MP; |
| 273 | break; |
Anup Patel | 4b12382 | 2014-04-29 11:24:20 +0530 | [diff] [blame] | 274 | case PSCI_0_2_FN_SYSTEM_OFF: |
| 275 | kvm_psci_system_off(vcpu); |
| 276 | /* |
| 277 | * We should'nt be going back to guest VCPU after |
| 278 | * receiving SYSTEM_OFF request. |
| 279 | * |
| 280 | * If user space accidently/deliberately resumes |
| 281 | * guest VCPU after SYSTEM_OFF request then guest |
| 282 | * VCPU should see internal failure from PSCI return |
| 283 | * value. To achieve this, we preload r0 (or x0) with |
| 284 | * PSCI return value INTERNAL_FAILURE. |
| 285 | */ |
| 286 | val = PSCI_RET_INTERNAL_FAILURE; |
| 287 | ret = 0; |
| 288 | break; |
| 289 | case PSCI_0_2_FN_SYSTEM_RESET: |
| 290 | kvm_psci_system_reset(vcpu); |
| 291 | /* |
| 292 | * Same reason as SYSTEM_OFF for preloading r0 (or x0) |
| 293 | * with PSCI return value INTERNAL_FAILURE. |
| 294 | */ |
| 295 | val = PSCI_RET_INTERNAL_FAILURE; |
| 296 | ret = 0; |
| 297 | break; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 298 | default: |
Lorenzo Pieralisi | e2d9973 | 2015-06-10 15:19:24 +0100 | [diff] [blame] | 299 | val = PSCI_RET_NOT_SUPPORTED; |
| 300 | break; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 301 | } |
| 302 | |
Mark Rutland | 54faafb | 2018-04-12 12:11:26 +0100 | [diff] [blame] | 303 | smccc_set_retval(vcpu, val, 0, 0, 0); |
Anup Patel | 4b12382 | 2014-04-29 11:24:20 +0530 | [diff] [blame] | 304 | return ret; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 305 | } |
| 306 | |
Mark Rutland | 4b1713f | 2018-04-12 12:11:27 +0100 | [diff] [blame] | 307 | static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu) |
| 308 | { |
| 309 | u32 psci_fn = smccc_get_function(vcpu); |
| 310 | u32 feature; |
| 311 | unsigned long val; |
| 312 | int ret = 1; |
| 313 | |
| 314 | switch(psci_fn) { |
| 315 | case PSCI_0_2_FN_PSCI_VERSION: |
| 316 | val = KVM_ARM_PSCI_1_0; |
| 317 | break; |
| 318 | case PSCI_1_0_FN_PSCI_FEATURES: |
| 319 | feature = smccc_get_arg1(vcpu); |
| 320 | switch(feature) { |
| 321 | case PSCI_0_2_FN_PSCI_VERSION: |
| 322 | case PSCI_0_2_FN_CPU_SUSPEND: |
| 323 | case PSCI_0_2_FN64_CPU_SUSPEND: |
| 324 | case PSCI_0_2_FN_CPU_OFF: |
| 325 | case PSCI_0_2_FN_CPU_ON: |
| 326 | case PSCI_0_2_FN64_CPU_ON: |
| 327 | case PSCI_0_2_FN_AFFINITY_INFO: |
| 328 | case PSCI_0_2_FN64_AFFINITY_INFO: |
| 329 | case PSCI_0_2_FN_MIGRATE_INFO_TYPE: |
| 330 | case PSCI_0_2_FN_SYSTEM_OFF: |
| 331 | case PSCI_0_2_FN_SYSTEM_RESET: |
| 332 | case PSCI_1_0_FN_PSCI_FEATURES: |
Mark Rutland | 6681f3c | 2018-04-12 12:11:28 +0100 | [diff] [blame] | 333 | case ARM_SMCCC_VERSION_FUNC_ID: |
Mark Rutland | 4b1713f | 2018-04-12 12:11:27 +0100 | [diff] [blame] | 334 | val = 0; |
| 335 | break; |
| 336 | default: |
| 337 | val = PSCI_RET_NOT_SUPPORTED; |
| 338 | break; |
| 339 | } |
| 340 | break; |
| 341 | default: |
| 342 | return kvm_psci_0_2_call(vcpu); |
| 343 | } |
| 344 | |
| 345 | smccc_set_retval(vcpu, val, 0, 0, 0); |
| 346 | return ret; |
| 347 | } |
| 348 | |
Anup Patel | e8e7fcc | 2014-04-29 11:24:18 +0530 | [diff] [blame] | 349 | static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu) |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 350 | { |
Andrew Jones | 7b0d439 | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 351 | struct kvm *kvm = vcpu->kvm; |
Mark Rutland | 54faafb | 2018-04-12 12:11:26 +0100 | [diff] [blame] | 352 | unsigned long psci_fn = smccc_get_function(vcpu); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 353 | unsigned long val; |
| 354 | |
| 355 | switch (psci_fn) { |
| 356 | case KVM_PSCI_FN_CPU_OFF: |
| 357 | kvm_psci_vcpu_off(vcpu); |
| 358 | val = PSCI_RET_SUCCESS; |
| 359 | break; |
| 360 | case KVM_PSCI_FN_CPU_ON: |
Andrew Jones | 7b0d439 | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 361 | mutex_lock(&kvm->lock); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 362 | val = kvm_psci_vcpu_on(vcpu); |
Andrew Jones | 7b0d439 | 2017-04-18 17:59:58 +0200 | [diff] [blame] | 363 | mutex_unlock(&kvm->lock); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 364 | break; |
Lorenzo Pieralisi | e2d9973 | 2015-06-10 15:19:24 +0100 | [diff] [blame] | 365 | default: |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 366 | val = PSCI_RET_NOT_SUPPORTED; |
| 367 | break; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 368 | } |
| 369 | |
Mark Rutland | 54faafb | 2018-04-12 12:11:26 +0100 | [diff] [blame] | 370 | smccc_set_retval(vcpu, val, 0, 0, 0); |
Anup Patel | e8e7fcc | 2014-04-29 11:24:18 +0530 | [diff] [blame] | 371 | return 1; |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * kvm_psci_call - handle PSCI call if r0 value is in range |
| 376 | * @vcpu: Pointer to the VCPU struct |
| 377 | * |
Dave P Martin | 24a7f67 | 2013-05-01 17:49:28 +0100 | [diff] [blame] | 378 | * Handle PSCI calls from guests through traps from HVC instructions. |
Anup Patel | e8e7fcc | 2014-04-29 11:24:18 +0530 | [diff] [blame] | 379 | * The calling convention is similar to SMC calls to the secure world |
| 380 | * where the function number is placed in r0. |
| 381 | * |
| 382 | * This function returns: > 0 (success), 0 (success but exit to user |
| 383 | * space), and < 0 (errors) |
| 384 | * |
| 385 | * Errors: |
| 386 | * -EINVAL: Unrecognized PSCI function |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 387 | */ |
Mark Rutland | 6681f3c | 2018-04-12 12:11:28 +0100 | [diff] [blame] | 388 | static int kvm_psci_call(struct kvm_vcpu *vcpu) |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 389 | { |
Mark Rutland | 142cfd6 | 2018-04-12 12:11:30 +0100 | [diff] [blame] | 390 | switch (kvm_psci_version(vcpu, vcpu->kvm)) { |
Mark Rutland | 4b1713f | 2018-04-12 12:11:27 +0100 | [diff] [blame] | 391 | case KVM_ARM_PSCI_1_0: |
| 392 | return kvm_psci_1_0_call(vcpu); |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 393 | case KVM_ARM_PSCI_0_2: |
| 394 | return kvm_psci_0_2_call(vcpu); |
| 395 | case KVM_ARM_PSCI_0_1: |
| 396 | return kvm_psci_0_1_call(vcpu); |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 397 | default: |
Anup Patel | e8e7fcc | 2014-04-29 11:24:18 +0530 | [diff] [blame] | 398 | return -EINVAL; |
Anup Patel | 7d0f84a | 2014-04-29 11:24:16 +0530 | [diff] [blame] | 399 | }; |
Marc Zyngier | aa024c2f | 2013-01-20 18:28:13 -0500 | [diff] [blame] | 400 | } |
Mark Rutland | 6681f3c | 2018-04-12 12:11:28 +0100 | [diff] [blame] | 401 | |
| 402 | int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) |
| 403 | { |
| 404 | u32 func_id = smccc_get_function(vcpu); |
| 405 | u32 val = PSCI_RET_NOT_SUPPORTED; |
Mark Rutland | c9ae3d5 | 2018-04-12 12:11:31 +0100 | [diff] [blame] | 406 | u32 feature; |
Mark Rutland | 6681f3c | 2018-04-12 12:11:28 +0100 | [diff] [blame] | 407 | |
| 408 | switch (func_id) { |
| 409 | case ARM_SMCCC_VERSION_FUNC_ID: |
| 410 | val = ARM_SMCCC_VERSION_1_1; |
| 411 | break; |
| 412 | case ARM_SMCCC_ARCH_FEATURES_FUNC_ID: |
Mark Rutland | c9ae3d5 | 2018-04-12 12:11:31 +0100 | [diff] [blame] | 413 | feature = smccc_get_arg1(vcpu); |
| 414 | switch(feature) { |
| 415 | case ARM_SMCCC_ARCH_WORKAROUND_1: |
| 416 | if (kvm_arm_harden_branch_predictor()) |
| 417 | val = 0; |
| 418 | break; |
| 419 | } |
Mark Rutland | 6681f3c | 2018-04-12 12:11:28 +0100 | [diff] [blame] | 420 | break; |
| 421 | default: |
| 422 | return kvm_psci_call(vcpu); |
| 423 | } |
| 424 | |
| 425 | smccc_set_retval(vcpu, val, 0, 0, 0); |
| 426 | return 1; |
| 427 | } |