vmbus_timesync.c revision 311254
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: stable/10/sys/dev/hyperv/utilities/vmbus_timesync.c 311254 2017-01-04 05:24:16Z sephe $");
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#define VMBUS_TIMESYNC_FWVER_MAJOR	3
44#define VMBUS_TIMESYNC_FWVER		\
45	VMBUS_IC_VERSION(VMBUS_TIMESYNC_FWVER_MAJOR, 0)
46
47#define VMBUS_TIMESYNC_MSGVER_MAJOR	4
48#define VMBUS_TIMESYNC_MSGVER		\
49	VMBUS_IC_VERSION(VMBUS_TIMESYNC_MSGVER_MAJOR, 0)
50
51#define VMBUS_TIMESYNC_DORTT(sc)	\
52	((sc)->ic_msgver >= VMBUS_IC_VERSION(4, 0) && \
53	 (hyperv_features & CPUID_HV_MSR_TIME_REFCNT))
54
55static int			vmbus_timesync_probe(device_t);
56static int			vmbus_timesync_attach(device_t);
57
58static const struct vmbus_ic_desc vmbus_timesync_descs[] = {
59	{
60		.ic_guid = { .hv_guid = {
61		    0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
62		    0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf } },
63		.ic_desc = "Hyper-V Timesync"
64	},
65	VMBUS_IC_DESC_END
66};
67
68static device_method_t vmbus_timesync_methods[] = {
69	/* Device interface */
70	DEVMETHOD(device_probe,		vmbus_timesync_probe),
71	DEVMETHOD(device_attach,	vmbus_timesync_attach),
72	DEVMETHOD(device_detach,	vmbus_ic_detach),
73	DEVMETHOD_END
74};
75
76static driver_t vmbus_timesync_driver = {
77	"hvtimesync",
78	vmbus_timesync_methods,
79	sizeof(struct vmbus_ic_softc)
80};
81
82static devclass_t vmbus_timesync_devclass;
83
84DRIVER_MODULE(hv_timesync, vmbus, vmbus_timesync_driver,
85    vmbus_timesync_devclass, NULL, NULL);
86MODULE_VERSION(hv_timesync, 1);
87MODULE_DEPEND(hv_timesync, vmbus, 1, 1, 1);
88
89SYSCTL_NODE(_hw, OID_AUTO, hvtimesync, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
90    "Hyper-V timesync interface");
91
92static int vmbus_ts_ignore_sync = 0;
93SYSCTL_INT(_hw_hvtimesync, OID_AUTO, ignore_sync, CTLFLAG_RWTUN,
94    &vmbus_ts_ignore_sync, 0, "Ignore the sync request.");
95
96/*
97 * Trigger sample sync when drift exceeds threshold (ms).
98 * Ignore the sample request when set to 0.
99 */
100static int vmbus_ts_sample_thresh = 100;
101SYSCTL_INT(_hw_hvtimesync, OID_AUTO, sample_thresh, CTLFLAG_RWTUN,
102    &vmbus_ts_sample_thresh, 0,
103    "Threshold that makes sample request trigger the sync (unit: ms).");
104
105static int vmbus_ts_sample_verbose = 0;
106SYSCTL_INT(_hw_hvtimesync, OID_AUTO, sample_verbose, CTLFLAG_RWTUN,
107    &vmbus_ts_sample_verbose, 0, "Increase sample request verbosity.");
108
109static void
110vmbus_timesync(struct vmbus_ic_softc *sc, uint64_t hvtime, uint64_t sent_tc,
111    uint8_t tsflags)
112{
113	struct timespec vm_ts;
114	uint64_t hv_ns, vm_ns, rtt = 0;
115
116	if (VMBUS_TIMESYNC_DORTT(sc))
117		rtt = rdmsr(MSR_HV_TIME_REF_COUNT) - sent_tc;
118
119	hv_ns = (hvtime - VMBUS_ICMSG_TS_BASE + rtt) * HYPERV_TIMER_NS_FACTOR;
120	nanotime(&vm_ts);
121	vm_ns = (vm_ts.tv_sec * NANOSEC) + vm_ts.tv_nsec;
122
123	if ((tsflags & VMBUS_ICMSG_TS_FLAG_SYNC) && !vmbus_ts_ignore_sync) {
124		struct timespec hv_ts;
125
126		if (bootverbose) {
127			device_printf(sc->ic_dev, "apply sync request, "
128			    "hv: %ju, vm: %ju\n",
129			    (uintmax_t)hv_ns, (uintmax_t)vm_ns);
130		}
131		hv_ts.tv_sec = hv_ns / NANOSEC;
132		hv_ts.tv_nsec = hv_ns % NANOSEC;
133		kern_clock_settime(curthread, CLOCK_REALTIME, &hv_ts);
134		/* Done! */
135		return;
136	}
137
138	if ((tsflags & VMBUS_ICMSG_TS_FLAG_SAMPLE) &&
139	    vmbus_ts_sample_thresh > 0) {
140		int64_t diff;
141
142		if (vmbus_ts_sample_verbose) {
143			device_printf(sc->ic_dev, "sample request, "
144			    "hv: %ju, vm: %ju\n",
145			    (uintmax_t)hv_ns, (uintmax_t)vm_ns);
146		}
147
148		if (hv_ns > vm_ns)
149			diff = hv_ns - vm_ns;
150		else
151			diff = vm_ns - hv_ns;
152		/* nanosec -> millisec */
153		diff /= 1000000;
154
155		if (diff > vmbus_ts_sample_thresh) {
156			struct timespec hv_ts;
157
158			if (bootverbose) {
159				device_printf(sc->ic_dev,
160				    "apply sample request, hv: %ju, vm: %ju\n",
161				    (uintmax_t)hv_ns, (uintmax_t)vm_ns);
162			}
163			hv_ts.tv_sec = hv_ns / NANOSEC;
164			hv_ts.tv_nsec = hv_ns % NANOSEC;
165			kern_clock_settime(curthread, CLOCK_REALTIME, &hv_ts);
166		}
167		/* Done */
168		return;
169	}
170}
171
172static void
173vmbus_timesync_cb(struct vmbus_channel *chan, void *xsc)
174{
175	struct vmbus_ic_softc *sc = xsc;
176	struct vmbus_icmsg_hdr *hdr;
177	const struct vmbus_icmsg_timesync *msg;
178	int dlen, error;
179	uint64_t xactid;
180	void *data;
181
182	/*
183	 * Receive request.
184	 */
185	data = sc->ic_buf;
186	dlen = sc->ic_buflen;
187	error = vmbus_chan_recv(chan, data, &dlen, &xactid);
188	KASSERT(error != ENOBUFS, ("icbuf is not large enough"));
189	if (error)
190		return;
191
192	if (dlen < sizeof(*hdr)) {
193		device_printf(sc->ic_dev, "invalid data len %d\n", dlen);
194		return;
195	}
196	hdr = data;
197
198	/*
199	 * Update request, which will be echoed back as response.
200	 */
201	switch (hdr->ic_type) {
202	case VMBUS_ICMSG_TYPE_NEGOTIATE:
203		error = vmbus_ic_negomsg(sc, data, &dlen,
204		    VMBUS_TIMESYNC_FWVER, VMBUS_TIMESYNC_MSGVER);
205		if (error)
206			return;
207		if (VMBUS_TIMESYNC_DORTT(sc))
208			device_printf(sc->ic_dev, "RTT\n");
209		break;
210
211	case VMBUS_ICMSG_TYPE_TIMESYNC:
212		if (dlen < sizeof(*msg)) {
213			device_printf(sc->ic_dev, "invalid timesync len %d\n",
214			    dlen);
215			return;
216		}
217		msg = data;
218		vmbus_timesync(sc, msg->ic_hvtime, msg->ic_sent_tc,
219		    msg->ic_tsflags);
220		break;
221
222	default:
223		device_printf(sc->ic_dev, "got 0x%08x icmsg\n", hdr->ic_type);
224		break;
225	}
226
227	/*
228	 * Send response by echoing the request back.
229	 */
230	vmbus_ic_sendresp(sc, chan, data, dlen, xactid);
231}
232
233static int
234vmbus_timesync_probe(device_t dev)
235{
236
237	return (vmbus_ic_probe(dev, vmbus_timesync_descs));
238}
239
240static int
241vmbus_timesync_attach(device_t dev)
242{
243
244	return (vmbus_ic_attach(dev, vmbus_timesync_cb));
245}
246