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