From patchwork Thu Feb 6 11:31:42 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 24251 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ie0-f200.google.com (mail-ie0-f200.google.com [209.85.223.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 5B17720445 for ; Thu, 6 Feb 2014 11:33:18 +0000 (UTC) Received: by mail-ie0-f200.google.com with SMTP id tp5sf3400468ieb.11 for ; Thu, 06 Feb 2014 03:33:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=JDj/fyiHhW71GYj0pe/CXc7gvbg/LyGfiaSFgFwiSHg=; b=PBtlOOgr3vGtUaEl6X08TfEh2jZaaVUlC2Rjv/HNTP4MYRxR9Yj94xFHz/x1HdbnRc GXgzBZkI7Y7yV+b5yqXIflJiJaimS84jtCItyQ1HfaX/0C1vNrNR+SODD0H1u8yKzlcN onVa093Dh7dGHBTcwLcU2kJP3nFjfudE/c3Mf2fLilaFJKRMiwbCHukscP+MrmcRJsCC KkOJa8AUi2Tuy3FV85GVFH56wWglBknqoo44Bub3jnDjWAnqcbNwRz3zs9gmyDQXjzSZ MBtG+wbCbIvwVn5PB+bmIQxDGmO80KUarcq2bytKe0+vSTWaNy4yGc5hci8CYIQ7Tnwy uYRw== X-Gm-Message-State: ALoCoQm1QmP3ujcIrwHC82QLwKxrk2uO9N8vSRkFh659EVaiUNXgcksjvNQCT61nrqq4T84B0JNN X-Received: by 10.50.108.46 with SMTP id hh14mr12857290igb.1.1391686397605; Thu, 06 Feb 2014 03:33:17 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.97.196 with SMTP id m62ls587359qge.11.gmail; Thu, 06 Feb 2014 03:33:17 -0800 (PST) X-Received: by 10.220.247.68 with SMTP id mb4mr71564vcb.37.1391686397405; Thu, 06 Feb 2014 03:33:17 -0800 (PST) Received: from mail-vc0-f171.google.com (mail-vc0-f171.google.com [209.85.220.171]) by mx.google.com with ESMTPS id sm10si158708vec.119.2014.02.06.03.33.17 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 06 Feb 2014 03:33:17 -0800 (PST) Received-SPF: neutral (google.com: 209.85.220.171 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.220.171; Received: by mail-vc0-f171.google.com with SMTP id le5so1310462vcb.2 for ; Thu, 06 Feb 2014 03:33:17 -0800 (PST) X-Received: by 10.58.172.132 with SMTP id bc4mr64638vec.45.1391686397289; Thu, 06 Feb 2014 03:33:17 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp5295vcz; Thu, 6 Feb 2014 03:33:16 -0800 (PST) X-Received: by 10.68.20.1 with SMTP id j1mr12090727pbe.148.1391686396275; Thu, 06 Feb 2014 03:33:16 -0800 (PST) Received: from mail-pb0-f45.google.com (mail-pb0-f45.google.com [209.85.160.45]) by mx.google.com with ESMTPS id sz7si834489pab.58.2014.02.06.03.33.15 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 06 Feb 2014 03:33:16 -0800 (PST) Received-SPF: neutral (google.com: 209.85.160.45 is neither permitted nor denied by best guess record for domain of anup.patel@linaro.org) client-ip=209.85.160.45; Received: by mail-pb0-f45.google.com with SMTP id un15so1641413pbc.4 for ; Thu, 06 Feb 2014 03:33:15 -0800 (PST) X-Received: by 10.68.138.165 with SMTP id qr5mr12018141pbb.123.1391686395821; Thu, 06 Feb 2014 03:33:15 -0800 (PST) Received: from pnqlab006.amcc.com ([182.73.239.130]) by mx.google.com with ESMTPSA id yd4sm2594758pbc.13.2014.02.06.03.33.10 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 06 Feb 2014 03:33:14 -0800 (PST) From: Anup Patel To: kvmarm@lists.cs.columbia.edu Cc: linux-arm-kernel@lists.infradead.org, linaro-kernel@lists.linaro.org, patches@linaro.org, patches@apm.com, Marc Zyngier , Christoffer Dall , Mark Rutland , Pranavkumar Sawargaonkar , Anup Patel Subject: [PATCH v4 10/10] ARM/ARM64: KVM: Emulate PSCI v0.2 CPU_SUSPEND Date: Thu, 6 Feb 2014 17:01:42 +0530 Message-Id: <1391686302-19451-11-git-send-email-anup.patel@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1391686302-19451-1-git-send-email-anup.patel@linaro.org> References: <1391686302-19451-1-git-send-email-anup.patel@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: anup.patel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.220.171 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , This patch adds emulation of PSCI v0.2 CPU_SUSPEND function call for KVM ARM/ARM64. This is a VCPU-level function call which can suspend current VCPU or all VCPUs within current VCPU's affinity level. The CPU_SUSPEND emulation is not tested much because currently there is no CPUIDLE driver in Linux kernel that uses PSCI CPU_SUSPEND. The PSCI CPU_SUSPEND implementation in ARM64 kernel was tested using a Simple CPUIDLE driver which is not published due to unstable DT-bindings for PSCI. (For more info, http://lwn.net/Articles/574950/) Even if we had stable DT-bindings for PSCI and CPUIDLE driver that uses PSCI CPU_SUSPEND then still we need to define SUSPEND states for KVM ARM/ARM64. Due to this, the CPU_SUSPEND emulation added by this patch only pause a VCPU and to wakeup a VCPU we need to explicity call PSCI CPU_ON from Guest. Signed-off-by: Anup Patel Signed-off-by: Pranavkumar Sawargaonkar --- arch/arm/include/asm/kvm_host.h | 5 +++ arch/arm/include/asm/kvm_psci.h | 1 + arch/arm/kvm/psci.c | 88 +++++++++++++++++++++++++++++++++++-- arch/arm/kvm/reset.c | 4 ++ arch/arm64/include/asm/kvm_host.h | 5 +++ arch/arm64/include/asm/kvm_psci.h | 1 + arch/arm64/kvm/reset.c | 4 ++ 7 files changed, 104 insertions(+), 4 deletions(-) diff --git a/arch/arm/include/asm/kvm_host.h b/arch/arm/include/asm/kvm_host.h index 193ceaf..2cc36a6 100644 --- a/arch/arm/include/asm/kvm_host.h +++ b/arch/arm/include/asm/kvm_host.h @@ -131,6 +131,11 @@ struct kvm_vcpu_arch { /* Don't run the guest on this vcpu */ bool pause; + /* PSCI suspend state */ + bool suspend; + u32 suspend_entry; + u32 suspend_context_id; + /* IO related fields */ struct kvm_decode mmio_decode; diff --git a/arch/arm/include/asm/kvm_psci.h b/arch/arm/include/asm/kvm_psci.h index 6bda945..6a05ada 100644 --- a/arch/arm/include/asm/kvm_psci.h +++ b/arch/arm/include/asm/kvm_psci.h @@ -22,6 +22,7 @@ #define KVM_ARM_PSCI_0_2 2 int kvm_psci_version(struct kvm_vcpu *vcpu); +void kvm_psci_reset(struct kvm_vcpu *vcpu); int kvm_psci_call(struct kvm_vcpu *vcpu); #endif /* __ARM_KVM_PSCI_H__ */ diff --git a/arch/arm/kvm/psci.c b/arch/arm/kvm/psci.c index 675866e..482b0f6 100644 --- a/arch/arm/kvm/psci.c +++ b/arch/arm/kvm/psci.c @@ -15,6 +15,7 @@ * along with this program. If not, see . */ +#include #include #include @@ -27,9 +28,81 @@ * as described in ARM document number ARM DEN 0022A. */ +struct psci_suspend_info { + struct kvm_vcpu *vcpu; + unsigned long saved_entry; + unsigned long saved_context_id; +}; + +static void psci_do_suspend(void *context) +{ + struct psci_suspend_info *sinfo = context; + + sinfo->vcpu->arch.pause = true; + sinfo->vcpu->arch.suspend = true; + sinfo->vcpu->arch.suspend_entry = sinfo->saved_entry; + sinfo->vcpu->arch.suspend_context_id = sinfo->saved_context_id; +} + +static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu) +{ + int i; + unsigned long mpidr; + unsigned long target_affinity; + unsigned long target_affinity_mask; + unsigned long lowest_affinity_level; + struct kvm *kvm = vcpu->kvm; + struct kvm_vcpu *tmp; + struct psci_suspend_info sinfo; + + target_affinity = kvm_vcpu_get_mpidr(vcpu); + lowest_affinity_level = (*vcpu_reg(vcpu, 1) >> 24) & 0x3; + + /* Determine target affinity mask */ + target_affinity_mask = MPIDR_HWID_BITMASK; + switch (lowest_affinity_level) { + case 0: /* All affinity levels are valid */ + target_affinity_mask &= ~0x0UL; + break; + case 1: /* Aff0 ignored */ + target_affinity_mask &= ~0xFFUL; + break; + case 2: /* Aff0 and Aff1 ignored */ + target_affinity_mask &= ~0xFFFFUL; + break; + case 3: /* Aff0, Aff1, and Aff2 ignored */ + target_affinity_mask &= ~0xFFFFFFUL; + break; + default: + return KVM_PSCI_RET_INVAL; + }; + + /* Ignore other bits of target affinity */ + target_affinity &= target_affinity_mask; + + /* Prepare suspend info */ + sinfo.vcpu = NULL; + sinfo.saved_entry = *vcpu_reg(vcpu, 2); + sinfo.saved_context_id = *vcpu_reg(vcpu, 3); + + /* Suspend all VCPUs within target affinity */ + kvm_for_each_vcpu(i, tmp, kvm) { + mpidr = kvm_vcpu_get_mpidr(tmp); + if (((mpidr & target_affinity_mask) == target_affinity) && + !tmp->arch.suspend) { + sinfo.vcpu = tmp; + smp_call_function_single(tmp->cpu, + psci_do_suspend, &sinfo, 1); + } + } + + return KVM_PSCI_RET_SUCCESS; +} + static void kvm_psci_vcpu_off(struct kvm_vcpu *vcpu) { vcpu->arch.pause = true; + vcpu->arch.suspend = false; } static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu, @@ -179,6 +252,10 @@ static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu) */ val = 2; break; + case KVM_PSCI_0_2_FN_CPU_SUSPEND: + case KVM_PSCI_0_2_FN64_CPU_SUSPEND: + val = kvm_psci_vcpu_suspend(vcpu); + break; case KVM_PSCI_0_2_FN_CPU_OFF: kvm_psci_vcpu_off(vcpu); val = KVM_PSCI_RET_SUCCESS; @@ -216,10 +293,6 @@ static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu) val = KVM_PSCI_RET_SUCCESS; ret = 0; break; - case KVM_PSCI_0_2_FN_CPU_SUSPEND: - case KVM_PSCI_0_2_FN64_CPU_SUSPEND: - val = KVM_PSCI_RET_NI; - break; default: return -EINVAL; } @@ -253,6 +326,13 @@ static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu) return 1; } +void kvm_psci_reset(struct kvm_vcpu *vcpu) +{ + vcpu->arch.suspend = false; + vcpu->arch.suspend_entry = 0; + vcpu->arch.suspend_context_id = 0; +} + /** * kvm_psci_call - handle PSCI call if r0 value is in range * @vcpu: Pointer to the VCPU struct diff --git a/arch/arm/kvm/reset.c b/arch/arm/kvm/reset.c index f558c07..220c892 100644 --- a/arch/arm/kvm/reset.c +++ b/arch/arm/kvm/reset.c @@ -26,6 +26,7 @@ #include #include #include +#include #include @@ -79,5 +80,8 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) /* Reset arch_timer context */ kvm_timer_vcpu_reset(vcpu, cpu_vtimer_irq); + /* Reset PSCI state */ + kvm_psci_reset(vcpu); + return 0; } diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 92242ce..b2c97dc 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -119,6 +119,11 @@ struct kvm_vcpu_arch { /* Don't run the guest */ bool pause; + /* PSCI suspend state */ + bool suspend; + u64 suspend_entry; + u64 suspend_context_id; + /* IO related fields */ struct kvm_decode mmio_decode; diff --git a/arch/arm64/include/asm/kvm_psci.h b/arch/arm64/include/asm/kvm_psci.h index bc39e55..4da675d 100644 --- a/arch/arm64/include/asm/kvm_psci.h +++ b/arch/arm64/include/asm/kvm_psci.h @@ -22,6 +22,7 @@ #define KVM_ARM_PSCI_0_2 2 int kvm_psci_version(struct kvm_vcpu *vcpu); +void kvm_psci_reset(struct kvm_vcpu *vcpu); int kvm_psci_call(struct kvm_vcpu *vcpu); #endif /* __ARM64_KVM_PSCI_H__ */ diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c index 70a7816..aca9f65 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -29,6 +29,7 @@ #include #include #include +#include /* * ARMv8 Reset Values @@ -108,5 +109,8 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) /* Reset timer */ kvm_timer_vcpu_reset(vcpu, cpu_vtimer_irq); + /* Reset PSCI state */ + kvm_psci_reset(vcpu); + return 0; }