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