From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S941327AbcIHKeo (ORCPT ); Thu, 8 Sep 2016 06:34:44 -0400 Received: from p3plsmtps2ded03.prod.phx3.secureserver.net ([208.109.80.60]:45868 "EHLO p3plsmtps2ded03.prod.phx3.secureserver.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S941290AbcIHKel (ORCPT ); Thu, 8 Sep 2016 06:34:41 -0400 x-originating-ip: 72.167.245.219 From: kys@exchange.microsoft.com To: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, devel@linuxdriverproject.org, olaf@aepfle.de, apw@canonical.com, vkuznets@redhat.com, jasowang@redhat.com, leann.ogasawara@canonical.com, alexng@microsoft.com Cc: Alex Ng , "K. Y. Srinivasan" Subject: [PATCH 3/3] Drivers: hv: utils: Support TimeSync version 4.0 protocol samples. Date: Thu, 8 Sep 2016 05:24:14 -0700 Message-Id: <1473337454-6097-3-git-send-email-kys@exchange.microsoft.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1473337454-6097-1-git-send-email-kys@exchange.microsoft.com> References: <1473337400-6050-1-git-send-email-kys@exchange.microsoft.com> <1473337454-6097-1-git-send-email-kys@exchange.microsoft.com> Reply-To: kys@microsoft.com X-CMAE-Envelope: MS4wfAxMDwCfp0UNXbAaYjI8iSl37/XHWzC9V/yGdCxlK97BjpPuEzCIddZZcKFMbyCfhD4lODlQBl5Trh1Ld66xSjPvfQHJBZVqfTU6iiqKeGnI/X0tApvU TM748z5O3Xvjk4ht8szNCONixG5hF9cOxoqtHvqWYdmuvff2vyq/y5M7Rt5PnMWWYgtxSYdspvF8VFfe02vaFXjR0R+Bv4Xjd9a3Mje/EjX8LjCywlez+J9q fPWkTJm6W17wmgd6XIwmBFd8B7oZgy48MyHkOhbOlGtwUCeqQ9RsQ07KnFBEMbLnyqmLxKdxcpQ9gKss4v+mU/5hCO59ThH7hZ1XV9Ultj1Qzh9XSv2uNR2v vwKRMaMK40CM1ORosCE4SSQwhHUwtvzBuYkwdHNNE4RPJsZMC9/YdWMaGatLFWSwQf81Kjsp0U+3vZCcRhxcqKSoEsLizfs0Xn8VfeOPJWHhlCjhgBQscq3K 1qZ5fXyWgZQx8NasLWAzTzQul1l8cUq0RtLjY9ELx21nEwyNACURZEh1ptYCFOoqSaMYJSMYAc7R5m2D Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alex Ng This enables support for more accurate TimeSync v4 samples when hosted under Windows Server 2016 and newer hosts. The new time samples include a "vmreferencetime" field that represents the guest's TSC value when the host generated its time sample. This value lets the guest calculate the latency in receiving the time sample. The latency is added to the sample host time prior to updating the clock. Signed-off-by: Alex Ng Signed-off-by: K. Y. Srinivasan --- drivers/hv/hv_util.c | 82 ++++++++++++++++++++++++++++++++++------------- include/linux/hyperv.h | 9 +++++ 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/drivers/hv/hv_util.c b/drivers/hv/hv_util.c index 4002b71..6286bdc 100644 --- a/drivers/hv/hv_util.c +++ b/drivers/hv/hv_util.c @@ -37,13 +37,16 @@ #define SD_MAJOR_1 1 #define SD_VERSION_1 (SD_MAJOR_1 << 16 | SD_MINOR) -#define TS_MAJOR 3 +#define TS_MAJOR 4 #define TS_MINOR 0 #define TS_VERSION (TS_MAJOR << 16 | TS_MINOR) #define TS_MAJOR_1 1 #define TS_VERSION_1 (TS_MAJOR_1 << 16 | TS_MINOR) +#define TS_MAJOR_3 3 +#define TS_VERSION_3 (TS_MAJOR_3 << 16 | TS_MINOR) + #define HB_MAJOR 3 #define HB_MINOR 0 #define HB_VERSION (HB_MAJOR << 16 | HB_MINOR) @@ -161,34 +164,43 @@ static void shutdown_onchannelcallback(void *context) } /* - * Set guest time to host UTC time. - */ -static inline void do_adj_guesttime(u64 hosttime) -{ - s64 host_tns; - struct timespec host_ts; - - host_tns = (hosttime - WLTIMEDELTA) * 100; - host_ts = ns_to_timespec(host_tns); - - do_settimeofday(&host_ts); -} - -/* * Set the host time in a process context. */ struct adj_time_work { struct work_struct work; u64 host_time; + u64 ref_time; + u8 flags; }; static void hv_set_host_time(struct work_struct *work) { struct adj_time_work *wrk; + s64 host_tns; + u64 newtime; + struct timespec host_ts; wrk = container_of(work, struct adj_time_work, work); - do_adj_guesttime(wrk->host_time); + + newtime = wrk->host_time; + if (ts_srv_version > TS_VERSION_3) { + /* + * Some latency has been introduced since Hyper-V generated + * its time sample. Take that latency into account before + * using TSC reference time sample from Hyper-V. + * + * This sample is given by TimeSync v4 and above hosts. + */ + u64 current_tick; + + rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick); + newtime += (current_tick - wrk->ref_time); + } + host_tns = (newtime - WLTIMEDELTA) * 100; + host_ts = ns_to_timespec(host_tns); + + do_settimeofday(&host_ts); kfree(wrk); } @@ -205,7 +217,7 @@ static void hv_set_host_time(struct work_struct *work) * typically used as a hint to the guest. The guest is under no obligation * to discipline the clock. */ -static inline void adj_guesttime(u64 hosttime, u8 flags) +static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 flags) { struct adj_time_work *wrk; @@ -214,6 +226,8 @@ static inline void adj_guesttime(u64 hosttime, u8 flags) return; wrk->host_time = hosttime; + wrk->ref_time = reftime; + wrk->flags = flags; if ((flags & (ICTIMESYNCFLAG_SYNC | ICTIMESYNCFLAG_SAMPLE)) != 0) { INIT_WORK(&wrk->work, hv_set_host_time); schedule_work(&wrk->work); @@ -231,6 +245,7 @@ static void timesync_onchannelcallback(void *context) u64 requestid; struct icmsg_hdr *icmsghdrp; struct ictimesync_data *timedatap; + struct ictimesync_ref_data *refdata; u8 *time_txf_buf = util_timesynch.recv_buffer; struct icmsg_negotiate *negop = NULL; @@ -246,11 +261,27 @@ static void timesync_onchannelcallback(void *context) time_txf_buf, util_fw_version, ts_srv_version); + pr_info("Using TimeSync version %d.%d\n", + ts_srv_version >> 16, ts_srv_version & 0xFFFF); } else { - timedatap = (struct ictimesync_data *)&time_txf_buf[ - sizeof(struct vmbuspipe_hdr) + - sizeof(struct icmsg_hdr)]; - adj_guesttime(timedatap->parenttime, timedatap->flags); + if (ts_srv_version > TS_VERSION_3) { + refdata = (struct ictimesync_ref_data *) + &time_txf_buf[ + sizeof(struct vmbuspipe_hdr) + + sizeof(struct icmsg_hdr)]; + + adj_guesttime(refdata->parenttime, + refdata->vmreferencetime, + refdata->flags); + } else { + timedatap = (struct ictimesync_data *) + &time_txf_buf[ + sizeof(struct vmbuspipe_hdr) + + sizeof(struct icmsg_hdr)]; + adj_guesttime(timedatap->parenttime, + 0, + timedatap->flags); + } } icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION @@ -348,12 +379,17 @@ static int util_probe(struct hv_device *dev, ts_srv_version = TS_VERSION_1; hb_srv_version = HB_VERSION_1; break; - - default: + case(VERSION_WIN10): util_fw_version = UTIL_FW_VERSION; sd_srv_version = SD_VERSION; ts_srv_version = TS_VERSION; hb_srv_version = HB_VERSION; + break; + default: + util_fw_version = UTIL_FW_VERSION; + sd_srv_version = SD_VERSION; + ts_srv_version = TS_VERSION_3; + hb_srv_version = HB_VERSION; } ret = vmbus_open(dev->channel, 4 * PAGE_SIZE, 4 * PAGE_SIZE, NULL, 0, diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index 430619a..7d7cbff 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -1423,6 +1423,15 @@ struct ictimesync_data { u8 flags; } __packed; +struct ictimesync_ref_data { + u64 parenttime; + u64 vmreferencetime; + u8 flags; + char leapflags; + char stratum; + u8 reserved[3]; +} __packed; + struct hyperv_service_callback { u8 msg_type; char *log_msg; -- 1.7.4.1