1 /*- 2 * Copyright (c) 2015,2016-2017 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, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <sys/param.h> 29 #include <sys/bus.h> 30 #include <sys/kernel.h> 31 #include <sys/module.h> 32 #include <sys/proc.h> 33 #include <sys/smp.h> 34 #include <sys/systm.h> 35 #include <sys/timeet.h> 36 37 #include <dev/hyperv/include/hyperv.h> 38 #if defined(__aarch64__) 39 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h> 40 #else 41 #include <dev/hyperv/vmbus/x86/hyperv_reg.h> 42 #endif 43 #include <dev/hyperv/vmbus/hyperv_var.h> 44 #include <dev/hyperv/vmbus/vmbus_var.h> 45 #include <dev/hyperv/vmbus/hyperv_common_reg.h> 46 47 #define VMBUS_ET_NAME "hvet" 48 49 #define MSR_HV_STIMER0_CFG_SINT \ 50 ((((uint64_t)VMBUS_SINT_TIMER) << MSR_HV_STIMER_CFG_SINT_SHIFT) & \ 51 MSR_HV_STIMER_CFG_SINT_MASK) 52 53 /* 54 * Additionally required feature: 55 * - SynIC is needed for interrupt generation. 56 */ 57 #define CPUID_HV_ET_MASK (CPUID_HV_MSR_SYNIC | \ 58 CPUID_HV_MSR_SYNTIMER) 59 60 static void vmbus_et_identify(driver_t *, device_t); 61 static int vmbus_et_probe(device_t); 62 static int vmbus_et_attach(device_t); 63 static int vmbus_et_detach(device_t); 64 static int vmbus_et_start(struct eventtimer *, sbintime_t, 65 sbintime_t); 66 67 static struct eventtimer vmbus_et; 68 69 static device_method_t vmbus_et_methods[] = { 70 DEVMETHOD(device_identify, vmbus_et_identify), 71 DEVMETHOD(device_probe, vmbus_et_probe), 72 DEVMETHOD(device_attach, vmbus_et_attach), 73 DEVMETHOD(device_detach, vmbus_et_detach), 74 75 DEVMETHOD_END 76 }; 77 78 static driver_t vmbus_et_driver = { 79 VMBUS_ET_NAME, 80 vmbus_et_methods, 81 0 82 }; 83 84 DRIVER_MODULE(hv_et, vmbus, vmbus_et_driver, NULL, NULL); 85 MODULE_VERSION(hv_et, 1); 86 87 static __inline uint64_t 88 hyperv_sbintime2count(sbintime_t time) 89 { 90 struct timespec val; 91 92 val = sbttots(time); 93 return (val.tv_sec * HYPERV_TIMER_FREQ) + 94 (val.tv_nsec / HYPERV_TIMER_NS_FACTOR); 95 } 96 97 static int 98 vmbus_et_start(struct eventtimer *et __unused, sbintime_t first, 99 sbintime_t period __unused) 100 { 101 uint64_t current; 102 103 current = hyperv_tc64(); 104 current += hyperv_sbintime2count(first); 105 wrmsr(MSR_HV_STIMER0_COUNT, current); 106 107 return (0); 108 } 109 110 void 111 vmbus_et_intr(struct trapframe *frame) 112 { 113 struct trapframe *oldframe; 114 struct thread *td; 115 116 if (vmbus_et.et_active) { 117 td = curthread; 118 td->td_intr_nesting_level++; 119 oldframe = td->td_intr_frame; 120 td->td_intr_frame = frame; 121 vmbus_et.et_event_cb(&vmbus_et, vmbus_et.et_arg); 122 td->td_intr_frame = oldframe; 123 td->td_intr_nesting_level--; 124 } 125 } 126 127 static void 128 vmbus_et_identify(driver_t *driver, device_t parent) 129 { 130 if (device_get_unit(parent) != 0 || 131 device_find_child(parent, VMBUS_ET_NAME, -1) != NULL || 132 (hyperv_features & CPUID_HV_ET_MASK) != CPUID_HV_ET_MASK || 133 hyperv_tc64 == NULL) 134 return; 135 136 device_add_child(parent, VMBUS_ET_NAME, -1); 137 } 138 139 static int 140 vmbus_et_probe(device_t dev) 141 { 142 if (resource_disabled(VMBUS_ET_NAME, 0)) 143 return (ENXIO); 144 145 device_set_desc(dev, "Hyper-V event timer"); 146 147 return (BUS_PROBE_NOWILDCARD); 148 } 149 150 static void 151 vmbus_et_config(void *arg __unused) 152 { 153 /* 154 * Make sure that STIMER0 is really disabled before writing 155 * to STIMER0_CONFIG. 156 * 157 * "Writing to the configuration register of a timer that 158 * is already enabled may result in undefined behaviour." 159 */ 160 for (;;) { 161 uint64_t val; 162 163 /* Stop counting, and this also implies disabling STIMER0 */ 164 wrmsr(MSR_HV_STIMER0_COUNT, 0); 165 166 val = rdmsr(MSR_HV_STIMER0_CONFIG); 167 if ((val & MSR_HV_STIMER_CFG_ENABLE) == 0) 168 break; 169 cpu_spinwait(); 170 } 171 wrmsr(MSR_HV_STIMER0_CONFIG, 172 MSR_HV_STIMER_CFG_AUTOEN | MSR_HV_STIMER0_CFG_SINT); 173 } 174 175 static int 176 vmbus_et_attach(device_t dev) 177 { 178 /* TODO: use independent IDT vector */ 179 180 vmbus_et.et_name = "Hyper-V"; 181 vmbus_et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU; 182 vmbus_et.et_quality = 1000; 183 vmbus_et.et_frequency = HYPERV_TIMER_FREQ; 184 vmbus_et.et_min_period = (0x00000001ULL << 32) / HYPERV_TIMER_FREQ; 185 vmbus_et.et_max_period = (0xfffffffeULL << 32) / HYPERV_TIMER_FREQ; 186 vmbus_et.et_start = vmbus_et_start; 187 188 /* 189 * Delay a bit to make sure that hyperv_tc64 will not return 0, 190 * since writing 0 to STIMER0_COUNT will disable STIMER0. 191 */ 192 DELAY(100); 193 smp_rendezvous(NULL, vmbus_et_config, NULL, NULL); 194 195 return (et_register(&vmbus_et)); 196 } 197 198 static int 199 vmbus_et_detach(device_t dev) 200 { 201 return (et_deregister(&vmbus_et)); 202 } 203