From patchwork Tue Jun 20 16:33:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 13286203 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87ADDEB64D8 for ; Tue, 20 Jun 2023 16:35:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232181AbjFTQfq (ORCPT ); Tue, 20 Jun 2023 12:35:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232079AbjFTQer (ORCPT ); Tue, 20 Jun 2023 12:34:47 -0400 Received: from out-37.mta0.migadu.com (out-37.mta0.migadu.com [91.218.175.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EE25DC for ; Tue, 20 Jun 2023 09:34:44 -0700 (PDT) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1687278882; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IkhM42NunqJv/e3R8Dd762DlgFbGOO/v/eNMc3dZiCM=; b=GGzrdL0hapImWBWE2cjjy6n942I6FcsG7VzP/BNg//nXmfyMXLozMYffTkRjr2+bLPD5oU YCuI9GAJybLuP83znu+xsn+PNtnCTyuBuaa4eEoHURdAGSo+a7Xd+Z9/pjCAVOnmytK4dR NZyeENriNAX2xWyJsQ/yfvRIySBFYXA= From: Oliver Upton To: kvmarm@lists.linux.dev Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Suzuki K Poulose , Zenghui Yu , Will Deacon , Julien Thierry , Salil Mehta , Oliver Upton Subject: [PATCH v2 14/20] aarch64: psci: Implement CPU_SUSPEND Date: Tue, 20 Jun 2023 11:33:47 -0500 Message-ID: <20230620163353.2688567-15-oliver.upton@linux.dev> In-Reply-To: <20230620163353.2688567-1-oliver.upton@linux.dev> References: <20230620163353.2688567-1-oliver.upton@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Implement support for PSCI CPU_SUSPEND, leveraging in-kernel suspend emulation (i.e. a WFI state). Eagerly resume the vCPU for any wakeup event. Signed-off-by: Oliver Upton --- arm/aarch64/kvm-cpu.c | 18 ++++++++++++++++++ arm/aarch64/psci.c | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/arm/aarch64/kvm-cpu.c b/arm/aarch64/kvm-cpu.c index 4feed9f..316e20c 100644 --- a/arm/aarch64/kvm-cpu.c +++ b/arm/aarch64/kvm-cpu.c @@ -263,6 +263,16 @@ void kvm_cpu__show_registers(struct kvm_cpu *vcpu) dprintf(debug_fd, " LR: 0x%lx\n", data); } +static void handle_wakeup(struct kvm_cpu *vcpu) +{ + struct kvm_mp_state mp_state = { + .mp_state = KVM_MP_STATE_RUNNABLE, + }; + + if (ioctl(vcpu->vcpu_fd, KVM_SET_MP_STATE, &mp_state)) + die_perror("KVM_SET_MP_STATE failed"); +} + bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu) { struct kvm_run *run = vcpu->kvm_run; @@ -271,6 +281,14 @@ bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu) case KVM_EXIT_HYPERCALL: handle_hypercall(vcpu); return true; + case KVM_EXIT_SYSTEM_EVENT: + switch (run->system_event.type) { + case KVM_SYSTEM_EVENT_WAKEUP: + handle_wakeup(vcpu); + return true; + default: + return false; + } default: return false; } diff --git a/arm/aarch64/psci.c b/arm/aarch64/psci.c index 482b9a7..abfdc76 100644 --- a/arm/aarch64/psci.c +++ b/arm/aarch64/psci.c @@ -15,12 +15,27 @@ static void psci_features(struct kvm_cpu *vcpu, struct arm_smccc_res *res) return; switch (arg) { + case PSCI_0_2_FN_CPU_SUSPEND: + case PSCI_0_2_FN64_CPU_SUSPEND: case ARM_SMCCC_VERSION_FUNC_ID: res->a0 = PSCI_RET_SUCCESS; break; } } +static void cpu_suspend(struct kvm_cpu *vcpu, struct arm_smccc_res *res) +{ + struct kvm_mp_state mp_state = { + .mp_state = KVM_MP_STATE_SUSPENDED, + }; + + /* Rely on in-kernel emulation of a 'suspended' (i.e. WFI) state. */ + if (ioctl(vcpu->vcpu_fd, KVM_SET_MP_STATE, &mp_state)) + die_perror("KVM_SET_MP_STATE failed"); + + res->a0 = PSCI_RET_SUCCESS; +} + void handle_psci(struct kvm_cpu *vcpu, struct arm_smccc_res *res) { switch (vcpu->kvm_run->hypercall.nr) { @@ -30,6 +45,10 @@ void handle_psci(struct kvm_cpu *vcpu, struct arm_smccc_res *res) case PSCI_1_0_FN_PSCI_FEATURES: psci_features(vcpu, res); break; + case PSCI_0_2_FN_CPU_SUSPEND: + case PSCI_0_2_FN64_CPU_SUSPEND: + cpu_suspend(vcpu, res); + break; default: res->a0 = PSCI_RET_NOT_SUPPORTED; }