vmbus_et.c revision 307466
1293873Ssephe/*-
2298446Ssephe * Copyright (c) 2015,2016 Microsoft Corp.
3293873Ssephe * All rights reserved.
4293873Ssephe *
5293873Ssephe * Redistribution and use in source and binary forms, with or without
6293873Ssephe * modification, are permitted provided that the following conditions
7293873Ssephe * are met:
8293873Ssephe * 1. Redistributions of source code must retain the above copyright
9293873Ssephe *      notice, this list of conditions and the following disclaimer.
10293873Ssephe * 2. Redistributions in binary form must reproduce the above copyright
11293873Ssephe *      notice, this list of conditions and the following disclaimer in the
12293873Ssephe *      documentation and/or other materials provided with the distribution.
13293873Ssephe *
14293873Ssephe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15293873Ssephe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16293873Ssephe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17293873Ssephe * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18293873Ssephe * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19293873Ssephe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20293873Ssephe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21293873Ssephe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22293873Ssephe * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23293873Ssephe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24293873Ssephe * SUCH DAMAGE.
25293873Ssephe */
26293873Ssephe
27293873Ssephe#include <sys/cdefs.h>
28293873Ssephe__FBSDID("$FreeBSD: stable/11/sys/dev/hyperv/vmbus/vmbus_et.c 307466 2016-10-17 03:48:22Z sephe $");
29293873Ssephe
30293873Ssephe#include <sys/param.h>
31298449Ssephe#include <sys/bus.h>
32298449Ssephe#include <sys/kernel.h>
33298449Ssephe#include <sys/module.h>
34293873Ssephe#include <sys/proc.h>
35307466Ssephe#include <sys/smp.h>
36293873Ssephe#include <sys/systm.h>
37293873Ssephe#include <sys/timeet.h>
38293873Ssephe
39300827Ssephe#include <dev/hyperv/vmbus/hyperv_reg.h>
40300834Ssephe#include <dev/hyperv/vmbus/hyperv_var.h>
41300988Ssephe#include <dev/hyperv/vmbus/vmbus_var.h>
42293873Ssephe
43300993Ssephe#define VMBUS_ET_NAME			"hvet"
44300993Ssephe
45300827Ssephe#define MSR_HV_STIMER0_CFG_SINT		\
46300988Ssephe	((((uint64_t)VMBUS_SINT_TIMER) << MSR_HV_STIMER_CFG_SINT_SHIFT) & \
47300827Ssephe	 MSR_HV_STIMER_CFG_SINT_MASK)
48300827Ssephe
49300834Ssephe/*
50300834Ssephe * Two additionally required features:
51300834Ssephe * - SynIC is needed for interrupt generation.
52300834Ssephe * - Time reference counter is needed to set ABS reference count to
53300834Ssephe *   STIMER0_COUNT.
54300834Ssephe */
55300834Ssephe#define CPUID_HV_ET_MASK		(CPUID_HV_MSR_TIME_REFCNT |	\
56300834Ssephe					 CPUID_HV_MSR_SYNIC |		\
57300834Ssephe					 CPUID_HV_MSR_SYNTIMER)
58300834Ssephe
59307466Ssephestatic void			vmbus_et_identify(driver_t *, device_t);
60307466Ssephestatic int			vmbus_et_probe(device_t);
61307466Ssephestatic int			vmbus_et_attach(device_t);
62307466Ssephestatic int			vmbus_et_detach(device_t);
63307466Ssephestatic int			vmbus_et_start(struct eventtimer *, sbintime_t,
64307466Ssephe				    sbintime_t);
65307466Ssephe
66300989Ssephestatic struct eventtimer	vmbus_et;
67293873Ssephe
68307466Ssephestatic device_method_t vmbus_et_methods[] = {
69307466Ssephe	DEVMETHOD(device_identify,	vmbus_et_identify),
70307466Ssephe	DEVMETHOD(device_probe,		vmbus_et_probe),
71307466Ssephe	DEVMETHOD(device_attach,	vmbus_et_attach),
72307466Ssephe	DEVMETHOD(device_detach,	vmbus_et_detach),
73307466Ssephe
74307466Ssephe	DEVMETHOD_END
75307466Ssephe};
76307466Ssephe
77307466Ssephestatic driver_t vmbus_et_driver = {
78307466Ssephe	VMBUS_ET_NAME,
79307466Ssephe	vmbus_et_methods,
80307466Ssephe	0
81307466Ssephe};
82307466Ssephe
83307466Ssephestatic devclass_t vmbus_et_devclass;
84307466Ssephe
85307466SsepheDRIVER_MODULE(hv_et, vmbus, vmbus_et_driver, vmbus_et_devclass, NULL, NULL);
86307466SsepheMODULE_VERSION(hv_et, 1);
87307466Ssephe
88300987Ssephestatic __inline uint64_t
89300993Ssephehyperv_sbintime2count(sbintime_t time)
90293873Ssephe{
91293873Ssephe	struct timespec val;
92293873Ssephe
93293873Ssephe	val = sbttots(time);
94300992Ssephe	return (val.tv_sec * HYPERV_TIMER_FREQ) +
95300992Ssephe	    (val.tv_nsec / HYPERV_TIMER_NS_FACTOR);
96293873Ssephe}
97293873Ssephe
98293873Ssephestatic int
99300993Ssephevmbus_et_start(struct eventtimer *et __unused, sbintime_t first,
100300993Ssephe    sbintime_t period __unused)
101293873Ssephe{
102300987Ssephe	uint64_t current;
103293873Ssephe
104300827Ssephe	current = rdmsr(MSR_HV_TIME_REF_COUNT);
105300993Ssephe	current += hyperv_sbintime2count(first);
106300827Ssephe	wrmsr(MSR_HV_STIMER0_COUNT, current);
107293873Ssephe
108293873Ssephe	return (0);
109293873Ssephe}
110293873Ssephe
111293873Ssephevoid
112300988Ssephevmbus_et_intr(struct trapframe *frame)
113293873Ssephe{
114293873Ssephe	struct trapframe *oldframe;
115293873Ssephe	struct thread *td;
116293873Ssephe
117300989Ssephe	if (vmbus_et.et_active) {
118293873Ssephe		td = curthread;
119293873Ssephe		td->td_intr_nesting_level++;
120293873Ssephe		oldframe = td->td_intr_frame;
121293873Ssephe		td->td_intr_frame = frame;
122300989Ssephe		vmbus_et.et_event_cb(&vmbus_et, vmbus_et.et_arg);
123293873Ssephe		td->td_intr_frame = oldframe;
124293873Ssephe		td->td_intr_nesting_level--;
125293873Ssephe	}
126293873Ssephe}
127293873Ssephe
128298449Ssephestatic void
129300993Ssephevmbus_et_identify(driver_t *driver, device_t parent)
130293873Ssephe{
131300989Ssephe	if (device_get_unit(parent) != 0 ||
132300993Ssephe	    device_find_child(parent, VMBUS_ET_NAME, -1) != NULL ||
133300834Ssephe	    (hyperv_features & CPUID_HV_ET_MASK) != CPUID_HV_ET_MASK)
134298449Ssephe		return;
135298449Ssephe
136300993Ssephe	device_add_child(parent, VMBUS_ET_NAME, -1);
137293873Ssephe}
138293873Ssephe
139298449Ssephestatic int
140300993Ssephevmbus_et_probe(device_t dev)
141298449Ssephe{
142300994Ssephe	if (resource_disabled(VMBUS_ET_NAME, 0))
143300994Ssephe		return (ENXIO);
144300994Ssephe
145298449Ssephe	device_set_desc(dev, "Hyper-V event timer");
146298449Ssephe
147298449Ssephe	return (BUS_PROBE_NOWILDCARD);
148298449Ssephe}
149298449Ssephe
150300987Ssephestatic void
151300987Ssephevmbus_et_config(void *arg __unused)
152300987Ssephe{
153300987Ssephe	/*
154300987Ssephe	 * Make sure that STIMER0 is really disabled before writing
155300987Ssephe	 * to STIMER0_CONFIG.
156300987Ssephe	 *
157300987Ssephe	 * "Writing to the configuration register of a timer that
158300987Ssephe	 *  is already enabled may result in undefined behaviour."
159300987Ssephe	 */
160300987Ssephe	for (;;) {
161300987Ssephe		uint64_t val;
162300987Ssephe
163300987Ssephe		/* Stop counting, and this also implies disabling STIMER0 */
164300987Ssephe		wrmsr(MSR_HV_STIMER0_COUNT, 0);
165300987Ssephe
166300987Ssephe		val = rdmsr(MSR_HV_STIMER0_CONFIG);
167300987Ssephe		if ((val & MSR_HV_STIMER_CFG_ENABLE) == 0)
168300987Ssephe			break;
169300987Ssephe		cpu_spinwait();
170300987Ssephe	}
171300987Ssephe	wrmsr(MSR_HV_STIMER0_CONFIG,
172300987Ssephe	    MSR_HV_STIMER_CFG_AUTOEN | MSR_HV_STIMER0_CFG_SINT);
173300987Ssephe}
174300987Ssephe
175298449Ssephestatic int
176300993Ssephevmbus_et_attach(device_t dev)
177298449Ssephe{
178300989Ssephe	/* TODO: use independent IDT vector */
179298449Ssephe
180300989Ssephe	vmbus_et.et_name = "Hyper-V";
181300989Ssephe	vmbus_et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
182300989Ssephe	vmbus_et.et_quality = 1000;
183300992Ssephe	vmbus_et.et_frequency = HYPERV_TIMER_FREQ;
184300992Ssephe	vmbus_et.et_min_period = (0x00000001ULL << 32) / HYPERV_TIMER_FREQ;
185300992Ssephe	vmbus_et.et_max_period = (0xfffffffeULL << 32) / HYPERV_TIMER_FREQ;
186300993Ssephe	vmbus_et.et_start = vmbus_et_start;
187298449Ssephe
188300987Ssephe	/*
189300987Ssephe	 * Delay a bit to make sure that MSR_HV_TIME_REF_COUNT will
190300987Ssephe	 * not return 0, since writing 0 to STIMER0_COUNT will disable
191300987Ssephe	 * STIMER0.
192300987Ssephe	 */
193300987Ssephe	DELAY(100);
194300987Ssephe	smp_rendezvous(NULL, vmbus_et_config, NULL, NULL);
195300987Ssephe
196300989Ssephe	return (et_register(&vmbus_et));
197298449Ssephe}
198298449Ssephe
199298449Ssephestatic int
200300993Ssephevmbus_et_detach(device_t dev)
201298449Ssephe{
202300989Ssephe	return (et_deregister(&vmbus_et));
203298449Ssephe}
204