2 * Copyright (c) 2015 Microsoft Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/timeet.h>
37 #include "hv_vmbus_priv.h"
39 #define HV_TIMER_FREQUENCY (10 * 1000 * 1000LL) /* 100ns period */
40 #define HV_MAX_DELTA_TICKS 0xffffffffLL
41 #define HV_MIN_DELTA_TICKS 1LL
43 static struct eventtimer et;
44 static uint64_t periodticks[MAXCPU];
46 static inline uint64_t
47 sbintime2tick(sbintime_t time)
52 return val.tv_sec * HV_TIMER_FREQUENCY + val.tv_nsec / 100;
56 hv_et_start(struct eventtimer *et, sbintime_t firsttime, sbintime_t periodtime)
58 union hv_timer_config timer_cfg;
61 timer_cfg.as_uint64 = 0;
62 timer_cfg.auto_enable = 1;
63 timer_cfg.sintx = HV_VMBUS_TIMER_SINT;
65 periodticks[curcpu] = sbintime2tick(periodtime);
67 firsttime = periodtime;
69 current = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
70 current += sbintime2tick(firsttime);
72 wrmsr(HV_X64_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
73 wrmsr(HV_X64_MSR_STIMER0_COUNT, current);
79 hv_et_stop(struct eventtimer *et)
81 wrmsr(HV_X64_MSR_STIMER0_CONFIG, 0);
82 wrmsr(HV_X64_MSR_STIMER0_COUNT, 0);
88 hv_et_intr(struct trapframe *frame)
90 union hv_timer_config timer_cfg;
91 struct trapframe *oldframe;
94 if (periodticks[curcpu] != 0) {
95 uint64_t tick = sbintime2tick(periodticks[curcpu]);
96 timer_cfg.as_uint64 = rdmsr(HV_X64_MSR_STIMER0_CONFIG);
98 timer_cfg.auto_enable = 1;
99 timer_cfg.periodic = 1;
100 periodticks[curcpu] = 0;
102 wrmsr(HV_X64_MSR_STIMER0_CONFIG, timer_cfg.as_uint64);
103 wrmsr(HV_X64_MSR_STIMER0_COUNT, tick);
108 td->td_intr_nesting_level++;
109 oldframe = td->td_intr_frame;
110 td->td_intr_frame = frame;
111 et.et_event_cb(&et, et.et_arg);
112 td->td_intr_frame = oldframe;
113 td->td_intr_nesting_level--;
120 et.et_name = "HyperV";
121 et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU | ET_FLAGS_PERIODIC;
122 et.et_quality = 1000;
123 et.et_frequency = HV_TIMER_FREQUENCY;
124 et.et_min_period = (1LL << 32) / HV_TIMER_FREQUENCY;
125 et.et_max_period = HV_MAX_DELTA_TICKS * ((1LL << 32) / HV_TIMER_FREQUENCY);
126 et.et_start = hv_et_start;
127 et.et_stop = hv_et_stop;