16689fb8fSOliver Upton // SPDX-License-Identifier: GPL-2.0-only 26689fb8fSOliver Upton /* 3448e7116SOliver Upton * psci_test - Tests relating to KVM's PSCI implementation. 46689fb8fSOliver Upton * 56689fb8fSOliver Upton * Copyright (c) 2021 Google LLC. 66689fb8fSOliver Upton * 7448e7116SOliver Upton * This test includes: 8448e7116SOliver Upton * - A regression test for a race between KVM servicing the PSCI CPU_ON call 9448e7116SOliver Upton * and userspace reading the targeted vCPU's registers. 10448e7116SOliver Upton * - A test for KVM's handling of PSCI SYSTEM_SUSPEND and the associated 11448e7116SOliver 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 8367a36a82SOliver Upton vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init); 8467a36a82SOliver Upton init.features[0] |= (1 << KVM_ARM_VCPU_PSCI_0_2); 8567a36a82SOliver Upton 86b093da65SSean Christopherson *source = aarch64_vcpu_add(vm, 0, &init, guest_code); 87b093da65SSean Christopherson *target = aarch64_vcpu_add(vm, 1, &init, guest_code); 8867a36a82SOliver Upton 8967a36a82SOliver Upton return vm; 9067a36a82SOliver Upton } 9167a36a82SOliver Upton 92b093da65SSean Christopherson static void enter_guest(struct kvm_vcpu *vcpu) 9367a36a82SOliver Upton { 9467a36a82SOliver Upton struct ucall uc; 9567a36a82SOliver Upton 96768e9a61SSean Christopherson vcpu_run(vcpu); 97768e9a61SSean Christopherson if (get_ucall(vcpu, &uc) == UCALL_ABORT) 98594a1c27SColton Lewis REPORT_GUEST_ASSERT(uc); 9967a36a82SOliver Upton } 10067a36a82SOliver Upton 101b093da65SSean Christopherson static void assert_vcpu_reset(struct kvm_vcpu *vcpu) 10267a36a82SOliver Upton { 10367a36a82SOliver Upton uint64_t obs_pc, obs_x0; 10467a36a82SOliver Upton 105768e9a61SSean Christopherson vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pc), &obs_pc); 106768e9a61SSean Christopherson vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.regs[0]), &obs_x0); 10767a36a82SOliver Upton 10867a36a82SOliver Upton TEST_ASSERT(obs_pc == CPU_ON_ENTRY_ADDR, 10967a36a82SOliver Upton "unexpected target cpu pc: %lx (expected: %lx)", 11067a36a82SOliver Upton obs_pc, CPU_ON_ENTRY_ADDR); 11167a36a82SOliver Upton TEST_ASSERT(obs_x0 == CPU_ON_CONTEXT_ID, 11267a36a82SOliver Upton "unexpected target context id: %lx (expected: %lx)", 11367a36a82SOliver Upton obs_x0, CPU_ON_CONTEXT_ID); 11467a36a82SOliver Upton } 11567a36a82SOliver Upton 11667a36a82SOliver Upton static void guest_test_cpu_on(uint64_t target_cpu) 11767a36a82SOliver Upton { 1186689fb8fSOliver Upton uint64_t target_state; 1196689fb8fSOliver Upton 12067a36a82SOliver Upton GUEST_ASSERT(!psci_cpu_on(target_cpu, CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID)); 12167a36a82SOliver Upton 1226689fb8fSOliver Upton do { 1236689fb8fSOliver Upton target_state = psci_affinity_info(target_cpu, 0); 1246689fb8fSOliver Upton 1256689fb8fSOliver Upton GUEST_ASSERT((target_state == PSCI_0_2_AFFINITY_LEVEL_ON) || 1266689fb8fSOliver Upton (target_state == PSCI_0_2_AFFINITY_LEVEL_OFF)); 1276689fb8fSOliver Upton } while (target_state != PSCI_0_2_AFFINITY_LEVEL_ON); 1286689fb8fSOliver Upton 1296689fb8fSOliver Upton GUEST_DONE(); 1306689fb8fSOliver Upton } 1316689fb8fSOliver Upton 13267a36a82SOliver Upton static void host_test_cpu_on(void) 133d135399aSOliver Upton { 134b093da65SSean Christopherson struct kvm_vcpu *source, *target; 13567a36a82SOliver Upton uint64_t target_mpidr; 1366689fb8fSOliver Upton struct kvm_vm *vm; 1376689fb8fSOliver Upton struct ucall uc; 1386689fb8fSOliver Upton 139b093da65SSean Christopherson vm = setup_vm(guest_test_cpu_on, &source, &target); 1406689fb8fSOliver Upton 1416689fb8fSOliver Upton /* 1426689fb8fSOliver Upton * make sure the target is already off when executing the test. 1436689fb8fSOliver Upton */ 144b093da65SSean Christopherson vcpu_power_off(target); 1456689fb8fSOliver Upton 146768e9a61SSean Christopherson vcpu_get_reg(target, KVM_ARM64_SYS_REG(SYS_MPIDR_EL1), &target_mpidr); 147768e9a61SSean Christopherson vcpu_args_set(source, 1, target_mpidr & MPIDR_HWID_BITMASK); 148b093da65SSean Christopherson enter_guest(source); 1496689fb8fSOliver Upton 150768e9a61SSean Christopherson if (get_ucall(source, &uc) != UCALL_DONE) 1516689fb8fSOliver Upton TEST_FAIL("Unhandled ucall: %lu", uc.cmd); 15267a36a82SOliver Upton 153b093da65SSean Christopherson assert_vcpu_reset(target); 15467a36a82SOliver Upton kvm_vm_free(vm); 1556689fb8fSOliver Upton } 1566689fb8fSOliver Upton 157b26dafc8SOliver Upton static void guest_test_system_suspend(void) 158b26dafc8SOliver Upton { 159b26dafc8SOliver Upton uint64_t ret; 160b26dafc8SOliver Upton 161b26dafc8SOliver Upton /* assert that SYSTEM_SUSPEND is discoverable */ 162b26dafc8SOliver Upton GUEST_ASSERT(!psci_features(PSCI_1_0_FN_SYSTEM_SUSPEND)); 163b26dafc8SOliver Upton GUEST_ASSERT(!psci_features(PSCI_1_0_FN64_SYSTEM_SUSPEND)); 164b26dafc8SOliver Upton 165b26dafc8SOliver Upton ret = psci_system_suspend(CPU_ON_ENTRY_ADDR, CPU_ON_CONTEXT_ID); 166b26dafc8SOliver Upton GUEST_SYNC(ret); 167b26dafc8SOliver Upton } 168b26dafc8SOliver Upton 169b26dafc8SOliver Upton static void host_test_system_suspend(void) 170b26dafc8SOliver Upton { 171b093da65SSean Christopherson struct kvm_vcpu *source, *target; 172b26dafc8SOliver Upton struct kvm_run *run; 173b26dafc8SOliver Upton struct kvm_vm *vm; 174b26dafc8SOliver Upton 175b093da65SSean Christopherson vm = setup_vm(guest_test_system_suspend, &source, &target); 176a12c86c4SSean Christopherson vm_enable_cap(vm, KVM_CAP_ARM_SYSTEM_SUSPEND, 0); 177b26dafc8SOliver Upton 178b093da65SSean Christopherson vcpu_power_off(target); 179b093da65SSean Christopherson run = source->run; 180b26dafc8SOliver Upton 181b093da65SSean Christopherson enter_guest(source); 182b26dafc8SOliver Upton 183*c96f57b0SVipin Sharma TEST_ASSERT_KVM_EXIT_REASON(source, KVM_EXIT_SYSTEM_EVENT); 184b26dafc8SOliver Upton TEST_ASSERT(run->system_event.type == KVM_SYSTEM_EVENT_SUSPEND, 185b26dafc8SOliver Upton "Unhandled system event: %u (expected: %u)", 186b26dafc8SOliver Upton run->system_event.type, KVM_SYSTEM_EVENT_SUSPEND); 187b26dafc8SOliver Upton 188b26dafc8SOliver Upton kvm_vm_free(vm); 189b26dafc8SOliver Upton } 190b26dafc8SOliver Upton 19167a36a82SOliver Upton int main(void) 19267a36a82SOliver Upton { 1939393cb13SSean Christopherson TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_SYSTEM_SUSPEND)); 194b26dafc8SOliver Upton 19567a36a82SOliver Upton host_test_cpu_on(); 196b26dafc8SOliver Upton host_test_system_suspend(); 1976689fb8fSOliver Upton return 0; 1986689fb8fSOliver Upton } 199