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