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