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/param.h>
28 #include <sys/bus.h>
29 #include <sys/kernel.h>
30 #include <sys/module.h>
31 #include <sys/proc.h>
32 #include <sys/smp.h>
33 #include <sys/systm.h>
34 #include <sys/timeet.h>
35
36 #include <dev/hyperv/include/hyperv.h>
37 #if defined(__aarch64__)
38 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h>
39 #else
40 #include <dev/hyperv/vmbus/x86/hyperv_reg.h>
41 #endif
42 #include <dev/hyperv/vmbus/hyperv_var.h>
43 #include <dev/hyperv/vmbus/vmbus_var.h>
44 #include <dev/hyperv/vmbus/hyperv_common_reg.h>
45
46 #define VMBUS_ET_NAME "hvet"
47
48 #define MSR_HV_STIMER0_CFG_SINT \
49 ((((uint64_t)VMBUS_SINT_TIMER) << MSR_HV_STIMER_CFG_SINT_SHIFT) & \
50 MSR_HV_STIMER_CFG_SINT_MASK)
51
52 /*
53 * Additionally required feature:
54 * - SynIC is needed for interrupt generation.
55 */
56 #define CPUID_HV_ET_MASK (CPUID_HV_MSR_SYNIC | \
57 CPUID_HV_MSR_SYNTIMER)
58
59 static void vmbus_et_identify(driver_t *, device_t);
60 static int vmbus_et_probe(device_t);
61 static int vmbus_et_attach(device_t);
62 static int vmbus_et_detach(device_t);
63 static int vmbus_et_start(struct eventtimer *, sbintime_t,
64 sbintime_t);
65
66 static struct eventtimer vmbus_et;
67
68 static device_method_t vmbus_et_methods[] = {
69 DEVMETHOD(device_identify, vmbus_et_identify),
70 DEVMETHOD(device_probe, vmbus_et_probe),
71 DEVMETHOD(device_attach, vmbus_et_attach),
72 DEVMETHOD(device_detach, vmbus_et_detach),
73
74 DEVMETHOD_END
75 };
76
77 static driver_t vmbus_et_driver = {
78 VMBUS_ET_NAME,
79 vmbus_et_methods,
80 0
81 };
82
83 DRIVER_MODULE(hv_et, vmbus, vmbus_et_driver, NULL, NULL);
84 MODULE_VERSION(hv_et, 1);
85
86 static __inline uint64_t
hyperv_sbintime2count(sbintime_t time)87 hyperv_sbintime2count(sbintime_t time)
88 {
89 struct timespec val;
90
91 val = sbttots(time);
92 return (val.tv_sec * HYPERV_TIMER_FREQ) +
93 (val.tv_nsec / HYPERV_TIMER_NS_FACTOR);
94 }
95
96 static int
vmbus_et_start(struct eventtimer * et __unused,sbintime_t first,sbintime_t period __unused)97 vmbus_et_start(struct eventtimer *et __unused, sbintime_t first,
98 sbintime_t period __unused)
99 {
100 uint64_t current;
101
102 current = hyperv_tc64();
103 current += hyperv_sbintime2count(first);
104 wrmsr(MSR_HV_STIMER0_COUNT, current);
105
106 return (0);
107 }
108
109 void
vmbus_et_intr(struct trapframe * frame)110 vmbus_et_intr(struct trapframe *frame)
111 {
112 struct trapframe *oldframe;
113 struct thread *td;
114
115 if (vmbus_et.et_active) {
116 td = curthread;
117 td->td_intr_nesting_level++;
118 oldframe = td->td_intr_frame;
119 td->td_intr_frame = frame;
120 vmbus_et.et_event_cb(&vmbus_et, vmbus_et.et_arg);
121 td->td_intr_frame = oldframe;
122 td->td_intr_nesting_level--;
123 }
124 }
125
126 static void
vmbus_et_identify(driver_t * driver,device_t parent)127 vmbus_et_identify(driver_t *driver, device_t parent)
128 {
129 if (device_get_unit(parent) != 0 ||
130 device_find_child(parent, VMBUS_ET_NAME, -1) != NULL ||
131 (hyperv_features & CPUID_HV_ET_MASK) != CPUID_HV_ET_MASK ||
132 hyperv_tc64 == NULL)
133 return;
134
135 device_add_child(parent, VMBUS_ET_NAME, -1);
136 }
137
138 static int
vmbus_et_probe(device_t dev)139 vmbus_et_probe(device_t dev)
140 {
141 if (resource_disabled(VMBUS_ET_NAME, 0))
142 return (ENXIO);
143
144 device_set_desc(dev, "Hyper-V event timer");
145
146 return (BUS_PROBE_NOWILDCARD);
147 }
148
149 static void
vmbus_et_config(void * arg __unused)150 vmbus_et_config(void *arg __unused)
151 {
152 /*
153 * Make sure that STIMER0 is really disabled before writing
154 * to STIMER0_CONFIG.
155 *
156 * "Writing to the configuration register of a timer that
157 * is already enabled may result in undefined behaviour."
158 */
159 for (;;) {
160 uint64_t val;
161
162 /* Stop counting, and this also implies disabling STIMER0 */
163 wrmsr(MSR_HV_STIMER0_COUNT, 0);
164
165 val = rdmsr(MSR_HV_STIMER0_CONFIG);
166 if ((val & MSR_HV_STIMER_CFG_ENABLE) == 0)
167 break;
168 cpu_spinwait();
169 }
170 wrmsr(MSR_HV_STIMER0_CONFIG,
171 MSR_HV_STIMER_CFG_AUTOEN | MSR_HV_STIMER0_CFG_SINT);
172 }
173
174 static int
vmbus_et_attach(device_t dev)175 vmbus_et_attach(device_t dev)
176 {
177 /* TODO: use independent IDT vector */
178
179 vmbus_et.et_name = "Hyper-V";
180 vmbus_et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
181 vmbus_et.et_quality = 1000;
182 vmbus_et.et_frequency = HYPERV_TIMER_FREQ;
183 vmbus_et.et_min_period = (0x00000001ULL << 32) / HYPERV_TIMER_FREQ;
184 vmbus_et.et_max_period = (0xfffffffeULL << 32) / HYPERV_TIMER_FREQ;
185 vmbus_et.et_start = vmbus_et_start;
186
187 /*
188 * Delay a bit to make sure that hyperv_tc64 will not return 0,
189 * since writing 0 to STIMER0_COUNT will disable STIMER0.
190 */
191 DELAY(100);
192 smp_rendezvous(NULL, vmbus_et_config, NULL, NULL);
193
194 return (et_register(&vmbus_et));
195 }
196
197 static int
vmbus_et_detach(device_t dev)198 vmbus_et_detach(device_t dev)
199 {
200 return (et_deregister(&vmbus_et));
201 }
202