2 * Copyright (c) 2014,2016-2017 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>
43 #define VMBUS_TIMESYNC_FWVER_MAJOR 3
44 #define VMBUS_TIMESYNC_FWVER \
45 VMBUS_IC_VERSION(VMBUS_TIMESYNC_FWVER_MAJOR, 0)
47 #define VMBUS_TIMESYNC_MSGVER_MAJOR 4
48 #define VMBUS_TIMESYNC_MSGVER \
49 VMBUS_IC_VERSION(VMBUS_TIMESYNC_MSGVER_MAJOR, 0)
51 #define VMBUS_TIMESYNC_MSGVER4(sc) \
52 VMBUS_ICVER_LE(VMBUS_IC_VERSION(4, 0), (sc)->ic_msgver)
54 #define VMBUS_TIMESYNC_DORTT(sc) \
55 (VMBUS_TIMESYNC_MSGVER4((sc)) && hyperv_tc64 != NULL)
57 static int vmbus_timesync_probe(device_t);
58 static int vmbus_timesync_attach(device_t);
60 static const struct vmbus_ic_desc vmbus_timesync_descs[] = {
62 .ic_guid = { .hv_guid = {
63 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
64 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf } },
65 .ic_desc = "Hyper-V Timesync"
70 static device_method_t vmbus_timesync_methods[] = {
71 /* Device interface */
72 DEVMETHOD(device_probe, vmbus_timesync_probe),
73 DEVMETHOD(device_attach, vmbus_timesync_attach),
74 DEVMETHOD(device_detach, vmbus_ic_detach),
78 static driver_t vmbus_timesync_driver = {
80 vmbus_timesync_methods,
81 sizeof(struct vmbus_ic_softc)
84 static devclass_t vmbus_timesync_devclass;
86 DRIVER_MODULE(hv_timesync, vmbus, vmbus_timesync_driver,
87 vmbus_timesync_devclass, NULL, NULL);
88 MODULE_VERSION(hv_timesync, 1);
89 MODULE_DEPEND(hv_timesync, vmbus, 1, 1, 1);
91 SYSCTL_NODE(_hw, OID_AUTO, hvtimesync, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
92 "Hyper-V timesync interface");
94 static int vmbus_ts_ignore_sync = 0;
95 SYSCTL_INT(_hw_hvtimesync, OID_AUTO, ignore_sync, CTLFLAG_RWTUN,
96 &vmbus_ts_ignore_sync, 0, "Ignore the sync request.");
99 * Trigger sample sync when drift exceeds threshold (ms).
100 * Ignore the sample request when set to 0.
102 static int vmbus_ts_sample_thresh = 100;
103 SYSCTL_INT(_hw_hvtimesync, OID_AUTO, sample_thresh, CTLFLAG_RWTUN,
104 &vmbus_ts_sample_thresh, 0,
105 "Threshold that makes sample request trigger the sync (unit: ms).");
107 static int vmbus_ts_sample_verbose = 0;
108 SYSCTL_INT(_hw_hvtimesync, OID_AUTO, sample_verbose, CTLFLAG_RWTUN,
109 &vmbus_ts_sample_verbose, 0, "Increase sample request verbosity.");
112 vmbus_timesync(struct vmbus_ic_softc *sc, uint64_t hvtime, uint64_t sent_tc,
115 struct timespec vm_ts;
116 uint64_t hv_ns, vm_ns, rtt = 0;
118 if (VMBUS_TIMESYNC_DORTT(sc))
119 rtt = hyperv_tc64() - sent_tc;
121 hv_ns = (hvtime - VMBUS_ICMSG_TS_BASE + rtt) * HYPERV_TIMER_NS_FACTOR;
123 vm_ns = (vm_ts.tv_sec * NANOSEC) + vm_ts.tv_nsec;
125 if ((tsflags & VMBUS_ICMSG_TS_FLAG_SYNC) && !vmbus_ts_ignore_sync) {
126 struct timespec hv_ts;
129 device_printf(sc->ic_dev, "apply sync request, "
130 "hv: %ju, vm: %ju\n",
131 (uintmax_t)hv_ns, (uintmax_t)vm_ns);
133 hv_ts.tv_sec = hv_ns / NANOSEC;
134 hv_ts.tv_nsec = hv_ns % NANOSEC;
135 kern_clock_settime(curthread, CLOCK_REALTIME, &hv_ts);
140 if ((tsflags & VMBUS_ICMSG_TS_FLAG_SAMPLE) &&
141 vmbus_ts_sample_thresh >= 0) {
144 if (vmbus_ts_sample_verbose) {
145 device_printf(sc->ic_dev, "sample request, "
146 "hv: %ju, vm: %ju\n",
147 (uintmax_t)hv_ns, (uintmax_t)vm_ns);
151 diff = hv_ns - vm_ns;
153 diff = vm_ns - hv_ns;
154 /* nanosec -> millisec */
157 if (diff > vmbus_ts_sample_thresh) {
158 struct timespec hv_ts;
161 device_printf(sc->ic_dev,
162 "apply sample request, hv: %ju, vm: %ju\n",
163 (uintmax_t)hv_ns, (uintmax_t)vm_ns);
165 hv_ts.tv_sec = hv_ns / NANOSEC;
166 hv_ts.tv_nsec = hv_ns % NANOSEC;
167 kern_clock_settime(curthread, CLOCK_REALTIME, &hv_ts);
175 vmbus_timesync_cb(struct vmbus_channel *chan, void *xsc)
177 struct vmbus_ic_softc *sc = xsc;
178 struct vmbus_icmsg_hdr *hdr;
187 dlen = sc->ic_buflen;
188 error = vmbus_chan_recv(chan, data, &dlen, &xactid);
189 KASSERT(error != ENOBUFS, ("icbuf is not large enough"));
193 if (dlen < sizeof(*hdr)) {
194 device_printf(sc->ic_dev, "invalid data len %d\n", dlen);
200 * Update request, which will be echoed back as response.
202 switch (hdr->ic_type) {
203 case VMBUS_ICMSG_TYPE_NEGOTIATE:
204 error = vmbus_ic_negomsg(sc, data, &dlen,
205 VMBUS_TIMESYNC_FWVER, VMBUS_TIMESYNC_MSGVER);
208 if (VMBUS_TIMESYNC_DORTT(sc))
209 device_printf(sc->ic_dev, "RTT\n");
212 case VMBUS_ICMSG_TYPE_TIMESYNC:
213 if (VMBUS_TIMESYNC_MSGVER4(sc)) {
214 const struct vmbus_icmsg_timesync4 *msg4;
216 if (dlen < sizeof(*msg4)) {
217 device_printf(sc->ic_dev, "invalid timesync4 "
222 vmbus_timesync(sc, msg4->ic_hvtime, msg4->ic_sent_tc,
225 const struct vmbus_icmsg_timesync *msg;
227 if (dlen < sizeof(*msg)) {
228 device_printf(sc->ic_dev, "invalid timesync "
233 vmbus_timesync(sc, msg->ic_hvtime, 0, msg->ic_tsflags);
238 device_printf(sc->ic_dev, "got 0x%08x icmsg\n", hdr->ic_type);
243 * Send response by echoing the request back.
245 vmbus_ic_sendresp(sc, chan, data, dlen, xactid);
249 vmbus_timesync_probe(device_t dev)
252 return (vmbus_ic_probe(dev, vmbus_timesync_descs));
256 vmbus_timesync_attach(device_t dev)
259 return (vmbus_ic_attach(dev, vmbus_timesync_cb));