From patchwork Tue Jun 20 16:33:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 13286205 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 4AF29EB64D8 for ; Tue, 20 Jun 2023 16:35:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232478AbjFTQfv (ORCPT ); Tue, 20 Jun 2023 12:35:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232540AbjFTQey (ORCPT ); Tue, 20 Jun 2023 12:34:54 -0400 Received: from out-49.mta0.migadu.com (out-49.mta0.migadu.com [IPv6:2001:41d0:1004:224b::31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1E7119A1 for ; Tue, 20 Jun 2023 09:34:48 -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=1687278886; 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=L8eatDjTlaJ0yZ5K8fFcIfrqz9KneiHfnlsZdXM1Okg=; b=r5Q87BhTvLXnerGmzuHA87Rv+RNbl/687Dz1HJ6QLm8FtTz5FQxei+2hU5akEdTtxtDZ9f yGT+dUWz/9bHabqyIX7n9ebovOw5Dy8E1VpTIMIyDbj4md33HqMJmAeiWsPKL5xV7zlOFI FC3rEm6S9gQs8Q0xf3idEy66nkwXcks= 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 16/20] aarch64: psci: Implement CPU_ON Date: Tue, 20 Jun 2023 11:33:49 -0500 Message-ID: <20230620163353.2688567-17-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 Add support for the PSCI CPU_ON call, wherein a caller can power on a targeted CPU and reset it with the provided context (i.e. entrypoint and context id). Rely on the KVM_ARM_VCPU_INIT ioctl, which has the effect of an architectural warm reset, to do the heavy lifting. Signed-off-by: Oliver Upton --- arm/aarch64/psci.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/arm/aarch64/psci.c b/arm/aarch64/psci.c index 72429b3..254e16e 100644 --- a/arm/aarch64/psci.c +++ b/arm/aarch64/psci.c @@ -18,6 +18,8 @@ static void psci_features(struct kvm_cpu *vcpu, struct arm_smccc_res *res) case PSCI_0_2_FN_CPU_SUSPEND: case PSCI_0_2_FN64_CPU_SUSPEND: case PSCI_0_2_FN_CPU_OFF: + case PSCI_0_2_FN_CPU_ON: + case PSCI_0_2_FN64_CPU_ON: case ARM_SMCCC_VERSION_FUNC_ID: res->a0 = PSCI_RET_SUCCESS; break; @@ -47,6 +49,68 @@ static void cpu_off(struct kvm_cpu *vcpu, struct arm_smccc_res *res) die_perror("KVM_SET_MP_STATE failed"); } +static void reset_cpu_with_context(struct kvm_cpu *vcpu, u64 entry_addr, u64 ctx_id) +{ + struct kvm_one_reg reg; + + if (ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_INIT, &vcpu->init)) + die_perror("KVM_ARM_VCPU_INIT failed"); + + reg = (struct kvm_one_reg) { + .id = ARM64_CORE_REG(regs.pc), + .addr = (u64)&entry_addr, + }; + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®)) + die_perror("KVM_SET_ONE_REG failed"); + + reg = (struct kvm_one_reg) { + .id = ARM64_CORE_REG(regs.regs[0]), + .addr = (u64)&ctx_id, + }; + if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®)) + die_perror("KVM_SET_ONE_REG failed"); +} + +static bool psci_valid_affinity(u64 affinity) +{ + return !(affinity & ~ARM_MPIDR_HWID_BITMASK); +} + +static void cpu_on(struct kvm_cpu *vcpu, struct arm_smccc_res *res) +{ + u64 target_mpidr = smccc_get_arg(vcpu, 1); + u64 entry_addr = smccc_get_arg(vcpu, 2); + u64 ctx_id = smccc_get_arg(vcpu, 3); + struct kvm_mp_state mp_state; + struct kvm_cpu *target; + + if (!psci_valid_affinity(target_mpidr)) { + res->a0 = PSCI_RET_INVALID_PARAMS; + return; + } + + kvm_cpu__pause_vm(vcpu); + + target = kvm__arch_mpidr_to_vcpu(vcpu->kvm, target_mpidr); + if (!target) { + res->a0 = PSCI_RET_INVALID_PARAMS; + goto out_continue; + } + + if (ioctl(target->vcpu_fd, KVM_GET_MP_STATE, &mp_state)) + die_perror("KVM_GET_MP_STATE failed"); + + if (mp_state.mp_state != KVM_MP_STATE_STOPPED) { + res->a0 = PSCI_RET_ALREADY_ON; + goto out_continue; + } + + reset_cpu_with_context(target, entry_addr, ctx_id); + res->a0 = PSCI_RET_SUCCESS; +out_continue: + kvm_cpu__continue_vm(vcpu); +} + void handle_psci(struct kvm_cpu *vcpu, struct arm_smccc_res *res) { switch (vcpu->kvm_run->hypercall.nr) { @@ -63,6 +127,10 @@ void handle_psci(struct kvm_cpu *vcpu, struct arm_smccc_res *res) case PSCI_0_2_FN_CPU_OFF: cpu_off(vcpu, res); break; + case PSCI_0_2_FN_CPU_ON: + case PSCI_0_2_FN64_CPU_ON: + cpu_on(vcpu, res); + break; default: res->a0 = PSCI_RET_NOT_SUPPORTED; }