• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 - ARM Ltd
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6 
7 #include <linux/arm-smccc.h>
8 #include <linux/preempt.h>
9 #include <linux/kvm_host.h>
10 #include <linux/uaccess.h>
11 #include <linux/wait.h>
12 
13 #include <asm/cputype.h>
14 #include <asm/kvm_emulate.h>
15 
16 #include <kvm/arm_psci.h>
17 #include <kvm/arm_hypercalls.h>
18 
19 /*
20  * This is an implementation of the Power State Coordination Interface
21  * as described in ARM document number ARM DEN 0022A.
22  */
23 
kvm_psci_vcpu_suspend(struct kvm_vcpu * vcpu)24 static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)
25 {
26 	/*
27 	 * NOTE: For simplicity, we make VCPU suspend emulation to be
28 	 * same-as WFI (Wait-for-interrupt) emulation.
29 	 *
30 	 * This means for KVM the wakeup events are interrupts and
31 	 * this is consistent with intended use of StateID as described
32 	 * in section 5.4.1 of PSCI v0.2 specification (ARM DEN 0022A).
33 	 *
34 	 * Further, we also treat power-down request to be same as
35 	 * stand-by request as-per section 5.4.2 clause 3 of PSCI v0.2
36 	 * specification (ARM DEN 0022A). This means all suspend states
37 	 * for KVM will preserve the register state.
38 	 */
39 	kvm_vcpu_block(vcpu);
40 	kvm_clear_request(KVM_REQ_UNHALT, vcpu);
41 
42 	return PSCI_RET_SUCCESS;
43 }
44 
kvm_psci_vcpu_off(struct kvm_vcpu * vcpu)45 static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu)
46 {
47 	vcpu->arch.power_off = true;
48 	kvm_make_request(KVM_REQ_SLEEP, vcpu);
49 	kvm_vcpu_kick(vcpu);
50 }
51 
kvm_psci_vcpu_on(struct kvm_vcpu * source_vcpu)52 static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)
53 {
54 	struct vcpu_reset_state *reset_state;
55 	struct kvm *kvm = source_vcpu->kvm;
56 	struct kvm_vcpu *vcpu = NULL;
57 	unsigned long cpu_id;
58 
59 	cpu_id = smccc_get_arg1(source_vcpu);
60 	if (!kvm_psci_valid_affinity(source_vcpu, cpu_id))
61 		return PSCI_RET_INVALID_PARAMS;
62 
63 	vcpu = kvm_mpidr_to_vcpu(kvm, cpu_id);
64 
65 	/*
66 	 * Make sure the caller requested a valid CPU and that the CPU is
67 	 * turned off.
68 	 */
69 	if (!vcpu)
70 		return PSCI_RET_INVALID_PARAMS;
71 	if (!vcpu->arch.power_off) {
72 		if (kvm_psci_version(source_vcpu, kvm) != KVM_ARM_PSCI_0_1)
73 			return PSCI_RET_ALREADY_ON;
74 		else
75 			return PSCI_RET_INVALID_PARAMS;
76 	}
77 
78 	reset_state = &vcpu->arch.reset_state;
79 
80 	reset_state->pc = smccc_get_arg2(source_vcpu);
81 
82 	/* Propagate caller endianness */
83 	reset_state->be = kvm_vcpu_is_be(source_vcpu);
84 
85 	/*
86 	 * NOTE: We always update r0 (or x0) because for PSCI v0.1
87 	 * the general purpose registers are undefined upon CPU_ON.
88 	 */
89 	reset_state->r0 = smccc_get_arg3(source_vcpu);
90 
91 	WRITE_ONCE(reset_state->reset, true);
92 	kvm_make_request(KVM_REQ_VCPU_RESET, vcpu);
93 
94 	/*
95 	 * Make sure the reset request is observed if the change to
96 	 * power_off is observed.
97 	 */
98 	smp_wmb();
99 
100 	vcpu->arch.power_off = false;
101 	kvm_vcpu_wake_up(vcpu);
102 
103 	return PSCI_RET_SUCCESS;
104 }
105 
kvm_psci_vcpu_affinity_info(struct kvm_vcpu * vcpu)106 static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu)
107 {
108 	int i, matching_cpus = 0;
109 	unsigned long mpidr;
110 	unsigned long target_affinity;
111 	unsigned long target_affinity_mask;
112 	unsigned long lowest_affinity_level;
113 	struct kvm *kvm = vcpu->kvm;
114 	struct kvm_vcpu *tmp;
115 
116 	target_affinity = smccc_get_arg1(vcpu);
117 	lowest_affinity_level = smccc_get_arg2(vcpu);
118 
119 	if (!kvm_psci_valid_affinity(vcpu, target_affinity))
120 		return PSCI_RET_INVALID_PARAMS;
121 
122 	/* Determine target affinity mask */
123 	target_affinity_mask = psci_affinity_mask(lowest_affinity_level);
124 	if (!target_affinity_mask)
125 		return PSCI_RET_INVALID_PARAMS;
126 
127 	/* Ignore other bits of target affinity */
128 	target_affinity &= target_affinity_mask;
129 
130 	/*
131 	 * If one or more VCPU matching target affinity are running
132 	 * then ON else OFF
133 	 */
134 	kvm_for_each_vcpu(i, tmp, kvm) {
135 		mpidr = kvm_vcpu_get_mpidr_aff(tmp);
136 		if ((mpidr & target_affinity_mask) == target_affinity) {
137 			matching_cpus++;
138 			if (!tmp->arch.power_off)
139 				return PSCI_0_2_AFFINITY_LEVEL_ON;
140 		}
141 	}
142 
143 	if (!matching_cpus)
144 		return PSCI_RET_INVALID_PARAMS;
145 
146 	return PSCI_0_2_AFFINITY_LEVEL_OFF;
147 }
148 
kvm_prepare_system_event(struct kvm_vcpu * vcpu,u32 type,u64 flags)149 static void kvm_prepare_system_event(struct kvm_vcpu *vcpu, u32 type, u64 flags)
150 {
151 	int i;
152 	struct kvm_vcpu *tmp;
153 
154 	/*
155 	 * The KVM ABI specifies that a system event exit may call KVM_RUN
156 	 * again and may perform shutdown/reboot at a later time that when the
157 	 * actual request is made.  Since we are implementing PSCI and a
158 	 * caller of PSCI reboot and shutdown expects that the system shuts
159 	 * down or reboots immediately, let's make sure that VCPUs are not run
160 	 * after this call is handled and before the VCPUs have been
161 	 * re-initialized.
162 	 */
163 	kvm_for_each_vcpu(i, tmp, vcpu->kvm)
164 		tmp->arch.power_off = true;
165 	kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_SLEEP);
166 
167 	memset(&vcpu->run->system_event, 0, sizeof(vcpu->run->system_event));
168 	vcpu->run->system_event.type = type;
169 	vcpu->run->system_event.flags = flags;
170 	vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
171 }
172 
kvm_psci_system_off(struct kvm_vcpu * vcpu)173 static void kvm_psci_system_off(struct kvm_vcpu *vcpu)
174 {
175 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_SHUTDOWN, 0);
176 }
177 
kvm_psci_system_reset(struct kvm_vcpu * vcpu)178 static void kvm_psci_system_reset(struct kvm_vcpu *vcpu)
179 {
180 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET, 0);
181 }
182 
kvm_psci_system_reset2(struct kvm_vcpu * vcpu)183 static void kvm_psci_system_reset2(struct kvm_vcpu *vcpu)
184 {
185 	kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET,
186 				 KVM_SYSTEM_EVENT_RESET_FLAG_PSCI_RESET2);
187 }
188 
kvm_psci_check_allowed_function(struct kvm_vcpu * vcpu,u32 fn)189 static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32 fn)
190 {
191 	/*
192 	 * Prevent 32 bit guests from calling 64 bit PSCI functions.
193 	 */
194 	if ((fn & PSCI_0_2_64BIT) && vcpu_mode_is_32bit(vcpu))
195 		return PSCI_RET_NOT_SUPPORTED;
196 
197 	return 0;
198 }
199 
kvm_psci_0_2_call(struct kvm_vcpu * vcpu)200 static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)
201 {
202 	struct kvm *kvm = vcpu->kvm;
203 	u32 psci_fn = smccc_get_function(vcpu);
204 	unsigned long val;
205 	int ret = 1;
206 
207 	switch (psci_fn) {
208 	case PSCI_0_2_FN_PSCI_VERSION:
209 		/*
210 		 * Bits[31:16] = Major Version = 0
211 		 * Bits[15:0] = Minor Version = 2
212 		 */
213 		val = KVM_ARM_PSCI_0_2;
214 		break;
215 	case PSCI_0_2_FN_CPU_SUSPEND:
216 	case PSCI_0_2_FN64_CPU_SUSPEND:
217 		val = kvm_psci_vcpu_suspend(vcpu);
218 		break;
219 	case PSCI_0_2_FN_CPU_OFF:
220 		kvm_psci_vcpu_off(vcpu);
221 		val = PSCI_RET_SUCCESS;
222 		break;
223 	case PSCI_0_2_FN_CPU_ON:
224 		kvm_psci_narrow_to_32bit(vcpu);
225 		fallthrough;
226 	case PSCI_0_2_FN64_CPU_ON:
227 		mutex_lock(&kvm->lock);
228 		val = kvm_psci_vcpu_on(vcpu);
229 		mutex_unlock(&kvm->lock);
230 		break;
231 	case PSCI_0_2_FN_AFFINITY_INFO:
232 		kvm_psci_narrow_to_32bit(vcpu);
233 		fallthrough;
234 	case PSCI_0_2_FN64_AFFINITY_INFO:
235 		val = kvm_psci_vcpu_affinity_info(vcpu);
236 		break;
237 	case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
238 		/*
239 		 * Trusted OS is MP hence does not require migration
240 	         * or
241 		 * Trusted OS is not present
242 		 */
243 		val = PSCI_0_2_TOS_MP;
244 		break;
245 	case PSCI_0_2_FN_SYSTEM_OFF:
246 		kvm_psci_system_off(vcpu);
247 		/*
248 		 * We shouldn't be going back to guest VCPU after
249 		 * receiving SYSTEM_OFF request.
250 		 *
251 		 * If user space accidentally/deliberately resumes
252 		 * guest VCPU after SYSTEM_OFF request then guest
253 		 * VCPU should see internal failure from PSCI return
254 		 * value. To achieve this, we preload r0 (or x0) with
255 		 * PSCI return value INTERNAL_FAILURE.
256 		 */
257 		val = PSCI_RET_INTERNAL_FAILURE;
258 		ret = 0;
259 		break;
260 	case PSCI_0_2_FN_SYSTEM_RESET:
261 		kvm_psci_system_reset(vcpu);
262 		/*
263 		 * Same reason as SYSTEM_OFF for preloading r0 (or x0)
264 		 * with PSCI return value INTERNAL_FAILURE.
265 		 */
266 		val = PSCI_RET_INTERNAL_FAILURE;
267 		ret = 0;
268 		break;
269 	default:
270 		val = PSCI_RET_NOT_SUPPORTED;
271 		break;
272 	}
273 
274 	smccc_set_retval(vcpu, val, 0, 0, 0);
275 	return ret;
276 }
277 
kvm_psci_1_x_call(struct kvm_vcpu * vcpu,u32 minor)278 static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor)
279 {
280 	u32 psci_fn = smccc_get_function(vcpu);
281 	u32 arg;
282 	unsigned long val;
283 	int ret = 1;
284 
285 	if (minor > 1)
286 		return -EINVAL;
287 
288 	switch(psci_fn) {
289 	case PSCI_0_2_FN_PSCI_VERSION:
290 		val = minor == 0 ? KVM_ARM_PSCI_1_0 : KVM_ARM_PSCI_1_1;
291 		break;
292 	case PSCI_1_0_FN_PSCI_FEATURES:
293 		arg = smccc_get_arg1(vcpu);
294 		val = kvm_psci_check_allowed_function(vcpu, arg);
295 		if (val)
296 			break;
297 
298 		switch(arg) {
299 		case PSCI_0_2_FN_PSCI_VERSION:
300 		case PSCI_0_2_FN_CPU_SUSPEND:
301 		case PSCI_0_2_FN64_CPU_SUSPEND:
302 		case PSCI_0_2_FN_CPU_OFF:
303 		case PSCI_0_2_FN_CPU_ON:
304 		case PSCI_0_2_FN64_CPU_ON:
305 		case PSCI_0_2_FN_AFFINITY_INFO:
306 		case PSCI_0_2_FN64_AFFINITY_INFO:
307 		case PSCI_0_2_FN_MIGRATE_INFO_TYPE:
308 		case PSCI_0_2_FN_SYSTEM_OFF:
309 		case PSCI_0_2_FN_SYSTEM_RESET:
310 		case PSCI_1_0_FN_PSCI_FEATURES:
311 		case ARM_SMCCC_VERSION_FUNC_ID:
312 			val = 0;
313 			break;
314 		case PSCI_1_1_FN_SYSTEM_RESET2:
315 		case PSCI_1_1_FN64_SYSTEM_RESET2:
316 			if (minor >= 1) {
317 				val = 0;
318 				break;
319 			}
320 			fallthrough;
321 		default:
322 			val = PSCI_RET_NOT_SUPPORTED;
323 			break;
324 		}
325 		break;
326 	case PSCI_1_1_FN_SYSTEM_RESET2:
327 		kvm_psci_narrow_to_32bit(vcpu);
328 		fallthrough;
329 	case PSCI_1_1_FN64_SYSTEM_RESET2:
330 		if (minor >= 1) {
331 			arg = smccc_get_arg1(vcpu);
332 
333 			if (arg <= PSCI_1_1_RESET_TYPE_SYSTEM_WARM_RESET ||
334 			    arg >= PSCI_1_1_RESET_TYPE_VENDOR_START) {
335 				kvm_psci_system_reset2(vcpu);
336 				vcpu_set_reg(vcpu, 0, PSCI_RET_INTERNAL_FAILURE);
337 				return 0;
338 			}
339 
340 			val = PSCI_RET_INVALID_PARAMS;
341 			break;
342 		};
343 		fallthrough;
344 	default:
345 		return kvm_psci_0_2_call(vcpu);
346 	}
347 
348 	smccc_set_retval(vcpu, val, 0, 0, 0);
349 	return ret;
350 }
351 
kvm_psci_0_1_call(struct kvm_vcpu * vcpu)352 static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu)
353 {
354 	struct kvm *kvm = vcpu->kvm;
355 	u32 psci_fn = smccc_get_function(vcpu);
356 	unsigned long val;
357 
358 	switch (psci_fn) {
359 	case KVM_PSCI_FN_CPU_OFF:
360 		kvm_psci_vcpu_off(vcpu);
361 		val = PSCI_RET_SUCCESS;
362 		break;
363 	case KVM_PSCI_FN_CPU_ON:
364 		mutex_lock(&kvm->lock);
365 		val = kvm_psci_vcpu_on(vcpu);
366 		mutex_unlock(&kvm->lock);
367 		break;
368 	default:
369 		val = PSCI_RET_NOT_SUPPORTED;
370 		break;
371 	}
372 
373 	smccc_set_retval(vcpu, val, 0, 0, 0);
374 	return 1;
375 }
376 
377 /**
378  * kvm_psci_call - handle PSCI call if r0 value is in range
379  * @vcpu: Pointer to the VCPU struct
380  *
381  * Handle PSCI calls from guests through traps from HVC instructions.
382  * The calling convention is similar to SMC calls to the secure world
383  * where the function number is placed in r0.
384  *
385  * This function returns: > 0 (success), 0 (success but exit to user
386  * space), and < 0 (errors)
387  *
388  * Errors:
389  * -EINVAL: Unrecognized PSCI function
390  */
kvm_psci_call(struct kvm_vcpu * vcpu)391 int kvm_psci_call(struct kvm_vcpu *vcpu)
392 {
393 	u32 psci_fn = smccc_get_function(vcpu);
394 	unsigned long val;
395 
396 	val = kvm_psci_check_allowed_function(vcpu, psci_fn);
397 	if (val) {
398 		smccc_set_retval(vcpu, val, 0, 0, 0);
399 		return 1;
400 	}
401 
402 	switch (kvm_psci_version(vcpu, vcpu->kvm)) {
403 	case KVM_ARM_PSCI_1_1:
404 		return kvm_psci_1_x_call(vcpu, 1);
405 	case KVM_ARM_PSCI_1_0:
406 		return kvm_psci_1_x_call(vcpu, 0);
407 	case KVM_ARM_PSCI_0_2:
408 		return kvm_psci_0_2_call(vcpu);
409 	case KVM_ARM_PSCI_0_1:
410 		return kvm_psci_0_1_call(vcpu);
411 	default:
412 		return -EINVAL;
413 	};
414 }
415 
kvm_arm_get_fw_num_regs(struct kvm_vcpu * vcpu)416 int kvm_arm_get_fw_num_regs(struct kvm_vcpu *vcpu)
417 {
418 	return 4;		/* PSCI version and three workaround registers */
419 }
420 
kvm_arm_copy_fw_reg_indices(struct kvm_vcpu * vcpu,u64 __user * uindices)421 int kvm_arm_copy_fw_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
422 {
423 	if (put_user(KVM_REG_ARM_PSCI_VERSION, uindices++))
424 		return -EFAULT;
425 
426 	if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1, uindices++))
427 		return -EFAULT;
428 
429 	if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2, uindices++))
430 		return -EFAULT;
431 
432 	if (put_user(KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3, uindices++))
433 		return -EFAULT;
434 
435 	return 0;
436 }
437 
438 #define KVM_REG_FEATURE_LEVEL_WIDTH	4
439 #define KVM_REG_FEATURE_LEVEL_MASK	(BIT(KVM_REG_FEATURE_LEVEL_WIDTH) - 1)
440 
441 /*
442  * Convert the workaround level into an easy-to-compare number, where higher
443  * values mean better protection.
444  */
get_kernel_wa_level(u64 regid)445 static int get_kernel_wa_level(u64 regid)
446 {
447 	switch (regid) {
448 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
449 		switch (arm64_get_spectre_v2_state()) {
450 		case SPECTRE_VULNERABLE:
451 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
452 		case SPECTRE_MITIGATED:
453 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_AVAIL;
454 		case SPECTRE_UNAFFECTED:
455 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_REQUIRED;
456 		}
457 		return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1_NOT_AVAIL;
458 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
459 		switch (arm64_get_spectre_v4_state()) {
460 		case SPECTRE_MITIGATED:
461 			/*
462 			 * As for the hypercall discovery, we pretend we
463 			 * don't have any FW mitigation if SSBS is there at
464 			 * all times.
465 			 */
466 			if (cpus_have_final_cap(ARM64_SSBS))
467 				return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL;
468 			fallthrough;
469 		case SPECTRE_UNAFFECTED:
470 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED;
471 		case SPECTRE_VULNERABLE:
472 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL;
473 		}
474 		break;
475 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3:
476 		switch (arm64_get_spectre_bhb_state()) {
477 		case SPECTRE_VULNERABLE:
478 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_AVAIL;
479 		case SPECTRE_MITIGATED:
480 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_AVAIL;
481 		case SPECTRE_UNAFFECTED:
482 			return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_REQUIRED;
483 		}
484 		return KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3_NOT_AVAIL;
485 	}
486 
487 	return -EINVAL;
488 }
489 
kvm_arm_get_fw_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)490 int kvm_arm_get_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
491 {
492 	void __user *uaddr = (void __user *)(long)reg->addr;
493 	u64 val;
494 
495 	switch (reg->id) {
496 	case KVM_REG_ARM_PSCI_VERSION:
497 		val = kvm_psci_version(vcpu, vcpu->kvm);
498 		break;
499 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
500 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
501 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3:
502 		val = get_kernel_wa_level(reg->id) & KVM_REG_FEATURE_LEVEL_MASK;
503 		break;
504 	default:
505 		return -ENOENT;
506 	}
507 
508 	if (copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)))
509 		return -EFAULT;
510 
511 	return 0;
512 }
513 
kvm_arm_set_fw_reg(struct kvm_vcpu * vcpu,const struct kvm_one_reg * reg)514 int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
515 {
516 	void __user *uaddr = (void __user *)(long)reg->addr;
517 	u64 val;
518 	int wa_level;
519 
520 	if (KVM_REG_SIZE(reg->id) != sizeof(val))
521 		return -ENOENT;
522 	if (copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id)))
523 		return -EFAULT;
524 
525 	switch (reg->id) {
526 	case KVM_REG_ARM_PSCI_VERSION:
527 	{
528 		bool wants_02;
529 
530 		wants_02 = test_bit(KVM_ARM_VCPU_PSCI_0_2, vcpu->arch.features);
531 
532 		switch (val) {
533 		case KVM_ARM_PSCI_0_1:
534 			if (wants_02)
535 				return -EINVAL;
536 			vcpu->kvm->arch.psci_version = val;
537 			return 0;
538 		case KVM_ARM_PSCI_0_2:
539 		case KVM_ARM_PSCI_1_0:
540 		case KVM_ARM_PSCI_1_1:
541 			if (!wants_02)
542 				return -EINVAL;
543 			vcpu->kvm->arch.psci_version = val;
544 			return 0;
545 		}
546 		break;
547 	}
548 
549 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
550 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_3:
551 		if (val & ~KVM_REG_FEATURE_LEVEL_MASK)
552 			return -EINVAL;
553 
554 		if (get_kernel_wa_level(reg->id) < val)
555 			return -EINVAL;
556 
557 		return 0;
558 
559 	case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2:
560 		if (val & ~(KVM_REG_FEATURE_LEVEL_MASK |
561 			    KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED))
562 			return -EINVAL;
563 
564 		/* The enabled bit must not be set unless the level is AVAIL. */
565 		if ((val & KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_ENABLED) &&
566 		    (val & KVM_REG_FEATURE_LEVEL_MASK) != KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL)
567 			return -EINVAL;
568 
569 		/*
570 		 * Map all the possible incoming states to the only two we
571 		 * really want to deal with.
572 		 */
573 		switch (val & KVM_REG_FEATURE_LEVEL_MASK) {
574 		case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL:
575 		case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_UNKNOWN:
576 			wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_AVAIL;
577 			break;
578 		case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_AVAIL:
579 		case KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED:
580 			wa_level = KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_2_NOT_REQUIRED;
581 			break;
582 		default:
583 			return -EINVAL;
584 		}
585 
586 		/*
587 		 * We can deal with NOT_AVAIL on NOT_REQUIRED, but not the
588 		 * other way around.
589 		 */
590 		if (get_kernel_wa_level(reg->id) < wa_level)
591 			return -EINVAL;
592 
593 		return 0;
594 	default:
595 		return -ENOENT;
596 	}
597 
598 	return -EINVAL;
599 }
600