]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/utilities/hv_timesync.c
MFC 310312-310314
[FreeBSD/stable/10.git] / sys / dev / hyperv / utilities / hv_timesync.c
1 /*-
2  * Copyright (c) 2014,2016 Microsoft Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/bus.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>
37
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>
42
43 #include "vmbus_if.h"
44
45 #define VMBUS_TIMESYNC_FWVER_MAJOR      3
46 #define VMBUS_TIMESYNC_FWVER            \
47         VMBUS_IC_VERSION(VMBUS_TIMESYNC_FWVER_MAJOR, 0)
48
49 #define VMBUS_TIMESYNC_MSGVER_MAJOR     4
50 #define VMBUS_TIMESYNC_MSGVER           \
51         VMBUS_IC_VERSION(VMBUS_TIMESYNC_MSGVER_MAJOR, 0)
52
53 #define VMBUS_TIMESYNC_DORTT(sc)        \
54         ((sc)->ic_msgver >= VMBUS_IC_VERSION(4, 0) && \
55          (hyperv_features & CPUID_HV_MSR_TIME_REFCNT))
56
57 static const struct vmbus_ic_desc vmbus_timesync_descs[] = {
58         {
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"
63         },
64         VMBUS_IC_DESC_END
65 };
66
67 SYSCTL_NODE(_hw, OID_AUTO, hvtimesync, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
68     "Hyper-V timesync interface");
69
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.");
73
74 /*
75  * Trigger sample sync when drift exceeds threshold (ms).
76  * Ignore the sample request when set to 0.
77  */
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).");
82
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.");
86
87 static void
88 vmbus_timesync(struct vmbus_ic_softc *sc, uint64_t hvtime, uint64_t sent_tc,
89     uint8_t tsflags)
90 {
91         struct timespec vm_ts;
92         uint64_t hv_ns, vm_ns, rtt = 0;
93
94         if (VMBUS_TIMESYNC_DORTT(sc))
95                 rtt = rdmsr(MSR_HV_TIME_REF_COUNT) - sent_tc;
96
97         hv_ns = (hvtime - VMBUS_ICMSG_TS_BASE + rtt) * HYPERV_TIMER_NS_FACTOR;
98         nanotime(&vm_ts);
99         vm_ns = (vm_ts.tv_sec * NANOSEC) + vm_ts.tv_nsec;
100
101         if ((tsflags & VMBUS_ICMSG_TS_FLAG_SYNC) && !vmbus_ts_ignore_sync) {
102                 struct timespec hv_ts;
103
104                 if (bootverbose) {
105                         device_printf(sc->ic_dev, "apply sync request, "
106                             "hv: %ju, vm: %ju\n",
107                             (uintmax_t)hv_ns, (uintmax_t)vm_ns);
108                 }
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);
112                 /* Done! */
113                 return;
114         }
115
116         if ((tsflags & VMBUS_ICMSG_TS_FLAG_SAMPLE) &&
117             vmbus_ts_sample_thresh > 0) {
118                 int64_t diff;
119
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);
124                 }
125
126                 if (hv_ns > vm_ns)
127                         diff = hv_ns - vm_ns;
128                 else
129                         diff = vm_ns - hv_ns;
130                 /* nanosec -> millisec */
131                 diff /= 1000000;
132
133                 if (diff > vmbus_ts_sample_thresh) {
134                         struct timespec hv_ts;
135
136                         if (bootverbose) {
137                                 device_printf(sc->ic_dev,
138                                     "apply sample request, hv: %ju, vm: %ju\n",
139                                     (uintmax_t)hv_ns, (uintmax_t)vm_ns);
140                         }
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);
144                 }
145                 /* Done */
146                 return;
147         }
148 }
149
150 static void
151 vmbus_timesync_cb(struct vmbus_channel *chan, void *xsc)
152 {
153         struct vmbus_ic_softc *sc = xsc;
154         struct vmbus_icmsg_hdr *hdr;
155         const struct vmbus_icmsg_timesync *msg;
156         int dlen, error;
157         uint64_t xactid;
158         void *data;
159
160         /*
161          * Receive request.
162          */
163         data = sc->ic_buf;
164         dlen = sc->ic_buflen;
165         error = vmbus_chan_recv(chan, data, &dlen, &xactid);
166         KASSERT(error != ENOBUFS, ("icbuf is not large enough"));
167         if (error)
168                 return;
169
170         if (dlen < sizeof(*hdr)) {
171                 device_printf(sc->ic_dev, "invalid data len %d\n", dlen);
172                 return;
173         }
174         hdr = data;
175
176         /*
177          * Update request, which will be echoed back as response.
178          */
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);
183                 if (error)
184                         return;
185                 if (VMBUS_TIMESYNC_DORTT(sc))
186                         device_printf(sc->ic_dev, "RTT\n");
187                 break;
188
189         case VMBUS_ICMSG_TYPE_TIMESYNC:
190                 if (dlen < sizeof(*msg)) {
191                         device_printf(sc->ic_dev, "invalid timesync len %d\n",
192                             dlen);
193                         return;
194                 }
195                 msg = data;
196                 vmbus_timesync(sc, msg->ic_hvtime, msg->ic_sent_tc,
197                     msg->ic_tsflags);
198                 break;
199
200         default:
201                 device_printf(sc->ic_dev, "got 0x%08x icmsg\n", hdr->ic_type);
202                 break;
203         }
204
205         /*
206          * Send response by echoing the request back.
207          */
208         vmbus_ic_sendresp(sc, chan, data, dlen, xactid);
209 }
210
211 static int
212 hv_timesync_probe(device_t dev)
213 {
214
215         return (vmbus_ic_probe(dev, vmbus_timesync_descs));
216 }
217
218 static int
219 hv_timesync_attach(device_t dev)
220 {
221
222         return (vmbus_ic_attach(dev, vmbus_timesync_cb));
223 }
224
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),
230         { 0, 0 }
231 };
232
233 static driver_t timesync_driver = {
234         "hvtimesync",
235         timesync_methods,
236         sizeof(struct vmbus_ic_softc)
237 };
238
239 static devclass_t timesync_devclass;
240
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);