Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp460772ybt; Fri, 19 Jun 2020 06:06:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhVsSe6pgA23kRFGo7oVs9eYHFOQAOqYNRlhTJicK7VfFKO7kLaECLQfAh2EQCBRO7OVbh X-Received: by 2002:a50:d55c:: with SMTP id f28mr3122551edj.87.1592572013734; Fri, 19 Jun 2020 06:06:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592572013; cv=none; d=google.com; s=arc-20160816; b=hY+drkbeBCC1TyC7IxCHti1G4qLT5lP9SJ22H3NxV2oJLbdUL5NC5gPSGXRe+q0LuM OQ9sOnRxariCCa33QXXAAwhqLeQPHw08tuwLR/IQBBRk2y6vIQWxTQRbsjQK4/wK7qG2 NmG7/2ohzQp9zTphQ+4fl8b5fIi+A3I04ESVq8DG3F/rtyVLWaKsPf4tPWtCoNeHAE/o TfJyYuSb5JswzZocyka94FcQP7GbzF1I5MSd+8yieT+yiEBzwMRbIG5KnJiqJGgfGqWE qy6rNIl7z8zMYtr5DVJIbVh9VmC0KOFzT4ly/1tZAuwIh9CCBaXsg2K+XffWJBIRzvx+ ZeHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=3cKoEpAfTKhnqDpuj4SsgCInxw1mkFpjiiVkA2kTSFM=; b=pXxwq2xCvGo2t/JAmvslYFUmDzO0xO+g5IdxcR9yHlOYCAGoYDpEGAdPbn9xor+k2D 8CAqoTCdCFQi+OBj13JIxTRP0iTO8jh4mSoTRdH7J/DBI+/zktska0pddeDEfjaFGviG liRAYEH7LLvlT88nAPXHBkuAzSrnbbY9fQcbci8qwkLkQ0ssVusvK7WPW66s29PP/bjV En0/3U8FfUgsfLkizVuNiUVEXaHsm3NO7HbYpzz6hCKu76f1uWCfgZlAS1s5DvLhkPw+ 56J0LvpsEc6ougFsGgo54a6C4X/2alWleWaIdgSOoUJ9He5RxK7ZbRk0yBOBmYGsQAF5 C4lQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id do13si4882529ejc.250.2020.06.19.06.06.31; Fri, 19 Jun 2020 06:06:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732990AbgFSNCd (ORCPT + 99 others); Fri, 19 Jun 2020 09:02:33 -0400 Received: from foss.arm.com ([217.140.110.172]:57576 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732960AbgFSNC1 (ORCPT ); Fri, 19 Jun 2020 09:02:27 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A129311B3; Fri, 19 Jun 2020 06:02:26 -0700 (PDT) Received: from entos-d05.shanghai.arm.com (entos-d05.shanghai.arm.com [10.169.40.35]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 736CB3F6CF; Fri, 19 Jun 2020 06:02:20 -0700 (PDT) From: Jianyong Wu To: netdev@vger.kernel.org, yangbo.lu@nxp.com, john.stultz@linaro.org, tglx@linutronix.de, pbonzini@redhat.com, sean.j.christopherson@intel.com, maz@kernel.org, richardcochran@gmail.com, Mark.Rutland@arm.com, will@kernel.org, suzuki.poulose@arm.com, steven.price@arm.com Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, Steve.Capper@arm.com, Kaly.Xin@arm.com, justin.he@arm.com, Wei.Chen@arm.com, jianyong.wu@arm.com, nd@arm.com Subject: [PATCH v13 7/9] arm64/kvm: Add hypercall service for kvm ptp. Date: Fri, 19 Jun 2020 21:01:18 +0800 Message-Id: <20200619130120.40556-8-jianyong.wu@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200619130120.40556-1-jianyong.wu@arm.com> References: <20200619130120.40556-1-jianyong.wu@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ptp_kvm will get this service through smccc call. The service offers wall time and counter cycle of host for guest. caller must explicitly determines which cycle of virtual counter or physical counter to return if it needs counter cycle. Signed-off-by: Jianyong Wu --- arch/arm64/kvm/Kconfig | 6 +++++ arch/arm64/kvm/hypercalls.c | 50 +++++++++++++++++++++++++++++++++++++ include/linux/arm-smccc.h | 30 ++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index 13489aff4440..79091f6e5e7a 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -60,6 +60,12 @@ config KVM_ARM_PMU config KVM_INDIRECT_VECTORS def_bool HARDEN_BRANCH_PREDICTOR || HARDEN_EL2_VECTORS +config ARM64_KVM_PTP_HOST + bool "KVM PTP host service for arm64" + default y + help + virtual kvm ptp clock hypercall service for arm64 + endif # KVM endif # VIRTUALIZATION diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index db6dce3d0e23..366b0646c360 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -3,6 +3,7 @@ #include #include +#include #include @@ -11,6 +12,10 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) { +#ifdef CONFIG_ARM64_KVM_PTP_HOST + struct system_time_snapshot systime_snapshot; + u64 cycles = 0; +#endif u32 func_id = smccc_get_function(vcpu); u32 val[4] = {SMCCC_RET_NOT_SUPPORTED}; u32 feature; @@ -70,7 +75,52 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) break; case ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID: val[0] = BIT(ARM_SMCCC_KVM_FUNC_FEATURES); + +#ifdef CONFIG_ARM64_KVM_PTP_HOST + val[0] |= BIT(ARM_SMCCC_KVM_FUNC_KVM_PTP); +#endif + break; + +#ifdef CONFIG_ARM64_KVM_PTP_HOST + /* + * This serves virtual kvm_ptp. + * Four values will be passed back. + * reg0 stores high 32-bit host ktime; + * reg1 stores low 32-bit host ktime; + * reg2 stores high 32-bit difference of host cycles and cntvoff; + * reg3 stores low 32-bit difference of host cycles and cntvoff. + */ + case ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID: + /* + * system time and counter value must captured in the same + * time to keep consistency and precision. + */ + ktime_get_snapshot(&systime_snapshot); + if (systime_snapshot.cs_id != CSID_ARM_ARCH_COUNTER) + break; + val[0] = upper_32_bits(systime_snapshot.real); + val[1] = lower_32_bits(systime_snapshot.real); + /* + * which of virtual counter or physical counter being + * asked for is decided by the first argument of smccc + * call. If no first argument or invalid argument, zero + * counter value will return; + */ + feature = smccc_get_arg1(vcpu); + switch (feature) { + case ARM_PTP_VIRT_COUNTER: + cycles = systime_snapshot.cycles - + vcpu_vtimer(vcpu)->cntvoff; + break; + case ARM_PTP_PHY_COUNTER: + cycles = systime_snapshot.cycles; + break; + } + val[2] = upper_32_bits(cycles); + val[3] = lower_32_bits(cycles); break; +#endif + default: return kvm_psci_call(vcpu); } diff --git a/include/linux/arm-smccc.h b/include/linux/arm-smccc.h index 86ff30131e7b..e593ec515f82 100644 --- a/include/linux/arm-smccc.h +++ b/include/linux/arm-smccc.h @@ -98,6 +98,9 @@ /* KVM "vendor specific" services */ #define ARM_SMCCC_KVM_FUNC_FEATURES 0 +#define ARM_SMCCC_KVM_FUNC_KVM_PTP 1 +#define ARM_SMCCC_KVM_FUNC_KVM_PTP_PHY 2 +#define ARM_SMCCC_KVM_FUNC_KVM_PTP_VIRT 3 #define ARM_SMCCC_KVM_FUNC_FEATURES_2 127 #define ARM_SMCCC_KVM_NUM_FUNCS 128 @@ -107,6 +110,33 @@ ARM_SMCCC_OWNER_VENDOR_HYP, \ ARM_SMCCC_KVM_FUNC_FEATURES) +/* + * kvm_ptp is a feature used for time sync between vm and host. + * kvm_ptp module in guest kernel will get service from host using + * this hypercall ID. + */ +#define ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_VENDOR_HYP, \ + ARM_SMCCC_KVM_FUNC_KVM_PTP) + +/* + * kvm_ptp may get counter cycle from host and should ask for which of + * physical counter or virtual counter by using ARM_PTP_PHY_COUNTER and + * ARM_PTP_VIRT_COUNTER explicitly. + */ +#define ARM_PTP_PHY_COUNTER \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_VENDOR_HYP, \ + ARM_SMCCC_KVM_FUNC_KVM_PTP_PHY) + +#define ARM_PTP_VIRT_COUNTER \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_VENDOR_HYP, \ + ARM_SMCCC_KVM_FUNC_KVM_PTP_VIRT) #ifndef __ASSEMBLY__ #include -- 2.17.1