2 * Copyright (c) 2014,2016 Microsoft Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/syscallsubr.h>
35 #include <sys/sysctl.h>
36 #include <sys/systm.h>
38 #include <dev/hyperv/include/hyperv.h>
39 #include <dev/hyperv/include/vmbus.h>
40 #include <dev/hyperv/utilities/vmbus_icreg.h>
41 #include <dev/hyperv/utilities/vmbus_icvar.h>
45 #define VMBUS_TIMESYNC_FWVER_MAJOR 3
46 #define VMBUS_TIMESYNC_FWVER \
47 VMBUS_IC_VERSION(VMBUS_TIMESYNC_FWVER_MAJOR, 0)
49 #define VMBUS_TIMESYNC_MSGVER_MAJOR 4
50 #define VMBUS_TIMESYNC_MSGVER \
51 VMBUS_IC_VERSION(VMBUS_TIMESYNC_MSGVER_MAJOR, 0)
53 #define VMBUS_TIMESYNC_DORTT(sc) \
54 ((sc)->ic_msgver >= VMBUS_IC_VERSION(4, 0) && \
55 (hyperv_features & CPUID_HV_MSR_TIME_REFCNT))
57 static const struct vmbus_ic_desc vmbus_timesync_descs[] = {
59 .ic_guid = { .hv_guid = {
60 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
61 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf } },
62 .ic_desc = "Hyper-V Timesync"
67 SYSCTL_NODE(_hw, OID_AUTO, hvtimesync, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
68 "Hyper-V timesync interface");
70 static int vmbus_ts_ignore_sync = 0;
71 SYSCTL_INT(_hw_hvtimesync, OID_AUTO, ignore_sync, CTLFLAG_RWTUN,
72 &vmbus_ts_ignore_sync, 0, "Ignore the sync request.");
75 * Trigger sample sync when drift exceeds threshold (ms).
76 * Ignore the sample request when set to 0.
78 static int vmbus_ts_sample_thresh = 100;
79 SYSCTL_INT(_hw_hvtimesync, OID_AUTO, sample_thresh, CTLFLAG_RWTUN,
80 &vmbus_ts_sample_thresh, 0,
81 "Threshold that makes sample request trigger the sync (unit: ms).");
83 static int vmbus_ts_sample_verbose = 0;
84 SYSCTL_INT(_hw_hvtimesync, OID_AUTO, sample_verbose, CTLFLAG_RWTUN,
85 &vmbus_ts_sample_verbose, 0, "Increase sample request verbosity.");
88 vmbus_timesync(struct vmbus_ic_softc *sc, uint64_t hvtime, uint64_t sent_tc,
91 struct timespec vm_ts;
92 uint64_t hv_ns, vm_ns, rtt = 0;
94 if (VMBUS_TIMESYNC_DORTT(sc))
95 rtt = rdmsr(MSR_HV_TIME_REF_COUNT) - sent_tc;
97 hv_ns = (hvtime - VMBUS_ICMSG_TS_BASE + rtt) * HYPERV_TIMER_NS_FACTOR;
99 vm_ns = (vm_ts.tv_sec * NANOSEC) + vm_ts.tv_nsec;
101 if ((tsflags & VMBUS_ICMSG_TS_FLAG_SYNC) && !vmbus_ts_ignore_sync) {
102 struct timespec hv_ts;
105 device_printf(sc->ic_dev, "apply sync request, "
106 "hv: %ju, vm: %ju\n",
107 (uintmax_t)hv_ns, (uintmax_t)vm_ns);
109 hv_ts.tv_sec = hv_ns / NANOSEC;
110 hv_ts.tv_nsec = hv_ns % NANOSEC;
111 kern_clock_settime(curthread, CLOCK_REALTIME, &hv_ts);
116 if ((tsflags & VMBUS_ICMSG_TS_FLAG_SAMPLE) &&
117 vmbus_ts_sample_thresh > 0) {
120 if (vmbus_ts_sample_verbose) {
121 device_printf(sc->ic_dev, "sample request, "
122 "hv: %ju, vm: %ju\n",
123 (uintmax_t)hv_ns, (uintmax_t)vm_ns);
127 diff = hv_ns - vm_ns;
129 diff = vm_ns - hv_ns;
130 /* nanosec -> millisec */
133 if (diff > vmbus_ts_sample_thresh) {
134 struct timespec hv_ts;
137 device_printf(sc->ic_dev,
138 "apply sample request, hv: %ju, vm: %ju\n",
139 (uintmax_t)hv_ns, (uintmax_t)vm_ns);
141 hv_ts.tv_sec = hv_ns / NANOSEC;
142 hv_ts.tv_nsec = hv_ns % NANOSEC;
143 kern_clock_settime(curthread, CLOCK_REALTIME, &hv_ts);
151 vmbus_timesync_cb(struct vmbus_channel *chan, void *xsc)
153 struct vmbus_ic_softc *sc = xsc;
154 struct vmbus_icmsg_hdr *hdr;
155 const struct vmbus_icmsg_timesync *msg;
164 dlen = sc->ic_buflen;
165 error = vmbus_chan_recv(chan, data, &dlen, &xactid);
166 KASSERT(error != ENOBUFS, ("icbuf is not large enough"));
170 if (dlen < sizeof(*hdr)) {
171 device_printf(sc->ic_dev, "invalid data len %d\n", dlen);
177 * Update request, which will be echoed back as response.
179 switch (hdr->ic_type) {
180 case VMBUS_ICMSG_TYPE_NEGOTIATE:
181 error = vmbus_ic_negomsg(sc, data, &dlen,
182 VMBUS_TIMESYNC_FWVER, VMBUS_TIMESYNC_MSGVER);
185 if (VMBUS_TIMESYNC_DORTT(sc))
186 device_printf(sc->ic_dev, "RTT\n");
189 case VMBUS_ICMSG_TYPE_TIMESYNC:
190 if (dlen < sizeof(*msg)) {
191 device_printf(sc->ic_dev, "invalid timesync len %d\n",
196 vmbus_timesync(sc, msg->ic_hvtime, msg->ic_sent_tc,
201 device_printf(sc->ic_dev, "got 0x%08x icmsg\n", hdr->ic_type);
206 * Send response by echoing the request back.
208 vmbus_ic_sendresp(sc, chan, data, dlen, xactid);
212 hv_timesync_probe(device_t dev)
215 return (vmbus_ic_probe(dev, vmbus_timesync_descs));
219 hv_timesync_attach(device_t dev)
222 return (vmbus_ic_attach(dev, vmbus_timesync_cb));
225 static device_method_t timesync_methods[] = {
226 /* Device interface */
227 DEVMETHOD(device_probe, hv_timesync_probe),
228 DEVMETHOD(device_attach, hv_timesync_attach),
229 DEVMETHOD(device_detach, vmbus_ic_detach),
233 static driver_t timesync_driver = {
236 sizeof(struct vmbus_ic_softc)
239 static devclass_t timesync_devclass;
241 DRIVER_MODULE(hv_timesync, vmbus, timesync_driver, timesync_devclass, NULL, NULL);
242 MODULE_VERSION(hv_timesync, 1);
243 MODULE_DEPEND(hv_timesync, vmbus, 1, 1, 1);