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