Re: [PATCH v19 6/7] KVM: arm64: Add support for the KVM PTP service

2021-04-17 Thread Zenghui Yu

On 2021/4/17 17:10, Marc Zyngier wrote:

On Sat, 17 Apr 2021 09:59:39 +0100,
Zenghui Yu  wrote:


On 2021/3/30 22:54, Marc Zyngier wrote:

+PTP_KVM support for arm/arm64
+=
+
+PTP_KVM is used for high precision time sync between host and guests.
+It relies on transferring the wall clock and counter value from the
+host to the guest using a KVM-specific hypercall.
+
+* ARM_SMCCC_HYP_KVM_PTP_FUNC_ID: 0x8601


Per include/linux/arm-smccc.h, this should be
ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID.


Well spotted. Care to send a patch on top of my kvm-arm64/ptp branch?


Okay. I'll have a patch for that.


Zenghui


Re: [PATCH v19 6/7] KVM: arm64: Add support for the KVM PTP service

2021-04-17 Thread Marc Zyngier
On Sat, 17 Apr 2021 09:59:39 +0100,
Zenghui Yu  wrote:
> 
> On 2021/3/30 22:54, Marc Zyngier wrote:
> > +PTP_KVM support for arm/arm64
> > +=
> > +
> > +PTP_KVM is used for high precision time sync between host and guests.
> > +It relies on transferring the wall clock and counter value from the
> > +host to the guest using a KVM-specific hypercall.
> > +
> > +* ARM_SMCCC_HYP_KVM_PTP_FUNC_ID: 0x8601
> 
> Per include/linux/arm-smccc.h, this should be
> ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID.

Well spotted. Care to send a patch on top of my kvm-arm64/ptp branch?

Thanks,

M.

-- 
Without deviation from the norm, progress is not possible.


Re: [PATCH v19 6/7] KVM: arm64: Add support for the KVM PTP service

2021-04-17 Thread Zenghui Yu

On 2021/3/30 22:54, Marc Zyngier wrote:

+PTP_KVM support for arm/arm64
+=
+
+PTP_KVM is used for high precision time sync between host and guests.
+It relies on transferring the wall clock and counter value from the
+host to the guest using a KVM-specific hypercall.
+
+* ARM_SMCCC_HYP_KVM_PTP_FUNC_ID: 0x8601


Per include/linux/arm-smccc.h, this should be
ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID.


+
+This hypercall uses the SMC32/HVC32 calling convention:
+
+ARM_SMCCC_HYP_KVM_PTP_FUNC_ID


ditto


+=====
+Function ID: (uint32)  0x8601
+Arguments:   (uint32)  KVM_PTP_VIRT_COUNTER(0)
+   KVM_PTP_PHYS_COUNTER(1)


[PATCH v19 6/7] KVM: arm64: Add support for the KVM PTP service

2021-03-30 Thread Marc Zyngier
From: Jianyong Wu 

Implement the hypervisor side of the KVM PTP interface.

The service offers wall time and cycle count from host to guest.
The caller must specify whether they want the host's view of
either the virtual or physical counter.

Signed-off-by: Jianyong Wu 
Signed-off-by: Marc Zyngier 
Link: https://lore.kernel.org/r/20201209060932.212364-7-jianyong...@arm.com
---
 Documentation/virt/kvm/api.rst | 10 +
 Documentation/virt/kvm/arm/index.rst   |  1 +
 Documentation/virt/kvm/arm/ptp_kvm.rst | 25 
 arch/arm64/kvm/arm.c   |  1 +
 arch/arm64/kvm/hypercalls.c| 53 ++
 include/linux/arm-smccc.h  | 16 
 include/uapi/linux/kvm.h   |  1 +
 7 files changed, 107 insertions(+)
 create mode 100644 Documentation/virt/kvm/arm/ptp_kvm.rst

diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
index 38e327d4b479..987d99e39887 100644
--- a/Documentation/virt/kvm/api.rst
+++ b/Documentation/virt/kvm/api.rst
@@ -6724,3 +6724,13 @@ vcpu_info is set.
 The KVM_XEN_HVM_CONFIG_RUNSTATE flag indicates that the runstate-related
 features KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR/_CURRENT/_DATA/_ADJUST are
 supported by the KVM_XEN_VCPU_SET_ATTR/KVM_XEN_VCPU_GET_ATTR ioctls.
+
+8.31 KVM_CAP_PTP_KVM
+
+
+:Architectures: arm64
+
+This capability indicates that the KVM virtual PTP service is
+supported in the host. A VMM can check whether the service is
+available to the guest on migration.
+
diff --git a/Documentation/virt/kvm/arm/index.rst 
b/Documentation/virt/kvm/arm/index.rst
index 3e2b2aba90fc..78a9b670aafe 100644
--- a/Documentation/virt/kvm/arm/index.rst
+++ b/Documentation/virt/kvm/arm/index.rst
@@ -10,3 +10,4 @@ ARM
hyp-abi
psci
pvtime
+   ptp_kvm
diff --git a/Documentation/virt/kvm/arm/ptp_kvm.rst 
b/Documentation/virt/kvm/arm/ptp_kvm.rst
new file mode 100644
index ..68cffb50d8bf
--- /dev/null
+++ b/Documentation/virt/kvm/arm/ptp_kvm.rst
@@ -0,0 +1,25 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+PTP_KVM support for arm/arm64
+=
+
+PTP_KVM is used for high precision time sync between host and guests.
+It relies on transferring the wall clock and counter value from the
+host to the guest using a KVM-specific hypercall.
+
+* ARM_SMCCC_HYP_KVM_PTP_FUNC_ID: 0x8601
+
+This hypercall uses the SMC32/HVC32 calling convention:
+
+ARM_SMCCC_HYP_KVM_PTP_FUNC_ID
+=====
+Function ID: (uint32)  0x8601
+Arguments:   (uint32)  KVM_PTP_VIRT_COUNTER(0)
+   KVM_PTP_PHYS_COUNTER(1)
+Return Values:   (int32)   NOT_SUPPORTED(-1) on error, or
+ (uint32)  Upper 32 bits of wall clock time (r0)
+ (uint32)  Lower 32 bits of wall clock time (r1)
+ (uint32)  Upper 32 bits of counter (r2)
+ (uint32)  Lower 32 bits of counter (r3)
+Endianness:No Restrictions.
+=====
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 7f06ba76698d..46401798c644 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -206,6 +206,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
case KVM_CAP_ARM_INJECT_EXT_DABT:
case KVM_CAP_SET_GUEST_DEBUG:
case KVM_CAP_VCPU_ATTRIBUTES:
+   case KVM_CAP_PTP_KVM:
r = 1;
break;
case KVM_CAP_ARM_SET_DEVICE_ADDR:
diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c
index 78d32c34d49c..30da78f72b3b 100644
--- a/arch/arm64/kvm/hypercalls.c
+++ b/arch/arm64/kvm/hypercalls.c
@@ -9,6 +9,55 @@
 #include 
 #include 
 
+static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val)
+{
+   struct system_time_snapshot systime_snapshot;
+   u64 cycles = ~0UL;
+   u32 feature;
+
+   /*
+* system time and counter value must captured at the same
+* time to keep consistency and precision.
+*/
+   ktime_get_snapshot(_snapshot);
+
+   /*
+* This is only valid if the current clocksource is the
+* architected counter, as this is the only one the guest
+* can see.
+*/
+   if (systime_snapshot.cs_id != CSID_ARM_ARCH_COUNTER)
+   return;
+
+   /*
+* The guest selects one of the two reference counters
+* (virtual or physical) with the first argument of the SMCCC
+* call. In case the identifier is not supported, error out.
+*/
+   feature = smccc_get_arg1(vcpu);
+   switch (feature) {
+   case KVM_PTP_VIRT_COUNTER:
+   cycles = systime_snapshot.cycles - vcpu_read_sys_reg(vcpu, 
CNTVOFF_EL2);
+   break;
+   case KVM_PTP_PHYS_COUNTER:
+   cycles = systime_snapshot.cycles;
+   break;
+