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