From patchwork Wed Feb 23 04:18:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 1596481 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=Q+yEXSTr; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20210112 header.b=e49Ue+JZ; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4K3N8w2GQhz9sG2 for ; Wed, 23 Feb 2022 15:19:28 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=bNk0XRyWcEdme0G7STUvSD5NTp7+mHP3iZm0N2SvzgI=; b=Q+yEXSTrK35D7e+a2n85UURhyM CnXmvrr+rQ0EBItlOvjdH3Dr+pRf0Uyotqr2wa/PKcsNx6zAS9JR24tGQ8saMe3d3H+eiDsyDU1V/ SB1XCeBYdZSeaGkfMgZB9hR4eRv3MN+i0dIzaJabttwVe8+vQiTwx1wOCZv14MCeYfES7+jnGLVHE M8eXmgiNdPEQ0q5/rkrg/MUsEq7Xr+/RQh+3JAoWOgJvm5Pr1RPZkEx/VH+aobFKGpIZZazNY4RLI 4uLAz7lWmxVKxWzP0byAyA7IqOTANN2IK46nX6vvRA6A332an1dN5YXsHrPfu8CbaqHzhPYapVLAZ mRv+4Rmg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nMj7e-00CmQz-3y; Wed, 23 Feb 2022 04:19:26 +0000 Received: from mail-io1-xd49.google.com ([2607:f8b0:4864:20::d49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nMj7a-00CmNS-PP for kvm-riscv@lists.infradead.org; Wed, 23 Feb 2022 04:19:24 +0000 Received: by mail-io1-xd49.google.com with SMTP id z23-20020a5ec917000000b0064142c95dc1so4076206iol.20 for ; Tue, 22 Feb 2022 20:19:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=FNS4PyjEGxuJSaCVEOu72QQrSSYAZtKW2QUnGIVfau0=; b=e49Ue+JZMKJTF4FKVvs05LKdC7XnGotaG6I7g92j1SjIIS1FDLH1cN/N1EgS1P5ER+ gah/IkXs0/E7eMhyaJG1u5bystIS2zgyEXKjvahD5csHmw6iNiaPVCNsuCKAujpio2x4 x5h2Y66ca5V8RAvVKUdeUFdil43Yb/urcWb4FnhtRnCXPgeJLE2YgySa6eQJxpuDJeub LVjCsoTUqgB8NNAVskSi+oM+Jb0ENgqa5jZI21tOSFGXaYCGAhkduH5IHnjn+25Jhx+1 zK5j2ka7vSR2+bvhw5Z9Ql0PqR2fqoidoh4i9qnIHMVtxgpRkiuQ5XWuf3uMkjt8aeqD XYfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=FNS4PyjEGxuJSaCVEOu72QQrSSYAZtKW2QUnGIVfau0=; b=HmLwHpDV5f+9MxHP84/UVVgISm4Kom8lCZ548X2GXBasgVDVKbylq8RpKqcgdNAliE fIDYS6U7T8bffZkr6eqF4xoAlAAmF8KSZ9MGIg08b1gPWaei7pEV09raHlhHyGtdVo9z 9FqI2h3AcDZYt02gAiUocrzPNbBq1ySaGxs950ZZqEw07reNoHjJ3kvSWO3HZgJiIXSX s7NIRMzZujulR98DTrAHZBEvlcOVVW55mlcBCviAZcHXorX1TUMj8Bs91Ez8OLYFw3Ww 2SAii85CMlId4kgSRxwafGpbURIw0BFEhSxtomJ6fTcVL9g3wABY2GmN538Wa3Yiau9v OOag== X-Gm-Message-State: AOAM533NYK3bJBt23Xc9y0/sQfGc8RIazvba2Zhls4cIO/AdvrV+RGmv DU+a92um7qCT7Lp/l4LfRIYdIxcm008= X-Google-Smtp-Source: ABdhPJzk4sIaIqCtMwTE2XY7UxiYHAN+OIvodZsnh0NYxLMGJieovAoGPOgba0C6vnJ4V/BJcawGQCT4v2w= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6602:26d3:b0:63d:aa17:8742 with SMTP id g19-20020a05660226d300b0063daa178742mr21631674ioo.198.1645589961514; Tue, 22 Feb 2022 20:19:21 -0800 (PST) Date: Wed, 23 Feb 2022 04:18:34 +0000 In-Reply-To: <20220223041844.3984439-1-oupton@google.com> Message-Id: <20220223041844.3984439-10-oupton@google.com> Mime-Version: 1.0 References: <20220223041844.3984439-1-oupton@google.com> X-Mailer: git-send-email 2.35.1.473.g83b2b277ed-goog Subject: [PATCH v3 09/19] KVM: arm64: Implement PSCI SYSTEM_SUSPEND From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: Paolo Bonzini , Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Anup Patel , Atish Patra , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, Peter Shier , Reiji Watanabe , Ricardo Koller , Raghavendra Rao Ananta , Jing Zhang , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220222_201922_855903_959663CC X-CRM114-Status: GOOD ( 16.27 ) X-Spam-Score: -7.7 (-------) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: ARM DEN0022D.b 5.19 "SYSTEM_SUSPEND" describes a PSCI call that allows software to request that a system be placed in the deepest possible low-power state. Effectively, software can use this to suspen [...] Content analysis details: (-7.7 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:d49 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -7.5 USER_IN_DEF_DKIM_WL From: address is in the default DKIM white-list -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.0 DKIMWL_WL_MED DKIMwl.org - Medium trust sender X-BeenThere: kvm-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kvm-riscv" Errors-To: kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org ARM DEN0022D.b 5.19 "SYSTEM_SUSPEND" describes a PSCI call that allows software to request that a system be placed in the deepest possible low-power state. Effectively, software can use this to suspend itself to RAM. Note that the semantics of this PSCI call are very similar to CPU_SUSPEND, which is already implemented in KVM. Implement the SYSTEM_SUSPEND in KVM. Similar to CPU_SUSPEND, the low-power state is implemented as a guest WFI. Synchronously reset the calling CPU before entering the WFI, such that the vCPU may immediately resume execution when a wakeup event is recognized. Signed-off-by: Oliver Upton --- arch/arm64/kvm/psci.c | 51 ++++++++++++++++++++++++++++++++++++++++++ arch/arm64/kvm/reset.c | 3 ++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/psci.c b/arch/arm64/kvm/psci.c index 77a00913cdfd..41adaaf2234a 100644 --- a/arch/arm64/kvm/psci.c +++ b/arch/arm64/kvm/psci.c @@ -208,6 +208,50 @@ static void kvm_psci_system_reset(struct kvm_vcpu *vcpu) kvm_prepare_system_event(vcpu, KVM_SYSTEM_EVENT_RESET); } +static int kvm_psci_system_suspend(struct kvm_vcpu *vcpu) +{ + struct vcpu_reset_state reset_state; + struct kvm *kvm = vcpu->kvm; + struct kvm_vcpu *tmp; + bool denied = false; + unsigned long i; + + reset_state.pc = smccc_get_arg1(vcpu); + if (!kvm_ipa_valid(kvm, reset_state.pc)) { + smccc_set_retval(vcpu, PSCI_RET_INVALID_ADDRESS, 0, 0, 0); + return 1; + } + + reset_state.r0 = smccc_get_arg2(vcpu); + reset_state.be = kvm_vcpu_is_be(vcpu); + reset_state.reset = true; + + /* + * The SYSTEM_SUSPEND PSCI call requires that all vCPUs (except the + * calling vCPU) be in an OFF state, as determined by the + * implementation. + * + * See ARM DEN0022D, 5.19 "SYSTEM_SUSPEND" for more details. + */ + mutex_lock(&kvm->lock); + kvm_for_each_vcpu(i, tmp, kvm) { + if (tmp != vcpu && !kvm_arm_vcpu_powered_off(tmp)) { + denied = true; + break; + } + } + mutex_unlock(&kvm->lock); + + if (denied) { + smccc_set_retval(vcpu, PSCI_RET_DENIED, 0, 0, 0); + return 1; + } + + __kvm_reset_vcpu(vcpu, &reset_state); + kvm_vcpu_wfi(vcpu); + return 1; +} + static void kvm_psci_narrow_to_32bit(struct kvm_vcpu *vcpu) { int i; @@ -343,6 +387,8 @@ static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu) case PSCI_0_2_FN_MIGRATE_INFO_TYPE: case PSCI_0_2_FN_SYSTEM_OFF: case PSCI_0_2_FN_SYSTEM_RESET: + case PSCI_1_0_FN_SYSTEM_SUSPEND: + case PSCI_1_0_FN64_SYSTEM_SUSPEND: case PSCI_1_0_FN_PSCI_FEATURES: case ARM_SMCCC_VERSION_FUNC_ID: val = 0; @@ -352,6 +398,11 @@ static int kvm_psci_1_0_call(struct kvm_vcpu *vcpu) break; } break; + case PSCI_1_0_FN_SYSTEM_SUSPEND: + kvm_psci_narrow_to_32bit(vcpu); + fallthrough; + case PSCI_1_0_FN64_SYSTEM_SUSPEND: + return kvm_psci_system_suspend(vcpu); default: return kvm_psci_0_2_call(vcpu); } diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c index f879a8f6a99c..006e7a75ceba 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -215,7 +215,8 @@ static bool vcpu_allowed_register_width(struct kvm_vcpu *vcpu) * * Note: This function can be called from two paths: * - The KVM_ARM_VCPU_INIT ioctl - * - handling a request issued by another VCPU in the PSCI handling code + * - handling a request issued by possibly another VCPU in the PSCI handling + * code * * In the first case, the VCPU will not be loaded, and in the second case the * VCPU will be loaded. Because this function operates purely on the