vmbus_et.c revision 293873
1/*- 2 * Copyright (c) 2015 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: head/sys/dev/hyperv/vmbus/hv_et.c 293873 2016-01-14 03:05:10Z sephe $"); 29 30#include <sys/param.h> 31#include <sys/proc.h> 32#include <sys/systm.h> 33#include <sys/smp.h> 34#include <sys/time.h> 35#include <sys/timeet.h> 36 37#include "hv_vmbus_priv.h" 38 39#define HV_TIMER_FREQUENCY (10 * 1000 * 1000LL) /* 100ns period */ 40#define HV_MAX_DELTA_TICKS 0xffffffffLL 41#define HV_MIN_DELTA_TICKS 1LL 42 43static struct eventtimer et; 44static uint64_t periodticks[MAXCPU]; 45 46static inline uint64_t 47sbintime2tick(sbintime_t time) 48{ 49 struct timespec val; 50 51 val = sbttots(time); 52 return val.tv_sec * HV_TIMER_FREQUENCY + val.tv_nsec / 100; 53} 54 55static int 56hv_et_start(struct eventtimer *et, sbintime_t firsttime, sbintime_t periodtime) 57{ 58 union hv_timer_config timer_cfg; 59 uint64_t current; 60 61 timer_cfg.as_uint64 = 0; 62 timer_cfg.auto_enable = 1; 63 timer_cfg.sintx = HV_VMBUS_MESSAGE_SINT; 64 65 periodticks[curcpu] = sbintime2tick(periodtime); 66 if (firsttime == 0) 67 firsttime = periodtime; 68 69 current = rdmsr(HV_X64_MSR_TIME_REF_COUNT); 70 current += sbintime2tick(firsttime); 71 72 wrmsr(HV_X64_MSR_STIMER0_CONFIG, timer_cfg.as_uint64); 73 wrmsr(HV_X64_MSR_STIMER0_COUNT, current); 74 75 return (0); 76} 77 78static int 79hv_et_stop(struct eventtimer *et) 80{ 81 wrmsr(HV_X64_MSR_STIMER0_CONFIG, 0); 82 wrmsr(HV_X64_MSR_STIMER0_COUNT, 0); 83 84 return (0); 85} 86 87void 88hv_et_intr(struct trapframe *frame) 89{ 90 union hv_timer_config timer_cfg; 91 struct trapframe *oldframe; 92 struct thread *td; 93 94 if (periodticks[curcpu] != 0) { 95 uint64_t tick = sbintime2tick(periodticks[curcpu]); 96 timer_cfg.as_uint64 = rdmsr(HV_X64_MSR_STIMER0_CONFIG); 97 timer_cfg.enable = 0; 98 timer_cfg.auto_enable = 1; 99 timer_cfg.periodic = 1; 100 periodticks[curcpu] = 0; 101 102 wrmsr(HV_X64_MSR_STIMER0_CONFIG, timer_cfg.as_uint64); 103 wrmsr(HV_X64_MSR_STIMER0_COUNT, tick); 104 } 105 106 if (et.et_active) { 107 td = curthread; 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--; 114 } 115} 116 117void 118hv_et_init(void) 119{ 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; 128 et.et_priv = &et; 129 et_register(&et); 130} 131 132