vmbus_et.c revision 298449
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: head/sys/dev/hyperv/vmbus/hv_et.c 298449 2016-04-22 05:15:59Z 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>
35293873Ssephe#include <sys/systm.h>
36293873Ssephe#include <sys/smp.h>
37293873Ssephe#include <sys/time.h>
38293873Ssephe#include <sys/timeet.h>
39293873Ssephe
40293873Ssephe#include "hv_vmbus_priv.h"
41293873Ssephe
42293873Ssephe#define HV_TIMER_FREQUENCY		(10 * 1000 * 1000LL) /* 100ns period */
43293873Ssephe#define HV_MAX_DELTA_TICKS		0xffffffffLL
44293873Ssephe#define HV_MIN_DELTA_TICKS		1LL
45293873Ssephe
46298449Ssephestatic struct eventtimer *et;
47293873Ssephe
48293873Ssephestatic inline uint64_t
49293873Ssephesbintime2tick(sbintime_t time)
50293873Ssephe{
51293873Ssephe	struct timespec val;
52293873Ssephe
53293873Ssephe	val = sbttots(time);
54293873Ssephe	return val.tv_sec * HV_TIMER_FREQUENCY + val.tv_nsec / 100;
55293873Ssephe}
56293873Ssephe
57293873Ssephestatic int
58293873Ssephehv_et_start(struct eventtimer *et, sbintime_t firsttime, sbintime_t periodtime)
59293873Ssephe{
60293873Ssephe	union hv_timer_config timer_cfg;
61293873Ssephe	uint64_t current;
62293873Ssephe
63293873Ssephe	timer_cfg.as_uint64 = 0;
64293873Ssephe	timer_cfg.auto_enable = 1;
65297176Ssephe	timer_cfg.sintx = HV_VMBUS_TIMER_SINT;
66293873Ssephe
67293873Ssephe	current = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
68293873Ssephe	current += sbintime2tick(firsttime);
69293873Ssephe
70293873Ssephe	wrmsr(HV_X64_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
71293873Ssephe	wrmsr(HV_X64_MSR_STIMER0_COUNT, current);
72293873Ssephe
73293873Ssephe	return (0);
74293873Ssephe}
75293873Ssephe
76293873Ssephestatic int
77293873Ssephehv_et_stop(struct eventtimer *et)
78293873Ssephe{
79293873Ssephe	wrmsr(HV_X64_MSR_STIMER0_CONFIG, 0);
80293873Ssephe	wrmsr(HV_X64_MSR_STIMER0_COUNT, 0);
81293873Ssephe
82293873Ssephe	return (0);
83293873Ssephe}
84293873Ssephe
85293873Ssephevoid
86293873Ssephehv_et_intr(struct trapframe *frame)
87293873Ssephe{
88293873Ssephe	struct trapframe *oldframe;
89293873Ssephe	struct thread *td;
90293873Ssephe
91298449Ssephe	if (et->et_active) {
92293873Ssephe		td = curthread;
93293873Ssephe		td->td_intr_nesting_level++;
94293873Ssephe		oldframe = td->td_intr_frame;
95293873Ssephe		td->td_intr_frame = frame;
96298449Ssephe		et->et_event_cb(et, et->et_arg);
97293873Ssephe		td->td_intr_frame = oldframe;
98293873Ssephe		td->td_intr_nesting_level--;
99293873Ssephe	}
100293873Ssephe}
101293873Ssephe
102298449Ssephestatic void
103298449Ssephehv_et_identify (driver_t *driver, device_t parent)
104293873Ssephe{
105298449Ssephe	if (device_find_child(parent, "hv_et", -1) != NULL)
106298449Ssephe		return;
107298449Ssephe
108298449Ssephe	device_add_child(parent, "hv_et", -1);
109293873Ssephe}
110293873Ssephe
111298449Ssephestatic int
112298449Ssephehv_et_probe(device_t dev)
113298449Ssephe{
114298449Ssephe	device_set_desc(dev, "Hyper-V event timer");
115298449Ssephe
116298449Ssephe	return (BUS_PROBE_NOWILDCARD);
117298449Ssephe}
118298449Ssephe
119298449Ssephestatic int
120298449Ssephehv_et_attach(device_t dev)
121298449Ssephe{
122298449Ssephe	/* XXX: need allocate SINT and remove global et */
123298449Ssephe	et = device_get_softc(dev);
124298449Ssephe
125298449Ssephe	et->et_name = "Hyper-V";
126298449Ssephe	et->et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
127298449Ssephe	et->et_quality = 1000;
128298449Ssephe	et->et_frequency = HV_TIMER_FREQUENCY;
129298449Ssephe	et->et_min_period = HV_MIN_DELTA_TICKS * ((1LL << 32) / HV_TIMER_FREQUENCY);
130298449Ssephe	et->et_max_period = HV_MAX_DELTA_TICKS * ((1LL << 32) / HV_TIMER_FREQUENCY);
131298449Ssephe	et->et_start = hv_et_start;
132298449Ssephe	et->et_stop = hv_et_stop;
133298449Ssephe	et->et_priv = dev;
134298449Ssephe
135298449Ssephe	return (et_register(et));
136298449Ssephe}
137298449Ssephe
138298449Ssephestatic int
139298449Ssephehv_et_detach(device_t dev)
140298449Ssephe{
141298449Ssephe	return (et_deregister(et));
142298449Ssephe}
143298449Ssephe
144298449Ssephestatic device_method_t hv_et_methods[] = {
145298449Ssephe	DEVMETHOD(device_identify,      hv_et_identify),
146298449Ssephe	DEVMETHOD(device_probe,         hv_et_probe),
147298449Ssephe	DEVMETHOD(device_attach,        hv_et_attach),
148298449Ssephe	DEVMETHOD(device_detach,        hv_et_detach),
149298449Ssephe
150298449Ssephe	DEVMETHOD_END
151298449Ssephe};
152298449Ssephe
153298449Ssephestatic driver_t hv_et_driver = {
154298449Ssephe	"hv_et",
155298449Ssephe	hv_et_methods,
156298449Ssephe	sizeof(struct eventtimer)
157298449Ssephe};
158298449Ssephe
159298449Ssephestatic devclass_t hv_et_devclass;
160298449SsepheDRIVER_MODULE(hv_et, vmbus, hv_et_driver, hv_et_devclass, NULL, 0);
161298449SsepheMODULE_VERSION(hv_et, 1);
162