From patchwork Wed Nov 4 18:36:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Brazdil X-Patchwork-Id: 11881897 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2AA39C2D0A3 for ; Wed, 4 Nov 2020 18:40:15 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B573220719 for ; Wed, 4 Nov 2020 18:40:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="mnVcm9GZ"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="gEbk0tKi" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B573220719 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=igft53IzT5DU81Xp0rZqo0v796Ab3mdXZBK3T72ZImk=; b=mnVcm9GZslKqxcgwrI7ozw5tL OPuotkj/o6r14+myMdxAW3Hxmkbele32VK8x67S6yXTGhIs1TPwLTvjN6U+TEZxUxduWEDk5AY59P v22GJWS8ICOqBDWZAE6cHdxyCmcXh/BryWJO3EK75gXB8vQWL35txnuLyZKhU9isnJ9DXqeNdka4G 6B+V+04bxpU9ICL4BspE5kI2jwdOcmZk0Tw4QCJCzIWDk1IT/PkmUqGOfWN2x8p9Uoi3ru8l2PlD3 R6fQA4vOpJQfbIvhTongDv8Iti8kXCcYOzgDPkfNlX8krKveuMqYAe7jh2B2H+JoV3qYNpphL3HXu sQW/Vvftw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaNgy-0003BI-St; Wed, 04 Nov 2020 18:39:33 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaNen-0001ru-OY for linux-arm-kernel@lists.infradead.org; Wed, 04 Nov 2020 18:37:20 +0000 Received: by mail-wm1-x342.google.com with SMTP id s13so115444wmh.4 for ; Wed, 04 Nov 2020 10:37:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=k1mZbgF5Ouf6celHonuP3A/s1Eh7mvuRv0TPOHTEAQo=; b=gEbk0tKiTb2BPXDRISpv4WCdZLKXkbNLCjo9s0sAl7NmT5+hkNrkumyGOXB7xc/JYH YXXMWPL4izPCNAKWaVBjsFPxiTj85GP1FLZnDTuyUdZ7/PODY8ozmEI3bzsOGhIqrlPj PR5DFKv8a9eoIKBPgbZOXc7z6HwbThHepHAzSJ6MXbm/AvlBnQLNI1xKBJUgOs7R+/GI JpqrxNWZZk9x5mTbJY2i9dQ4QZtZ/ZxKVbnD2PT5EtwNYOfFK8lGQdFnx7W1IwaRFg7W n0BgSJehlQZsUSwbm1Db3EwQnJf2RK63eIxGuJPtxBov4BTrqTjJKFIyEAa0/J8LrmfX Z3qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=k1mZbgF5Ouf6celHonuP3A/s1Eh7mvuRv0TPOHTEAQo=; b=Sj6GWz9zIwdVKVYf4MTAjDt3/KH7BiQWkGp3qNdQHs9A0S6qyKLRd7H8zQHJX+kCE3 Rr9ru0DhdWgRBfiUU9BnH9n6kmQCygNmEAXpKjDp/mc0ZBGmhXmCuMT0OekzTbLKoO64 i+6TsYaIjR7ZJb6l9DQrvEF1E+kQgy8V35u4SZ5Ql4ctlBSMvQFwykNE/BxQS1cbcSB+ IXt9c5f6b+2GFlFoy5G3/e0f9WqD67WxQu2TdOYN/jS+iZmZaeW+d9NKoRn7ZUNf11CQ rFgLQrpVy1c9v22xck6gZRy7oQ9U+2b/M0XCxSDB0QyIqV3mFFf0C1P55jhI44TUM1p2 P2SA== X-Gm-Message-State: AOAM530DvsvdqLdZdSZMkEVrSjK6/Yxwt1gc7cP2T/vlU6xfMdNeWFvl Xnq5nVMazxD5rkODsKfl/NUewQ== X-Google-Smtp-Source: ABdhPJwJO5eUF6ZkZBcH8S7L48cw9sDQ5/YjzG6ipw3jIJSUlXblzToWcdhHw2LopEHH3RHQGlLScg== X-Received: by 2002:a7b:cf1a:: with SMTP id l26mr6109935wmg.18.1604515036611; Wed, 04 Nov 2020 10:37:16 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:c8d2:30f2:53c6:bc2]) by smtp.gmail.com with ESMTPSA id e7sm4292303wrm.6.2020.11.04.10.37.14 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Nov 2020 10:37:15 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Subject: [RFC PATCH 18/26] kvm: arm64: Intercept PSCI_CPU_OFF host SMC calls Date: Wed, 4 Nov 2020 18:36:22 +0000 Message-Id: <20201104183630.27513-19-dbrazdil@google.com> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201104183630.27513-1-dbrazdil@google.com> References: <20201104183630.27513-1-dbrazdil@google.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201104_133717_937961_FF6B5768 X-CRM114-Status: GOOD ( 19.35 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Lorenzo Pieralisi , kernel-team@android.com, Suzuki K Poulose , Marc Zyngier , Quentin Perret , linux-kernel@vger.kernel.org, James Morse , linux-arm-kernel@lists.infradead.org, Catalin Marinas , Tejun Heo , Dennis Zhou , Christoph Lameter , David Brazdil , Will Deacon , Julien Thierry , Andrew Scull Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a handler of the CPU_OFF PSCI host SMC trapped in KVM nVHE hyp code. When invoked, it changes the recorded state of the core to OFF before forwarding the call to EL3. If the call fails, it changes the state back to ON and returns the error to the host. Signed-off-by: David Brazdil --- arch/arm64/kvm/hyp/nvhe/psci.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/psci.c b/arch/arm64/kvm/hyp/nvhe/psci.c index c3d0a6246c66..00dc0cab860c 100644 --- a/arch/arm64/kvm/hyp/nvhe/psci.c +++ b/arch/arm64/kvm/hyp/nvhe/psci.c @@ -13,6 +13,8 @@ #include #include +#include + /* Config options set by the host. */ u32 kvm_host_psci_version = PSCI_VERSION(0, 0); u32 kvm_host_psci_function_id[PSCI_FN_MAX]; @@ -20,6 +22,7 @@ s64 hyp_physvirt_offset; #define __hyp_pa(x) ((phys_addr_t)(x) + hyp_physvirt_offset) +static DEFINE_PER_CPU(hyp_spinlock_t, psci_cpu_lock); DEFINE_PER_CPU(enum kvm_nvhe_psci_state, psci_cpu_state); static u64 get_psci_func_id(struct kvm_cpu_context *host_ctxt) @@ -76,9 +79,32 @@ static __noreturn unsigned long psci_forward_noreturn(struct kvm_cpu_context *ho hyp_panic(); /* unreachable */ } +static int psci_cpu_off(u64 func_id, struct kvm_cpu_context *host_ctxt) +{ + hyp_spinlock_t *cpu_lock = this_cpu_ptr(&psci_cpu_lock); + enum kvm_nvhe_psci_state *cpu_power = this_cpu_ptr(&psci_cpu_state); + u32 power_state = (u32)host_ctxt->regs.regs[1]; + int ret; + + /* Change the recorded state to OFF before forwarding the call. */ + hyp_spin_lock(cpu_lock); + *cpu_power = KVM_NVHE_PSCI_CPU_OFF; + hyp_spin_unlock(cpu_lock); + + ret = psci_call(func_id, power_state, 0, 0); + + /* Call was unsuccessful. Restore the recorded state and return to host. */ + hyp_spin_lock(cpu_lock); + *cpu_power = KVM_NVHE_PSCI_CPU_ON; + hyp_spin_unlock(cpu_lock); + return ret; +} + static unsigned long psci_0_1_handler(u64 func_id, struct kvm_cpu_context *host_ctxt) { - if (func_id == kvm_host_psci_function_id[PSCI_FN_MIGRATE]) + if (func_id == kvm_host_psci_function_id[PSCI_FN_CPU_OFF]) + return psci_cpu_off(func_id, host_ctxt); + else if (func_id == kvm_host_psci_function_id[PSCI_FN_MIGRATE]) return psci_forward(host_ctxt); else return PSCI_RET_NOT_SUPPORTED; @@ -97,6 +123,8 @@ static unsigned long psci_0_2_handler(u64 func_id, struct kvm_cpu_context *host_ case PSCI_0_2_FN_SYSTEM_RESET: psci_forward_noreturn(host_ctxt); unreachable(); + case PSCI_0_2_FN_CPU_OFF: + return psci_cpu_off(func_id, host_ctxt); default: return PSCI_RET_NOT_SUPPORTED; }