16689fb8fSOliver Upton // SPDX-License-Identifier: GPL-2.0-only
26689fb8fSOliver Upton /*
3*448e7116SOliver Upton  * psci_test - Tests relating to KVM's PSCI implementation.
46689fb8fSOliver Upton  *
56689fb8fSOliver Upton  * Copyright (c) 2021 Google LLC.
66689fb8fSOliver Upton  *
7*448e7116SOliver Upton  * This test includes:
8*448e7116SOliver Upton  *  - A regression test for a race between KVM servicing the PSCI CPU_ON call
9*448e7116SOliver Upton  *    and userspace reading the targeted vCPU's registers.
10*448e7116SOliver Upton  *  - A test for KVM's handling of PSCI SYSTEM_SUSPEND and the associated
11*448e7116SOliver Upton  *    KVM_SYSTEM_EVENT_SUSPEND UAPI.
126689fb8fSOliver Upton  */
136689fb8fSOliver Upton 
146689fb8fSOliver Upton #define _GNU_SOURCE
156689fb8fSOliver Upton 
166689fb8fSOliver Upton #include <linux/psci.h>
176689fb8fSOliver Upton 
186689fb8fSOliver Upton #include "kvm_util.h"
196689fb8fSOliver Upton #include "processor.h"
206689fb8fSOliver Upton #include "test_util.h"
216689fb8fSOliver Upton 
226689fb8fSOliver Upton #define CPU_ON_ENTRY_ADDR 0xfeedf00dul
236689fb8fSOliver Upton #define CPU_ON_CONTEXT_ID 0xdeadc0deul
246689fb8fSOliver Upton 
256689fb8fSOliver Upton static uint64_t psci_cpu_on(uint64_t target_cpu, uint64_t entry_addr,
266689fb8fSOliver Upton 			    uint64_t context_id)
276689fb8fSOliver Upton {
28694e3dccSOliver Upton 	struct arm_smccc_res res;
296689fb8fSOliver Upton 
30694e3dccSOliver Upton 	smccc_hvc(PSCI_0_2_FN64_CPU_ON, target_cpu, entry_addr, context_id,
31694e3dccSOliver Upton 		  0, 0, 0, 0, &res);
326689fb8fSOliver Upton 
33694e3dccSOliver Upton 	return res.a0;
346689fb8fSOliver Upton }
356689fb8fSOliver Upton 
366689fb8fSOliver Upton static uint64_t psci_affinity_info(uint64_t target_affinity,
376689fb8fSOliver Upton 				   uint64_t lowest_affinity_level)
386689fb8fSOliver Upton {
39694e3dccSOliver Upton 	struct arm_smccc_res res;
406689fb8fSOliver Upton 
41694e3dccSOliver Upton 	smccc_hvc(PSCI_0_2_FN64_AFFINITY_INFO, target_affinity, lowest_affinity_level,
42694e3dccSOliver Upton 		  0, 0, 0, 0, 0, &res);
436689fb8fSOliver Upton 
44694e3dccSOliver Upton 	return res.a0;
456689fb8fSOliver Upton }
466689fb8fSOliver Upton 
47b26dafc8SOliver Upton static uint64_t psci_system_suspend(uint64_t entry_addr, uint64_t context_id)
48b26dafc8SOliver Upton {
49b26dafc8SOliver Upton 	struct arm_smccc_res res;
50b26dafc8SOliver Upton 
51b26dafc8SOliver Upton 	smccc_hvc(PSCI_1_0_FN64_SYSTEM_SUSPEND, entry_addr, context_id,
52b26dafc8SOliver Upton 		  0, 0, 0, 0, 0, &res);
53b26dafc8SOliver Upton 
54b26dafc8SOliver Upton 	return res.a0;
55b26dafc8SOliver Upton }
56b26dafc8SOliver Upton 
57b26dafc8SOliver Upton static uint64_t psci_features(uint32_t func_id)
58b26dafc8SOliver Upton {
59b26dafc8SOliver Upton 	struct arm_smccc_res res;
60b26dafc8SOliver Upton 
61b26dafc8SOliver Upton 	smccc_hvc(PSCI_1_0_FN_PSCI_FEATURES, func_id, 0, 0, 0, 0, 0, 0, &res);
62b26dafc8SOliver Upton 
63b26dafc8SOliver Upton 	return res.a0;
64b26dafc8SOliver Upton }
65b26dafc8SOliver Upton 
66b093da65SSean Christopherson static void vcpu_power_off(struct kvm_vcpu *vcpu)
676689fb8fSOliver Upton {
6867a36a82SOliver Upton 	struct kvm_mp_state mp_state = {
6967a36a82SOliver Upton 		.mp_state = KVM_MP_STATE_STOPPED,
7067a36a82SOliver Upton 	};
7167a36a82SOliver Upton 
72768e9a61SSean Christopherson 	vcpu_mp_state_set(vcpu, &mp_state);
7367a36a82SOliver Upton }
7467a36a82SOliver Upton 
75b093da65SSean Christopherson static struct kvm_vm *setup_vm(void *guest_code, struct kvm_vcpu **source,
76b093da65SSean Christopherson 			       struct kvm_vcpu **target)
7767a36a82SOliver Upton {
7867a36a82SOliver Upton 	struct kvm_vcpu_init init;
7967a36a82SOliver Upton 	struct kvm_vm *vm;
8067a36a82SOliver Upton 
816e1d13bfSSean Christopherson 	vm = vm_create(2);
8267a36a82SOliver Upton 	ucall_init(vm, NULL);
8367a36a82SOliver Upton 
8467a36a82SOliver Upton 	vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init);
8567a36a82SOliver Upton 	init.features[0] |= (1 << KVM_ARM_VCPU_PSCI_0_2);
8667a36a82SOliver Upton 
87b093da65SSean Christopherson 	*source = aarch64_vcpu_add(vm, 0, &init, guest_code);
88b093da65SSean Christopherson 	*target = aarch64_vcpu_add(vm, 1, &init, guest_code);
8967a36a82SOliver Upton 
9067a36a82SOliver Upton 	return vm;
9167a36a82SOliver Upton }
9267a36a82SOliver Upton 
93b093da65SSean Christopherson static void enter_guest(struct kvm_vcpu *vcpu)
9467a36a82SOliver Upton {
9567a36a82SOliver Upton 	struct ucall uc;
9667a36a82SOliver Upton 
97768e9a61SSean Christopherson 	vcpu_run(vcpu);
98768e9a61SSean Christopherson 	if (get_ucall(vcpu, &uc) == UCALL_ABORT)
99594a1c27SColton Lewis 		REPORT_GUEST_ASSERT(uc);
10067a36a82SOliver Upton }
10167a36a82SOliver Upton 
102b093da65SSean Christopherson static void assert_vcpu_reset(struct kvm_vcpu *vcpu)
10367a36a82SOliver Upton {
10467a36a82SOliver Upton 	uint64_t obs_pc, obs_x0;
10567a36a82SOliver Upton 
106768e9a61SSean Christopherson 	vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pc), &obs_pc);
107768e9a61SSean Christopherson 	vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.regs[0]), &obs_x0);
10867a36a82SOliver Upton 
10967a36a82SOliver Upton 	TEST_ASSERT(obs_pc == CPU_ON_ENTRY_ADDR,
11067a36a82SOliver Upton 		    "unexpected target cpu pc: %lx (expected: %lx)",
11167a36a82SOliver Upton 		    obs_pc, CPU_ON_ENTRY_ADDR);
11267a36a82SOliver Upton 	TEST_ASSERT(obs_x0 == CPU_ON_CONTEXT_ID,
11367a36a82SOliver Upton 		    "unexpected target context id: %lx (expected: %lx)",
11467a36a82SOliver Upton 		    obs_x0, CPU_ON_CONTEXT_ID);
11567a36a82SOliver Upton }
11667a36a82SOliver Upton 
11767a36a82SOliver Upton static void guest_test_cpu_on(uint64_t target_cpu)
11867a36a82SOliver Upton {
1196689fb8fSOliver Upton 	uint64_t target_state;
1206689fb8fSOliver Upton 
12167a36a82SOliver Upton 	GUEST_ASSERT(!psci_cpu_on(target_cpu, CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID));
12267a36a82SOliver Upton 
1236689fb8fSOliver Upton 	do {
1246689fb8fSOliver Upton 		target_state = psci_affinity_info(target_cpu, 0);
1256689fb8fSOliver Upton 
1266689fb8fSOliver Upton 		GUEST_ASSERT((target_state == PSCI_0_2_AFFINITY_LEVEL_ON) ||
1276689fb8fSOliver Upton 			     (target_state == PSCI_0_2_AFFINITY_LEVEL_OFF));
1286689fb8fSOliver Upton 	} while (target_state != PSCI_0_2_AFFINITY_LEVEL_ON);
1296689fb8fSOliver Upton 
1306689fb8fSOliver Upton 	GUEST_DONE();
1316689fb8fSOliver Upton }
1326689fb8fSOliver Upton 
13367a36a82SOliver Upton static void host_test_cpu_on(void)
134d135399aSOliver Upton {
135b093da65SSean Christopherson 	struct kvm_vcpu *source, *target;
13667a36a82SOliver Upton 	uint64_t target_mpidr;
1376689fb8fSOliver Upton 	struct kvm_vm *vm;
1386689fb8fSOliver Upton 	struct ucall uc;
1396689fb8fSOliver Upton 
140b093da65SSean Christopherson 	vm = setup_vm(guest_test_cpu_on, &source, &target);
1416689fb8fSOliver Upton 
1426689fb8fSOliver Upton 	/*
1436689fb8fSOliver Upton 	 * make sure the target is already off when executing the test.
1446689fb8fSOliver Upton 	 */
145b093da65SSean Christopherson 	vcpu_power_off(target);
1466689fb8fSOliver Upton 
147768e9a61SSean Christopherson 	vcpu_get_reg(target, KVM_ARM64_SYS_REG(SYS_MPIDR_EL1), &target_mpidr);
148768e9a61SSean Christopherson 	vcpu_args_set(source, 1, target_mpidr & MPIDR_HWID_BITMASK);
149b093da65SSean Christopherson 	enter_guest(source);
1506689fb8fSOliver Upton 
151768e9a61SSean Christopherson 	if (get_ucall(source, &uc) != UCALL_DONE)
1526689fb8fSOliver Upton 		TEST_FAIL("Unhandled ucall: %lu", uc.cmd);
15367a36a82SOliver Upton 
154b093da65SSean Christopherson 	assert_vcpu_reset(target);
15567a36a82SOliver Upton 	kvm_vm_free(vm);
1566689fb8fSOliver Upton }
1576689fb8fSOliver Upton 
158b26dafc8SOliver Upton static void guest_test_system_suspend(void)
159b26dafc8SOliver Upton {
160b26dafc8SOliver Upton 	uint64_t ret;
161b26dafc8SOliver Upton 
162b26dafc8SOliver Upton 	/* assert that SYSTEM_SUSPEND is discoverable */
163b26dafc8SOliver Upton 	GUEST_ASSERT(!psci_features(PSCI_1_0_FN_SYSTEM_SUSPEND));
164b26dafc8SOliver Upton 	GUEST_ASSERT(!psci_features(PSCI_1_0_FN64_SYSTEM_SUSPEND));
165b26dafc8SOliver Upton 
166b26dafc8SOliver Upton 	ret = psci_system_suspend(CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID);
167b26dafc8SOliver Upton 	GUEST_SYNC(ret);
168b26dafc8SOliver Upton }
169b26dafc8SOliver Upton 
170b26dafc8SOliver Upton static void host_test_system_suspend(void)
171b26dafc8SOliver Upton {
172b093da65SSean Christopherson 	struct kvm_vcpu *source, *target;
173b26dafc8SOliver Upton 	struct kvm_run *run;
174b26dafc8SOliver Upton 	struct kvm_vm *vm;
175b26dafc8SOliver Upton 
176b093da65SSean Christopherson 	vm = setup_vm(guest_test_system_suspend, &source, &target);
177a12c86c4SSean Christopherson 	vm_enable_cap(vm, KVM_CAP_ARM_SYSTEM_SUSPEND, 0);
178b26dafc8SOliver Upton 
179b093da65SSean Christopherson 	vcpu_power_off(target);
180b093da65SSean Christopherson 	run = source->run;
181b26dafc8SOliver Upton 
182b093da65SSean Christopherson 	enter_guest(source);
183b26dafc8SOliver Upton 
184b26dafc8SOliver Upton 	TEST_ASSERT(run->exit_reason == KVM_EXIT_SYSTEM_EVENT,
185b26dafc8SOliver Upton 		    "Unhandled exit reason: %u (%s)",
186b26dafc8SOliver Upton 		    run->exit_reason, exit_reason_str(run->exit_reason));
187b26dafc8SOliver Upton 	TEST_ASSERT(run->system_event.type == KVM_SYSTEM_EVENT_SUSPEND,
188b26dafc8SOliver Upton 		    "Unhandled system event: %u (expected: %u)",
189b26dafc8SOliver Upton 		    run->system_event.type, KVM_SYSTEM_EVENT_SUSPEND);
190b26dafc8SOliver Upton 
191b26dafc8SOliver Upton 	kvm_vm_free(vm);
192b26dafc8SOliver Upton }
193b26dafc8SOliver Upton 
19467a36a82SOliver Upton int main(void)
19567a36a82SOliver Upton {
1969393cb13SSean Christopherson 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_SYSTEM_SUSPEND));
197b26dafc8SOliver Upton 
19867a36a82SOliver Upton 	host_test_cpu_on();
199b26dafc8SOliver Upton 	host_test_system_suspend();
2006689fb8fSOliver Upton 	return 0;
2016689fb8fSOliver Upton }
202