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