1293873Ssephe/*- 2322612Ssephe * Copyright (c) 2015,2016-2017 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 322612 2017-08-17 05:09: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 39311373Ssephe#include <dev/hyperv/include/hyperv.h> 40300827Ssephe#include <dev/hyperv/vmbus/hyperv_reg.h> 41300834Ssephe#include <dev/hyperv/vmbus/hyperv_var.h> 42300988Ssephe#include <dev/hyperv/vmbus/vmbus_var.h> 43293873Ssephe 44300993Ssephe#define VMBUS_ET_NAME "hvet" 45300993Ssephe 46300827Ssephe#define MSR_HV_STIMER0_CFG_SINT \ 47300988Ssephe ((((uint64_t)VMBUS_SINT_TIMER) << MSR_HV_STIMER_CFG_SINT_SHIFT) & \ 48300827Ssephe MSR_HV_STIMER_CFG_SINT_MASK) 49300827Ssephe 50300834Ssephe/* 51314003Ssephe * Additionally required feature: 52300834Ssephe * - SynIC is needed for interrupt generation. 53300834Ssephe */ 54314003Ssephe#define CPUID_HV_ET_MASK (CPUID_HV_MSR_SYNIC | \ 55300834Ssephe CPUID_HV_MSR_SYNTIMER) 56300834Ssephe 57307466Ssephestatic void vmbus_et_identify(driver_t *, device_t); 58307466Ssephestatic int vmbus_et_probe(device_t); 59307466Ssephestatic int vmbus_et_attach(device_t); 60307466Ssephestatic int vmbus_et_detach(device_t); 61307466Ssephestatic int vmbus_et_start(struct eventtimer *, sbintime_t, 62307466Ssephe sbintime_t); 63307466Ssephe 64300989Ssephestatic struct eventtimer vmbus_et; 65293873Ssephe 66307466Ssephestatic device_method_t vmbus_et_methods[] = { 67307466Ssephe DEVMETHOD(device_identify, vmbus_et_identify), 68307466Ssephe DEVMETHOD(device_probe, vmbus_et_probe), 69307466Ssephe DEVMETHOD(device_attach, vmbus_et_attach), 70307466Ssephe DEVMETHOD(device_detach, vmbus_et_detach), 71307466Ssephe 72307466Ssephe DEVMETHOD_END 73307466Ssephe}; 74307466Ssephe 75307466Ssephestatic driver_t vmbus_et_driver = { 76307466Ssephe VMBUS_ET_NAME, 77307466Ssephe vmbus_et_methods, 78307466Ssephe 0 79307466Ssephe}; 80307466Ssephe 81307466Ssephestatic devclass_t vmbus_et_devclass; 82307466Ssephe 83307466SsepheDRIVER_MODULE(hv_et, vmbus, vmbus_et_driver, vmbus_et_devclass, NULL, NULL); 84307466SsepheMODULE_VERSION(hv_et, 1); 85307466Ssephe 86300987Ssephestatic __inline uint64_t 87300993Ssephehyperv_sbintime2count(sbintime_t time) 88293873Ssephe{ 89293873Ssephe struct timespec val; 90293873Ssephe 91293873Ssephe val = sbttots(time); 92300992Ssephe return (val.tv_sec * HYPERV_TIMER_FREQ) + 93300992Ssephe (val.tv_nsec / HYPERV_TIMER_NS_FACTOR); 94293873Ssephe} 95293873Ssephe 96293873Ssephestatic int 97300993Ssephevmbus_et_start(struct eventtimer *et __unused, sbintime_t first, 98300993Ssephe sbintime_t period __unused) 99293873Ssephe{ 100300987Ssephe uint64_t current; 101293873Ssephe 102314003Ssephe current = hyperv_tc64(); 103300993Ssephe current += hyperv_sbintime2count(first); 104300827Ssephe wrmsr(MSR_HV_STIMER0_COUNT, current); 105293873Ssephe 106293873Ssephe return (0); 107293873Ssephe} 108293873Ssephe 109293873Ssephevoid 110300988Ssephevmbus_et_intr(struct trapframe *frame) 111293873Ssephe{ 112293873Ssephe struct trapframe *oldframe; 113293873Ssephe struct thread *td; 114293873Ssephe 115300989Ssephe if (vmbus_et.et_active) { 116293873Ssephe td = curthread; 117293873Ssephe td->td_intr_nesting_level++; 118293873Ssephe oldframe = td->td_intr_frame; 119293873Ssephe td->td_intr_frame = frame; 120300989Ssephe vmbus_et.et_event_cb(&vmbus_et, vmbus_et.et_arg); 121293873Ssephe td->td_intr_frame = oldframe; 122293873Ssephe td->td_intr_nesting_level--; 123293873Ssephe } 124293873Ssephe} 125293873Ssephe 126298449Ssephestatic void 127300993Ssephevmbus_et_identify(driver_t *driver, device_t parent) 128293873Ssephe{ 129300989Ssephe if (device_get_unit(parent) != 0 || 130300993Ssephe device_find_child(parent, VMBUS_ET_NAME, -1) != NULL || 131314003Ssephe (hyperv_features & CPUID_HV_ET_MASK) != CPUID_HV_ET_MASK || 132314003Ssephe hyperv_tc64 == NULL) 133298449Ssephe return; 134298449Ssephe 135300993Ssephe device_add_child(parent, VMBUS_ET_NAME, -1); 136293873Ssephe} 137293873Ssephe 138298449Ssephestatic int 139300993Ssephevmbus_et_probe(device_t dev) 140298449Ssephe{ 141300994Ssephe if (resource_disabled(VMBUS_ET_NAME, 0)) 142300994Ssephe return (ENXIO); 143300994Ssephe 144298449Ssephe device_set_desc(dev, "Hyper-V event timer"); 145298449Ssephe 146298449Ssephe return (BUS_PROBE_NOWILDCARD); 147298449Ssephe} 148298449Ssephe 149300987Ssephestatic void 150300987Ssephevmbus_et_config(void *arg __unused) 151300987Ssephe{ 152300987Ssephe /* 153300987Ssephe * Make sure that STIMER0 is really disabled before writing 154300987Ssephe * to STIMER0_CONFIG. 155300987Ssephe * 156300987Ssephe * "Writing to the configuration register of a timer that 157300987Ssephe * is already enabled may result in undefined behaviour." 158300987Ssephe */ 159300987Ssephe for (;;) { 160300987Ssephe uint64_t val; 161300987Ssephe 162300987Ssephe /* Stop counting, and this also implies disabling STIMER0 */ 163300987Ssephe wrmsr(MSR_HV_STIMER0_COUNT, 0); 164300987Ssephe 165300987Ssephe val = rdmsr(MSR_HV_STIMER0_CONFIG); 166300987Ssephe if ((val & MSR_HV_STIMER_CFG_ENABLE) == 0) 167300987Ssephe break; 168300987Ssephe cpu_spinwait(); 169300987Ssephe } 170300987Ssephe wrmsr(MSR_HV_STIMER0_CONFIG, 171300987Ssephe MSR_HV_STIMER_CFG_AUTOEN | MSR_HV_STIMER0_CFG_SINT); 172300987Ssephe} 173300987Ssephe 174298449Ssephestatic int 175300993Ssephevmbus_et_attach(device_t dev) 176298449Ssephe{ 177300989Ssephe /* TODO: use independent IDT vector */ 178298449Ssephe 179300989Ssephe vmbus_et.et_name = "Hyper-V"; 180300989Ssephe vmbus_et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU; 181300989Ssephe vmbus_et.et_quality = 1000; 182300992Ssephe vmbus_et.et_frequency = HYPERV_TIMER_FREQ; 183300992Ssephe vmbus_et.et_min_period = (0x00000001ULL << 32) / HYPERV_TIMER_FREQ; 184300992Ssephe vmbus_et.et_max_period = (0xfffffffeULL << 32) / HYPERV_TIMER_FREQ; 185300993Ssephe vmbus_et.et_start = vmbus_et_start; 186298449Ssephe 187300987Ssephe /* 188314003Ssephe * Delay a bit to make sure that hyperv_tc64 will not return 0, 189314003Ssephe * since writing 0 to STIMER0_COUNT will disable STIMER0. 190300987Ssephe */ 191300987Ssephe DELAY(100); 192300987Ssephe smp_rendezvous(NULL, vmbus_et_config, NULL, NULL); 193300987Ssephe 194300989Ssephe return (et_register(&vmbus_et)); 195298449Ssephe} 196298449Ssephe 197298449Ssephestatic int 198300993Ssephevmbus_et_detach(device_t dev) 199298449Ssephe{ 200300989Ssephe return (et_deregister(&vmbus_et)); 201298449Ssephe} 202